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

Function TestOptionalValuesEval

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

Source from the content-addressed store, hash-verified

3367}
3368
3369func TestOptionalValuesEval(t *testing.T) {
3370 env := testEnv(t,
3371 OptionalTypes(),
3372 // Container and test message types.
3373 Container("google.expr.proto2.test"),
3374 Types(&proto2pb.TestAllTypes{}),
3375 // Test variables.
3376 Variable("m", MapType(StringType, MapType(StringType, StringType))),
3377 Variable("l", ListType(StringType)),
3378 Variable("optm", OptionalType(MapType(StringType, MapType(StringType, StringType)))),
3379 Variable("optl", OptionalType(ListType(StringType))),
3380 Variable("x", OptionalType(IntType)),
3381 Variable("y", OptionalType(IntType)),
3382 Variable("z", IntType),
3383 )
3384 adapter := env.TypeAdapter()
3385 tests := []struct {
3386 expr string
3387 in map[string]any
3388 out any
3389 }{
3390 {
3391 expr: `has({'foo': optional.none()}.foo)`,
3392 out: types.True,
3393 },
3394 {
3395 expr: `has({'foo': optional.none()}.foo.value)`,
3396 out: types.False,
3397 },
3398 {
3399 expr: `has({?'foo': optional.none()}.foo)`,
3400 out: types.False,
3401 },
3402 {
3403 expr: `has({?'foo': optional.none()}.foo.value)`,
3404 out: "no such key: foo",
3405 },
3406 {
3407 expr: `{}.?invalid`,
3408 out: types.OptionalNone,
3409 },
3410 {
3411 expr: `{'null_field': dyn(null)}.?null_field`,
3412 out: types.OptionalOf(types.NullValue),
3413 },
3414 {
3415 expr: `{'null_field': dyn(null)}.?null_field.?nested`,
3416 out: types.OptionalNone,
3417 },
3418 {
3419 expr: `{'zero_field': dyn(0)}.?zero_field.?invalid`,
3420 out: types.OptionalNone,
3421 },
3422 {
3423 expr: `{0: dyn(0)}[?0].?invalid`,
3424 out: types.OptionalNone,
3425 },
3426 {

Callers

nothing calls this directly

Calls 15

OptionalOfFunction · 0.92
StringTypeAlias · 0.92
IntTypeAlias · 0.92
OptionalTypesFunction · 0.85
TypeAdapterMethod · 0.80
ErrMethod · 0.80
ProgramMethod · 0.80
testEnvFunction · 0.70
ContainerFunction · 0.70
TypesFunction · 0.70
VariableFunction · 0.70
NativeToValueMethod · 0.65

Tested by

no test coverage detected