forked from dop251/goja
-
Notifications
You must be signed in to change notification settings - Fork 22
/
object_goarray_reflect_test.go
304 lines (270 loc) · 5.62 KB
/
object_goarray_reflect_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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
package goja
import (
"testing"
)
func TestGoReflectArray(t *testing.T) {
vm := New()
vm.Set("a", [...]int{1, 2, 3})
_, err := vm.RunString(`
if (!Array.isArray(a)) {
throw new Error("isArray() returned false");
}
if (a[0] !== 1 || a[1] !== 2 || a[2] !== 3) {
throw new Error("Array contents is incorrect");
}
if (!a.hasOwnProperty("length")) {
throw new Error("hasOwnProperty() returned false");
}
let desc = Object.getOwnPropertyDescriptor(a, "length");
if (desc.value !== 3 || desc.writable || desc.enumerable || desc.configurable) {
throw new Error("incorrect property descriptor: " + JSON.stringify(desc));
}
`)
if err != nil {
t.Fatal(err)
}
}
func TestGoReflectArraySort(t *testing.T) {
vm := New()
vm.Set("a", [...]int{3, 1, 2})
v, err := vm.RunString(`
a.sort();
if (a[0] !== 1 || a[1] !== 2 || a[2] !== 3) {
throw new Error(a.toString());
}
a;
`)
if err != nil {
t.Fatal(err)
}
res := v.Export()
if a, ok := res.([3]int); ok {
if a[0] != 1 || a[1] != 2 || a[2] != 3 {
t.Fatal(a)
}
} else {
t.Fatalf("Wrong type: %T", res)
}
}
func TestGoReflectArrayCopyOnChange(t *testing.T) {
vm := New()
v, err := vm.RunString(`
a => {
let tmp = a[0];
if (tmp !== a[0]) {
throw new Error("tmp !== a[0]");
}
a[0] = a[1];
if (tmp === a[0]) {
throw new Error("tmp === a[0]");
}
if (tmp.Test !== "1") {
throw new Error("tmp.Test: " + tmp.Test + " (" + typeof tmp.Test + ")");
}
if (a[0].Test !== "2") {
throw new Error("a[0].Test: " + a[0].Test);
}
a[0].Test = "3";
if (a[0].Test !== "3") {
throw new Error("a[0].Test (1): " + a[0].Test);
}
tmp = a[0];
tmp.Test = "4";
if (a[0].Test !== "4") {
throw new Error("a[0].Test (2): " + a[0].Test);
}
delete a[0];
if (a[0] && a[0].Test !== "") {
throw new Error("a[0].Test (3): " + a[0].Test);
}
if (tmp.Test !== "4") {
throw new Error("tmp.Test (1): " + tmp.Test);
}
a[1] = tmp;
if (a[1].Test !== "4") {
throw new Error("a[1].Test: " + a[1].Test);
}
// grow
tmp = a[1];
a.push(null);
if (a.length !== 3) {
throw new Error("a.length after push: " + a.length);
}
tmp.Test = "5";
if (a[1].Test !== "5") {
throw new Error("a[1].Test (1): " + a[1].Test);
}
// shrink
a.length = 1;
if (a.length !== 1) {
throw new Error("a.length after shrink: " + a.length);
}
if (tmp.Test !== "5") {
throw new Error("tmp.Test (shrink): " + tmp.Test);
}
}
`)
if err != nil {
t.Fatal(err)
}
fn, ok := AssertFunction(v)
if !ok {
t.Fatal("Not a function")
}
t.Run("[]struct", func(t *testing.T) {
a := []struct {
Test string
}{{"1"}, {"2"}}
_, err := fn(nil, vm.ToValue(a))
if err != nil {
t.Fatal(err)
}
if a[0].Test != "" {
t.Fatalf("a[0]: %#v", a[0])
}
if a[1].Test != "4" {
t.Fatalf("a0[1]: %#v", a[1])
}
})
// The copy-on-change mechanism doesn't apply to the types below because the contained values are references.
// These tests are here for completeness and to prove that the behaviour is consistent.
t.Run("[]I", func(t *testing.T) {
type I interface {
Get() string
}
a := []I{&testGoReflectMethod_O{Test: "1"}, &testGoReflectMethod_O{Test: "2"}}
_, err = fn(nil, vm.ToValue(a))
if err != nil {
t.Fatal(err)
}
})
t.Run("[]interface{}", func(t *testing.T) {
a := []interface{}{&testGoReflectMethod_O{Test: "1"}, &testGoReflectMethod_O{Test: "2"}}
_, err = fn(nil, vm.ToValue(a))
if err != nil {
t.Fatal(err)
}
})
}
func TestCopyOnChangeReflectSlice(t *testing.T) {
vm := New()
v, err := vm.RunString(`
s => {
s.A.push(1);
if (s.A.length !== 1) {
throw new Error("s.A.length: " + s.A.length);
}
if (s.A[0] !== 1) {
throw new Error("s.A[0]: " + s.A[0]);
}
let tmp = s.A;
if (tmp !== s.A) {
throw new Error("tmp !== s.A");
}
s.A = [2];
if (tmp === s.A) {
throw new Error("tmp === s.A");
}
if (tmp[0] !== 1) {
throw new Error("tmp[0]: " + tmp[0]);
}
if (s.A[0] !== 2) {
throw new Error("s.A[0] (1): " + s.A[0]);
}
}
`)
if err != nil {
t.Fatal(err)
}
fn, ok := AssertFunction(v)
if !ok {
t.Fatal("Not a function")
}
t.Run("[]int", func(t *testing.T) {
type S struct {
A []int
}
var s S
_, err := fn(nil, vm.ToValue(&s))
if err != nil {
t.Fatal(err)
}
if len(s.A) != 1 {
t.Fatal(s)
}
if s.A[0] != 2 {
t.Fatal(s.A)
}
})
t.Run("[]interface{}", func(t *testing.T) {
type S struct {
A []interface{}
}
var s S
_, err := fn(nil, vm.ToValue(&s))
if err != nil {
t.Fatal(err)
}
if len(s.A) != 1 {
t.Fatal(s)
}
if s.A[0] != int64(2) {
t.Fatal(s.A)
}
})
}
func TestCopyOnChangeSort(t *testing.T) {
a := []struct {
Test string
}{{"2"}, {"1"}}
vm := New()
vm.Set("a", &a)
_, err := vm.RunString(`
let a0 = a[0];
let a1 = a[1];
a.sort((a, b) => a.Test.localeCompare(b.Test));
if (a[0].Test !== "1") {
throw new Error("a[0]: " + a[0]);
}
if (a[1].Test !== "2") {
throw new Error("a[1]: " + a[1]);
}
if (a0 !== a[1]) {
throw new Error("a0 !== a[1]");
}
if (a1 !== a[0]) {
throw new Error("a1 !== a[0]");
}
`)
if err != nil {
t.Fatal(err)
}
if a[0].Test != "1" || a[1].Test != "2" {
t.Fatal(a)
}
}
type testStringerArray [8]byte
func (a testStringerArray) String() string {
return "X"
}
func TestReflectArrayToString(t *testing.T) {
vm := New()
var a testStringerArray
vm.Set("a", &a)
res, err := vm.RunString("`${a}`")
if err != nil {
t.Fatal(err)
}
if exp := res.Export(); exp != "X" {
t.Fatal(exp)
}
var a1 [2]byte
vm.Set("a", &a1)
res, err = vm.RunString("`${a}`")
if err != nil {
t.Fatal(err)
}
if exp := res.Export(); exp != "0,0" {
t.Fatal(exp)
}
}