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

Function TestNativeNestedStruct

ext/native_test.go:1393–1448  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

1391}
1392
1393func TestNativeNestedStruct(t *testing.T) {
1394 var nativeTests = []struct {
1395 expr string
1396 in any
1397 }{
1398 {
1399 expr: `test.ListVal.exists(x, x.custom_name == "name")`,
1400 in: map[string]any{
1401 "test": &TestNestedStruct{ListVal: []*TestNestedType{{NestedCustomName: "name"}}},
1402 },
1403 },
1404 }
1405
1406 envOpts := []cel.EnvOption{
1407 NativeTypes(
1408 reflect.ValueOf(&TestNestedStruct{}),
1409 ParseStructTag("json"),
1410 ),
1411 cel.Variable("test", cel.ObjectType("ext.TestNestedStruct")),
1412 }
1413
1414 env, err := cel.NewEnv(envOpts...)
1415 if err != nil {
1416 t.Fatalf("cel.NewEnv(NativeTypes()) failed: %v", err)
1417 }
1418
1419 for i, tst := range nativeTests {
1420 tc := tst
1421 t.Run(fmt.Sprintf("[%d]", i), func(t *testing.T) {
1422 var asts []*cel.Ast
1423 pAst, iss := env.Parse(tc.expr)
1424 if iss.Err() != nil {
1425 t.Fatalf("env.Parse(%v) failed: %v", tc.expr, iss.Err())
1426 }
1427 asts = append(asts, pAst)
1428 cAst, iss := env.Check(pAst)
1429 if iss.Err() != nil {
1430 t.Fatalf("env.Check(%v) failed: %v", tc.expr, iss.Err())
1431 }
1432 asts = append(asts, cAst)
1433 for _, ast := range asts {
1434 prg, err := env.Program(ast)
1435 if err != nil {
1436 t.Fatal(err)
1437 }
1438 out, _, err := prg.Eval(tc.in)
1439 if err != nil {
1440 t.Fatal(err)
1441 }
1442 if !reflect.DeepEqual(out.Value(), true) {
1443 t.Errorf("got %v, wanted true for expr: %s", out.Value(), tc.expr)
1444 }
1445 }
1446 })
1447 }
1448}
1449
1450func TestNativeTypesVersion(t *testing.T) {

Callers

nothing calls this directly

Calls 10

ParseMethod · 0.95
CheckMethod · 0.95
ProgramMethod · 0.95
VariableFunction · 0.92
NewEnvFunction · 0.92
NativeTypesFunction · 0.85
ParseStructTagFunction · 0.85
ErrMethod · 0.80
EvalMethod · 0.65
ValueMethod · 0.65

Tested by

no test coverage detected