forked from cel-expr/cel-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathast_test.cc
More file actions
188 lines (162 loc) · 6.32 KB
/
Copy pathast_test.cc
File metadata and controls
188 lines (162 loc) · 6.32 KB
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
// Copyright 2022 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "common/ast.h"
#include <utility>
#include "absl/container/flat_hash_map.h"
#include "common/expr.h"
#include "common/source.h"
#include "internal/testing.h"
namespace cel {
namespace {
using ::testing::Pointee;
using ::testing::Truly;
TEST(AstImpl, RawExprCtor) {
// arrange
// make ast for 2 + 1 == 3
Expr expr;
auto& call = expr.mutable_call_expr();
expr.set_id(5);
call.set_function("_==_");
auto& eq_lhs = call.mutable_args().emplace_back();
eq_lhs.mutable_call_expr().set_function("_+_");
eq_lhs.set_id(3);
auto& sum_lhs = eq_lhs.mutable_call_expr().mutable_args().emplace_back();
sum_lhs.mutable_const_expr().set_int_value(2);
sum_lhs.set_id(1);
auto& sum_rhs = eq_lhs.mutable_call_expr().mutable_args().emplace_back();
sum_rhs.mutable_const_expr().set_int_value(1);
sum_rhs.set_id(2);
auto& eq_rhs = call.mutable_args().emplace_back();
eq_rhs.mutable_const_expr().set_int_value(3);
eq_rhs.set_id(4);
SourceInfo source_info;
source_info.mutable_positions()[5] = 6;
// act
Ast ast(std::move(expr), std::move(source_info));
// assert
ASSERT_FALSE(ast.is_checked());
EXPECT_EQ(ast.GetTypeOrDyn(1), TypeSpec(DynTypeSpec()));
EXPECT_EQ(ast.GetReturnType(), TypeSpec(DynTypeSpec()));
EXPECT_EQ(ast.GetReference(1), nullptr);
EXPECT_TRUE(ast.root_expr().has_call_expr());
EXPECT_EQ(ast.root_expr().call_expr().function(), "_==_");
EXPECT_EQ(ast.root_expr().id(), 5); // Parser IDs leaf to root.
EXPECT_EQ(ast.source_info().positions().at(5), 6); // start pos of ==
}
TEST(AstImpl, CheckedExprCtor) {
Expr expr;
expr.mutable_ident_expr().set_name("int_value");
expr.set_id(1);
Reference ref;
ref.set_name("com.int_value");
Ast::ReferenceMap reference_map;
reference_map[1] = Reference(ref);
Ast::TypeMap type_map;
type_map[1] = TypeSpec(PrimitiveType::kInt64);
SourceInfo source_info;
source_info.set_syntax_version("1.0");
Ast ast(std::move(expr), std::move(source_info), std::move(reference_map),
std::move(type_map), "1.0");
ASSERT_TRUE(ast.is_checked());
EXPECT_EQ(ast.GetTypeOrDyn(1), TypeSpec(PrimitiveType::kInt64));
EXPECT_THAT(ast.GetReference(1), Pointee(Truly([&ref](const Reference& arg) {
return arg.name() == ref.name();
})));
EXPECT_EQ(ast.GetReturnType(), TypeSpec(PrimitiveType::kInt64));
EXPECT_TRUE(ast.root_expr().has_ident_expr());
EXPECT_EQ(ast.root_expr().ident_expr().name(), "int_value");
EXPECT_EQ(ast.root_expr().id(), 1);
EXPECT_EQ(ast.source_info().syntax_version(), "1.0");
EXPECT_EQ(ast.expr_version(), "1.0");
}
TEST(AstImpl, CheckedExprDeepCopy) {
Expr root;
root.set_id(3);
root.mutable_call_expr().set_function("_==_");
root.mutable_call_expr().mutable_args().resize(2);
auto& lhs = root.mutable_call_expr().mutable_args()[0];
auto& rhs = root.mutable_call_expr().mutable_args()[1];
Ast::TypeMap type_map;
Ast::ReferenceMap reference_map;
SourceInfo source_info;
type_map[3] = TypeSpec(PrimitiveType::kBool);
lhs.mutable_ident_expr().set_name("int_value");
lhs.set_id(1);
Reference ref;
ref.set_name("com.int_value");
reference_map[1] = std::move(ref);
type_map[1] = TypeSpec(PrimitiveType::kInt64);
rhs.mutable_const_expr().set_int_value(2);
rhs.set_id(2);
type_map[2] = TypeSpec(PrimitiveType::kInt64);
source_info.set_syntax_version("1.0");
Ast ast(std::move(root), std::move(source_info), std::move(reference_map),
std::move(type_map), "1.0");
ASSERT_TRUE(ast.IsChecked());
EXPECT_EQ(ast.GetTypeOrDyn(1), TypeSpec(PrimitiveType::kInt64));
EXPECT_THAT(ast.GetReference(1), Pointee(Truly([](const Reference& arg) {
return arg.name() == "com.int_value";
})));
EXPECT_EQ(ast.GetReturnType(), TypeSpec(PrimitiveType::kBool));
EXPECT_TRUE(ast.root_expr().has_call_expr());
EXPECT_EQ(ast.root_expr().call_expr().function(), "_==_");
EXPECT_EQ(ast.root_expr().id(), 3);
EXPECT_EQ(ast.source_info().syntax_version(), "1.0");
}
TEST(AstImpl, ComputeSourceLocation) {
SourceInfo source_info;
source_info.set_line_offsets({10, 20, 30});
source_info.mutable_positions()[1] = 0; // Start of first line
source_info.mutable_positions()[2] = 5; // Middle of first line
source_info.mutable_positions()[3] = 10; // ...
source_info.mutable_positions()[4] = 15;
source_info.mutable_positions()[5] = 20;
source_info.mutable_positions()[6] = 25;
Ast ast(Expr{}, std::move(source_info));
EXPECT_EQ(ast.ComputeSourceLocation(1), (SourceLocation{1, 0}));
EXPECT_EQ(ast.ComputeSourceLocation(2), (SourceLocation{1, 5}));
EXPECT_EQ(ast.ComputeSourceLocation(3), (SourceLocation{2, 0}));
EXPECT_EQ(ast.ComputeSourceLocation(4), (SourceLocation{2, 5}));
EXPECT_EQ(ast.ComputeSourceLocation(5), (SourceLocation{3, 0}));
EXPECT_EQ(ast.ComputeSourceLocation(6), (SourceLocation{3, 5}));
}
TEST(AstImpl, ComputeSourceLocationFailures) {
SourceInfo source_info;
source_info.set_line_offsets({10, 20});
source_info.mutable_positions()[1] = -1; // Negative position
source_info.mutable_positions()[2] = 25; // Beyond last line offset
// ID 3 is missing
Ast ast;
ast.mutable_source_info() = std::move(source_info);
EXPECT_EQ(ast.ComputeSourceLocation(1), SourceLocation{});
EXPECT_EQ(ast.ComputeSourceLocation(2), SourceLocation{});
EXPECT_EQ(ast.ComputeSourceLocation(3), SourceLocation{});
}
TEST(AstImpl, ComputeSourceLocationInvalidLineOffsets) {
{
// Empty line offsets
Ast ast;
EXPECT_EQ(ast.ComputeSourceLocation(1), SourceLocation{});
}
{
// Non-monotonic
SourceInfo source_info;
source_info.set_line_offsets({10, 5});
source_info.mutable_positions()[1] = 12;
Ast ast(Expr{}, std::move(source_info));
EXPECT_EQ(ast.ComputeSourceLocation(1), SourceLocation{});
}
}
} // namespace
} // namespace cel