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

Function TestContextProto

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

Source from the content-addressed store, hash-verified

2295}
2296
2297func TestContextProto(t *testing.T) {
2298 descriptor := new(proto3pb.TestAllTypes).ProtoReflect().Descriptor()
2299 option := DeclareContextProto(descriptor)
2300 env := testEnv(t, option)
2301 expression := `
2302 single_int64 == 1
2303 && single_double == 1.0
2304 && single_bool == true
2305 && single_string == ''
2306 && single_nested_message == google.expr.proto3.test.TestAllTypes.NestedMessage{}
2307 && standalone_enum == google.expr.proto3.test.TestAllTypes.NestedEnum.FOO
2308 && single_duration == duration('5s')
2309 && single_timestamp == timestamp(63154820)
2310 && single_any == null
2311 && single_uint32_wrapper == null
2312 && single_uint64_wrapper == 0u
2313 && repeated_int32 == [1,2]
2314 && map_string_string == {'': ''}
2315 && map_int64_nested_type == {0 : google.expr.proto3.test.NestedTestAllTypes{}}`
2316 ast, iss := env.Compile(expression)
2317 if iss.Err() != nil {
2318 t.Fatalf("env.Compile(%s) failed: %s", expression, iss.Err())
2319 }
2320 prg, err := env.Program(ast)
2321 if err != nil {
2322 t.Fatalf("env.Program() failed: %v", err)
2323 }
2324 in := &proto3pb.TestAllTypes{
2325 SingleInt64: 1,
2326 SingleDouble: 1.0,
2327 SingleBool: true,
2328 NestedType: &proto3pb.TestAllTypes_SingleNestedMessage{
2329 SingleNestedMessage: &proto3pb.TestAllTypes_NestedMessage{},
2330 },
2331 StandaloneEnum: proto3pb.TestAllTypes_FOO,
2332 SingleDuration: &durationpb.Duration{Seconds: 5},
2333 SingleTimestamp: &timestamppb.Timestamp{
2334 Seconds: 63154820,
2335 },
2336 SingleUint64Wrapper: wrapperspb.UInt64(0),
2337 RepeatedInt32: []int32{1, 2},
2338 MapStringString: map[string]string{"": ""},
2339 MapInt64NestedType: map[int64]*proto3pb.NestedTestAllTypes{0: {}},
2340 }
2341 vars, err := ContextProtoVars(in)
2342 if err != nil {
2343 t.Fatalf("ContextProtoVars(%v) failed: %v", in, err)
2344 }
2345 out, _, err := prg.Eval(vars)
2346 if err != nil {
2347 t.Fatalf("prg.Eval() failed: %v", err)
2348 }
2349 if out.Equal(types.True) != types.True {
2350 t.Errorf("prg.Eval() got %v, wanted true", out)
2351 }
2352}
2353
2354func 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