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

Function TestConvertExpr

common/ast/conversion_test.go:368–508  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

366}
367
368func TestConvertExpr(t *testing.T) {
369 fac := ast.NewExprFactory()
370 tests := []struct {
371 expr string
372 wantExpr ast.Expr
373 macroCalls map[int64]ast.Expr
374 }{
375 {
376 expr: `true`,
377 wantExpr: fac.NewLiteral(1, types.True),
378 },
379 {
380 expr: `a`,
381 wantExpr: fac.NewIdent(1, "a"),
382 },
383 {
384 expr: `a.b`,
385 wantExpr: fac.NewSelect(2, fac.NewIdent(1, "a"), "b"),
386 },
387 {
388 expr: `has(a.b)`,
389 wantExpr: fac.NewPresenceTest(4, fac.NewIdent(2, "a"), "b"),
390 macroCalls: map[int64]ast.Expr{
391 4: fac.NewCall(0, "has", fac.NewSelect(3, fac.NewIdent(2, "a"), "b")),
392 },
393 },
394 {
395 expr: `!a`,
396 wantExpr: fac.NewCall(1, "!_", fac.NewIdent(2, "a")),
397 },
398 {
399 expr: `a.size()`,
400 wantExpr: fac.NewMemberCall(2, "size", fac.NewIdent(1, "a")),
401 },
402 {
403 expr: `[a]`,
404 wantExpr: fac.NewList(1, []ast.Expr{fac.NewIdent(2, "a")}, []int32{}),
405 },
406 {
407 expr: `[?a]`,
408 wantExpr: fac.NewList(1, []ast.Expr{fac.NewIdent(2, "a")}, []int32{0}),
409 },
410 {
411 expr: `{'string': 42}`,
412 wantExpr: fac.NewMap(1, []ast.EntryExpr{
413 fac.NewMapEntry(2,
414 fac.NewLiteral(3, types.String("string")),
415 fac.NewLiteral(4, types.Int(42)),
416 false),
417 }),
418 },
419 {
420 expr: `{?'string': a.?b}`,
421 wantExpr: fac.NewMap(1, []ast.EntryExpr{
422 fac.NewMapEntry(2,
423 fac.NewLiteral(3, types.String("string")),
424 fac.NewCall(6, "_?._", fac.NewIdent(4, "a"), fac.NewLiteral(5, types.String("b"))),
425 true),

Callers

nothing calls this directly

Calls 15

NewLiteralMethod · 0.95
NewIdentMethod · 0.95
NewSelectMethod · 0.95
NewPresenceTestMethod · 0.95
NewCallMethod · 0.95
NewMemberCallMethod · 0.95
NewListMethod · 0.95
NewMapMethod · 0.95
NewMapEntryMethod · 0.95
NewStructMethod · 0.95
NewStructFieldMethod · 0.95
NewComprehensionMethod · 0.95

Tested by

no test coverage detected