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

Function TestContextProto

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

Source from the content-addressed store, hash-verified

2457}
2458
2459func TestContextProto(t *testing.T) {
2460 descriptor := new(proto3pb.TestAllTypes).ProtoReflect().Descriptor()
2461 option := DeclareContextProto(descriptor)
2462 env := testEnv(t, option)
2463 expression := `
2464 single_int64 == 1
2465 && single_double == 1.0
2466 && single_bool == true
2467 && single_string == ''
2468 && single_nested_message == google.expr.proto3.test.TestAllTypes.NestedMessage{}
2469 && standalone_enum == google.expr.proto3.test.TestAllTypes.NestedEnum.FOO
2470 && single_duration == duration('5s')
2471 && single_timestamp == timestamp(63154820)
2472 && single_any == null
2473 && single_uint32_wrapper == null
2474 && single_uint64_wrapper == 0u
2475 && repeated_int32 == [1,2]
2476 && map_string_string == {'': ''}
2477 && map_int64_nested_type == {0 : google.expr.proto3.test.NestedTestAllTypes{}}`
2478 ast, iss := env.Compile(expression)
2479 if iss.Err() != nil {
2480 t.Fatalf("env.Compile(%s) failed: %s", expression, iss.Err())
2481 }
2482 prg, err := env.Program(ast)
2483 if err != nil {
2484 t.Fatalf("env.Program() failed: %v", err)
2485 }
2486 in := &proto3pb.TestAllTypes{
2487 SingleInt64: 1,
2488 SingleDouble: 1.0,
2489 SingleBool: true,
2490 NestedType: &proto3pb.TestAllTypes_SingleNestedMessage{
2491 SingleNestedMessage: &proto3pb.TestAllTypes_NestedMessage{},
2492 },
2493 StandaloneEnum: proto3pb.TestAllTypes_FOO,
2494 SingleDuration: &durationpb.Duration{Seconds: 5},
2495 SingleTimestamp: &timestamppb.Timestamp{
2496 Seconds: 63154820,
2497 },
2498 SingleUint64Wrapper: wrapperspb.UInt64(0),
2499 RepeatedInt32: []int32{1, 2},
2500 MapStringString: map[string]string{"": ""},
2501 MapInt64NestedType: map[int64]*proto3pb.NestedTestAllTypes{0: {}},
2502 }
2503 vars, err := ContextProtoVars(in)
2504 if err != nil {
2505 t.Fatalf("ContextProtoVars(%v) failed: %v", in, err)
2506 }
2507 out, _, err := prg.Eval(vars)
2508 if err != nil {
2509 t.Fatalf("prg.Eval() failed: %v", err)
2510 }
2511 if out.Equal(types.True) != types.True {
2512 t.Errorf("prg.Eval() got %v, wanted true", out)
2513 }
2514}
2515
2516func TestContextProtoJSONFieldNames(t *testing.T) {

Callers

nothing calls this directly

Calls 10

DeclareContextProtoFunction · 0.85
ContextProtoVarsFunction · 0.85
ErrMethod · 0.80
ProgramMethod · 0.80
testEnvFunction · 0.70
CompileMethod · 0.65
EvalMethod · 0.65
EqualMethod · 0.65
DescriptorMethod · 0.45
ProtoReflectMethod · 0.45

Tested by

no test coverage detected