forked from dop251/goja
-
Notifications
You must be signed in to change notification settings - Fork 0
/
builtin_json_test.go
140 lines (124 loc) · 3 KB
/
builtin_json_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package goja
import (
"encoding/json"
"errors"
"strings"
"testing"
"time"
)
func TestJSONMarshalObject(t *testing.T) {
vm := New()
o := vm.NewObject()
o.Set("test", 42)
o.Set("testfunc", vm.Get("Error"))
b, err := json.Marshal(o)
if err != nil {
t.Fatal(err)
}
if string(b) != `{"test":42}` {
t.Fatalf("Unexpected value: %s", b)
}
}
func TestJSONMarshalGoDate(t *testing.T) {
vm := New()
o := vm.NewObject()
o.Set("test", time.Unix(86400, 0).UTC())
b, err := json.Marshal(o)
if err != nil {
t.Fatal(err)
}
if string(b) != `{"test":"1970-01-02T00:00:00Z"}` {
t.Fatalf("Unexpected value: %s", b)
}
}
func TestJSONMarshalObjectCircular(t *testing.T) {
vm := New()
o := vm.NewObject()
o.Set("o", o)
_, err := json.Marshal(o)
if err == nil {
t.Fatal("Expected error")
}
if !strings.HasSuffix(err.Error(), "Converting circular structure to JSON") {
t.Fatalf("Unexpected error: %v", err)
}
}
func TestJSONStringifyCircularWrappedGo(t *testing.T) {
type CircularType struct {
Self *CircularType
}
vm := New()
v := CircularType{}
v.Self = &v
vm.Set("v", &v)
_, err := vm.RunString("JSON.stringify(v)")
if err == nil {
t.Fatal("Expected error")
}
if !strings.HasPrefix(err.Error(), "TypeError: Converting circular structure to JSON") {
t.Fatalf("Unexpected error: %v", err)
}
}
func TestJSONParseReviver(t *testing.T) {
// example from
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse
const SCRIPT = `
JSON.parse('{"p": 5}', function(key, value) {
return typeof value === 'number'
? value * 2 // return value * 2 for numbers
: value // return everything else unchanged
})["p"]
`
testScript(SCRIPT, intToValue(10), t)
}
func TestQuoteMalformedSurrogatePair(t *testing.T) {
testScript(`JSON.stringify("\uD800")`, asciiString(`"\ud800"`), t)
}
func TestEOFWrapping(t *testing.T) {
vm := New()
_, err := vm.RunString("JSON.parse('{')")
if err == nil {
t.Fatal("Expected error")
}
if !strings.Contains(err.Error(), "Unexpected end of JSON input") {
t.Fatalf("Error doesn't contain human-friendly wrapper: %v", err)
}
}
type testMarshalJSONErrorStruct struct {
e error
}
func (s *testMarshalJSONErrorStruct) MarshalJSON() ([]byte, error) {
return nil, s.e
}
func TestMarshalJSONError(t *testing.T) {
vm := New()
v := testMarshalJSONErrorStruct{e: errors.New("test error")}
vm.Set("v", &v)
_, err := vm.RunString("JSON.stringify(v)")
if !errors.Is(err, v.e) {
t.Fatalf("Unexpected error: %v", err)
}
}
func BenchmarkJSONStringify(b *testing.B) {
b.StopTimer()
vm := New()
var createObj func(level int) *Object
createObj = func(level int) *Object {
o := vm.NewObject()
o.Set("field1", "test")
o.Set("field2", 42)
if level > 0 {
level--
o.Set("obj1", createObj(level))
o.Set("obj2", createObj(level))
}
return o
}
o := createObj(3)
json := vm.Get("JSON").(*Object)
stringify, _ := AssertFunction(json.Get("stringify"))
b.StartTimer()
for i := 0; i < b.N; i++ {
stringify(nil, o)
}
}