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

Function TestEnableErrorOnBadPresenceTest

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

Source from the content-addressed store, hash-verified

3806}
3807
3808func TestEnableErrorOnBadPresenceTest(t *testing.T) {
3809 env := testEnv(t,
3810 OptionalTypes(),
3811 EnableErrorOnBadPresenceTest(true),
3812 )
3813 adapter := env.TypeAdapter()
3814 tests := []struct {
3815 expr string
3816 in map[string]any
3817 out any
3818 }{
3819 {
3820 expr: `{}.?invalid`,
3821 out: types.OptionalNone,
3822 },
3823 {
3824 expr: `{'null_field': dyn(null)}.?null_field`,
3825 out: types.OptionalOf(types.NullValue),
3826 },
3827 {
3828 expr: `{'null_field': dyn(null)}.?null_field.?nested`,
3829 out: "no such key: nested",
3830 },
3831 {
3832 expr: `{'zero_field': dyn(0)}.?zero_field.?invalid`,
3833 out: "no such key: invalid",
3834 },
3835 {
3836 expr: `{0: dyn(0)}[?0].?invalid`,
3837 out: "no such key: invalid",
3838 },
3839 {
3840 expr: `{true: dyn(0)}[?false].?invalid`,
3841 out: types.OptionalNone,
3842 },
3843 {
3844 expr: `{true: dyn(0)}[?true].?invalid`,
3845 out: "no such key: invalid",
3846 },
3847 }
3848
3849 for i, tst := range tests {
3850 tc := tst
3851 t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
3852 ast, iss := env.Compile(tc.expr)
3853 if iss.Err() != nil {
3854 t.Fatalf("%v failed: %v", tc.expr, iss.Err())
3855 }
3856 prg, err := env.Program(ast)
3857 if err != nil {
3858 t.Errorf("env.Program() failed: %v", err)
3859 }
3860 out, _, err := prg.Eval(tc.in)
3861 if err != nil && err.Error() != tc.out {
3862 t.Errorf("prg.Eval() got %v, wanted %v", err, tc.out)
3863 }
3864 want := adapter.NativeToValue(tc.out)
3865 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