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

Function TestContextProtoJSONFieldNames

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

Source from the content-addressed store, hash-verified

2647}
2648
2649func TestContextProtoJSONFieldNames(t *testing.T) {
2650 descriptor := new(proto3pb.TestAllTypes).ProtoReflect().Descriptor()
2651 env := testEnv(t, JSONFieldNames(true), DeclareContextProto(descriptor))
2652 expression := `
2653 singleInt64 == 1
2654 && singleDouble == 1.0
2655 && singleBool == true
2656 && singleString == ''
2657 && singleNestedMessage == google.expr.proto3.test.TestAllTypes.NestedMessage{}
2658 && standaloneEnum == google.expr.proto3.test.TestAllTypes.NestedEnum.FOO
2659 && singleDuration == duration('5s')
2660 && singleTimestamp == timestamp(63154820)
2661 && singleAny == null
2662 && singleUint32Wrapper == null
2663 && singleUint64Wrapper == 0u
2664 && repeatedInt32 == [1,2]
2665 && mapStringString == {'': ''}
2666 && mapInt64NestedType == {0 : google.expr.proto3.test.NestedTestAllTypes{}}`
2667 ast, iss := env.Compile(expression)
2668 if iss.Err() != nil {
2669 t.Fatalf("env.Compile(%s) failed: %s", expression, iss.Err())
2670 }
2671 prg, err := env.Program(ast)
2672 if err != nil {
2673 t.Fatalf("env.Program() failed: %v", err)
2674 }
2675 in := &proto3pb.TestAllTypes{
2676 SingleInt64: 1,
2677 SingleDouble: 1.0,
2678 SingleBool: true,
2679 NestedType: &proto3pb.TestAllTypes_SingleNestedMessage{
2680 SingleNestedMessage: &proto3pb.TestAllTypes_NestedMessage{},
2681 },
2682 StandaloneEnum: proto3pb.TestAllTypes_FOO,
2683 SingleDuration: &durationpb.Duration{Seconds: 5},
2684 SingleTimestamp: &timestamppb.Timestamp{
2685 Seconds: 63154820,
2686 },
2687 SingleUint64Wrapper: wrapperspb.UInt64(0),
2688 RepeatedInt32: []int32{1, 2},
2689 MapStringString: map[string]string{"": ""},
2690 MapInt64NestedType: map[int64]*proto3pb.NestedTestAllTypes{0: {}},
2691 }
2692 vars, err := ContextProtoVars(in, types.JSONFieldNames(true))
2693 if err != nil {
2694 t.Fatalf("ContextProtoVars(%v) failed: %v", in, err)
2695 }
2696 out, _, err := prg.Eval(vars)
2697 if err != nil {
2698 t.Fatalf("prg.Eval() failed: %v", err)
2699 }
2700 if out.Equal(types.True) != types.True {
2701 t.Errorf("prg.Eval() got %v, wanted true", out)
2702 }
2703}
2704
2705func TestRegexOptimizer(t *testing.T) {
2706 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