forked from carbon-language/carbon-lang
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ast_test_matchers_test.cpp
162 lines (136 loc) · 6.52 KB
/
ast_test_matchers_test.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
// Exceptions. See /LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
#include "explorer/ast/ast_test_matchers.h"
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include "explorer/ast/declaration.h"
#include "explorer/ast/expression.h"
#include "explorer/ast/pattern.h"
#include "explorer/ast/statement.h"
#include "explorer/base/arena.h"
namespace Carbon::Testing {
namespace {
using ::testing::_;
using ::testing::ElementsAre;
using ::testing::IsEmpty;
using ::testing::Not;
static constexpr SourceLocation DummyLoc("dummy", 0, FileKind::Main);
TEST(BlockContentsAreTest, BasicUsage) {
Block empty_block(DummyLoc, {});
EXPECT_THAT(empty_block, BlockContentsAre(IsEmpty()));
EXPECT_THAT(&empty_block, BlockContentsAre(IsEmpty()));
Break break_node(DummyLoc);
EXPECT_THAT(break_node, Not(BlockContentsAre(_)));
Block break_block(DummyLoc, {&break_node});
EXPECT_THAT(break_block, Not(BlockContentsAre(IsEmpty())));
}
TEST(MatchesLiteralTest, BasicUsage) {
IntLiteral literal(DummyLoc, 42);
EXPECT_THAT(literal, MatchesLiteral(42));
EXPECT_THAT(&literal, MatchesLiteral(42));
EXPECT_THAT(literal, Not(MatchesLiteral(43)));
EXPECT_THAT(StringLiteral(DummyLoc, "foo"), Not(MatchesLiteral(42)));
}
TEST(MatchesMulTest, BasicUsage) {
IntLiteral two(DummyLoc, 2);
IntLiteral three(DummyLoc, 3);
OperatorExpression mul(DummyLoc, Operator::Mul, {&two, &three});
EXPECT_THAT(mul, MatchesMul(MatchesLiteral(2), MatchesLiteral(3)));
EXPECT_THAT(&mul, MatchesMul(MatchesLiteral(2), MatchesLiteral(3)));
EXPECT_THAT(mul, MatchesMul(_, _));
EXPECT_THAT(mul, Not(MatchesMul(MatchesLiteral(2), MatchesLiteral(2))));
EXPECT_THAT(StringLiteral(DummyLoc, "foo"), Not(MatchesMul(_, _)));
EXPECT_THAT(OperatorExpression(DummyLoc, Operator::Deref, {&two}),
Not(MatchesMul(_, _)));
OperatorExpression nested(DummyLoc, Operator::Mul, {&two, &mul});
EXPECT_THAT(nested,
MatchesMul(MatchesLiteral(2),
MatchesMul(MatchesLiteral(2), MatchesLiteral(3))));
}
TEST(MatchesBinaryOpTest, BasicUsage) {
IntLiteral two(DummyLoc, 2);
IntLiteral three(DummyLoc, 3);
// Testing of MatchesMul provides most of the coverage for these matchers,
// since they are thin wrappers around a common implementation. We only test
// the others enough to detect copy-paste errors in the wrappers.
EXPECT_THAT(OperatorExpression(DummyLoc, Operator::Add, {&two, &three}),
MatchesAdd(MatchesLiteral(2), MatchesLiteral(3)));
EXPECT_THAT(OperatorExpression(DummyLoc, Operator::And, {&two, &three}),
MatchesAnd(MatchesLiteral(2), MatchesLiteral(3)));
EXPECT_THAT(OperatorExpression(DummyLoc, Operator::Eq, {&two, &three}),
MatchesEq(MatchesLiteral(2), MatchesLiteral(3)));
EXPECT_THAT(OperatorExpression(DummyLoc, Operator::Or, {&two, &three}),
MatchesOr(MatchesLiteral(2), MatchesLiteral(3)));
EXPECT_THAT(OperatorExpression(DummyLoc, Operator::Sub, {&two, &three}),
MatchesSub(MatchesLiteral(2), MatchesLiteral(3)));
}
TEST(MatchesReturnTest, BasicUsage) {
TupleLiteral unit(DummyLoc);
ReturnExpression empty_return(DummyLoc, &unit,
/*is_omitted_expression=*/true);
EXPECT_THAT(empty_return, MatchesEmptyReturn());
EXPECT_THAT(&empty_return, MatchesEmptyReturn());
EXPECT_THAT(empty_return, Not(MatchesReturn(_)));
IntLiteral int_val(DummyLoc, 42);
ReturnExpression explicit_return(DummyLoc, &int_val,
/*is_omitted_expression=*/false);
EXPECT_THAT(explicit_return, MatchesReturn(MatchesLiteral(42)));
EXPECT_THAT(explicit_return, Not(MatchesEmptyReturn()));
EXPECT_THAT(int_val, Not(MatchesEmptyReturn()));
EXPECT_THAT(int_val, Not(MatchesReturn(_)));
}
TEST(MatchesFunctionDeclarationTest, BasicUsage) {
TuplePattern params(DummyLoc, {});
Block body(DummyLoc, {});
FunctionDeclaration decl(DummyLoc, DeclaredName(DummyLoc, "Foo"), {},
std::nullopt, ¶ms, ReturnTerm::Omitted(DummyLoc),
&body, VirtualOverride::None);
EXPECT_THAT(decl, MatchesFunctionDeclaration());
EXPECT_THAT(&decl, MatchesFunctionDeclaration());
EXPECT_THAT(decl, MatchesFunctionDeclaration().WithName("Foo"));
EXPECT_THAT(decl, MatchesFunctionDeclaration().WithBody(_));
EXPECT_THAT(decl, MatchesFunctionDeclaration().WithName("Foo").WithBody(_));
EXPECT_THAT(decl, MatchesFunctionDeclaration().WithBody(_).WithName("Foo"));
EXPECT_THAT(decl, Not(MatchesFunctionDeclaration().WithName("Bar")));
EXPECT_THAT(decl,
Not(MatchesFunctionDeclaration().WithBody(MatchesLiteral(0))));
FunctionDeclaration forward_decl(
DummyLoc, DeclaredName(DummyLoc, "Foo"), {}, std::nullopt, ¶ms,
ReturnTerm::Omitted(DummyLoc), std::nullopt, VirtualOverride::None);
EXPECT_THAT(forward_decl, MatchesFunctionDeclaration().WithName("Foo"));
EXPECT_THAT(forward_decl, Not(MatchesFunctionDeclaration().WithBody(_)));
EXPECT_THAT(body, Not(MatchesFunctionDeclaration()));
}
TEST(MatchesUnimplementedExpressionTest, BasicUsage) {
IntLiteral two(DummyLoc, 2);
IntLiteral three(DummyLoc, 3);
UnimplementedExpression unimplemented(DummyLoc, "DummyLabel", &two, &three);
EXPECT_THAT(unimplemented, MatchesUnimplementedExpression(
"DummyLabel", ElementsAre(MatchesLiteral(2),
MatchesLiteral(3))));
EXPECT_THAT(
&unimplemented,
MatchesUnimplementedExpression(
"DummyLabel", ElementsAre(MatchesLiteral(2), MatchesLiteral(3))));
EXPECT_THAT(
unimplemented,
Not(MatchesUnimplementedExpression(
"WrongLabel", ElementsAre(MatchesLiteral(2), MatchesLiteral(3)))));
EXPECT_THAT(unimplemented,
Not(MatchesUnimplementedExpression("DummyLabel", IsEmpty())));
EXPECT_THAT(two,
Not(MatchesUnimplementedExpression("DummyLabel", IsEmpty())));
}
TEST(ASTDeclarationsTest, BasicUsage) {
TuplePattern params(DummyLoc, {});
Block body(DummyLoc, {});
FunctionDeclaration decl(DummyLoc, DeclaredName(DummyLoc, "Foo"), {},
std::nullopt, ¶ms, ReturnTerm::Omitted(DummyLoc),
&body, VirtualOverride::None);
AST ast = {.declarations = {&decl}};
EXPECT_THAT(ast, ASTDeclarations(ElementsAre(MatchesFunctionDeclaration())));
EXPECT_THAT(ast, Not(ASTDeclarations(IsEmpty())));
}
} // namespace
} // namespace Carbon::Testing