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

Function TestBridgeCreateClassInstanceEdgeCases

bridge_test.go:1729–1794  ·  view source on GitHub ↗

NEW TEST FOR SCHEME C: Test CreateClassInstance C function behavior

(t *testing.T)

Source from the content-addressed store, hash-verified

1727
1728// NEW TEST FOR SCHEME C: Test CreateClassInstance C function behavior
1729func TestBridgeCreateClassInstanceEdgeCases(t *testing.T) {
1730 t.Run("CreateClassInstance_NoProperties", func(t *testing.T) {
1731 rt := NewRuntime()
1732 defer rt.Close()
1733 ctx := rt.NewContext()
1734 defer ctx.Close()
1735
1736 // Create class without instance properties
1737 constructor, _ := NewClassBuilder("NoPropsClass").
1738 Constructor(func(ctx *Context, instance *Value, args []*Value) (interface{}, error) {
1739 return &Point{X: 1, Y: 2}, nil
1740 }).
1741 Build(ctx)
1742 require.False(t, constructor.IsException())
1743
1744 ctx.Globals().Set("NoPropsClass", constructor)
1745
1746 // Test that instances are created successfully even without properties
1747 result := ctx.Eval(`
1748 let obj = new NoPropsClass();
1749 typeof obj;
1750 `)
1751 require.False(t, result.IsException())
1752 defer result.Free()
1753 require.Equal(t, "object", result.ToString()) // Changed: String() → ToString()
1754
1755 t.Log("Successfully tested CreateClassInstance with no instance properties")
1756 })
1757
1758 t.Run("CreateClassInstance_ManyProperties", func(t *testing.T) {
1759 rt := NewRuntime()
1760 defer rt.Close()
1761 ctx := rt.NewContext()
1762 defer ctx.Close()
1763
1764 // Create class with many instance properties
1765 builder := NewClassBuilder("ManyPropsClass").
1766 Constructor(func(ctx *Context, instance *Value, args []*Value) (interface{}, error) {
1767 return &Point{X: 1, Y: 2}, nil
1768 })
1769
1770 // Add multiple instance properties
1771 for i := 0; i < 10; i++ {
1772 builder = builder.Property(fmt.Sprintf("prop%d", i), ctx.NewString(fmt.Sprintf("value%d", i))) // Changed: String() → NewString()
1773 }
1774
1775 constructor, _ := builder.Build(ctx)
1776 require.False(t, constructor.IsException())
1777
1778 ctx.Globals().Set("ManyPropsClass", constructor)
1779
1780 // Test that all properties are bound correctly
1781 result := ctx.Eval(`
1782 let obj = new ManyPropsClass();
1783 [obj.prop0, obj.prop5, obj.prop9];
1784 `)
1785 require.False(t, result.IsException())
1786 defer result.Free()

Callers

nothing calls this directly

Calls 15

CloseMethod · 0.95
NewContextMethod · 0.95
CloseMethod · 0.95
GlobalsMethod · 0.95
EvalMethod · 0.95
NewStringMethod · 0.95
NewRuntimeFunction · 0.85
NewClassBuilderFunction · 0.85
ConstructorMethod · 0.80
IsExceptionMethod · 0.80
SetMethod · 0.80
EqualMethod · 0.80

Tested by

no test coverage detected