TestReadOnlyAndWriteOnlyAccessors tests readonly and writeonly accessor functionality
(t *testing.T)
| 1744 | |
| 1745 | // TestReadOnlyAndWriteOnlyAccessors tests readonly and writeonly accessor functionality |
| 1746 | func TestReadOnlyAndWriteOnlyAccessors(t *testing.T) { |
| 1747 | rt := NewRuntime() |
| 1748 | defer rt.Close() |
| 1749 | ctx := rt.NewContext() |
| 1750 | defer ctx.Close() |
| 1751 | |
| 1752 | // Test ReadOnlyAccessor |
| 1753 | constructor1, _ := NewClassBuilder("ReadOnlyTest"). |
| 1754 | Constructor(func(ctx *Context, instance *Value, args []*Value) (interface{}, error) { |
| 1755 | return &Point{X: 10, Y: 20}, nil |
| 1756 | }). |
| 1757 | Accessor("readOnlyX", func(ctx *Context, this *Value) *Value { |
| 1758 | obj, _ := this.GetGoObject() |
| 1759 | point := obj.(*Point) |
| 1760 | return ctx.NewFloat64(point.X) // Updated: Use NewFloat64() |
| 1761 | }, nil). |
| 1762 | Build(ctx) |
| 1763 | |
| 1764 | if constructor1.IsException() { |
| 1765 | err := ctx.Exception() |
| 1766 | t.Fatalf("Failed to create ReadOnlyTest class: %v", err) |
| 1767 | } |
| 1768 | |
| 1769 | ctx.Globals().Set("ReadOnlyTest", constructor1) |
| 1770 | |
| 1771 | // Test reading works, writing doesn't change value |
| 1772 | result := ctx.Eval(` |
| 1773 | let obj1 = new ReadOnlyTest(); |
| 1774 | let original = obj1.readOnlyX; |
| 1775 | obj1.readOnlyX = 999; // Should not change |
| 1776 | [original, obj1.readOnlyX]; |
| 1777 | `) |
| 1778 | defer result.Free() |
| 1779 | if result.IsException() { |
| 1780 | err := ctx.Exception() |
| 1781 | t.Fatalf("ReadOnly accessor test failed: %v", err) |
| 1782 | } |
| 1783 | |
| 1784 | if result.GetIdx(0).ToFloat64() != 10.0 || result.GetIdx(1).ToFloat64() != 10.0 { // Updated: Use ToFloat64() |
| 1785 | t.Errorf("ReadOnly accessor failed: expected [10, 10], got [%f, %f]", |
| 1786 | result.GetIdx(0).ToFloat64(), result.GetIdx(1).ToFloat64()) |
| 1787 | } |
| 1788 | |
| 1789 | // Test WriteOnlyAccessor |
| 1790 | constructor2, _ := NewClassBuilder("WriteOnlyTest"). |
| 1791 | Constructor(func(ctx *Context, instance *Value, args []*Value) (interface{}, error) { |
| 1792 | return &Point{X: 0, Y: 0}, nil |
| 1793 | }). |
| 1794 | Accessor("writeOnlyX", nil, func(ctx *Context, this *Value, value *Value) *Value { |
| 1795 | obj, _ := this.GetGoObject() |
| 1796 | point := obj.(*Point) |
| 1797 | point.X = value.ToFloat64() // Updated: Use ToFloat64() |
| 1798 | return ctx.NewUndefined() // Updated: Use NewUndefined() |
| 1799 | }). |
| 1800 | Accessor("getX", func(ctx *Context, this *Value) *Value { |
| 1801 | obj, _ := this.GetGoObject() |
| 1802 | point := obj.(*Point) |
| 1803 | return ctx.NewFloat64(point.X) // Updated: Use NewFloat64() |
nothing calls this directly
no test coverage detected