-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathjsbuiltin_test.go
241 lines (222 loc) · 6.23 KB
/
jsbuiltin_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
// +build js
package jsbuiltin
import (
"testing"
"github.com/gopherjs/gopherjs/js"
)
var (
util = js.Global.Call("require", "util")
)
func inspect(i interface{}) string {
return util.Call("inspect", i).String()
}
func TestEncodeURI(t *testing.T) {
data := map[string]string{
"http://foo.com/?msg=Привет мир.": "http://foo.com/?msg=%D0%9F%D1%80%D0%B8%D0%B2%D0%B5%D1%82%20%D0%BC%D0%B8%D1%80.",
"http://user:[email protected]/": "http://user:[email protected]/",
}
for url, expected := range data {
result := EncodeURI(url)
if result != expected {
t.Fatalf("EncodeURI(%s) returned '%s', not '%s'", url, result, expected)
}
}
}
type testData struct {
URL string
ExpectedURL string
ExpectedError string
}
func TestDecodeURI(t *testing.T) {
data := []testData{
testData{
"http://foo.com/?msg=%D0%9F%D1%80%D0%B8%D0%B2%D0%B5%D1%82%20%D0%BC%D0%B8%D1%80.", "http://foo.com/?msg=Привет мир.", "",
},
testData{
"http://user:[email protected]/", "http://user:[email protected]/", "",
},
testData{
"http://foo.com/?invalidutf8=%80", "", "JavaScript error: URI malformed",
},
}
for _, test := range data {
result, err := DecodeURI(test.URL)
if test.ExpectedError != "" {
if err == nil {
t.Fatalf("DecodeURI(%s) should have resulted in an error", test.URL)
}
if err.Error() != test.ExpectedError {
t.Fatalf("DecodeURI(%s) should have resulted in error '%s', got '%s'", test.URL, test.ExpectedError, err)
}
} else {
if err != nil {
t.Fatalf("DecodeURI() resulted in an error: %s", err)
}
if result != test.ExpectedURL {
t.Fatalf("DecodeURI(%s) returned '%s', not '%s'", test.URL, result, test.ExpectedURL)
}
}
}
}
func TestEncodeURIComponentn(t *testing.T) {
data := map[string]string{
"Привет мир.": "%D0%9F%D1%80%D0%B8%D0%B2%D0%B5%D1%82%20%D0%BC%D0%B8%D1%80.",
"bar": "bar",
}
for url, expected := range data {
result := EncodeURIComponent(url)
if result != expected {
t.Fatalf("EncodeURIComponent(%s) returned '%s', not '%s'", url, result, expected)
}
}
}
func TestDecodeURIComponentn(t *testing.T) {
data := []testData{
testData{
"%D0%9F%D1%80%D0%B8%D0%B2%D0%B5%D1%82%20%D0%BC%D0%B8%D1%80.", "Привет мир.", "",
},
testData{
"bar", "bar", "",
},
testData{
"%80", "", "JavaScript error: URI malformed",
},
}
for _, test := range data {
result, err := DecodeURIComponent(test.URL)
if test.ExpectedError != "" {
if err == nil {
t.Fatalf("DecodeURIComponent(%s) should have resulted in an error", test.URL)
}
if err.Error() != test.ExpectedError {
t.Fatalf("DecodeURIComponent(%s) should have resulted in error '%s', got '%s'", test.URL, test.ExpectedError, err)
}
} else {
if err != nil {
t.Fatalf("DecodeURIComponent() resulted in an error: %s", err)
}
if result != test.ExpectedURL {
t.Fatalf("DecodeURIComponent(%s) returned '%s', not '%s'", test.URL, result, test.ExpectedURL)
}
}
}
}
func TestIsFinite(t *testing.T) {
data := map[interface{}]bool{
123: true,
-1.23: true,
5 - 2: true,
0: true,
"123": true,
"Hello": false,
"2005/12/12": false,
}
for value, expected := range data {
result := IsFinite(value)
if result != expected {
t.Fatalf("IsFinite(%s) returned %t, not %t", value, result, expected)
}
}
}
func TestIsNaN(t *testing.T) {
data := map[interface{}]bool{
123: false,
-1.23: false,
5 - 2: false,
0: false,
"123": false,
"Hello": true,
"2005/12/12": true,
}
for value, expected := range data {
result := IsNaN(value)
if result != expected {
t.Fatalf("IsNaN(%s) returned %t, not %t", value, result, expected)
}
}
}
type toTest struct {
value interface{}
result string
}
func TestTypeOf(t *testing.T) {
data := []toTest{
// Standard JS types
toTest{js.Undefined, "undefined"},
toTest{nil, "object"},
toTest{true, "boolean"},
toTest{false, "boolean"},
toTest{12345, "number"},
toTest{"one two three", "string"},
toTest{js.Global.Call, "function"},
toTest{js.Global, "object"},
}
// Check whether the JS interpretor supports the 'symbol' type (Node >= 0.12)
if TypeOf(js.Global.Get("Symbol")) == "function" {
symbol := js.Global.Call("Symbol", "foo")
data = append(data, toTest{&symbol, "symbol"})
}
for _, test := range data {
result := TypeOf(test.value)
if result != test.result {
t.Fatalf("Typeof(%s) returned %s, not %s", test.value, result, test.result)
}
}
if to := TypeOf(map[string]string{}); to != "object" {
t.Fatalf("Obscure type not recognized as object")
}
if to := TypeOf(js.Object{}); to != "object" {
t.Fatal("Invalid/empty JS object not recognized as object")
}
}
type ioTest struct {
value interface{}
object *js.Object
result bool
}
func TestInstanceOf(t *testing.T) {
data := []ioTest{
// Standard JS types
ioTest{js.Undefined, js.Global.Get("Object"), false},
ioTest{"a string", js.Global.Get("String"), false},
ioTest{js.Global.Get("String").New("foo"), js.Global.Get("String"), true},
ioTest{js.Global.Get("String").New("foo"), js.Global.Get("Object"), true},
}
for _, test := range data {
result := InstanceOf(test.value, test.object)
if result != test.result {
t.Errorf("InstanceOf(%s,%s) returned %t, not %t", test.value, test.object, result, test.result)
}
}
}
type inTest struct {
obj *js.Object
key string
result bool
err string
}
func TestIn(t *testing.T) {
obj := js.Global.Get("Object").New()
obj.Set("foo", "bar")
jsString := js.Global.Call("eval", `'"test string"'`)
data := []inTest{
{obj: obj, key: "foo", result: true},
{obj: obj, key: "bar", result: false},
{obj: js.Undefined, key: "foo", err: "obj not a JavaScript object"},
{obj: nil, key: "foo", err: "obj not a JavaScript object"},
{obj: jsString, key: "foo", err: "obj not a JavaScript object"},
}
for _, test := range data {
result, err := In(test.key, test.obj)
var msg string
if err != nil {
msg = err.Error()
}
if msg != test.err {
t.Errorf("Unexpected error for In(%v, %s): got %q, want %q", inspect(test.obj), test.key, msg, test.err)
}
if result != test.result {
t.Errorf("In(%v, %s) returned %t, not %t", inspect(test.obj), test.key, result, test.result)
}
}
}