ast_test_matchers_test.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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::IsEmpty;
  16. using ::testing::Not;
  17. static constexpr SourceLocation DummyLoc("dummy", 0);
  18. TEST(BlockContentsAreTest, BasicUsage) {
  19. Block empty_block(DummyLoc, {});
  20. EXPECT_THAT(empty_block, BlockContentsAre(IsEmpty()));
  21. EXPECT_THAT(&empty_block, BlockContentsAre(IsEmpty()));
  22. Break break_node(DummyLoc);
  23. EXPECT_THAT(break_node, Not(BlockContentsAre(_)));
  24. Block break_block(DummyLoc, {&break_node});
  25. EXPECT_THAT(break_block, Not(BlockContentsAre(IsEmpty())));
  26. }
  27. TEST(MatchesLiteralTest, BasicUsage) {
  28. IntLiteral literal(DummyLoc, 42);
  29. EXPECT_THAT(literal, MatchesLiteral(42));
  30. EXPECT_THAT(&literal, MatchesLiteral(42));
  31. EXPECT_THAT(literal, Not(MatchesLiteral(43)));
  32. EXPECT_THAT(StringLiteral(DummyLoc, "foo"), Not(MatchesLiteral(42)));
  33. }
  34. TEST(MatchesMulTest, BasicUsage) {
  35. IntLiteral two(DummyLoc, 2);
  36. IntLiteral three(DummyLoc, 3);
  37. PrimitiveOperatorExpression mul(DummyLoc, Operator::Mul, {&two, &three});
  38. EXPECT_THAT(mul, MatchesMul(MatchesLiteral(2), MatchesLiteral(3)));
  39. EXPECT_THAT(&mul, MatchesMul(MatchesLiteral(2), MatchesLiteral(3)));
  40. EXPECT_THAT(mul, MatchesMul(_, _));
  41. EXPECT_THAT(mul, Not(MatchesMul(MatchesLiteral(2), MatchesLiteral(2))));
  42. EXPECT_THAT(StringLiteral(DummyLoc, "foo"), Not(MatchesMul(_, _)));
  43. EXPECT_THAT(PrimitiveOperatorExpression(DummyLoc, Operator::Deref, {&two}),
  44. Not(MatchesMul(_, _)));
  45. PrimitiveOperatorExpression nested(DummyLoc, Operator::Mul, {&two, &mul});
  46. EXPECT_THAT(nested,
  47. MatchesMul(MatchesLiteral(2),
  48. MatchesMul(MatchesLiteral(2), MatchesLiteral(3))));
  49. }
  50. TEST(MatchesBinaryOpTest, BasicUsage) {
  51. IntLiteral two(DummyLoc, 2);
  52. IntLiteral three(DummyLoc, 3);
  53. // Testing of MatchesMul provides most of the coverage for these matchers,
  54. // since they are thin wrappers around a common implementation. We only test
  55. // the others enough to detect copy-paste errors in the wrappers.
  56. EXPECT_THAT(
  57. PrimitiveOperatorExpression(DummyLoc, Operator::Add, {&two, &three}),
  58. MatchesAdd(MatchesLiteral(2), MatchesLiteral(3)));
  59. EXPECT_THAT(
  60. PrimitiveOperatorExpression(DummyLoc, Operator::And, {&two, &three}),
  61. MatchesAnd(MatchesLiteral(2), MatchesLiteral(3)));
  62. EXPECT_THAT(
  63. PrimitiveOperatorExpression(DummyLoc, Operator::Eq, {&two, &three}),
  64. MatchesEq(MatchesLiteral(2), MatchesLiteral(3)));
  65. EXPECT_THAT(
  66. PrimitiveOperatorExpression(DummyLoc, Operator::Or, {&two, &three}),
  67. MatchesOr(MatchesLiteral(2), MatchesLiteral(3)));
  68. EXPECT_THAT(
  69. PrimitiveOperatorExpression(DummyLoc, Operator::Sub, {&two, &three}),
  70. MatchesSub(MatchesLiteral(2), MatchesLiteral(3)));
  71. }
  72. TEST(MatchesReturnTest, BasicUsage) {
  73. TupleLiteral unit(DummyLoc);
  74. Return empty_return(DummyLoc, &unit, /*is_omitted_expression=*/true);
  75. EXPECT_THAT(empty_return, MatchesEmptyReturn());
  76. EXPECT_THAT(&empty_return, MatchesEmptyReturn());
  77. EXPECT_THAT(empty_return, Not(MatchesReturn(_)));
  78. IntLiteral int_val(DummyLoc, 42);
  79. Return explicit_return(DummyLoc, &int_val, /*is_omitted_expression=*/false);
  80. EXPECT_THAT(explicit_return, MatchesReturn(MatchesLiteral(42)));
  81. EXPECT_THAT(explicit_return, Not(MatchesEmptyReturn()));
  82. EXPECT_THAT(int_val, Not(MatchesEmptyReturn()));
  83. EXPECT_THAT(int_val, Not(MatchesReturn(_)));
  84. }
  85. TEST(MatchesFunctionDeclarationTest, BasicUsage) {
  86. TuplePattern params(DummyLoc, {});
  87. Block body(DummyLoc, {});
  88. FunctionDeclaration decl(DummyLoc, "Foo", {}, &params,
  89. ReturnTerm::Omitted(DummyLoc), &body);
  90. EXPECT_THAT(decl, MatchesFunctionDeclaration());
  91. EXPECT_THAT(&decl, MatchesFunctionDeclaration());
  92. EXPECT_THAT(decl, MatchesFunctionDeclaration().WithName("Foo"));
  93. EXPECT_THAT(decl, MatchesFunctionDeclaration().WithBody(_));
  94. EXPECT_THAT(decl, MatchesFunctionDeclaration().WithName("Foo").WithBody(_));
  95. EXPECT_THAT(decl, MatchesFunctionDeclaration().WithBody(_).WithName("Foo"));
  96. EXPECT_THAT(decl, Not(MatchesFunctionDeclaration().WithName("Bar")));
  97. EXPECT_THAT(decl,
  98. Not(MatchesFunctionDeclaration().WithBody(MatchesLiteral(0))));
  99. FunctionDeclaration forward_decl(DummyLoc, "Foo", {}, &params,
  100. ReturnTerm::Omitted(DummyLoc), std::nullopt);
  101. EXPECT_THAT(forward_decl, MatchesFunctionDeclaration().WithName("Foo"));
  102. EXPECT_THAT(forward_decl, Not(MatchesFunctionDeclaration().WithBody(_)));
  103. EXPECT_THAT(body, Not(MatchesFunctionDeclaration()));
  104. }
  105. } // namespace
  106. } // namespace Carbon