expression_test.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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/common/arena.h"
  7. #include "executable_semantics/syntax/paren_contents.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 = global_arena->New<IntLiteral>(/*line_num=*/2, 42)}},
  50. .has_trailing_comma = false};
  51. const Expression* expression =
  52. ExpressionFromParenContents(/*line_num=*/1, contents);
  53. EXPECT_EQ(expression->LineNumber(), 2);
  54. ASSERT_EQ(expression->Tag(), Expression::Kind::IntLiteral);
  55. }
  56. TEST(ExpressionTest, UnaryNoCommaAsTuple) {
  57. ParenContents<Expression> contents = {
  58. .elements = {{.name = std::nullopt,
  59. .term = global_arena->New<IntLiteral>(/*line_num=*/2, 42)}},
  60. .has_trailing_comma = false};
  61. const Expression* tuple =
  62. TupleExpressionFromParenContents(/*line_num=*/1, contents);
  63. EXPECT_EQ(tuple->LineNumber(), 1);
  64. ASSERT_EQ(tuple->Tag(), Expression::Kind::TupleLiteral);
  65. EXPECT_THAT(cast<TupleLiteral>(*tuple).Fields(),
  66. ElementsAre(IntFieldNamed("0")));
  67. }
  68. TEST(ExpressionTest, UnaryWithCommaAsExpression) {
  69. ParenContents<Expression> contents = {
  70. .elements = {{.name = std::nullopt,
  71. .term = global_arena->New<IntLiteral>(/*line_num=*/2, 42)}},
  72. .has_trailing_comma = true};
  73. const Expression* expression =
  74. ExpressionFromParenContents(/*line_num=*/1, contents);
  75. EXPECT_EQ(expression->LineNumber(), 1);
  76. ASSERT_EQ(expression->Tag(), Expression::Kind::TupleLiteral);
  77. EXPECT_THAT(cast<TupleLiteral>(*expression).Fields(),
  78. ElementsAre(IntFieldNamed("0")));
  79. }
  80. TEST(ExpressionTest, UnaryWithCommaAsTuple) {
  81. ParenContents<Expression> contents = {
  82. .elements = {{.name = std::nullopt,
  83. .term = global_arena->New<IntLiteral>(/*line_num=*/2, 42)}},
  84. .has_trailing_comma = true};
  85. const Expression* tuple =
  86. TupleExpressionFromParenContents(/*line_num=*/1, contents);
  87. EXPECT_EQ(tuple->LineNumber(), 1);
  88. ASSERT_EQ(tuple->Tag(), Expression::Kind::TupleLiteral);
  89. EXPECT_THAT(cast<TupleLiteral>(*tuple).Fields(),
  90. ElementsAre(IntFieldNamed("0")));
  91. }
  92. TEST(ExpressionTest, BinaryAsExpression) {
  93. ParenContents<Expression> contents = {
  94. .elements = {{.name = std::nullopt,
  95. .term = global_arena->New<IntLiteral>(/*line_num=*/2, 42)},
  96. {.name = std::nullopt,
  97. .term = global_arena->New<IntLiteral>(/*line_num=*/3, 42)}},
  98. .has_trailing_comma = true};
  99. const Expression* expression =
  100. ExpressionFromParenContents(/*line_num=*/1, contents);
  101. EXPECT_EQ(expression->LineNumber(), 1);
  102. ASSERT_EQ(expression->Tag(), Expression::Kind::TupleLiteral);
  103. EXPECT_THAT(cast<TupleLiteral>(*expression).Fields(),
  104. ElementsAre(IntFieldNamed("0"), IntFieldNamed("1")));
  105. }
  106. TEST(ExpressionTest, BinaryAsTuple) {
  107. ParenContents<Expression> contents = {
  108. .elements = {{.name = std::nullopt,
  109. .term = global_arena->New<IntLiteral>(/*line_num=*/2, 42)},
  110. {.name = std::nullopt,
  111. .term = global_arena->New<IntLiteral>(/*line_num=*/3, 42)}},
  112. .has_trailing_comma = true};
  113. const Expression* tuple =
  114. TupleExpressionFromParenContents(/*line_num=*/1, contents);
  115. EXPECT_EQ(tuple->LineNumber(), 1);
  116. ASSERT_EQ(tuple->Tag(), Expression::Kind::TupleLiteral);
  117. EXPECT_THAT(cast<TupleLiteral>(*tuple).Fields(),
  118. ElementsAre(IntFieldNamed("0"), IntFieldNamed("1")));
  119. }
  120. } // namespace
  121. } // namespace Carbon