expression_test.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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/syntax/paren_contents.h"
  7. #include "gmock/gmock.h"
  8. #include "gtest/gtest.h"
  9. namespace Carbon {
  10. namespace {
  11. using testing::ElementsAre;
  12. using testing::IsEmpty;
  13. // Matches a FieldInitializer named `name` whose `expression` is an
  14. // `IntLiteral`
  15. MATCHER_P(IntFieldNamed, name, "") {
  16. return arg.name == std::string(name) &&
  17. arg.expression->tag() == ExpressionKind::IntLiteral;
  18. }
  19. TEST(ExpressionTest, EmptyAsExpression) {
  20. ParenContents<Expression> contents = {.elements = {},
  21. .has_trailing_comma = false};
  22. const Expression* expression =
  23. ExpressionFromParenContents(/*line_num=*/1, contents);
  24. EXPECT_EQ(expression->line_num, 1);
  25. ASSERT_EQ(expression->tag(), ExpressionKind::TupleLiteral);
  26. EXPECT_THAT(expression->GetTupleLiteral().fields, IsEmpty());
  27. }
  28. TEST(ExpressionTest, EmptyAsTuple) {
  29. ParenContents<Expression> contents = {.elements = {},
  30. .has_trailing_comma = false};
  31. const Expression* tuple =
  32. TupleExpressionFromParenContents(/*line_num=*/1, contents);
  33. EXPECT_EQ(tuple->line_num, 1);
  34. ASSERT_EQ(tuple->tag(), ExpressionKind::TupleLiteral);
  35. EXPECT_THAT(tuple->GetTupleLiteral().fields, IsEmpty());
  36. }
  37. TEST(ExpressionTest, UnaryNoCommaAsExpression) {
  38. // Equivalent to a code fragment like
  39. // ```
  40. // (
  41. // 42
  42. // )
  43. // ```
  44. ParenContents<Expression> contents = {
  45. .elements = {{.name = std::nullopt,
  46. .term = Expression::MakeIntLiteral(/*line_num=*/2, 42)}},
  47. .has_trailing_comma = false};
  48. const Expression* expression =
  49. ExpressionFromParenContents(/*line_num=*/1, contents);
  50. EXPECT_EQ(expression->line_num, 2);
  51. ASSERT_EQ(expression->tag(), ExpressionKind::IntLiteral);
  52. }
  53. TEST(ExpressionTest, UnaryNoCommaAsTuple) {
  54. ParenContents<Expression> contents = {
  55. .elements = {{.name = std::nullopt,
  56. .term = Expression::MakeIntLiteral(/*line_num=*/2, 42)}},
  57. .has_trailing_comma = false};
  58. const Expression* tuple =
  59. TupleExpressionFromParenContents(/*line_num=*/1, contents);
  60. EXPECT_EQ(tuple->line_num, 1);
  61. ASSERT_EQ(tuple->tag(), ExpressionKind::TupleLiteral);
  62. EXPECT_THAT(tuple->GetTupleLiteral().fields, ElementsAre(IntFieldNamed("0")));
  63. }
  64. TEST(ExpressionTest, UnaryWithCommaAsExpression) {
  65. ParenContents<Expression> contents = {
  66. .elements = {{.name = std::nullopt,
  67. .term = Expression::MakeIntLiteral(/*line_num=*/2, 42)}},
  68. .has_trailing_comma = true};
  69. const Expression* expression =
  70. ExpressionFromParenContents(/*line_num=*/1, contents);
  71. EXPECT_EQ(expression->line_num, 1);
  72. ASSERT_EQ(expression->tag(), ExpressionKind::TupleLiteral);
  73. EXPECT_THAT(expression->GetTupleLiteral().fields,
  74. ElementsAre(IntFieldNamed("0")));
  75. }
  76. TEST(ExpressionTest, UnaryWithCommaAsTuple) {
  77. ParenContents<Expression> contents = {
  78. .elements = {{.name = std::nullopt,
  79. .term = Expression::MakeIntLiteral(/*line_num=*/2, 42)}},
  80. .has_trailing_comma = true};
  81. const Expression* tuple =
  82. TupleExpressionFromParenContents(/*line_num=*/1, contents);
  83. EXPECT_EQ(tuple->line_num, 1);
  84. ASSERT_EQ(tuple->tag(), ExpressionKind::TupleLiteral);
  85. EXPECT_THAT(tuple->GetTupleLiteral().fields, ElementsAre(IntFieldNamed("0")));
  86. }
  87. TEST(ExpressionTest, BinaryAsExpression) {
  88. ParenContents<Expression> contents = {
  89. .elements = {{.name = std::nullopt,
  90. .term = Expression::MakeIntLiteral(/*line_num=*/2, 42)},
  91. {.name = std::nullopt,
  92. .term = Expression::MakeIntLiteral(/*line_num=*/3, 42)}},
  93. .has_trailing_comma = true};
  94. const Expression* expression =
  95. ExpressionFromParenContents(/*line_num=*/1, contents);
  96. EXPECT_EQ(expression->line_num, 1);
  97. ASSERT_EQ(expression->tag(), ExpressionKind::TupleLiteral);
  98. EXPECT_THAT(expression->GetTupleLiteral().fields,
  99. ElementsAre(IntFieldNamed("0"), IntFieldNamed("1")));
  100. }
  101. TEST(ExpressionTest, BinaryAsTuple) {
  102. ParenContents<Expression> contents = {
  103. .elements = {{.name = std::nullopt,
  104. .term = Expression::MakeIntLiteral(/*line_num=*/2, 42)},
  105. {.name = std::nullopt,
  106. .term = Expression::MakeIntLiteral(/*line_num=*/3, 42)}},
  107. .has_trailing_comma = true};
  108. const Expression* tuple =
  109. TupleExpressionFromParenContents(/*line_num=*/1, contents);
  110. EXPECT_EQ(tuple->line_num, 1);
  111. ASSERT_EQ(tuple->tag(), ExpressionKind::TupleLiteral);
  112. EXPECT_THAT(tuple->GetTupleLiteral().fields,
  113. ElementsAre(IntFieldNamed("0"), IntFieldNamed("1")));
  114. }
  115. } // namespace
  116. } // namespace Carbon