-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathCelTestSuiteYamlParser.java
More file actions
383 lines (353 loc) · 14.7 KB
/
CelTestSuiteYamlParser.java
File metadata and controls
383 lines (353 loc) · 14.7 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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
// Copyright 2025 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.
package dev.cel.testing.testrunner;
import static dev.cel.common.formats.YamlHelper.YamlNodeType.nodeType;
import static dev.cel.common.formats.YamlHelper.assertYamlType;
import static dev.cel.common.formats.YamlHelper.newBoolean;
import static dev.cel.common.formats.YamlHelper.newDouble;
import static dev.cel.common.formats.YamlHelper.newInteger;
import static dev.cel.common.formats.YamlHelper.newString;
import static dev.cel.common.formats.YamlHelper.parseYamlSource;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import dev.cel.common.CelIssue;
import dev.cel.common.annotations.Internal;
import dev.cel.common.formats.CelFileSource;
import dev.cel.common.formats.ParserContext;
import dev.cel.common.formats.YamlHelper.YamlNodeType;
import dev.cel.common.formats.YamlParserContextImpl;
import dev.cel.common.internal.CelCodePointArray;
import dev.cel.testing.testrunner.CelTestSuite.CelTestSection;
import dev.cel.testing.testrunner.CelTestSuite.CelTestSection.CelTestCase;
import dev.cel.testing.testrunner.CelTestSuite.CelTestSection.CelTestCase.Input.Binding;
import java.util.Optional;
import org.yaml.snakeyaml.nodes.MappingNode;
import org.yaml.snakeyaml.nodes.Node;
import org.yaml.snakeyaml.nodes.NodeTuple;
import org.yaml.snakeyaml.nodes.ScalarNode;
import org.yaml.snakeyaml.nodes.SequenceNode;
/**
* CelTestSuiteYamlParser intakes a YAML document that describes the structure of a CEL test suite,
* parses it then creates a {@link CelTestSuite}.
*
* <p>CEL Library Internals. Do Not Use.
*/
@Internal
public final class CelTestSuiteYamlParser {
/** Creates a new instance of {@link CelTestSuiteYamlParser}. */
public static CelTestSuiteYamlParser newInstance() {
return new CelTestSuiteYamlParser();
}
public CelTestSuite parse(String celTestSuiteYamlContent) throws CelTestSuiteException {
return parseYaml(celTestSuiteYamlContent, "<input>");
}
private CelTestSuite parseYaml(String celTestSuiteYamlContent, String description)
throws CelTestSuiteException {
Node node;
try {
node =
parseYamlSource(celTestSuiteYamlContent)
.orElseThrow(
() ->
new CelTestSuiteException(
String.format(
"YAML document empty or malformed: %s", celTestSuiteYamlContent)));
} catch (RuntimeException e) {
throw new CelTestSuiteException("YAML document is malformed: " + e.getMessage(), e);
}
CelFileSource testSuiteSource =
CelFileSource.newBuilder(CelCodePointArray.fromString(celTestSuiteYamlContent))
.setDescription(description)
.build();
ParserContext<Node> ctx = YamlParserContextImpl.newInstance(testSuiteSource);
CelTestSuite.Builder builder = parseTestSuite(ctx, node);
testSuiteSource = testSuiteSource.toBuilder().setPositionsMap(ctx.getIdToOffsetMap()).build();
if (!ctx.getIssues().isEmpty()) {
throw new CelTestSuiteException(CelIssue.toDisplayString(ctx.getIssues(), testSuiteSource));
}
return builder.setSource(testSuiteSource).build();
}
private CelTestSuite.Builder parseTestSuite(ParserContext<Node> ctx, Node node) {
CelTestSuite.Builder builder = CelTestSuite.newBuilder();
long id = ctx.collectMetadata(node);
if (!assertYamlType(ctx, id, node, YamlNodeType.MAP)) {
ctx.reportError(id, "Unknown test suite type: " + node.getTag());
return builder;
}
MappingNode rootNode = (MappingNode) node;
for (NodeTuple nodeTuple : rootNode.getValue()) {
Node keyNode = nodeTuple.getKeyNode();
long keyId = ctx.collectMetadata(keyNode);
if (!assertYamlType(ctx, keyId, keyNode, YamlNodeType.STRING, YamlNodeType.TEXT)) {
continue;
}
Node valueNode = nodeTuple.getValueNode();
String fieldName = ((ScalarNode) keyNode).getValue();
switch (fieldName) {
case "name":
builder.setName(newString(ctx, valueNode));
break;
case "description":
builder.setDescription(newString(ctx, valueNode));
break;
case "section":
case "sections":
builder.setSections(parseSections(ctx, valueNode));
break;
default:
ctx.reportError(keyId, "Unknown test suite tag: " + fieldName);
break;
}
}
return builder;
}
private ImmutableSet<CelTestSection> parseSections(ParserContext<Node> ctx, Node node) {
long valueId = ctx.collectMetadata(node);
ImmutableSet.Builder<CelTestSection> celTestSectionSetBuilder = ImmutableSet.builder();
if (!assertYamlType(ctx, valueId, node, YamlNodeType.LIST)) {
ctx.reportError(valueId, "Sections is not a list: " + node.getTag());
return celTestSectionSetBuilder.build();
}
SequenceNode sectionListNode = (SequenceNode) node;
for (Node elementNode : sectionListNode.getValue()) {
celTestSectionSetBuilder.add(parseSection(ctx, elementNode));
}
return celTestSectionSetBuilder.build();
}
private CelTestSection parseSection(ParserContext<Node> ctx, Node node) {
long valueId = ctx.collectMetadata(node);
if (!assertYamlType(ctx, valueId, node, YamlNodeType.MAP)) {
ctx.reportError(valueId, "Unknown section type: " + node.getTag());
return CelTestSection.newBuilder().build();
}
CelTestSection.Builder celTestSectionBuilder = CelTestSection.newBuilder();
MappingNode sectionNode = (MappingNode) node;
for (NodeTuple nodeTuple : sectionNode.getValue()) {
Node keyNode = nodeTuple.getKeyNode();
long keyId = ctx.collectMetadata(keyNode);
Node valueNode = nodeTuple.getValueNode();
String fieldName = ((ScalarNode) keyNode).getValue();
switch (fieldName) {
case "name":
celTestSectionBuilder.setName(newString(ctx, valueNode));
break;
case "description":
celTestSectionBuilder.setDescription(newString(ctx, valueNode));
break;
case "tests":
celTestSectionBuilder.setTests(parseTests(ctx, valueNode));
break;
default:
ctx.reportError(keyId, "Unknown test section tag: " + fieldName);
break;
}
}
return celTestSectionBuilder.build();
}
private ImmutableSet<CelTestCase> parseTests(ParserContext<Node> ctx, Node node) {
long valueId = ctx.collectMetadata(node);
ImmutableSet.Builder<CelTestCase> celTestCaseSetBuilder = ImmutableSet.builder();
if (!assertYamlType(ctx, valueId, node, YamlNodeType.LIST)) {
ctx.reportError(valueId, "Tests is not a list: " + node.getTag());
return celTestCaseSetBuilder.build();
}
SequenceNode testCasesListNode = (SequenceNode) node;
for (Node elementNode : testCasesListNode.getValue()) {
celTestCaseSetBuilder.add(parseTestCase(ctx, elementNode));
}
return celTestCaseSetBuilder.build();
}
private CelTestCase parseTestCase(ParserContext<Node> ctx, Node node) {
long valueId = ctx.collectMetadata(node);
CelTestCase.Builder celTestCaseBuilder = CelTestCase.newBuilder();
if (!assertYamlType(ctx, valueId, node, YamlNodeType.MAP)) {
ctx.reportError(valueId, "Testcase is not a map: " + node.getTag());
return celTestCaseBuilder.build();
}
MappingNode testCaseNode = (MappingNode) node;
for (NodeTuple nodeTuple : testCaseNode.getValue()) {
Node keyNode = nodeTuple.getKeyNode();
long keyId = ctx.collectMetadata(keyNode);
Node valueNode = nodeTuple.getValueNode();
String fieldName = ((ScalarNode) keyNode).getValue();
switch (fieldName) {
case "name":
celTestCaseBuilder.setName(newString(ctx, valueNode));
break;
case "description":
celTestCaseBuilder.setDescription(newString(ctx, valueNode));
break;
case "input":
celTestCaseBuilder.setInput(parseInput(ctx, valueNode));
break;
case "context_expr":
celTestCaseBuilder.setInput(parseContextExpr(ctx, valueNode));
break;
case "output":
celTestCaseBuilder.setOutput(parseOutput(ctx, valueNode));
break;
default:
ctx.reportError(keyId, "Unknown test case tag: " + fieldName);
break;
}
}
return celTestCaseBuilder.build();
}
private CelTestCase.Input parseInput(ParserContext<Node> ctx, Node node) {
long valueId = ctx.collectMetadata(node);
if (!assertYamlType(ctx, valueId, node, YamlNodeType.MAP)) {
ctx.reportError(valueId, "Input is not a map: " + node.getTag());
return CelTestCase.Input.ofNoInput();
}
MappingNode inputNode = (MappingNode) node;
ImmutableMap.Builder<String, Binding> bindingsBuilder = ImmutableMap.builder();
for (NodeTuple nodeTuple : inputNode.getValue()) {
Node valueNode = nodeTuple.getValueNode();
Optional<Binding> binding = parseBindingValueNode(ctx, valueNode);
binding.ifPresent(
b -> bindingsBuilder.put(((ScalarNode) nodeTuple.getKeyNode()).getValue(), b));
}
return CelTestCase.Input.ofBindings(bindingsBuilder.buildOrThrow());
}
private Optional<Binding> parseBindingValueNode(ParserContext<Node> ctx, Node node) {
long valueId = ctx.collectMetadata(node);
if (!assertYamlType(ctx, valueId, node, YamlNodeType.MAP)) {
ctx.reportError(valueId, "Input binding node is not a map: " + node.getTag());
return Optional.empty();
}
MappingNode bindingValueNode = (MappingNode) node;
if (bindingValueNode.getValue().size() != 1) {
ctx.reportError(valueId, "Input binding node must have exactly one value: " + node.getTag());
return Optional.empty();
}
for (NodeTuple nodeTuple : bindingValueNode.getValue()) {
Node keyNode = nodeTuple.getKeyNode();
long keyId = ctx.collectMetadata(keyNode);
Node valueNode = nodeTuple.getValueNode();
String fieldName = ((ScalarNode) keyNode).getValue();
switch (fieldName) {
case "value":
return Optional.of(Binding.ofValue(parseNodeValue(ctx, valueNode)));
case "expr":
return Optional.of(Binding.ofExpr(newString(ctx, valueNode)));
default:
ctx.reportError(keyId, "Unknown input binding value tag: " + fieldName);
break;
}
}
return Optional.empty();
}
// TODO: Create a CelTestSuiteNodeValue class to represent the value of a test suite
// node.
private Object parseNodeValue(ParserContext<Node> ctx, Node node) {
Object value = null;
Optional<YamlNodeType> yamlNodeType = nodeType(node.getTag().getValue());
if (yamlNodeType.isPresent()) {
switch (yamlNodeType.get()) {
case STRING:
case TEXT:
value = newString(ctx, node);
break;
case BOOLEAN:
value = newBoolean(ctx, node);
break;
case INTEGER:
value = newInteger(ctx, node);
break;
case DOUBLE:
value = newDouble(ctx, node);
break;
case MAP:
value = parseMap(ctx, node);
break;
case LIST:
value = parseList(ctx, node);
break;
}
}
return value;
}
private ImmutableMap<Object, Object> parseMap(ParserContext<Node> ctx, Node node) {
ImmutableMap.Builder<Object, Object> mapBuilder = ImmutableMap.builder();
MappingNode mapNode = (MappingNode) node;
mapNode
.getValue()
.forEach(
nodeTuple -> {
Node keyNode = nodeTuple.getKeyNode();
Node valueNode = nodeTuple.getValueNode();
mapBuilder.put(parseNodeValue(ctx, keyNode), parseNodeValue(ctx, valueNode));
});
return mapBuilder.buildOrThrow();
}
private ImmutableList<Object> parseList(ParserContext<Node> ctx, Node node) {
ImmutableList.Builder<Object> listBuilder = ImmutableList.builder();
SequenceNode listNode = (SequenceNode) node;
listNode.getValue().forEach(childNode -> listBuilder.add(parseNodeValue(ctx, childNode)));
return listBuilder.build();
}
private ImmutableList<Long> parseUnknown(ParserContext<Node> ctx, Node node) {
ImmutableList<Object> unknown = parseList(ctx, node);
ImmutableList.Builder<Long> unknownBuilder = ImmutableList.builder();
for (Object object : unknown) {
if (object instanceof Integer) {
unknownBuilder.add(Long.valueOf((Integer) object));
} else {
ctx.reportError(
ctx.collectMetadata(node),
"Only integer ids are supported in unknown list. Found: "
+ object.getClass().getName());
}
}
return unknownBuilder.build();
}
private CelTestCase.Input parseContextExpr(ParserContext<Node> ctx, Node node) {
long valueId = ctx.collectMetadata(node);
if (!assertYamlType(ctx, valueId, node, YamlNodeType.STRING)) {
ctx.reportError(valueId, "Input context is not a string: " + node.getTag());
return CelTestCase.Input.ofNoInput();
}
return CelTestCase.Input.ofContextExpr(newString(ctx, node));
}
private CelTestCase.Output parseOutput(ParserContext<Node> ctx, Node node) {
long valueId = ctx.collectMetadata(node);
if (!assertYamlType(ctx, valueId, node, YamlNodeType.MAP)) {
ctx.reportError(valueId, "Output is not a map: " + node.getTag());
return CelTestCase.Output.ofNoOutput();
}
MappingNode outputNode = (MappingNode) node;
for (NodeTuple nodeTuple : outputNode.getValue()) {
Node keyNode = nodeTuple.getKeyNode();
long keyId = ctx.collectMetadata(keyNode);
Node valueNode = nodeTuple.getValueNode();
String fieldName = ((ScalarNode) keyNode).getValue();
switch (fieldName) {
case "value":
return CelTestCase.Output.ofResultValue(parseNodeValue(ctx, valueNode));
case "expr":
return CelTestCase.Output.ofResultExpr(newString(ctx, valueNode));
case "error_set":
return CelTestCase.Output.ofEvalError(parseList(ctx, valueNode));
case "unknown":
return CelTestCase.Output.ofUnknownSet(parseUnknown(ctx, valueNode));
default:
ctx.reportError(keyId, "Unknown output tag: " + fieldName);
break;
}
}
return CelTestCase.Output.ofNoOutput();
}
private CelTestSuiteYamlParser() {}
}