-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgov7_test.go
170 lines (134 loc) · 3.05 KB
/
gov7_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
package gov7
import "testing"
func TestExec(t *testing.T) {
v7 := New()
defer v7.Destroy()
result, err := v7.Exec("1")
if err != nil {
t.Fatal(err.Error())
}
n, err := v7.ToNumber(result)
if err != nil {
t.Fatal(err.Error())
}
if n != 1 {
t.Fatalf("wrong return number: expected 1, but got %f", n)
}
}
func TestCreateString(t *testing.T) {
v7 := New()
defer v7.Destroy()
str := "abcあいうえお123"
v7Str := v7.CreateString(str)
s, err := v7.ToString(v7Str)
if err != nil {
t.Fatal(err.Error())
}
if s != str {
t.Fatalf("wrong return string: want `%s`, got `%s`", str, s)
}
}
func TestToJSON(t *testing.T) {
v7 := New()
defer v7.Destroy()
str := "abcあいうえお123"
jstr := "\"" + str + "\""
j := v7.ToJSON(v7.CreateString(str), 256)
if j != jstr {
t.Fatalf("error making JSON: want `%s`, got `%s`", jstr, j)
}
}
func TestExecFunction(t *testing.T) {
v7 := New()
defer v7.Destroy()
result, err := v7.Exec("function myfunc(){return 'str'}")
if err != nil {
t.Fatal(err.Error())
}
result, err = v7.Exec("myfunc()")
if err != nil {
t.Fatal(err.Error())
}
s, err := v7.ToString(result)
if err != nil {
t.Fatal(err.Error())
}
if s != "str" {
t.Fatalf("wrong return string: want \"str\", got %s", s)
}
}
func TestApply(t *testing.T) {
v7 := New()
defer v7.Destroy()
str := "abcあいうえお123"
result, err := v7.Exec("function myfunc(x){return x}")
if err != nil {
t.Fatal(err.Error())
}
global := v7.GetGlobalObject()
if v7.IsUndefined(global) {
t.Fatal("could not get global object")
}
ary := v7.CreateArray()
v7.ArrayPush(ary, v7.CreateString(str))
fn := v7.Get(global, "myfunc")
if v7.IsUndefined(global) {
t.Fatal("could not get target function")
}
result, err = v7.Apply(fn, global, ary)
if err != nil {
t.Fatalf("error calling function: %s", err.Error())
}
s, err := v7.ToString(result)
if err != nil {
t.Fatal(err.Error())
}
if s != str {
t.Fatalf("wrong return string: want `%s`, got `%s`", str, s)
}
}
func TestSetGet(t *testing.T) {
v7 := New()
defer v7.Destroy()
global := v7.GetGlobalObject()
if v7.IsUndefined(global) {
t.Fatal("could not get global object")
}
str := "abcあいうえお123"
v7Str := v7.CreateString(str)
err := v7.Set(global, "myproperty", PROPERTY_READ_ONLY, v7Str)
if err != nil {
t.Fatal("could not set property")
}
result := v7.Get(global, "myproperty")
if v7.IsUndefined(global) {
t.Fatal("could not get target property")
}
s, err := v7.ToString(result)
if err != nil {
t.Fatal(err.Error())
}
if s != str {
t.Fatalf("wrong return string: want \"str\", got %s", s)
}
}
func TestSyntaxError(t *testing.T) {
v7 := New()
defer v7.Destroy()
_, err := v7.Exec("var;")
if err != nil {
t.Logf("expected syntax error: %s", err.Error())
} else {
t.Fatal("syntax error did not occur")
}
}
func TestJSError(t *testing.T) {
v7 := New()
defer v7.Destroy()
_, err := v7.Exec("throw new Error('yey')")
if err != nil {
t.Logf("expected syntax error: %s", err.Error())
} else {
t.Fatal("syntax error did not occur")
}
}