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

Function TestAstToProto

cel/io_test.go:79–130  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

77}
78
79func TestAstToProto(t *testing.T) {
80 stdEnv, _ := NewEnv(Declarations(
81 decls.NewVar("a", decls.Dyn),
82 decls.NewVar("b", decls.Dyn),
83 ))
84 ast, iss := stdEnv.Parse("a + b")
85 if iss.Err() != nil {
86 t.Fatalf("Parse('a + b') failed: %v", iss.Err())
87 }
88 parsed, err := AstToParsedExpr(ast)
89 if err != nil {
90 t.Fatalf("AstToParsedExpr() failed: %v", err)
91 }
92 ast2 := ParsedExprToAst(parsed)
93 if !proto.Equal(ast2.Expr(), ast.Expr()) {
94 t.Errorf("got expr %v, wanted %v", ast2, ast)
95 }
96 ast3 := ParsedExprToAstWithSource(parsed, ast.Source())
97 if !proto.Equal(ast3.Expr(), ast.Expr()) {
98 t.Errorf("got expr %v, wanted %v", ast2, ast)
99 }
100 if ast3.Source() != ast.Source() {
101 t.Errorf("got source %v, wanted %v", ast3.Source(), ast.Source())
102 }
103
104 _, err = AstToCheckedExpr(ast)
105 if err == nil {
106 t.Error("expected error converting unchecked ast")
107 }
108 ast, iss = stdEnv.Check(ast)
109 if iss != nil && iss.Err() != nil {
110 t.Fatalf("stdEnv.Check(ast) failed: %v", iss.Err())
111 }
112 checked, err := AstToCheckedExpr(ast)
113 if err != nil {
114 t.Fatalf("AstToCheckedExpr(ast) failed: %v", err)
115 }
116 ast4 := CheckedExprToAst(checked)
117 if !proto.Equal(ast4.Expr(), ast.Expr()) {
118 t.Fatalf("got ast %v, wanted %v", ast4, ast)
119 }
120 ast5, err := CheckedExprToAstWithSource(checked, ast.Source())
121 if err != nil {
122 t.Fatalf("CheckedExprToAstWithSource() failed: %v", err)
123 }
124 if !proto.Equal(ast5.Expr(), ast.Expr()) {
125 t.Errorf("got expr %v, wanted %v", ast5, ast)
126 }
127 if ast5.Source() != ast.Source() {
128 t.Errorf("got source %v, wanted %v", ast5.Source(), ast.Source())
129 }
130}
131
132func TestAstToString(t *testing.T) {
133 stdEnv, err := NewEnv()

Callers

nothing calls this directly

Calls 15

ParseMethod · 0.95
CheckMethod · 0.95
NewVarFunction · 0.92
ExprInterface · 0.92
DeclarationsFunction · 0.85
AstToParsedExprFunction · 0.85
ParsedExprToAstFunction · 0.85
AstToCheckedExprFunction · 0.85
CheckedExprToAstFunction · 0.85
ErrMethod · 0.80

Tested by

no test coverage detected