expression_test.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. // Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  2. // Exceptions. See /LICENSE for license information.
  3. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. #include "executable_semantics/ast/expression.h"
  5. #include <string>
  6. #include "executable_semantics/ast/paren_contents.h"
  7. #include "executable_semantics/common/arena.h"
  8. #include "gmock/gmock.h"
  9. #include "gtest/gtest.h"
  10. #include "llvm/Support/Casting.h"
  11. namespace Carbon {
  12. namespace {
  13. using llvm::cast;
  14. using testing::ElementsAre;
  15. using testing::IsEmpty;
  16. // Matches a FieldInitializer named `name` whose `expression` is an
  17. // `IntLiteral`
  18. MATCHER_P(IntFieldNamed, name, "") {
  19. return arg.name == std::string(name) &&
  20. arg.expression->Tag() == Expression::Kind::IntLiteral;
  21. }
  22. static auto FakeSourceLoc(int line_num) -> SourceLocation {
  23. return SourceLocation("<test>", line_num);
  24. }
  25. TEST(ExpressionTest, EmptyAsExpression) {
  26. ParenContents<Expression> contents = {.elements = {},
  27. .has_trailing_comma = false};
  28. Ptr<const Expression> expression =
  29. ExpressionFromParenContents(FakeSourceLoc(1), contents);
  30. EXPECT_EQ(expression->SourceLoc(), FakeSourceLoc(1));
  31. ASSERT_EQ(expression->Tag(), Expression::Kind::TupleLiteral);
  32. EXPECT_THAT(cast<TupleLiteral>(*expression).Fields(), IsEmpty());
  33. }
  34. TEST(ExpressionTest, EmptyAsTuple) {
  35. ParenContents<Expression> contents = {.elements = {},
  36. .has_trailing_comma = false};
  37. Ptr<const Expression> tuple =
  38. TupleExpressionFromParenContents(FakeSourceLoc(1), contents);
  39. EXPECT_EQ(tuple->SourceLoc(), FakeSourceLoc(1));
  40. ASSERT_EQ(tuple->Tag(), Expression::Kind::TupleLiteral);
  41. EXPECT_THAT(cast<TupleLiteral>(*tuple).Fields(), IsEmpty());
  42. }
  43. TEST(ExpressionTest, UnaryNoCommaAsExpression) {
  44. // Equivalent to a code fragment like
  45. // ```
  46. // (
  47. // 42
  48. // )
  49. // ```
  50. ParenContents<Expression> contents = {
  51. .elements = {{.name = std::nullopt,
  52. .term =
  53. global_arena->New<IntLiteral>(FakeSourceLoc(2), 42)}},
  54. .has_trailing_comma = false};
  55. Ptr<const Expression> expression =
  56. ExpressionFromParenContents(FakeSourceLoc(1), contents);
  57. EXPECT_EQ(expression->SourceLoc(), FakeSourceLoc(2));
  58. ASSERT_EQ(expression->Tag(), Expression::Kind::IntLiteral);
  59. }
  60. TEST(ExpressionTest, UnaryNoCommaAsTuple) {
  61. ParenContents<Expression> contents = {
  62. .elements = {{.name = std::nullopt,
  63. .term =
  64. global_arena->New<IntLiteral>(FakeSourceLoc(2), 42)}},
  65. .has_trailing_comma = false};
  66. Ptr<const Expression> tuple =
  67. TupleExpressionFromParenContents(FakeSourceLoc(1), contents);
  68. EXPECT_EQ(tuple->SourceLoc(), FakeSourceLoc(1));
  69. ASSERT_EQ(tuple->Tag(), Expression::Kind::TupleLiteral);
  70. EXPECT_THAT(cast<TupleLiteral>(*tuple).Fields(),
  71. ElementsAre(IntFieldNamed("0")));
  72. }
  73. TEST(ExpressionTest, UnaryWithCommaAsExpression) {
  74. ParenContents<Expression> contents = {
  75. .elements = {{.name = std::nullopt,
  76. .term =
  77. global_arena->New<IntLiteral>(FakeSourceLoc(2), 42)}},
  78. .has_trailing_comma = true};
  79. Ptr<const Expression> expression =
  80. ExpressionFromParenContents(FakeSourceLoc(1), contents);
  81. EXPECT_EQ(expression->SourceLoc(), FakeSourceLoc(1));
  82. ASSERT_EQ(expression->Tag(), Expression::Kind::TupleLiteral);
  83. EXPECT_THAT(cast<TupleLiteral>(*expression).Fields(),
  84. ElementsAre(IntFieldNamed("0")));
  85. }
  86. TEST(ExpressionTest, UnaryWithCommaAsTuple) {
  87. ParenContents<Expression> contents = {
  88. .elements = {{.name = std::nullopt,
  89. .term =
  90. global_arena->New<IntLiteral>(FakeSourceLoc(2), 42)}},
  91. .has_trailing_comma = true};
  92. Ptr<const Expression> tuple =
  93. TupleExpressionFromParenContents(FakeSourceLoc(1), contents);
  94. EXPECT_EQ(tuple->SourceLoc(), FakeSourceLoc(1));
  95. ASSERT_EQ(tuple->Tag(), Expression::Kind::TupleLiteral);
  96. EXPECT_THAT(cast<TupleLiteral>(*tuple).Fields(),
  97. ElementsAre(IntFieldNamed("0")));
  98. }
  99. TEST(ExpressionTest, BinaryAsExpression) {
  100. ParenContents<Expression> contents = {
  101. .elements = {{.name = std::nullopt,
  102. .term =
  103. global_arena->New<IntLiteral>(FakeSourceLoc(2), 42)},
  104. {.name = std::nullopt,
  105. .term =
  106. global_arena->New<IntLiteral>(FakeSourceLoc(3), 42)}},
  107. .has_trailing_comma = true};
  108. Ptr<const Expression> expression =
  109. ExpressionFromParenContents(FakeSourceLoc(1), contents);
  110. EXPECT_EQ(expression->SourceLoc(), FakeSourceLoc(1));
  111. ASSERT_EQ(expression->Tag(), Expression::Kind::TupleLiteral);
  112. EXPECT_THAT(cast<TupleLiteral>(*expression).Fields(),
  113. ElementsAre(IntFieldNamed("0"), IntFieldNamed("1")));
  114. }
  115. TEST(ExpressionTest, BinaryAsTuple) {
  116. ParenContents<Expression> contents = {
  117. .elements = {{.name = std::nullopt,
  118. .term =
  119. global_arena->New<IntLiteral>(FakeSourceLoc(2), 42)},
  120. {.name = std::nullopt,
  121. .term =
  122. global_arena->New<IntLiteral>(FakeSourceLoc(3), 42)}},
  123. .has_trailing_comma = true};
  124. Ptr<const Expression> tuple =
  125. TupleExpressionFromParenContents(FakeSourceLoc(1), contents);
  126. EXPECT_EQ(tuple->SourceLoc(), FakeSourceLoc(1));
  127. ASSERT_EQ(tuple->Tag(), Expression::Kind::TupleLiteral);
  128. EXPECT_THAT(cast<TupleLiteral>(*tuple).Fields(),
  129. ElementsAre(IntFieldNamed("0"), IntFieldNamed("1")));
  130. }
  131. } // namespace
  132. } // namespace Carbon