This repository was archived by the owner on Apr 30, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathjs_test.go
226 lines (203 loc) · 6.02 KB
/
js_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
// Copyright 2018 The GopherWasm Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package js_test
import (
"fmt"
"math"
"testing"
"github.com/gopherjs/gopherwasm/js"
)
func TestNull(t *testing.T) {
want := "null"
if got := js.Null().String(); got != want {
t.Errorf("got %#v, want %#v", got, want)
}
}
func TestCallback(t *testing.T) {
ch := make(chan int)
c := js.NewCallback(func(args []js.Value) {
ch <- args[0].Int() + args[1].Int()
})
defer c.Release()
js.ValueOf(c).Invoke(1, 2)
got := <-ch
want := 3
if got != want {
t.Errorf("got %#v, want %#v", got, want)
}
}
func TestCallbackObject(t *testing.T) {
ch := make(chan string)
c := js.NewCallback(func(args []js.Value) {
ch <- args[0].Get("foo").String()
})
defer c.Release()
js.ValueOf(c).Invoke(js.Global().Call("eval", `({"foo": "bar"})`))
got := <-ch
want := "bar"
if got != want {
t.Errorf("got %#v, want %#v", got, want)
}
}
func TestString(t *testing.T) {
obj := js.Global().Call("eval", "'Hello'")
got := obj.String()
if want := "Hello"; got != want {
t.Errorf("got %#v, want %#v", got, want)
}
obj = js.Global().Call("eval", "123")
got = obj.String()
if want := "123"; got != want {
t.Errorf("got %#v, want %#v", got, want)
}
}
func TestInt64(t *testing.T) {
var i int64 = math.MaxInt64
got := js.ValueOf(i).String()
// js.Value keeps the value only in 53-bit precision.
if want := "9223372036854776000"; got != want {
t.Errorf("got %#v, want %#v", got, want)
}
}
func TestInstanceOf(t *testing.T) {
arr := js.Global().Call("eval", "[]")
got := arr.InstanceOf(js.Global().Call("eval", "Array"))
want := true
if got != want {
t.Errorf("got %#v, want %#v", got, want)
}
got = arr.InstanceOf(js.Global().Call("eval", "Object"))
want = true
if got != want {
t.Errorf("got %#v, want %#v", got, want)
}
got = arr.InstanceOf(js.Global().Call("eval", "String"))
want = false
if got != want {
t.Errorf("got %#v, want %#v", got, want)
}
str := js.Global().Call("eval", "String").New()
got = str.InstanceOf(js.Global().Call("eval", "Array"))
want = false
if got != want {
t.Errorf("got %#v, want %#v", got, want)
}
got = str.InstanceOf(js.Global().Call("eval", "Object"))
want = true
if got != want {
t.Errorf("got %#v, want %#v", got, want)
}
got = str.InstanceOf(js.Global().Call("eval", "String"))
want = true
if got != want {
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)
}
a2 := js.TypedArrayOf(slice)
v := js.ValueOf(a2)
got = v.Index(1).Float()
a2.Release()
if 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)
}
}
func TestValueOf(t *testing.T) {
JSON := js.Global().Get("JSON")
for _, test := range []struct {
in interface{}
wantType js.Type
wantString string
}{
{js.Value(js.ValueOf(42)), js.TypeNumber, "42"},
{js.NewCallback(func(args []js.Value) {}), js.TypeFunction, ""},
{js.TypedArrayOf([]int8{1, 2, 3}), js.TypeObject, `{"0":1,"1":2,"2":3}`},
{nil, js.TypeNull, "null"},
{bool(true), js.TypeBoolean, "true"},
{int(1), js.TypeNumber, "1"},
{int8(2), js.TypeNumber, "2"},
{int16(3), js.TypeNumber, "3"},
{int32(4), js.TypeNumber, "4"},
{int64(5), js.TypeNumber, "5"},
{uint(6), js.TypeNumber, "6"},
{uint8(7), js.TypeNumber, "7"},
{uint16(8), js.TypeNumber, "8"},
{uint32(9), js.TypeNumber, "9"},
{uint64(10), js.TypeNumber, "10"},
{float32(11), js.TypeNumber, "11"},
{float64(12), js.TypeNumber, "12"},
// FIXME this doesn't work {unsafe.Pointer(&x19), js.TypeNumber, ""},
{string("hello"), js.TypeString, `"hello"`},
{map[string]interface{}{"a": 1}, js.TypeObject, `{"a":1}`},
{[]interface{}{1, 2, 3}, js.TypeObject, "[1,2,3]"},
} {
t.Run(fmt.Sprintf("%T", test.in), func(t *testing.T) {
got := js.ValueOf(test.in)
if got.Type() != test.wantType {
t.Errorf("type: got %v want %v", got.Type(), test.wantType)
}
gotString := JSON.Call("stringify", got).String()
if test.wantString != "" && gotString != test.wantString {
t.Errorf("string: got %v want %v", gotString, test.wantString)
}
})
}
// Check unknown types panic
didPanic := false
func() {
defer func() {
if r := recover(); r != nil {
didPanic = true
}
}()
_ = js.ValueOf([]struct{}{})
}()
if !didPanic {
t.Errorf("Unknown type didn't panic")
}
}