TestInstanceMethods tests instance method functionality
(t *testing.T)
| 214 | |
| 215 | // TestInstanceMethods tests instance method functionality |
| 216 | func TestInstanceMethods(t *testing.T) { |
| 217 | rt := NewRuntime() |
| 218 | defer rt.Close() |
| 219 | |
| 220 | context := rt.NewContext() |
| 221 | defer context.Close() |
| 222 | |
| 223 | // Create and register Point class |
| 224 | pointConstructor, _ := createPointClass(context) |
| 225 | if pointConstructor.IsException() { |
| 226 | defer pointConstructor.Free() |
| 227 | err := context.Exception() |
| 228 | t.Fatalf("Failed to create Point class: %v", err) |
| 229 | } |
| 230 | |
| 231 | // Register Point class globally |
| 232 | // Note: Globals will manage the memory automatically |
| 233 | context.Globals().Set("Point", pointConstructor) |
| 234 | |
| 235 | // Test norm method |
| 236 | result := context.Eval(` |
| 237 | let p1 = new Point(3, 4); |
| 238 | p1.norm(); |
| 239 | `) |
| 240 | defer result.Free() |
| 241 | if result.IsException() { |
| 242 | err := context.Exception() |
| 243 | t.Fatalf("Failed to evaluate norm method: %v", err) |
| 244 | } |
| 245 | |
| 246 | if math.Abs(result.ToFloat64()-5.0) > 0.001 { // Updated: Use ToFloat64() |
| 247 | t.Errorf("Expected norm 5.0, got %f", result.ToFloat64()) |
| 248 | } |
| 249 | |
| 250 | // Test toString method |
| 251 | result2 := context.Eval(` |
| 252 | let p2 = new Point(1.5, 2.5); |
| 253 | p2.toString(); |
| 254 | `) |
| 255 | defer result2.Free() |
| 256 | if result2.IsException() { |
| 257 | err := context.Exception() |
| 258 | t.Fatalf("Failed to evaluate toString method: %v", err) |
| 259 | } |
| 260 | |
| 261 | expected := "Point(1.50, 2.50)" |
| 262 | if result2.ToString() != expected { // Updated: Use ToString() |
| 263 | t.Errorf("Expected toString '%s', got '%s'", expected, result2.ToString()) |
| 264 | } |
| 265 | } |
| 266 | |
| 267 | // TestAccessors tests getter and setter accessor functionality |
| 268 | func TestAccessors(t *testing.T) { |
nothing calls this directly
no test coverage detected