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

Function TestContextProtoJSONFieldNames

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

Source from the content-addressed store, hash-verified

2352}
2353
2354func TestContextProtoJSONFieldNames(t *testing.T) {
2355 descriptor := new(proto3pb.TestAllTypes).ProtoReflect().Descriptor()
2356 env := testEnv(t, JSONFieldNames(true), DeclareContextProto(descriptor))
2357 expression := `
2358 singleInt64 == 1
2359 && singleDouble == 1.0
2360 && singleBool == true
2361 && singleString == ''
2362 && singleNestedMessage == google.expr.proto3.test.TestAllTypes.NestedMessage{}
2363 && standaloneEnum == google.expr.proto3.test.TestAllTypes.NestedEnum.FOO
2364 && singleDuration == duration('5s')
2365 && singleTimestamp == timestamp(63154820)
2366 && singleAny == null
2367 && singleUint32Wrapper == null
2368 && singleUint64Wrapper == 0u
2369 && repeatedInt32 == [1,2]
2370 && mapStringString == {'': ''}
2371 && mapInt64NestedType == {0 : google.expr.proto3.test.NestedTestAllTypes{}}`
2372 ast, iss := env.Compile(expression)
2373 if iss.Err() != nil {
2374 t.Fatalf("env.Compile(%s) failed: %s", expression, iss.Err())
2375 }
2376 prg, err := env.Program(ast)
2377 if err != nil {
2378 t.Fatalf("env.Program() failed: %v", err)
2379 }
2380 in := &proto3pb.TestAllTypes{
2381 SingleInt64: 1,
2382 SingleDouble: 1.0,
2383 SingleBool: true,
2384 NestedType: &proto3pb.TestAllTypes_SingleNestedMessage{
2385 SingleNestedMessage: &proto3pb.TestAllTypes_NestedMessage{},
2386 },
2387 StandaloneEnum: proto3pb.TestAllTypes_FOO,
2388 SingleDuration: &durationpb.Duration{Seconds: 5},
2389 SingleTimestamp: &timestamppb.Timestamp{
2390 Seconds: 63154820,
2391 },
2392 SingleUint64Wrapper: wrapperspb.UInt64(0),
2393 RepeatedInt32: []int32{1, 2},
2394 MapStringString: map[string]string{"": ""},
2395 MapInt64NestedType: map[int64]*proto3pb.NestedTestAllTypes{0: {}},
2396 }
2397 vars, err := ContextProtoVars(in, types.JSONFieldNames(true))
2398 if err != nil {
2399 t.Fatalf("ContextProtoVars(%v) failed: %v", in, err)
2400 }
2401 out, _, err := prg.Eval(vars)
2402 if err != nil {
2403 t.Fatalf("prg.Eval() failed: %v", err)
2404 }
2405 if out.Equal(types.True) != types.True {
2406 t.Errorf("prg.Eval() got %v, wanted true", out)
2407 }
2408}
2409
2410func TestRegexOptimizer(t *testing.T) {
2411 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