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

Function TestClassInstanceChecking

class_test.go:744–811  ·  view source on GitHub ↗

TestClassInstanceChecking tests class instance detection methods

(t *testing.T)

Source from the content-addressed store, hash-verified

742
743// TestClassInstanceChecking tests class instance detection methods
744func TestClassInstanceChecking(t *testing.T) {
745 rt := NewRuntime()
746 defer rt.Close()
747
748 context := rt.NewContext()
749 defer context.Close()
750
751 // Create and register Point class
752 pointConstructor, pointClassID := createPointClass(context)
753 if pointConstructor.IsException() {
754 defer pointConstructor.Free()
755 err := context.Exception()
756 t.Fatalf("Failed to create Point class: %v", err)
757 }
758
759 // Register Point class globally
760 // Note: Globals will manage the memory automatically
761 context.Globals().Set("Point", pointConstructor)
762
763 // Create test instance
764 instance := context.Eval(`new Point(1, 2)`)
765 defer instance.Free()
766 if instance.IsException() {
767 err := context.Exception()
768 t.Fatalf("Failed to create test instance: %v", err)
769 }
770
771 // Test IsClassInstance
772 if !instance.IsClassInstance() {
773 t.Errorf("Expected IsClassInstance to return true for Point instance")
774 }
775
776 // Test IsInstanceOfClass
777 if !instance.IsInstanceOfClassID(pointClassID) {
778 t.Errorf("Expected IsInstanceOfClass to return true for correct class ID")
779 }
780
781 // Test with wrong class ID
782 if instance.IsInstanceOfClassID(999) {
783 t.Errorf("Expected IsInstanceOfClass to return false for wrong class ID")
784 }
785
786 // Test GetClassID
787 if instance.GetClassID() != pointClassID {
788 t.Errorf("Expected GetClassID to return %d, got %d", pointClassID, instance.GetClassID())
789 }
790
791 // Test HasInstanceData
792 if !instance.HasInstanceData() {
793 t.Errorf("Expected HasInstanceData to return true for Point instance")
794 }
795
796 // Test with regular object
797 regularObj := context.Eval(`({})`)
798 defer regularObj.Free()
799 if regularObj.IsException() {
800 err := context.Exception()
801 t.Fatalf("Failed to create regular object: %v", err)

Callers

nothing calls this directly

Calls 15

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
IsClassInstanceMethod · 0.80
IsInstanceOfClassIDMethod · 0.80

Tested by

no test coverage detected