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

Function TestConstructorFunctionality

class_test.go:163–213  ·  view source on GitHub ↗

TestConstructorFunctionality tests constructor with different parameter counts

(t *testing.T)

Source from the content-addressed store, hash-verified

161
162// TestConstructorFunctionality tests constructor with different parameter counts
163func TestConstructorFunctionality(t *testing.T) {
164 rt := NewRuntime()
165 defer rt.Close()
166
167 context := rt.NewContext()
168 defer context.Close()
169
170 // Create and register Point class
171 pointConstructor, _ := createPointClass(context)
172 if pointConstructor.IsException() {
173 defer pointConstructor.Free()
174 err := context.Exception()
175 t.Fatalf("Failed to create Point class: %v", err)
176 }
177
178 // Register Point class globally
179 // Note: Globals will manage the memory automatically
180 context.Globals().Set("Point", pointConstructor)
181
182 // Test constructor with no arguments
183 result := context.Eval(`
184 let p1 = new Point();
185 [p1.x, p1.y];
186 `)
187 defer result.Free()
188 if result.IsException() {
189 err := context.Exception()
190 t.Fatalf("Failed to evaluate no-args constructor: %v", err)
191 }
192
193 if result.GetIdx(0).ToFloat64() != 0.0 || result.GetIdx(1).ToFloat64() != 0.0 { // Updated: Use ToFloat64()
194 t.Errorf("Expected Point(0, 0), got Point(%f, %f)",
195 result.GetIdx(0).ToFloat64(), result.GetIdx(1).ToFloat64())
196 }
197
198 // Test constructor with partial arguments
199 result2 := context.Eval(`
200 let p2 = new Point(5);
201 [p2.x, p2.y];
202 `)
203 defer result2.Free()
204 if result2.IsException() {
205 err := context.Exception()
206 t.Fatalf("Failed to evaluate partial-args constructor: %v", err)
207 }
208
209 if result2.GetIdx(0).ToFloat64() != 5.0 || result2.GetIdx(1).ToFloat64() != 0.0 { // Updated: Use ToFloat64()
210 t.Errorf("Expected Point(5, 0), got Point(%f, %f)",
211 result2.GetIdx(0).ToFloat64(), result2.GetIdx(1).ToFloat64())
212 }
213}
214
215// TestInstanceMethods tests instance method functionality
216func TestInstanceMethods(t *testing.T) {

Callers

nothing calls this directly

Calls 13

CloseMethod · 0.95
NewContextMethod · 0.95
CloseMethod · 0.95
ExceptionMethod · 0.95
GlobalsMethod · 0.95
EvalMethod · 0.95
NewRuntimeFunction · 0.85
createPointClassFunction · 0.85
IsExceptionMethod · 0.80
SetMethod · 0.80
ToFloat64Method · 0.80
GetIdxMethod · 0.80

Tested by

no test coverage detected