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

Function TestEnableErrorOnBadPresenceTest

cel/cel_test.go:3581–3644  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

3579}
3580
3581func TestEnableErrorOnBadPresenceTest(t *testing.T) {
3582 env := testEnv(t,
3583 OptionalTypes(),
3584 EnableErrorOnBadPresenceTest(true),
3585 )
3586 adapter := env.TypeAdapter()
3587 tests := []struct {
3588 expr string
3589 in map[string]any
3590 out any
3591 }{
3592 {
3593 expr: `{}.?invalid`,
3594 out: types.OptionalNone,
3595 },
3596 {
3597 expr: `{'null_field': dyn(null)}.?null_field`,
3598 out: types.OptionalOf(types.NullValue),
3599 },
3600 {
3601 expr: `{'null_field': dyn(null)}.?null_field.?nested`,
3602 out: "no such key: nested",
3603 },
3604 {
3605 expr: `{'zero_field': dyn(0)}.?zero_field.?invalid`,
3606 out: "no such key: invalid",
3607 },
3608 {
3609 expr: `{0: dyn(0)}[?0].?invalid`,
3610 out: "no such key: invalid",
3611 },
3612 {
3613 expr: `{true: dyn(0)}[?false].?invalid`,
3614 out: types.OptionalNone,
3615 },
3616 {
3617 expr: `{true: dyn(0)}[?true].?invalid`,
3618 out: "no such key: invalid",
3619 },
3620 }
3621
3622 for i, tst := range tests {
3623 tc := tst
3624 t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
3625 ast, iss := env.Compile(tc.expr)
3626 if iss.Err() != nil {
3627 t.Fatalf("%v failed: %v", tc.expr, iss.Err())
3628 }
3629 prg, err := env.Program(ast)
3630 if err != nil {
3631 t.Errorf("env.Program() failed: %v", err)
3632 }
3633 out, _, err := prg.Eval(tc.in)
3634 if err != nil && err.Error() != tc.out {
3635 t.Errorf("prg.Eval() got %v, wanted %v", err, tc.out)
3636 }
3637 want := adapter.NativeToValue(tc.out)
3638 if err == nil && out.Equal(want) != types.True {

Callers

nothing calls this directly

Calls 12

OptionalOfFunction · 0.92
OptionalTypesFunction · 0.85
TypeAdapterMethod · 0.80
ErrMethod · 0.80
ProgramMethod · 0.80
testEnvFunction · 0.70
CompileMethod · 0.65
EvalMethod · 0.65
NativeToValueMethod · 0.65
EqualMethod · 0.65
ErrorMethod · 0.45

Tested by

no test coverage detected