ast_test_matchers_test.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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/ast_test_matchers.h"
  5. #include <gmock/gmock.h>
  6. #include <gtest/gtest.h>
  7. #include "executable_semantics/ast/declaration.h"
  8. #include "executable_semantics/ast/expression.h"
  9. #include "executable_semantics/ast/pattern.h"
  10. #include "executable_semantics/ast/statement.h"
  11. #include "executable_semantics/common/arena.h"
  12. namespace Carbon {
  13. namespace {
  14. using ::testing::_;
  15. using ::testing::ElementsAre;
  16. using ::testing::IsEmpty;
  17. using ::testing::Not;
  18. static constexpr SourceLocation DummyLoc("dummy", 0);
  19. TEST(BlockContentsAreTest, BasicUsage) {
  20. Block empty_block(DummyLoc, {});
  21. EXPECT_THAT(empty_block, BlockContentsAre(IsEmpty()));
  22. EXPECT_THAT(&empty_block, BlockContentsAre(IsEmpty()));
  23. Break break_node(DummyLoc);
  24. EXPECT_THAT(break_node, Not(BlockContentsAre(_)));
  25. Block break_block(DummyLoc, {&break_node});
  26. EXPECT_THAT(break_block, Not(BlockContentsAre(IsEmpty())));
  27. }
  28. TEST(MatchesLiteralTest, BasicUsage) {
  29. IntLiteral literal(DummyLoc, 42);
  30. EXPECT_THAT(literal, MatchesLiteral(42));
  31. EXPECT_THAT(&literal, MatchesLiteral(42));
  32. EXPECT_THAT(literal, Not(MatchesLiteral(43)));
  33. EXPECT_THAT(StringLiteral(DummyLoc, "foo"), Not(MatchesLiteral(42)));
  34. }
  35. TEST(MatchesMulTest, BasicUsage) {
  36. IntLiteral two(DummyLoc, 2);
  37. IntLiteral three(DummyLoc, 3);
  38. PrimitiveOperatorExpression mul(DummyLoc, Operator::Mul, {&two, &three});
  39. EXPECT_THAT(mul, MatchesMul(MatchesLiteral(2), MatchesLiteral(3)));
  40. EXPECT_THAT(&mul, MatchesMul(MatchesLiteral(2), MatchesLiteral(3)));
  41. EXPECT_THAT(mul, MatchesMul(_, _));
  42. EXPECT_THAT(mul, Not(MatchesMul(MatchesLiteral(2), MatchesLiteral(2))));
  43. EXPECT_THAT(StringLiteral(DummyLoc, "foo"), Not(MatchesMul(_, _)));
  44. EXPECT_THAT(PrimitiveOperatorExpression(DummyLoc, Operator::Deref, {&two}),
  45. Not(MatchesMul(_, _)));
  46. PrimitiveOperatorExpression nested(DummyLoc, Operator::Mul, {&two, &mul});
  47. EXPECT_THAT(nested,
  48. MatchesMul(MatchesLiteral(2),
  49. MatchesMul(MatchesLiteral(2), MatchesLiteral(3))));
  50. }
  51. TEST(MatchesBinaryOpTest, BasicUsage) {
  52. IntLiteral two(DummyLoc, 2);
  53. IntLiteral three(DummyLoc, 3);
  54. // Testing of MatchesMul provides most of the coverage for these matchers,
  55. // since they are thin wrappers around a common implementation. We only test
  56. // the others enough to detect copy-paste errors in the wrappers.
  57. EXPECT_THAT(
  58. PrimitiveOperatorExpression(DummyLoc, Operator::Add, {&two, &three}),
  59. MatchesAdd(MatchesLiteral(2), MatchesLiteral(3)));
  60. EXPECT_THAT(
  61. PrimitiveOperatorExpression(DummyLoc, Operator::And, {&two, &three}),
  62. MatchesAnd(MatchesLiteral(2), MatchesLiteral(3)));
  63. EXPECT_THAT(
  64. PrimitiveOperatorExpression(DummyLoc, Operator::Eq, {&two, &three}),
  65. MatchesEq(MatchesLiteral(2), MatchesLiteral(3)));
  66. EXPECT_THAT(
  67. PrimitiveOperatorExpression(DummyLoc, Operator::Or, {&two, &three}),
  68. MatchesOr(MatchesLiteral(2), MatchesLiteral(3)));
  69. EXPECT_THAT(
  70. PrimitiveOperatorExpression(DummyLoc, Operator::Sub, {&two, &three}),
  71. MatchesSub(MatchesLiteral(2), MatchesLiteral(3)));
  72. }
  73. TEST(MatchesReturnTest, BasicUsage) {
  74. TupleLiteral unit(DummyLoc);
  75. Return empty_return(DummyLoc, &unit, /*is_omitted_expression=*/true);
  76. EXPECT_THAT(empty_return, MatchesEmptyReturn());
  77. EXPECT_THAT(&empty_return, MatchesEmptyReturn());
  78. EXPECT_THAT(empty_return, Not(MatchesReturn(_)));
  79. IntLiteral int_val(DummyLoc, 42);
  80. Return explicit_return(DummyLoc, &int_val, /*is_omitted_expression=*/false);
  81. EXPECT_THAT(explicit_return, MatchesReturn(MatchesLiteral(42)));
  82. EXPECT_THAT(explicit_return, Not(MatchesEmptyReturn()));
  83. EXPECT_THAT(int_val, Not(MatchesEmptyReturn()));
  84. EXPECT_THAT(int_val, Not(MatchesReturn(_)));
  85. }
  86. TEST(MatchesFunctionDeclarationTest, BasicUsage) {
  87. TuplePattern params(DummyLoc, {});
  88. Block body(DummyLoc, {});
  89. FunctionDeclaration decl(DummyLoc, "Foo", {}, &params,
  90. ReturnTerm::Omitted(DummyLoc), &body);
  91. EXPECT_THAT(decl, MatchesFunctionDeclaration());
  92. EXPECT_THAT(&decl, MatchesFunctionDeclaration());
  93. EXPECT_THAT(decl, MatchesFunctionDeclaration().WithName("Foo"));
  94. EXPECT_THAT(decl, MatchesFunctionDeclaration().WithBody(_));
  95. EXPECT_THAT(decl, MatchesFunctionDeclaration().WithName("Foo").WithBody(_));
  96. EXPECT_THAT(decl, MatchesFunctionDeclaration().WithBody(_).WithName("Foo"));
  97. EXPECT_THAT(decl, Not(MatchesFunctionDeclaration().WithName("Bar")));
  98. EXPECT_THAT(decl,
  99. Not(MatchesFunctionDeclaration().WithBody(MatchesLiteral(0))));
  100. FunctionDeclaration forward_decl(DummyLoc, "Foo", {}, &params,
  101. ReturnTerm::Omitted(DummyLoc), std::nullopt);
  102. EXPECT_THAT(forward_decl, MatchesFunctionDeclaration().WithName("Foo"));
  103. EXPECT_THAT(forward_decl, Not(MatchesFunctionDeclaration().WithBody(_)));
  104. EXPECT_THAT(body, Not(MatchesFunctionDeclaration()));
  105. }
  106. TEST(MatchesUnimplementedExpressionTest, BasicUsage) {
  107. IntLiteral two(DummyLoc, 2);
  108. IntLiteral three(DummyLoc, 3);
  109. UnimplementedExpression unimplemented(DummyLoc, "DummyLabel", &two, &three);
  110. EXPECT_THAT(unimplemented, MatchesUnimplementedExpression(
  111. "DummyLabel", ElementsAre(MatchesLiteral(2),
  112. MatchesLiteral(3))));
  113. EXPECT_THAT(
  114. &unimplemented,
  115. MatchesUnimplementedExpression(
  116. "DummyLabel", ElementsAre(MatchesLiteral(2), MatchesLiteral(3))));
  117. EXPECT_THAT(
  118. unimplemented,
  119. Not(MatchesUnimplementedExpression(
  120. "WrongLabel", ElementsAre(MatchesLiteral(2), MatchesLiteral(3)))));
  121. EXPECT_THAT(unimplemented,
  122. Not(MatchesUnimplementedExpression("DummyLabel", IsEmpty())));
  123. EXPECT_THAT(two,
  124. Not(MatchesUnimplementedExpression("DummyLabel", IsEmpty())));
  125. }
  126. TEST(ASTDeclarationsTest, BasicUsage) {
  127. TuplePattern params(DummyLoc, {});
  128. Block body(DummyLoc, {});
  129. FunctionDeclaration decl(DummyLoc, "Foo", {}, &params,
  130. ReturnTerm::Omitted(DummyLoc), &body);
  131. AST ast = {.declarations = {&decl}};
  132. EXPECT_THAT(ast, ASTDeclarations(ElementsAre(MatchesFunctionDeclaration())));
  133. EXPECT_THAT(ast, Not(ASTDeclarations(IsEmpty())));
  134. }
  135. } // namespace
  136. } // namespace Carbon