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

Function TestInstanceMethods

class_test.go:216–265  ·  view source on GitHub ↗

TestInstanceMethods tests instance method functionality

(t *testing.T)

Source from the content-addressed store, hash-verified

214
215// TestInstanceMethods tests instance method functionality
216func 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
268func TestAccessors(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
FreeMethod · 0.45

Tested by

no test coverage detected