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

Function TestContextProtoJSONFieldNames

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

Source from the content-addressed store, hash-verified

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

Callers

nothing calls this directly

Calls 12

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

Tested by

no test coverage detected