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

Function TestNativeToValueDelegatesUnregisteredStructs

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

Source from the content-addressed store, hash-verified

1640}
1641
1642func TestNativeToValueDelegatesUnregisteredStructs(t *testing.T) {
1643 custom := &recordingAdapter{base: types.DefaultTypeAdapter}
1644 env, err := cel.NewEnv(
1645 cel.CustomTypeAdapter(custom),
1646 NativeTypes(reflect.TypeOf(registeredNativeStruct{})),
1647 )
1648 if err != nil {
1649 t.Fatalf("cel.NewEnv() failed: %v", err)
1650 }
1651 adapter := env.CELTypeAdapter()
1652
1653 // An unregistered struct must reach the composed base adapter.
1654 got := adapter.NativeToValue(unregisteredNativeStruct{Name: "x"})
1655 if !custom.saw {
1656 t.Error("base adapter was not consulted for an unregistered struct")
1657 }
1658 if got.Equal(types.String("from-base-adapter")) != types.True {
1659 t.Errorf("NativeToValue(unregisteredNativeStruct) = %v, want the base adapter's value", got)
1660 }
1661
1662 // A registered native type must still be wrapped as a native object.
1663 custom.saw = false
1664 gotReg := adapter.NativeToValue(registeredNativeStruct{Name: "y"})
1665 if custom.saw {
1666 t.Error("base adapter was consulted for a registered native type")
1667 }
1668 if tn := gotReg.Type().TypeName(); !strings.Contains(tn, "registeredNativeStruct") {
1669 t.Errorf("NativeToValue(registeredNativeStruct).Type() = %q, want a native object type", tn)
1670 }
1671}
1672
1673func BenchmarkNativeTypesEval(b *testing.B) {
1674 benchmarks := []struct {

Callers

nothing calls this directly

Calls 11

CELTypeAdapterMethod · 0.95
NewEnvFunction · 0.92
CustomTypeAdapterFunction · 0.92
StringTypeAlias · 0.92
NativeTypesFunction · 0.85
NativeToValueMethod · 0.65
EqualMethod · 0.65
TypeNameMethod · 0.65
TypeMethod · 0.65
ContainsMethod · 0.65
ErrorMethod · 0.45

Tested by

no test coverage detected