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

Function TestNativeStructEmbedded

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

Source from the content-addressed store, hash-verified

930}
931
932func TestNativeStructEmbedded(t *testing.T) {
933 var nativeTests = []struct {
934 expr string
935 in any
936 }{
937 {
938 expr: `test.embedded.custom_name == "name"`,
939 in: map[string]any{
940 "test": &TestEmbeddedTypes{TestNestedType{NestedCustomName: "name"}},
941 },
942 },
943 }
944
945 envOpts := []cel.EnvOption{
946 NativeTypes(
947 reflect.TypeOf(&TestEmbeddedTypes{}),
948 reflect.TypeOf(&TestNestedType{}),
949 ParseStructTag("json"),
950 ),
951 cel.Variable("test", cel.ObjectType("ext.TestEmbeddedTypes")),
952 }
953
954 env, err := cel.NewEnv(envOpts...)
955 if err != nil {
956 t.Fatalf("cel.NewEnv(NativeTypes()) failed: %v", err)
957 }
958
959 for i, tst := range nativeTests {
960 tc := tst
961 t.Run(fmt.Sprintf("[%d]", i), func(t *testing.T) {
962 var asts []*cel.Ast
963 pAst, iss := env.Parse(tc.expr)
964 if iss.Err() != nil {
965 t.Fatalf("env.Parse(%v) failed: %v", tc.expr, iss.Err())
966 }
967 asts = append(asts, pAst)
968 cAst, iss := env.Check(pAst)
969 if iss.Err() != nil {
970 t.Fatalf("env.Check(%v) failed: %v", tc.expr, iss.Err())
971 }
972 asts = append(asts, cAst)
973 for _, ast := range asts {
974 prg, err := env.Program(ast)
975 if err != nil {
976 t.Fatal(err)
977 }
978 out, _, err := prg.Eval(tc.in)
979 if err != nil {
980 t.Fatal(err)
981 }
982 if !reflect.DeepEqual(out.Value(), true) {
983 t.Errorf("got %v, wanted true for expr: %s", out.Value(), tc.expr)
984 }
985 }
986 })
987 }
988}
989

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