ast_test_matchers_test.cpp 6.5 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 "explorer/ast/ast_test_matchers.h"
  5. #include <gmock/gmock.h>
  6. #include <gtest/gtest.h>
  7. #include "explorer/ast/declaration.h"
  8. #include "explorer/ast/expression.h"
  9. #include "explorer/ast/pattern.h"
  10. #include "explorer/ast/statement.h"
  11. #include "explorer/base/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, FileKind::Main);
  19. TEST(ASTTestMatchers, BlockContentsAreTest) {
  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(ASTTestMatchers, MatchesLiteralTest) {
  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(ASTTestMatchers, MatchesMulTest) {
  36. IntLiteral two(DummyLoc, 2);
  37. IntLiteral three(DummyLoc, 3);
  38. OperatorExpression 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(OperatorExpression(DummyLoc, Operator::Deref, {&two}),
  45. Not(MatchesMul(_, _)));
  46. OperatorExpression nested(DummyLoc, Operator::Mul, {&two, &mul});
  47. EXPECT_THAT(nested,
  48. MatchesMul(MatchesLiteral(2),
  49. MatchesMul(MatchesLiteral(2), MatchesLiteral(3))));
  50. }
  51. TEST(ASTTestMatchers, MatchesBinaryOpTest) {
  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(OperatorExpression(DummyLoc, Operator::Add, {&two, &three}),
  58. MatchesAdd(MatchesLiteral(2), MatchesLiteral(3)));
  59. EXPECT_THAT(OperatorExpression(DummyLoc, Operator::And, {&two, &three}),
  60. MatchesAnd(MatchesLiteral(2), MatchesLiteral(3)));
  61. EXPECT_THAT(OperatorExpression(DummyLoc, Operator::Eq, {&two, &three}),
  62. MatchesEq(MatchesLiteral(2), MatchesLiteral(3)));
  63. EXPECT_THAT(OperatorExpression(DummyLoc, Operator::Or, {&two, &three}),
  64. MatchesOr(MatchesLiteral(2), MatchesLiteral(3)));
  65. EXPECT_THAT(OperatorExpression(DummyLoc, Operator::Sub, {&two, &three}),
  66. MatchesSub(MatchesLiteral(2), MatchesLiteral(3)));
  67. }
  68. TEST(ASTTestMatchers, MatchesReturnTest) {
  69. TupleLiteral unit(DummyLoc);
  70. ReturnExpression empty_return(DummyLoc, &unit,
  71. /*is_omitted_expression=*/true);
  72. EXPECT_THAT(empty_return, MatchesEmptyReturn());
  73. EXPECT_THAT(&empty_return, MatchesEmptyReturn());
  74. EXPECT_THAT(empty_return, Not(MatchesReturn(_)));
  75. IntLiteral int_val(DummyLoc, 42);
  76. ReturnExpression explicit_return(DummyLoc, &int_val,
  77. /*is_omitted_expression=*/false);
  78. EXPECT_THAT(explicit_return, MatchesReturn(MatchesLiteral(42)));
  79. EXPECT_THAT(explicit_return, Not(MatchesEmptyReturn()));
  80. EXPECT_THAT(int_val, Not(MatchesEmptyReturn()));
  81. EXPECT_THAT(int_val, Not(MatchesReturn(_)));
  82. }
  83. TEST(ASTTestMatchers, MatchesFunctionDeclarationTest) {
  84. TuplePattern params(DummyLoc, {});
  85. Block body(DummyLoc, {});
  86. FunctionDeclaration decl(DummyLoc, DeclaredName(DummyLoc, "Foo"), {},
  87. std::nullopt, &params, ReturnTerm::Omitted(DummyLoc),
  88. &body, VirtualOverride::None);
  89. EXPECT_THAT(decl, MatchesFunctionDeclaration());
  90. EXPECT_THAT(&decl, MatchesFunctionDeclaration());
  91. EXPECT_THAT(decl, MatchesFunctionDeclaration().WithName("Foo"));
  92. EXPECT_THAT(decl, MatchesFunctionDeclaration().WithBody(_));
  93. EXPECT_THAT(decl, MatchesFunctionDeclaration().WithName("Foo").WithBody(_));
  94. EXPECT_THAT(decl, MatchesFunctionDeclaration().WithBody(_).WithName("Foo"));
  95. EXPECT_THAT(decl, Not(MatchesFunctionDeclaration().WithName("Bar")));
  96. EXPECT_THAT(decl,
  97. Not(MatchesFunctionDeclaration().WithBody(MatchesLiteral(0))));
  98. FunctionDeclaration forward_decl(
  99. DummyLoc, DeclaredName(DummyLoc, "Foo"), {}, std::nullopt, &params,
  100. ReturnTerm::Omitted(DummyLoc), std::nullopt, VirtualOverride::None);
  101. EXPECT_THAT(forward_decl, MatchesFunctionDeclaration().WithName("Foo"));
  102. EXPECT_THAT(forward_decl, Not(MatchesFunctionDeclaration().WithBody(_)));
  103. EXPECT_THAT(body, Not(MatchesFunctionDeclaration()));
  104. }
  105. TEST(ASTTestMatchers, MatchesUnimplementedExpressionTest) {
  106. IntLiteral two(DummyLoc, 2);
  107. IntLiteral three(DummyLoc, 3);
  108. UnimplementedExpression unimplemented(DummyLoc, "DummyLabel", &two, &three);
  109. EXPECT_THAT(unimplemented, MatchesUnimplementedExpression(
  110. "DummyLabel", ElementsAre(MatchesLiteral(2),
  111. MatchesLiteral(3))));
  112. EXPECT_THAT(
  113. &unimplemented,
  114. MatchesUnimplementedExpression(
  115. "DummyLabel", ElementsAre(MatchesLiteral(2), MatchesLiteral(3))));
  116. EXPECT_THAT(
  117. unimplemented,
  118. Not(MatchesUnimplementedExpression(
  119. "WrongLabel", ElementsAre(MatchesLiteral(2), MatchesLiteral(3)))));
  120. EXPECT_THAT(unimplemented,
  121. Not(MatchesUnimplementedExpression("DummyLabel", IsEmpty())));
  122. EXPECT_THAT(two,
  123. Not(MatchesUnimplementedExpression("DummyLabel", IsEmpty())));
  124. }
  125. TEST(ASTTestMatchers, ASTDeclarationsTest) {
  126. TuplePattern params(DummyLoc, {});
  127. Block body(DummyLoc, {});
  128. FunctionDeclaration decl(DummyLoc, DeclaredName(DummyLoc, "Foo"), {},
  129. std::nullopt, &params, ReturnTerm::Omitted(DummyLoc),
  130. &body, VirtualOverride::None);
  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