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

Function TestParse

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

Source from the content-addressed store, hash-verified

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