MCPcopy Create free account
hub / github.com/dop251/goja / TestRuntime_ExportTo_funcVariadic

Function TestRuntime_ExportTo_funcVariadic

runtime_test.go:964–1012  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

962}
963
964func TestRuntime_ExportTo_funcVariadic(t *testing.T) {
965 const SCRIPT = `
966 function f(...args) {
967 return args.join("#");
968 }
969 `
970 vm := New()
971 _, err := vm.RunString(SCRIPT)
972 if err != nil {
973 panic(err)
974 }
975
976 t.Run("no args", func(t *testing.T) {
977 var fn func(args ...any) string
978 err = vm.ExportTo(vm.Get("f"), &fn)
979 if err != nil {
980 panic(err)
981 }
982 res := fn()
983 if res != "" {
984 t.Fatal(res)
985 }
986 })
987
988 t.Run("non-variadic args", func(t *testing.T) {
989 var fn func(firstArg any, args ...any) string
990 err = vm.ExportTo(vm.Get("f"), &fn)
991 if err != nil {
992 panic(err)
993 }
994 res := fn("first")
995 if res != "first" {
996 t.Fatal(res)
997 }
998 })
999
1000 t.Run("non-variadic and variadic args", func(t *testing.T) {
1001 var fn func(firstArg any, args ...any) string
1002 err = vm.ExportTo(vm.Get("f"), &fn)
1003 if err != nil {
1004 panic(err)
1005 }
1006 res := fn("first", "second")
1007 if res != "first#second" {
1008 t.Fatal(res)
1009 }
1010 })
1011
1012}
1013
1014func TestRuntime_ExportToFuncFail(t *testing.T) {
1015 const SCRIPT = `

Callers

nothing calls this directly

Calls 4

NewFunction · 0.85
RunStringMethod · 0.80
ExportToMethod · 0.80
GetMethod · 0.65

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…