expression_test.cpp 5.3 KB

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