MCPcopy Create free account
hub / github.com/cel-expr/cel-go / TestASTCopy

Function TestASTCopy

common/ast/ast_test.go:32–85  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

30)
31
32func TestASTCopy(t *testing.T) {
33 tests := []string{
34 `'a' == 'b'`,
35 `'a'.size()`,
36 `size('a')`,
37 `has({'a': 1}.a)`,
38 `{'a': 1}`,
39 `{'a': 1}['a']`,
40 `[1, 2, 3].exists(i, i % 2 == 1)`,
41 `google.expr.proto3.test.TestAllTypes{}`,
42 `google.expr.proto3.test.TestAllTypes{repeated_int32: [1, 2]}`,
43 }
44
45 for _, tst := range tests {
46 checked := mustTypeCheck(t, tst)
47 copyChecked := ast.Copy(checked)
48 if !reflect.DeepEqual(copyChecked.Expr(), checked.Expr()) {
49 t.Errorf("Copy() got expr %v, wanted %v", copyChecked.Expr(), checked.Expr())
50 }
51 if !reflect.DeepEqual(copyChecked.SourceInfo(), checked.SourceInfo()) {
52 t.Errorf("Copy() got source info %v, wanted %v", copyChecked.SourceInfo(), checked.SourceInfo())
53 }
54 copyParsed := ast.Copy(ast.NewAST(checked.Expr(), checked.SourceInfo()))
55 if !reflect.DeepEqual(copyParsed.Expr(), checked.Expr()) {
56 t.Errorf("Copy() got expr %v, wanted %v", copyParsed.Expr(), checked.Expr())
57 }
58 if !reflect.DeepEqual(copyParsed.SourceInfo(), checked.SourceInfo()) {
59 t.Errorf("Copy() got source info %v, wanted %v", copyParsed.SourceInfo(), checked.SourceInfo())
60 }
61 checkedPB, err := ast.ToProto(checked)
62 if err != nil {
63 t.Errorf("ast.ToProto() failed: %v", err)
64 }
65 copyCheckedPB, err := ast.ToProto(copyChecked)
66 if err != nil {
67 t.Errorf("ast.ToProto() failed: %v", err)
68 }
69 if !proto.Equal(checkedPB, copyCheckedPB) {
70 t.Errorf("Copy() produced different proto results, got %v, wanted %v",
71 prototext.Format(checkedPB), prototext.Format(copyCheckedPB))
72 }
73 checkedRoundtrip, err := ast.ToAST(checkedPB)
74 if err != nil {
75 t.Errorf("ast.ToAST() failed: %v", err)
76 }
77 same := reflect.DeepEqual(checked.Expr(), checkedRoundtrip.Expr()) &&
78 reflect.DeepEqual(checked.ReferenceMap(), checkedRoundtrip.ReferenceMap()) &&
79 reflect.DeepEqual(checked.TypeMap(), checkedRoundtrip.TypeMap()) &&
80 reflect.DeepEqual(checked.SourceInfo().MacroCalls(), checkedRoundtrip.SourceInfo().MacroCalls())
81 if !same {
82 t.Errorf("Roundtrip got %v, wanted %v", checkedRoundtrip, checked)
83 }
84 }
85}
86
87func TestASTJsonNames(t *testing.T) {
88 tests := []string{

Callers

nothing calls this directly

Calls 11

CopyFunction · 0.92
NewASTFunction · 0.92
ToProtoFunction · 0.92
ToASTFunction · 0.92
mustTypeCheckFunction · 0.85
ReferenceMapMethod · 0.80
TypeMapMethod · 0.80
ExprMethod · 0.65
EqualMethod · 0.65
SourceInfoMethod · 0.45
MacroCallsMethod · 0.45

Tested by

no test coverage detected