backported JSVM toString

This commit is contained in:
Gani Georgiev
2024-10-18 16:32:43 +03:00
parent e18ea88113
commit cffbe83399
4 changed files with 6550 additions and 6463 deletions
+36 -1
View File
@@ -47,7 +47,7 @@ func TestBaseBindsCount(t *testing.T) {
vm := goja.New()
baseBinds(vm)
testBindsCount(vm, "this", 17, t)
testBindsCount(vm, "this", 18, t)
}
func TestBaseBindsSleep(t *testing.T) {
@@ -92,6 +92,41 @@ func TestBaseBindsReaderToString(t *testing.T) {
}
}
func TestBaseBindsToString(t *testing.T) {
vm := goja.New()
baseBinds(vm)
vm.Set("scenarios", []struct {
Name string
Value any
Expected string
}{
{"null", nil, ""},
{"string", "test", "test"},
{"number", -12.4, "-12.4"},
{"bool", true, "true"},
{"arr", []int{1, 2, 3}, `[1,2,3]`},
{"obj", map[string]any{"test": 123}, `{"test":123}`},
{"reader", strings.NewReader("test"), "test"},
{"struct", struct {
Name string
private string
}{Name: "123", private: "456"}, `{"Name":"123"}`},
})
_, err := vm.RunString(`
for (let s of scenarios) {
let result = toString(s.value)
if (result != s.expected) {
throw new Error('[' + s.name + '] Expected string ' + s.expected + ', got ' + result);
}
}
`)
if err != nil {
t.Fatal(err)
}
}
func TestBaseBindsCookie(t *testing.T) {
app, _ := tests.NewTestApp()
defer app.Cleanup()