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

Function TestParse

parser/parser_test.go:2792–2904  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

2790}
2791
2792func TestParse(t *testing.T) {
2793 for _, pratt := range []bool{false, true} {
2794 name := fmt.Sprintf("enablePrattParser=%t", pratt)
2795 t.Run(name, func(t *testing.T) {
2796 defaultParser := newTestParser(t, EnablePrattParser(pratt))
2797 for i, tst := range testCases {
2798 name := fmt.Sprintf("%d %s", i, tst.I)
2799 // Local variable required as the closure will reference the value for the last
2800 // 'tst' value rather than the local 'tc' instance declared within the loop.
2801 tc := tst
2802 t.Run(name, func(t *testing.T) {
2803 // Runs the tests in parallel to ensure that there are no data races
2804 // due to shared mutable state across tests.
2805 t.Parallel()
2806 p := defaultParser
2807 if len(tc.Opts) > 0 {
2808 p = newTestParser(t, append([]Option{EnablePrattParser(pratt)}, tc.Opts...)...)
2809 }
2810 src := common.NewTextSource(tc.I)
2811 parsed, errors := p.Parse(src)
2812 wantP := tc.P
2813 wantE := tc.E
2814 wantL := tc.L
2815 wantM := tc.M
2816 if pratt {
2817 if tc.PrattP != "" {
2818 wantP = tc.PrattP
2819 }
2820 if tc.PrattE == "-" {
2821 wantE = ""
2822 } else if tc.PrattE != "" {
2823 wantE = tc.PrattE
2824 }
2825 if tc.PrattL != "" {
2826 wantL = tc.PrattL
2827 }
2828 if tc.PrattM != "" {
2829 wantM = tc.PrattM
2830 }
2831 }
2832 if len(errors.GetErrors()) > 0 {
2833 actualErr := errors.ToDisplayString()
2834 if wantE == "" {
2835 t.Fatalf("Unexpected errors: %v", actualErr)
2836 } else if !test.Compare(actualErr, wantE) {
2837 t.Fatal(test.DiffMessage("Error mismatch", actualErr, wantE))
2838 }
2839 return
2840 } else if wantE != "" {
2841 t.Fatalf("Expected error not thrown: '%s'", wantE)
2842 }
2843 failureDisplayMethod := fmt.Sprintf("Parse(\"%s\")", tc.I)
2844 if wantP != "" {
2845 actualWithKind := debug.ToAdornedDebugString(parsed.Expr(), &kindAndIDAdorner{})
2846 if !test.Compare(actualWithKind, wantP) {
2847 t.Fatal(test.DiffMessage(fmt.Sprintf("Structure - %s", failureDisplayMethod), actualWithKind, wantP))
2848 }
2849 }

Callers

nothing calls this directly

Calls 15

NewTextSourceFunction · 0.92
CompareFunction · 0.92
DiffMessageFunction · 0.92
ToAdornedDebugStringFunction · 0.92
ToDebugStringWithIDsFunction · 0.92
NewLocationFunction · 0.92
newTestParserFunction · 0.85
EnablePrattParserFunction · 0.85
GetErrorsMethod · 0.80
OffsetRangesMethod · 0.80
GetOffsetRangeMethod · 0.80

Tested by

no test coverage detected