Skip to content

Commit dc9d240

Browse files
ExprToString test case
1 parent dac9e7d commit dc9d240

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

cel/io_test.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ import (
2323
"google.golang.org/protobuf/proto"
2424

2525
"github.com/google/cel-go/checker/decls"
26+
celast "github.com/google/cel-go/common/ast"
27+
"github.com/google/cel-go/common/operators"
2628
"github.com/google/cel-go/common/types"
2729

2830
proto3pb "github.com/google/cel-go/test/proto3pb"
@@ -145,6 +147,34 @@ func TestAstToString(t *testing.T) {
145147
}
146148
}
147149

150+
func TestExprToString(t *testing.T) {
151+
stdEnv, err := NewEnv()
152+
if err != nil {
153+
t.Fatalf("NewEnv() failed: %v", err)
154+
}
155+
in := "a + b - (c ? (-d + 4) : e)"
156+
ast, iss := stdEnv.Parse(in)
157+
if iss.Err() != nil {
158+
t.Fatalf("stdEnv.Parse(%q) failed: %v", in, iss.Err())
159+
}
160+
expr, err := ExprToString(ast.NativeRep().Expr(), ast.NativeRep().SourceInfo())
161+
if err != nil {
162+
t.Fatalf("ExprToString(ast) failed: %v", err)
163+
}
164+
if expr != in {
165+
t.Errorf("got %v, wanted %v", expr, in)
166+
}
167+
navExpr := celast.NavigateAST(ast.NativeRep())
168+
condExpr := celast.MatchDescendants(navExpr, celast.FunctionMatcher(operators.Conditional))[0]
169+
expr, err = ExprToString(condExpr, ast.NativeRep().SourceInfo())
170+
if err != nil {
171+
t.Fatalf("ExprToString(ast) failed: %v", err)
172+
}
173+
if expr != `c ? (-d + 4) : e` {
174+
t.Errorf("got %v, wanted %v", expr, in)
175+
}
176+
}
177+
148178
func TestAstToStringNil(t *testing.T) {
149179
expr, err := AstToString(nil)
150180
if err == nil || !strings.Contains(err.Error(), "unsupported expr") {

0 commit comments

Comments
 (0)