TestGetGoObject tests retrieving Go objects from JS instances
(t *testing.T)
| 812 | |
| 813 | // TestGetGoObject tests retrieving Go objects from JS instances |
| 814 | func 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 |
nothing calls this directly
no test coverage detected