TestBasicClassCreation tests basic class creation and registration
(t *testing.T)
| 124 | |
| 125 | // TestBasicClassCreation tests basic class creation and registration |
| 126 | func TestBasicClassCreation(t *testing.T) { |
| 127 | rt := NewRuntime() |
| 128 | defer rt.Close() |
| 129 | |
| 130 | context := rt.NewContext() |
| 131 | defer context.Close() |
| 132 | |
| 133 | // Create Point class |
| 134 | pointConstructor, _ := createPointClass(context) |
| 135 | if pointConstructor.IsException() { |
| 136 | defer pointConstructor.Free() |
| 137 | err := context.Exception() |
| 138 | t.Fatalf("Failed to create Point class: %v", err) |
| 139 | } |
| 140 | |
| 141 | // Register Point class globally |
| 142 | // Note: Once set as global property, Globals will manage the memory automatically |
| 143 | context.Globals().Set("Point", pointConstructor) |
| 144 | |
| 145 | // Test basic constructor call |
| 146 | result := context.Eval(` |
| 147 | let p = new Point(3, 4); |
| 148 | p.norm(); |
| 149 | `) |
| 150 | defer result.Free() |
| 151 | if result.IsException() { |
| 152 | err := context.Exception() |
| 153 | t.Fatalf("Failed to evaluate basic constructor test: %v", err) |
| 154 | } |
| 155 | |
| 156 | expected := 5.0 // sqrt(3^2 + 4^2) = 5 |
| 157 | if math.Abs(result.ToFloat64()-expected) > 0.001 { // Updated: Use ToFloat64() |
| 158 | t.Errorf("Expected norm to be %f, got %f", expected, result.ToFloat64()) |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | // TestConstructorFunctionality tests constructor with different parameter counts |
| 163 | func TestConstructorFunctionality(t *testing.T) { |
nothing calls this directly
no test coverage detected