============================================================================= CONSTRUCTOR TESTS =============================================================================
(t *testing.T)
| 258 | // ============================================================================= |
| 259 | |
| 260 | func TestReflectionConstructorModes(t *testing.T) { |
| 261 | newTestContext := func(t *testing.T) *Context { |
| 262 | rt := NewRuntime() |
| 263 | ctx := rt.NewContext() |
| 264 | require.NotNil(t, ctx) |
| 265 | t.Cleanup(func() { |
| 266 | ctx.Close() |
| 267 | rt.Close() |
| 268 | }) |
| 269 | return ctx |
| 270 | } |
| 271 | |
| 272 | testCases := []struct { |
| 273 | name string |
| 274 | js string |
| 275 | want []interface{} // [firstName, lastName, age, salary, isActive] |
| 276 | }{ |
| 277 | { |
| 278 | "positional_args", |
| 279 | `new Person("Alice", "Smith", 28, 60000.0, true)`, |
| 280 | []interface{}{"Alice", "Smith", int32(28), 60000.0, true}, |
| 281 | }, |
| 282 | { |
| 283 | "named_args", |
| 284 | `new Person({firstName: "Bob", lastName: "Jones", age: 32, salary: 70000.0, isActive: false})`, |
| 285 | []interface{}{"Bob", "Jones", int32(32), 70000.0, false}, |
| 286 | }, |
| 287 | { |
| 288 | "partial_args", |
| 289 | `new Person("Carol", "Brown")`, |
| 290 | []interface{}{"Carol", "Brown", int32(0), 0.0, false}, |
| 291 | }, |
| 292 | { |
| 293 | "empty_constructor", |
| 294 | `new Person()`, |
| 295 | []interface{}{"", "", int32(0), 0.0, false}, |
| 296 | }, |
| 297 | } |
| 298 | |
| 299 | for _, tc := range testCases { |
| 300 | t.Run(tc.name, func(t *testing.T) { |
| 301 | ctx := newTestContext(t) |
| 302 | constructor, _ := ctx.BindClass(&Person{}) |
| 303 | require.False(t, constructor.IsException()) |
| 304 | ctx.Globals().Set("Person", constructor) |
| 305 | |
| 306 | result := ctx.Eval(fmt.Sprintf(` |
| 307 | (function() { |
| 308 | let person = %s; |
| 309 | return [person.firstName, person.lastName, person.age, person.salary, person.isActive]; |
| 310 | })(); |
| 311 | `, tc.js)) |
| 312 | defer result.Free() |
| 313 | require.False(t, result.IsException()) |
| 314 | |
| 315 | for i, expected := range tc.want { |
| 316 | switch exp := expected.(type) { |
| 317 | case string: |
nothing calls this directly
no test coverage detected