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

Function TestInheritanceAndNewTarget

class_test.go:681–741  ·  view source on GitHub ↗

TestInheritanceAndNewTarget tests inheritance support with new.target

(t *testing.T)

Source from the content-addressed store, hash-verified

679
680// TestInheritanceAndNewTarget tests inheritance support with new.target
681func TestInheritanceAndNewTarget(t *testing.T) {
682 rt := NewRuntime()
683 defer rt.Close()
684
685 context := rt.NewContext()
686 defer context.Close()
687
688 // Create and register Point class
689 pointConstructor, _ := createPointClass(context)
690 if pointConstructor.IsException() {
691 defer pointConstructor.Free()
692 err := context.Exception()
693 t.Fatalf("Failed to create Point class: %v", err)
694 }
695
696 // Register Point class globally
697 // Note: Globals will manage the memory automatically
698 context.Globals().Set("Point", pointConstructor)
699
700 // Test inheritance using extends
701 result := context.Eval(`
702 class Point3D extends Point {
703 constructor(x, y, z) {
704 super(x, y);
705 this.z = z || 0;
706 }
707
708 norm() {
709 return Math.sqrt(this.x * this.x + this.y * this.y + this.z * this.z);
710 }
711 }
712
713 let p3d1 = new Point3D(3, 4, 12);
714 p3d1.norm(); // sqrt(3^2 + 4^2 + 12^2) = sqrt(169) = 13
715 `)
716 defer result.Free()
717 if result.IsException() {
718 err := context.Exception()
719 t.Fatalf("Failed to evaluate inheritance test: %v", err)
720 }
721
722 expected := 13.0 // sqrt(9 + 16 + 144) = 13
723 if math.Abs(result.ToFloat64()-expected) > 0.001 { // Updated: Use ToFloat64()
724 t.Errorf("Expected 3D norm 13.0, got %f", result.ToFloat64())
725 }
726
727 // Test that inherited object is still instance of Point
728 result2 := context.Eval(`
729 let p3d2 = new Point3D(1, 2, 3);
730 p3d2 instanceof Point;
731 `)
732 defer result2.Free()
733 if result2.IsException() {
734 err := context.Exception()
735 t.Fatalf("Failed to evaluate instanceof test: %v", err)
736 }
737
738 if !result2.ToBool() { // Updated: Use ToBool()

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
ToBoolMethod · 0.80

Tested by

no test coverage detected