TestErrorHandling tests error conditions and edge cases
(t *testing.T)
| 931 | |
| 932 | // TestErrorHandling tests error conditions and edge cases |
| 933 | func TestErrorHandling(t *testing.T) { |
| 934 | rt := NewRuntime() |
| 935 | defer rt.Close() |
| 936 | |
| 937 | context := rt.NewContext() |
| 938 | defer context.Close() |
| 939 | |
| 940 | // Test creating class with empty name |
| 941 | ctor, _ := NewClassBuilder(""). |
| 942 | Constructor(func(ctx *Context, instance *Value, args []*Value) (interface{}, error) { |
| 943 | return nil, nil |
| 944 | }). |
| 945 | Method("getValue", func(ctx *Context, this *Value, args []*Value) *Value { |
| 946 | return ctx.NewFloat64(0) // Updated: Use NewFloat64() |
| 947 | }). |
| 948 | Accessor("y", |
| 949 | func(ctx *Context, this *Value) *Value { // getter |
| 950 | obj, err := this.GetGoObject() |
| 951 | if err != nil { |
| 952 | return ctx.ThrowError(err) |
| 953 | } |
| 954 | point := obj.(*Point) |
| 955 | return ctx.NewFloat64(point.Y) // Updated: Use NewFloat64() |
| 956 | }, |
| 957 | func(ctx *Context, this *Value, value *Value) *Value { // setter |
| 958 | obj, err := this.GetGoObject() |
| 959 | if err != nil { |
| 960 | return ctx.ThrowError(err) |
| 961 | } |
| 962 | point := obj.(*Point) |
| 963 | point.Y = value.ToFloat64() // Updated: Use ToFloat64() |
| 964 | return ctx.NewUndefined() // Updated: Use NewUndefined() |
| 965 | }). |
| 966 | Build(context) |
| 967 | defer ctor.Free() |
| 968 | |
| 969 | if ctor.IsException() { |
| 970 | err := context.Exception() |
| 971 | require.Contains(t, err.Error(), "class name is required") |
| 972 | } |
| 973 | |
| 974 | // Keep C-layer empty-name failure path covered: "\x00" passes Go non-empty check |
| 975 | // but becomes empty after C string conversion. |
| 976 | ctorCEmptyName, _ := NewClassBuilder("\x00"). |
| 977 | Constructor(func(ctx *Context, instance *Value, args []*Value) (interface{}, error) { |
| 978 | return &Point{X: 0, Y: 0}, nil |
| 979 | }). |
| 980 | Method("noop", func(ctx *Context, this *Value, args []*Value) *Value { |
| 981 | return ctx.NewUndefined() |
| 982 | }). |
| 983 | Accessor("x", |
| 984 | func(ctx *Context, this *Value) *Value { |
| 985 | return ctx.NewInt32(0) |
| 986 | }, |
| 987 | func(ctx *Context, this *Value, value *Value) *Value { |
| 988 | return ctx.NewUndefined() |
| 989 | }). |
| 990 | Build(context) |
nothing calls this directly
no test coverage detected