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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
|
// Copyright 2018 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// +build js,wasm
package js_test
import (
"fmt"
"math"
"syscall/js"
"testing"
)
var dummys = js.Global().Call("eval", `({
someBool: true,
someString: "abc\u1234",
someInt: 42,
someFloat: 42.123,
someArray: [41, 42, 43],
add: function(a, b) {
return a + b;
},
NaN: NaN,
})`)
func TestBool(t *testing.T) {
want := true
o := dummys.Get("someBool")
if got := o.Bool(); got != want {
t.Errorf("got %#v, want %#v", got, want)
}
dummys.Set("otherBool", want)
if got := dummys.Get("otherBool").Bool(); got != want {
t.Errorf("got %#v, want %#v", got, want)
}
if dummys.Get("someBool") != dummys.Get("someBool") {
t.Errorf("same value not equal")
}
}
func TestString(t *testing.T) {
want := "abc\u1234"
o := dummys.Get("someString")
if got := o.String(); got != want {
t.Errorf("got %#v, want %#v", got, want)
}
dummys.Set("otherString", want)
if got := dummys.Get("otherString").String(); got != want {
t.Errorf("got %#v, want %#v", got, want)
}
if dummys.Get("someString") != dummys.Get("someString") {
t.Errorf("same value not equal")
}
wantInt := "42"
o = dummys.Get("someInt")
if got := o.String(); got != wantInt {
t.Errorf("got %#v, want %#v", got, wantInt)
}
}
func TestInt(t *testing.T) {
want := 42
o := dummys.Get("someInt")
if got := o.Int(); got != want {
t.Errorf("got %#v, want %#v", got, want)
}
dummys.Set("otherInt", want)
if got := dummys.Get("otherInt").Int(); got != want {
t.Errorf("got %#v, want %#v", got, want)
}
if dummys.Get("someInt") != dummys.Get("someInt") {
t.Errorf("same value not equal")
}
}
func TestIntConversion(t *testing.T) {
testIntConversion(t, 0)
testIntConversion(t, 1)
testIntConversion(t, -1)
testIntConversion(t, 1<<20)
testIntConversion(t, -1<<20)
testIntConversion(t, 1<<40)
testIntConversion(t, -1<<40)
testIntConversion(t, 1<<60)
testIntConversion(t, -1<<60)
}
func testIntConversion(t *testing.T, want int) {
if got := js.ValueOf(want).Int(); got != want {
t.Errorf("got %#v, want %#v", got, want)
}
}
func TestFloat(t *testing.T) {
want := 42.123
o := dummys.Get("someFloat")
if got := o.Float(); got != want {
t.Errorf("got %#v, want %#v", got, want)
}
dummys.Set("otherFloat", want)
if got := dummys.Get("otherFloat").Float(); got != want {
t.Errorf("got %#v, want %#v", got, want)
}
if dummys.Get("someFloat") != dummys.Get("someFloat") {
t.Errorf("same value not equal")
}
}
func TestObject(t *testing.T) {
if dummys.Get("someArray") != dummys.Get("someArray") {
t.Errorf("same value not equal")
}
// An object and its prototype should not be equal.
proto := js.Global().Get("Object").Get("prototype")
o := js.Global().Call("eval", "new Object()")
if proto == o {
t.Errorf("object equals to its prototype")
}
}
func TestFrozenObject(t *testing.T) {
o := js.Global().Call("eval", "(function () { let o = new Object(); o.field = 5; Object.freeze(o); return o; })()")
want := 5
if got := o.Get("field").Int(); want != got {
t.Errorf("got %#v, want %#v", got, want)
}
}
func TestTypedArrayOf(t *testing.T) {
testTypedArrayOf(t, "[]int8", []int8{0, -42, 0}, -42)
testTypedArrayOf(t, "[]int16", []int16{0, -42, 0}, -42)
testTypedArrayOf(t, "[]int32", []int32{0, -42, 0}, -42)
testTypedArrayOf(t, "[]uint8", []uint8{0, 42, 0}, 42)
testTypedArrayOf(t, "[]uint16", []uint16{0, 42, 0}, 42)
testTypedArrayOf(t, "[]uint32", []uint32{0, 42, 0}, 42)
testTypedArrayOf(t, "[]float32", []float32{0, -42.5, 0}, -42.5)
testTypedArrayOf(t, "[]float64", []float64{0, -42.5, 0}, -42.5)
}
func testTypedArrayOf(t *testing.T, name string, slice interface{}, want float64) {
t.Run(name, func(t *testing.T) {
a := js.TypedArrayOf(slice)
got := a.Index(1).Float()
a.Release()
if got != want {
t.Errorf("got %#v, want %#v", got, want)
}
})
}
func TestNaN(t *testing.T) {
want := js.ValueOf(math.NaN())
got := dummys.Get("NaN")
if got != want {
t.Errorf("got %#v, want %#v", got, want)
}
}
func TestUndefined(t *testing.T) {
dummys.Set("test", js.Undefined())
if dummys == js.Undefined() || dummys.Get("test") != js.Undefined() || dummys.Get("xyz") != js.Undefined() {
t.Errorf("js.Undefined expected")
}
}
func TestNull(t *testing.T) {
dummys.Set("test1", nil)
dummys.Set("test2", js.Null())
if dummys == js.Null() || dummys.Get("test1") != js.Null() || dummys.Get("test2") != js.Null() {
t.Errorf("js.Null expected")
}
}
func TestLength(t *testing.T) {
if got := dummys.Get("someArray").Length(); got != 3 {
t.Errorf("got %#v, want %#v", got, 3)
}
}
func TestIndex(t *testing.T) {
if got := dummys.Get("someArray").Index(1).Int(); got != 42 {
t.Errorf("got %#v, want %#v", got, 42)
}
}
func TestSetIndex(t *testing.T) {
dummys.Get("someArray").SetIndex(2, 99)
if got := dummys.Get("someArray").Index(2).Int(); got != 99 {
t.Errorf("got %#v, want %#v", got, 99)
}
}
func TestCall(t *testing.T) {
var i int64 = 40
if got := dummys.Call("add", i, 2).Int(); got != 42 {
t.Errorf("got %#v, want %#v", got, 42)
}
if got := dummys.Call("add", js.Global().Call("eval", "40"), 2).Int(); got != 42 {
t.Errorf("got %#v, want %#v", got, 42)
}
}
func TestInvoke(t *testing.T) {
var i int64 = 40
if got := dummys.Get("add").Invoke(i, 2).Int(); got != 42 {
t.Errorf("got %#v, want %#v", got, 42)
}
}
func TestNew(t *testing.T) {
if got := js.Global().Get("Array").New(42).Length(); got != 42 {
t.Errorf("got %#v, want %#v", got, 42)
}
}
func TestInstanceOf(t *testing.T) {
someArray := js.Global().Get("Array").New()
if got, want := someArray.InstanceOf(js.Global().Get("Array")), true; got != want {
t.Errorf("got %#v, want %#v", got, want)
}
if got, want := someArray.InstanceOf(js.Global().Get("Function")), false; got != want {
t.Errorf("got %#v, want %#v", got, want)
}
}
func TestType(t *testing.T) {
if got, want := js.Undefined().Type(), js.TypeUndefined; got != want {
t.Errorf("got %s, want %s", got, want)
}
if got, want := js.Null().Type(), js.TypeNull; got != want {
t.Errorf("got %s, want %s", got, want)
}
if got, want := js.ValueOf(true).Type(), js.TypeBoolean; got != want {
t.Errorf("got %s, want %s", got, want)
}
if got, want := js.ValueOf(42).Type(), js.TypeNumber; got != want {
t.Errorf("got %s, want %s", got, want)
}
if got, want := js.ValueOf("test").Type(), js.TypeString; got != want {
t.Errorf("got %s, want %s", got, want)
}
if got, want := js.Global().Get("Symbol").Invoke("test").Type(), js.TypeSymbol; got != want {
t.Errorf("got %s, want %s", got, want)
}
if got, want := js.Global().Get("Array").New().Type(), js.TypeObject; got != want {
t.Errorf("got %s, want %s", got, want)
}
if got, want := js.Global().Get("Array").Type(), js.TypeFunction; got != want {
t.Errorf("got %s, want %s", got, want)
}
}
type object = map[string]interface{}
type array = []interface{}
func TestValueOf(t *testing.T) {
a := js.ValueOf(array{0, array{0, 42, 0}, 0})
if got := a.Index(1).Index(1).Int(); got != 42 {
t.Errorf("got %v, want %v", got, 42)
}
o := js.ValueOf(object{"x": object{"y": 42}})
if got := o.Get("x").Get("y").Int(); got != 42 {
t.Errorf("got %v, want %v", got, 42)
}
}
func TestCallback(t *testing.T) {
c := make(chan struct{})
cb := js.NewCallback(func(args []js.Value) {
if got := args[0].Int(); got != 42 {
t.Errorf("got %#v, want %#v", got, 42)
}
c <- struct{}{}
})
defer cb.Release()
js.Global().Call("setTimeout", cb, 0, 42)
<-c
}
func TestEventCallback(t *testing.T) {
for _, name := range []string{"preventDefault", "stopPropagation", "stopImmediatePropagation"} {
c := make(chan struct{})
var flags js.EventCallbackFlag
switch name {
case "preventDefault":
flags = js.PreventDefault
case "stopPropagation":
flags = js.StopPropagation
case "stopImmediatePropagation":
flags = js.StopImmediatePropagation
}
cb := js.NewEventCallback(flags, func(event js.Value) {
c <- struct{}{}
})
defer cb.Release()
event := js.Global().Call("eval", fmt.Sprintf("({ called: false, %s: function() { this.called = true; } })", name))
cb.Invoke(event)
if !event.Get("called").Bool() {
t.Errorf("%s not called", name)
}
<-c
}
}
func ExampleNewCallback() {
var cb js.Callback
cb = js.NewCallback(func(args []js.Value) {
fmt.Println("button clicked")
cb.Release() // release the callback if the button will not be clicked again
})
js.Global().Get("document").Call("getElementById", "myButton").Call("addEventListener", "click", cb)
}
|