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

Function TestParse

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

Source from the content-addressed store, hash-verified

2216}
2217
2218func TestParse(t *testing.T) {
2219 defaultParser := newTestParser(t)
2220 for i, tst := range testCases {
2221 name := fmt.Sprintf("%d %s", i, tst.I)
2222 // Local variable required as the closure will reference the value for the last
2223 // 'tst' value rather than the local 'tc' instance declared within the loop.
2224 tc := tst
2225 t.Run(name, func(t *testing.T) {
2226 // Runs the tests in parallel to ensure that there are no data races
2227 // due to shared mutable state across tests.
2228 t.Parallel()
2229 p := defaultParser
2230 if len(tc.Opts) > 0 {
2231 p = newTestParser(t, tc.Opts...)
2232 }
2233 src := common.NewTextSource(tc.I)
2234 parsed, errors := p.Parse(src)
2235 if len(errors.GetErrors()) > 0 {
2236 actualErr := errors.ToDisplayString()
2237 if tc.E == "" {
2238 t.Fatalf("Unexpected errors: %v", actualErr)
2239 } else if !test.Compare(actualErr, tc.E) {
2240 t.Fatal(test.DiffMessage("Error mismatch", actualErr, tc.E))
2241 }
2242 return
2243 } else if tc.E != "" {
2244 t.Fatalf("Expected error not thrown: '%s'", tc.E)
2245 }
2246 failureDisplayMethod := fmt.Sprintf("Parse(\"%s\")", tc.I)
2247 actualWithKind := debug.ToAdornedDebugString(parsed.Expr(), &kindAndIDAdorner{})
2248 if !test.Compare(actualWithKind, tc.P) {
2249 t.Fatal(test.DiffMessage(fmt.Sprintf("Structure - %s", failureDisplayMethod), actualWithKind, tc.P))
2250 }
2251
2252 if tc.L != "" {
2253 actualWithLocation := debug.ToAdornedDebugString(parsed.Expr(), &locationAdorner{parsed.SourceInfo()})
2254 if !test.Compare(actualWithLocation, tc.L) {
2255 t.Fatal(test.DiffMessage(fmt.Sprintf("Location - %s", failureDisplayMethod), actualWithLocation, tc.L))
2256 }
2257 }
2258
2259 if tc.M != "" {
2260 actualAdornedMacroCalls := convertMacroCallsToString(parsed.SourceInfo())
2261 if !test.Compare(actualAdornedMacroCalls, tc.M) {
2262 t.Fatal(test.DiffMessage(fmt.Sprintf("Macro Calls - %s", failureDisplayMethod), actualAdornedMacroCalls, tc.M))
2263 }
2264 }
2265
2266 // Verify there are no unused IDs in the source info.
2267 astIDs := parsed.IDs()
2268 unusedIDs := []int64{}
2269 for id := range parsed.SourceInfo().OffsetRanges() {
2270 if !astIDs[id] {
2271 unusedIDs = append(unusedIDs, id)
2272 }
2273 }
2274 if len(unusedIDs) > 0 {
2275 t.Errorf("SourceInfo has offset range for IDs %v, but no such nodes exists in AST: %s",

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
GetErrorsMethod · 0.80
OffsetRangesMethod · 0.80
GetOffsetRangeMethod · 0.80
ParseMethod · 0.65

Tested by

no test coverage detected