TestAccessors tests getter and setter accessor functionality
(t *testing.T)
| 266 | |
| 267 | // TestAccessors tests getter and setter accessor functionality |
| 268 | func TestAccessors(t *testing.T) { |
| 269 | rt := NewRuntime() |
| 270 | defer rt.Close() |
| 271 | |
| 272 | context := rt.NewContext() |
| 273 | defer context.Close() |
| 274 | |
| 275 | // Create and register Point class |
| 276 | pointConstructor, _ := createPointClass(context) |
| 277 | if pointConstructor.IsException() { |
| 278 | defer pointConstructor.Free() |
| 279 | err := context.Exception() |
| 280 | t.Fatalf("Failed to create Point class: %v", err) |
| 281 | } |
| 282 | |
| 283 | // Register Point class globally |
| 284 | // Note: Globals will manage the memory automatically |
| 285 | context.Globals().Set("Point", pointConstructor) |
| 286 | |
| 287 | // Test accessor getters |
| 288 | result := context.Eval(` |
| 289 | let p1 = new Point(3, 4); |
| 290 | [p1.x, p1.y]; |
| 291 | `) |
| 292 | defer result.Free() |
| 293 | if result.IsException() { |
| 294 | err := context.Exception() |
| 295 | t.Fatalf("Failed to evaluate accessor getters: %v", err) |
| 296 | } |
| 297 | |
| 298 | if result.GetIdx(0).ToFloat64() != 3.0 || result.GetIdx(1).ToFloat64() != 4.0 { // Updated: Use ToFloat64() |
| 299 | t.Errorf("Expected [3, 4], got [%f, %f]", |
| 300 | result.GetIdx(0).ToFloat64(), result.GetIdx(1).ToFloat64()) |
| 301 | } |
| 302 | |
| 303 | // Test accessor setters |
| 304 | result2 := context.Eval(` |
| 305 | let p2 = new Point(1, 2); |
| 306 | p2.x = 10; |
| 307 | p2.y = 20; |
| 308 | [p2.x, p2.y]; |
| 309 | `) |
| 310 | defer result2.Free() |
| 311 | if result2.IsException() { |
| 312 | err := context.Exception() |
| 313 | t.Fatalf("Failed to evaluate accessor setters: %v", err) |
| 314 | } |
| 315 | |
| 316 | if result2.GetIdx(0).ToFloat64() != 10.0 || result2.GetIdx(1).ToFloat64() != 20.0 { // Updated: Use ToFloat64() |
| 317 | t.Errorf("Expected [10, 20], got [%f, %f]", |
| 318 | result2.GetIdx(0).ToFloat64(), result2.GetIdx(1).ToFloat64()) |
| 319 | } |
| 320 | } |
| 321 | |
| 322 | // TestStaticMethods tests static method functionality |
| 323 | func TestStaticMethods(t *testing.T) { |
nothing calls this directly
no test coverage detected