MCPcopy Create free account
hub / github.com/buke/quickjs-go / TestClassBuilder_ValueSpecProperties

Function TestClassBuilder_ValueSpecProperties

class_test.go:1129–1466  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

1127}
1128
1129func TestClassBuilder_ValueSpecProperties(t *testing.T) {
1130 useStableOwnerHooksForLegacySubtests(t)
1131
1132 rt := NewRuntime()
1133 defer rt.Close()
1134 ctx := rt.NewContext()
1135 defer ctx.Close()
1136
1137 findClassBuilderSnapshot := func(className string) (*ClassBuilder, bool) {
1138 var snapshot *ClassBuilder
1139 ctx.handleStore.handles.Range(func(_, value interface{}) bool {
1140 h, ok := value.(cgo.Handle)
1141 if !ok {
1142 return true
1143 }
1144 cb, ok := h.Value().(*ClassBuilder)
1145 if ok && cb != nil && cb.name == className {
1146 snapshot = cb
1147 return false
1148 }
1149 return true
1150 })
1151 return snapshot, snapshot != nil
1152 }
1153
1154 t.Run("PropertyValueAndLiteral", func(t *testing.T) {
1155 constructor, _ := NewClassBuilder("SpecPropertyClass").
1156 Constructor(func(ctx *Context, instance *Value, args []*Value) (interface{}, error) {
1157 return &Point{X: 0, Y: 0}, nil
1158 }).
1159 PropertyLiteral("label", "spec").
1160 PropertyValue("num", FactorySpec{Factory: func(ctx *Context) (*Value, error) {
1161 return ctx.NewInt32(7), nil
1162 }}).
1163 PropertyValue("meta", MarshalSpec{Value: map[string]interface{}{"x": 1}}).
1164 StaticProperty("kind", ctx.NewString("legacy-static")).
1165 Build(ctx)
1166 require.False(t, constructor.IsException())
1167
1168 ctx.Globals().Set("SpecPropertyClass", constructor)
1169 result := ctx.Eval(`
1170 (() => {
1171 const o = new SpecPropertyClass();
1172 return [o.label, o.num, o.meta.x, SpecPropertyClass.kind].join(':');
1173 })()
1174 `)
1175 defer result.Free()
1176 require.False(t, result.IsException())
1177 require.Equal(t, "spec:7:1:legacy-static", result.ToString())
1178 })
1179
1180 t.Run("InvalidInstancePropertySpec", func(t *testing.T) {
1181 constructor, _ := NewClassBuilder("SpecInvalidInstanceClass").
1182 Constructor(func(ctx *Context, instance *Value, args []*Value) (interface{}, error) {
1183 return &Point{X: 0, Y: 0}, nil
1184 }).
1185 PropertyValue("bad", FactorySpec{}).
1186 Build(ctx)

Callers

nothing calls this directly

Calls 15

CloseMethod · 0.95
NewContextMethod · 0.95
CloseMethod · 0.95
NewInt32Method · 0.95
NewStringMethod · 0.95
GlobalsMethod · 0.95
EvalMethod · 0.95
ExceptionMethod · 0.95
NewRuntimeFunction · 0.85
NewClassBuilderFunction · 0.85
cloneClassBuilderFunction · 0.85

Tested by

no test coverage detected