expression_test.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. class ExpressionTest : public ::testing::Test {
  26. protected:
  27. Arena arena;
  28. };
  29. TEST_F(ExpressionTest, EmptyAsExpression) {
  30. ParenContents<Expression> contents = {.elements = {},
  31. .has_trailing_comma = false};
  32. Nonnull<const Expression*> expression =
  33. ExpressionFromParenContents(&arena, FakeSourceLoc(1), contents);
  34. EXPECT_EQ(expression->SourceLoc(), FakeSourceLoc(1));
  35. ASSERT_EQ(expression->Tag(), Expression::Kind::TupleLiteral);
  36. EXPECT_THAT(cast<TupleLiteral>(*expression).Fields(), IsEmpty());
  37. }
  38. TEST_F(ExpressionTest, EmptyAsTuple) {
  39. ParenContents<Expression> contents = {.elements = {},
  40. .has_trailing_comma = false};
  41. Nonnull<const Expression*> tuple =
  42. TupleExpressionFromParenContents(&arena, FakeSourceLoc(1), contents);
  43. EXPECT_EQ(tuple->SourceLoc(), FakeSourceLoc(1));
  44. ASSERT_EQ(tuple->Tag(), Expression::Kind::TupleLiteral);
  45. EXPECT_THAT(cast<TupleLiteral>(*tuple).Fields(), IsEmpty());
  46. }
  47. TEST_F(ExpressionTest, UnaryNoCommaAsExpression) {
  48. // Equivalent to a code fragment like
  49. // ```
  50. // (
  51. // 42
  52. // )
  53. // ```
  54. ParenContents<Expression> contents = {
  55. .elements = {{.name = std::nullopt,
  56. .term = arena.New<IntLiteral>(FakeSourceLoc(2), 42)}},
  57. .has_trailing_comma = false};
  58. Nonnull<const Expression*> expression =
  59. ExpressionFromParenContents(&arena, FakeSourceLoc(1), contents);
  60. EXPECT_EQ(expression->SourceLoc(), FakeSourceLoc(2));
  61. ASSERT_EQ(expression->Tag(), Expression::Kind::IntLiteral);
  62. }
  63. TEST_F(ExpressionTest, UnaryNoCommaAsTuple) {
  64. ParenContents<Expression> contents = {
  65. .elements = {{.name = std::nullopt,
  66. .term = arena.New<IntLiteral>(FakeSourceLoc(2), 42)}},
  67. .has_trailing_comma = false};
  68. Nonnull<const Expression*> tuple =
  69. TupleExpressionFromParenContents(&arena, FakeSourceLoc(1), contents);
  70. EXPECT_EQ(tuple->SourceLoc(), FakeSourceLoc(1));
  71. ASSERT_EQ(tuple->Tag(), Expression::Kind::TupleLiteral);
  72. EXPECT_THAT(cast<TupleLiteral>(*tuple).Fields(),
  73. ElementsAre(IntFieldNamed("0")));
  74. }
  75. TEST_F(ExpressionTest, UnaryWithCommaAsExpression) {
  76. ParenContents<Expression> contents = {
  77. .elements = {{.name = std::nullopt,
  78. .term = arena.New<IntLiteral>(FakeSourceLoc(2), 42)}},
  79. .has_trailing_comma = true};
  80. Nonnull<const Expression*> expression =
  81. ExpressionFromParenContents(&arena, FakeSourceLoc(1), contents);
  82. EXPECT_EQ(expression->SourceLoc(), FakeSourceLoc(1));
  83. ASSERT_EQ(expression->Tag(), Expression::Kind::TupleLiteral);
  84. EXPECT_THAT(cast<TupleLiteral>(*expression).Fields(),
  85. ElementsAre(IntFieldNamed("0")));
  86. }
  87. TEST_F(ExpressionTest, UnaryWithCommaAsTuple) {
  88. ParenContents<Expression> contents = {
  89. .elements = {{.name = std::nullopt,
  90. .term = arena.New<IntLiteral>(FakeSourceLoc(2), 42)}},
  91. .has_trailing_comma = true};
  92. Nonnull<const Expression*> tuple =
  93. TupleExpressionFromParenContents(&arena, 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_F(ExpressionTest, BinaryAsExpression) {
  100. ParenContents<Expression> contents = {
  101. .elements = {{.name = std::nullopt,
  102. .term = arena.New<IntLiteral>(FakeSourceLoc(2), 42)},
  103. {.name = std::nullopt,
  104. .term = arena.New<IntLiteral>(FakeSourceLoc(3), 42)}},
  105. .has_trailing_comma = true};
  106. Nonnull<const Expression*> expression =
  107. ExpressionFromParenContents(&arena, FakeSourceLoc(1), contents);
  108. EXPECT_EQ(expression->SourceLoc(), FakeSourceLoc(1));
  109. ASSERT_EQ(expression->Tag(), Expression::Kind::TupleLiteral);
  110. EXPECT_THAT(cast<TupleLiteral>(*expression).Fields(),
  111. ElementsAre(IntFieldNamed("0"), IntFieldNamed("1")));
  112. }
  113. TEST_F(ExpressionTest, BinaryAsTuple) {
  114. ParenContents<Expression> contents = {
  115. .elements = {{.name = std::nullopt,
  116. .term = arena.New<IntLiteral>(FakeSourceLoc(2), 42)},
  117. {.name = std::nullopt,
  118. .term = arena.New<IntLiteral>(FakeSourceLoc(3), 42)}},
  119. .has_trailing_comma = true};
  120. Nonnull<const Expression*> tuple =
  121. TupleExpressionFromParenContents(&arena, FakeSourceLoc(1), contents);
  122. EXPECT_EQ(tuple->SourceLoc(), FakeSourceLoc(1));
  123. ASSERT_EQ(tuple->Tag(), Expression::Kind::TupleLiteral);
  124. EXPECT_THAT(cast<TupleLiteral>(*tuple).Fields(),
  125. ElementsAre(IntFieldNamed("0"), IntFieldNamed("1")));
  126. }
  127. } // namespace
  128. } // namespace Carbon