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

Function TestUnparseEscapedCalls

parser/unparser_test.go:638–703  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

636}
637
638func TestUnparseEscapedCalls(t *testing.T) {
639 tests := []struct {
640 name string
641 in string
642 out string
643 }{
644 {name: "select_escaped", in: "a.`b-c`.d"},
645 {name: "select_escaped_in", in: "a.`in`"},
646 {name: "select_quoted", in: "a.`b-c`"},
647 {name: "opt_select_quoted", in: "a.?`b.c`"},
648 {name: "message_create_quoted", in: "MyType{`in`: false}"},
649 {name: "call_member_escaped", in: "a.`@mapInsert`(b, c)"},
650 {name: "call_global_escaped", in: "`@mapInsert`(a, b)"},
651 {name: "call_namespaced_escaped", in: "cel.`@mapInsert`(a, b)"},
652 {name: "call_escaped_dash", in: "a.`custom-fn`(b)"},
653 {name: "ident_at", in: "`@result` + 1"},
654 {name: "ident_escaped_dash", in: "`my-ident`"},
655 {name: "ident_escaped_keyword", in: "`in`"},
656 {name: "ident_leading_dot_escaped", in: ".`my-ident`"},
657 }
658
659 for _, tc := range tests {
660 t.Run(tc.name, func(t *testing.T) {
661 prsr, err := NewParser(
662 Macros(AllMacros...),
663 EnablePrattParser(true),
664 EnableOptionalSyntax(true),
665 EnableIdentEscapeSyntax(true),
666 EnableCallEscapeSyntax(true),
667 )
668 if err != nil {
669 t.Fatalf("NewParser() failed: %v", err)
670 }
671 p, iss := prsr.Parse(common.NewTextSource(tc.in))
672 if len(iss.GetErrors()) > 0 {
673 t.Fatalf("parser.Parse(%s) failed: %v", tc.in, iss.ToDisplayString())
674 }
675 out, err := Unparse(p.Expr(), p.SourceInfo(), EscapeIdentifiers(true))
676 if err != nil {
677 t.Fatalf("Unparse(%s) failed: %v", tc.in, err)
678 }
679 want := tc.in
680 if tc.out != "" {
681 want = tc.out
682 }
683 if out != want {
684 t.Errorf("Unparse() got '%s', wanted '%s'", out, want)
685 }
686 p2, iss := prsr.Parse(common.NewTextSource(out))
687 if len(iss.GetErrors()) > 0 {
688 t.Fatalf("parser.Parse(%s) roundtrip failed: %v", out, iss.ToDisplayString())
689 }
690 before, err := ast.ExprToProto(p.Expr())
691 if err != nil {
692 t.Fatalf("ast.ExprToProto() failed: %v", err)
693 }
694 after, err := ast.ExprToProto(p2.Expr())
695 if err != nil {

Callers

nothing calls this directly

Calls 15

ParseMethod · 0.95
NewTextSourceFunction · 0.92
ExprToProtoFunction · 0.92
EnablePrattParserFunction · 0.85
EnableOptionalSyntaxFunction · 0.85
EnableIdentEscapeSyntaxFunction · 0.85
EnableCallEscapeSyntaxFunction · 0.85
UnparseFunction · 0.85
EscapeIdentifiersFunction · 0.85
GetErrorsMethod · 0.80
NewParserFunction · 0.70
MacrosFunction · 0.70

Tested by

no test coverage detected