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

Function TestGetGoObject

class_test.go:814–870  ·  view source on GitHub ↗

TestGetGoObject tests retrieving Go objects from JS instances

(t *testing.T)

Source from the content-addressed store, hash-verified

812
813// TestGetGoObject tests retrieving Go objects from JS instances
814func TestGetGoObject(t *testing.T) {
815 rt := NewRuntime()
816 defer rt.Close()
817
818 context := rt.NewContext()
819 defer context.Close()
820
821 // Create and register Point class
822 pointConstructor, _ := createPointClass(context)
823 if pointConstructor.IsException() {
824 defer pointConstructor.Free()
825 err := context.Exception()
826 t.Fatalf("Failed to create Point class: %v", err)
827 }
828
829 // Register Point class globally
830 // Note: Globals will manage the memory automatically
831 context.Globals().Set("Point", pointConstructor)
832
833 // Create test instance
834 instance := context.Eval(`new Point(3.14, 2.71)`)
835 defer instance.Free()
836 if instance.IsException() {
837 err := context.Exception()
838 t.Fatalf("Failed to create test instance: %v", err)
839 }
840
841 // Use GetGoObject to retrieve Go object
842 obj, err := instance.GetGoObject()
843 if err != nil {
844 t.Fatalf("Failed to get instance data: %v", err)
845 }
846
847 point, ok := obj.(*Point)
848 if !ok {
849 t.Fatalf("Expected *Point, got %T", obj)
850 }
851
852 if point.X != 3.14 || point.Y != 2.71 {
853 t.Errorf("Expected Point(3.14, 2.71), got Point(%f, %f)", point.X, point.Y)
854 }
855
856 // Test GetGoObject via Context method again for consistency
857 obj2, err := instance.GetGoObject()
858 if err != nil {
859 t.Fatalf("Failed to get instance data via context: %v", err)
860 }
861
862 point2, ok := obj2.(*Point)
863 if !ok {
864 t.Fatalf("Expected *Point, got %T", obj2)
865 }
866
867 if point2.X != 3.14 || point2.Y != 2.71 {
868 t.Errorf("Expected Point(3.14, 2.71), got Point(%f, %f)", point2.X, point2.Y)
869 }
870}
871

Callers

nothing calls this directly

Calls 12

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
GetGoObjectMethod · 0.80
FreeMethod · 0.45

Tested by

no test coverage detected