forked from carbon-language/carbon-lang
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexpression_test.cpp
138 lines (114 loc) · 4.87 KB
/
expression_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
// 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/expression.h"
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include <string>
#include "explorer/ast/paren_contents.h"
#include "explorer/common/arena.h"
#include "llvm/Support/Casting.h"
namespace Carbon::Testing {
namespace {
using llvm::cast;
using testing::ElementsAre;
using testing::IsEmpty;
// Matches any `IntLiteral`.
MATCHER(IntField, "") { return arg->kind() == ExpressionKind::IntLiteral; }
static auto FakeSourceLoc(int line_num) -> SourceLocation {
return SourceLocation("<test>", line_num);
}
class ExpressionTest : public ::testing::Test {
protected:
Arena arena;
};
TEST_F(ExpressionTest, EmptyAsExpression) {
ParenContents<Expression> contents = {.elements = {},
.has_trailing_comma = false};
Nonnull<const Expression*> expression =
ExpressionFromParenContents(&arena, FakeSourceLoc(1), contents);
EXPECT_EQ(expression->source_loc(), FakeSourceLoc(1));
ASSERT_EQ(expression->kind(), ExpressionKind::TupleLiteral);
EXPECT_THAT(cast<TupleLiteral>(*expression).fields(), IsEmpty());
}
TEST_F(ExpressionTest, EmptyAsTuple) {
ParenContents<Expression> contents = {.elements = {},
.has_trailing_comma = false};
Nonnull<const Expression*> tuple =
TupleExpressionFromParenContents(&arena, FakeSourceLoc(1), contents);
EXPECT_EQ(tuple->source_loc(), FakeSourceLoc(1));
ASSERT_EQ(tuple->kind(), ExpressionKind::TupleLiteral);
EXPECT_THAT(cast<TupleLiteral>(*tuple).fields(), IsEmpty());
}
TEST_F(ExpressionTest, UnaryNoCommaAsExpression) {
// Equivalent to a code fragment like
// ```
// (
// 42
// )
// ```
ParenContents<Expression> contents = {
.elements = {arena.New<IntLiteral>(FakeSourceLoc(2), 42)},
.has_trailing_comma = false};
Nonnull<const Expression*> expression =
ExpressionFromParenContents(&arena, FakeSourceLoc(1), contents);
EXPECT_EQ(expression->source_loc(), FakeSourceLoc(2));
ASSERT_EQ(expression->kind(), ExpressionKind::IntLiteral);
}
TEST_F(ExpressionTest, UnaryNoCommaAsTuple) {
ParenContents<Expression> contents = {
.elements = {arena.New<IntLiteral>(FakeSourceLoc(2), 42)},
.has_trailing_comma = false};
Nonnull<const Expression*> tuple =
TupleExpressionFromParenContents(&arena, FakeSourceLoc(1), contents);
EXPECT_EQ(tuple->source_loc(), FakeSourceLoc(1));
ASSERT_EQ(tuple->kind(), ExpressionKind::TupleLiteral);
EXPECT_THAT(cast<TupleLiteral>(*tuple).fields(), ElementsAre(IntField()));
}
TEST_F(ExpressionTest, UnaryWithCommaAsExpression) {
ParenContents<Expression> contents = {
.elements = {arena.New<IntLiteral>(FakeSourceLoc(2), 42)},
.has_trailing_comma = true};
Nonnull<const Expression*> expression =
ExpressionFromParenContents(&arena, FakeSourceLoc(1), contents);
EXPECT_EQ(expression->source_loc(), FakeSourceLoc(1));
ASSERT_EQ(expression->kind(), ExpressionKind::TupleLiteral);
EXPECT_THAT(cast<TupleLiteral>(*expression).fields(),
ElementsAre(IntField()));
}
TEST_F(ExpressionTest, UnaryWithCommaAsTuple) {
ParenContents<Expression> contents = {
.elements = {arena.New<IntLiteral>(FakeSourceLoc(2), 42)},
.has_trailing_comma = true};
Nonnull<const Expression*> tuple =
TupleExpressionFromParenContents(&arena, FakeSourceLoc(1), contents);
EXPECT_EQ(tuple->source_loc(), FakeSourceLoc(1));
ASSERT_EQ(tuple->kind(), ExpressionKind::TupleLiteral);
EXPECT_THAT(cast<TupleLiteral>(*tuple).fields(), ElementsAre(IntField()));
}
TEST_F(ExpressionTest, BinaryAsExpression) {
ParenContents<Expression> contents = {
.elements = {arena.New<IntLiteral>(FakeSourceLoc(2), 42),
arena.New<IntLiteral>(FakeSourceLoc(3), 42)},
.has_trailing_comma = true};
Nonnull<const Expression*> expression =
ExpressionFromParenContents(&arena, FakeSourceLoc(1), contents);
EXPECT_EQ(expression->source_loc(), FakeSourceLoc(1));
ASSERT_EQ(expression->kind(), ExpressionKind::TupleLiteral);
EXPECT_THAT(cast<TupleLiteral>(*expression).fields(),
ElementsAre(IntField(), IntField()));
}
TEST_F(ExpressionTest, BinaryAsTuple) {
ParenContents<Expression> contents = {
.elements = {arena.New<IntLiteral>(FakeSourceLoc(2), 42),
arena.New<IntLiteral>(FakeSourceLoc(3), 42)},
.has_trailing_comma = true};
Nonnull<const Expression*> tuple =
TupleExpressionFromParenContents(&arena, FakeSourceLoc(1), contents);
EXPECT_EQ(tuple->source_loc(), FakeSourceLoc(1));
ASSERT_EQ(tuple->kind(), ExpressionKind::TupleLiteral);
EXPECT_THAT(cast<TupleLiteral>(*tuple).fields(),
ElementsAre(IntField(), IntField()));
}
} // namespace
} // namespace Carbon::Testing