TestValueFunctionCalls tests function calls and constructors
(t *testing.T)
| 1243 | |
| 1244 | // TestValueFunctionCalls tests function calls and constructors |
| 1245 | func TestValueFunctionCalls(t *testing.T) { |
| 1246 | useStableOwnerHooksForLegacySubtests(t) |
| 1247 | |
| 1248 | rt := NewRuntime() |
| 1249 | defer rt.Close() |
| 1250 | ctx := rt.NewContext() |
| 1251 | defer ctx.Close() |
| 1252 | |
| 1253 | // Updated to use New* methods |
| 1254 | obj := ctx.NewObject() |
| 1255 | defer obj.Free() |
| 1256 | |
| 1257 | // Test function calls - UPDATED: function signature now uses pointers and New* methods |
| 1258 | addFunc := ctx.NewFunction(func(ctx *Context, this *Value, args []*Value) *Value { |
| 1259 | if len(args) < 2 { |
| 1260 | return ctx.NewInt32(0) |
| 1261 | } |
| 1262 | return ctx.NewInt32(args[0].ToInt32() + args[1].ToInt32()) |
| 1263 | }) |
| 1264 | obj.Set("add", addFunc) |
| 1265 | |
| 1266 | // Call with arguments |
| 1267 | result := obj.Call("add", ctx.NewInt32(3), ctx.NewInt32(4)) |
| 1268 | defer result.Free() |
| 1269 | require.Equal(t, int32(7), result.ToInt32()) |
| 1270 | |
| 1271 | // Call without arguments (covers len(cargs) == 0 branch) |
| 1272 | noArgsFunc := ctx.NewFunction(func(ctx *Context, this *Value, args []*Value) *Value { |
| 1273 | return ctx.NewString("no arguments") |
| 1274 | }) |
| 1275 | obj.Set("noArgs", noArgsFunc) |
| 1276 | |
| 1277 | noArgsResult := obj.Call("noArgs") |
| 1278 | defer noArgsResult.Free() |
| 1279 | require.Equal(t, "no arguments", noArgsResult.String()) |
| 1280 | |
| 1281 | // Execute method |
| 1282 | execResult := addFunc.Execute(ctx.NewNull(), ctx.NewInt32(5), ctx.NewInt32(6)) |
| 1283 | defer execResult.Free() |
| 1284 | require.Equal(t, int32(11), execResult.ToInt32()) |
| 1285 | |
| 1286 | // Test constructors - FIXED: removed error handling |
| 1287 | constructorFunc := ctx.Eval(` |
| 1288 | function TestClass(value) { |
| 1289 | this.value = value; |
| 1290 | } |
| 1291 | TestClass; |
| 1292 | `) |
| 1293 | defer constructorFunc.Free() |
| 1294 | require.False(t, constructorFunc.IsException()) // Check for exceptions instead of error |
| 1295 | |
| 1296 | // CallConstructor with arguments |
| 1297 | instance := constructorFunc.CallConstructor(ctx.NewString("test_value")) |
| 1298 | defer instance.Free() |
| 1299 | require.True(t, instance.IsObject()) |
| 1300 | |
| 1301 | // New (alias for CallConstructor) without arguments |
| 1302 | instance2 := constructorFunc.New() |
nothing calls this directly
no test coverage detected