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

Function TestOptionalValuesEvalErrorCases

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

Source from the content-addressed store, hash-verified

3994}
3995
3996func TestOptionalValuesEvalErrorCases(t *testing.T) {
3997 env := testEnv(t,
3998 OptionalTypes(),
3999 // Container and test message types.
4000 Container("google.expr.proto2.test"),
4001 Types(&proto2pb.TestAllTypes{}),
4002 // Test variables.
4003 Variable("x", OptionalType(IntType)),
4004 Variable("y", OptionalType(IntType)),
4005 Variable("z", IntType),
4006 )
4007 tests := []struct {
4008 expr string
4009 in map[string]any
4010 wantErr string
4011 }{
4012 {
4013 expr: `dyn(1).or(optional.of(2))`,
4014 wantErr: "no such overload",
4015 },
4016 {
4017 expr: `dyn(1).orValue(2)`,
4018 wantErr: "no such overload",
4019 },
4020 {
4021 expr: `optional.of(1/0).or(optional.of(2))`,
4022 wantErr: "division by zero",
4023 },
4024 {
4025 expr: `optional.of(1/0).orValue(2)`,
4026 wantErr: "division by zero",
4027 },
4028 }
4029 for i, tst := range tests {
4030 tc := tst
4031 t.Run(fmt.Sprintf("%s/%d", tc.expr, i), func(t *testing.T) {
4032 ast, iss := env.Compile(tc.expr)
4033 if iss.Err() != nil {
4034 t.Fatalf("%v failed: %v", tc.expr, iss.Err())
4035 }
4036 prg, err := env.Program(ast)
4037 if err != nil {
4038 t.Errorf("env.Program() failed: %v", err)
4039 }
4040 _, _, err = prg.Eval(tc.in)
4041 if err == nil {
4042 t.Fatalf("prg.Eval got nil error, wanted %v", tc.wantErr)
4043 }
4044 if !strings.Contains(err.Error(), tc.wantErr) {
4045 t.Errorf("prg.Eval() got %v, wanted %v", err, tc.wantErr)
4046 }
4047 })
4048 }
4049}
4050
4051func TestEnableErrorOnBadPresenceTest(t *testing.T) {
4052 env := testEnv(t,

Callers

nothing calls this directly

Calls 11

OptionalTypesFunction · 0.85
ErrMethod · 0.80
ProgramMethod · 0.80
testEnvFunction · 0.70
ContainerFunction · 0.70
TypesFunction · 0.70
VariableFunction · 0.70
CompileMethod · 0.65
EvalMethod · 0.65
ContainsMethod · 0.65
ErrorMethod · 0.45

Tested by

no test coverage detected