-
Notifications
You must be signed in to change notification settings - Fork 5
/
tests.tcl
executable file
·279 lines (253 loc) · 8.32 KB
/
tests.tcl
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
#! /usr/bin/env tclsh
# Tcl bindings for Duktape.
# Copyright (c) 2015, 2016, 2017, 2019, 2020
# D. Bohdan and contributors listed in AUTHORS
# This code is released under the terms of the MIT license. See the file
# LICENSE for details.
package require tcltest
namespace eval ::duktape::tests {
variable path [pwd]
variable setup [list apply {{path} {
lappend ::auto_path $path
package require duktape
cd $path
}} $path]
tcltest::testConstraint tcloo [expr {
![catch { package require TclOO }]
}]
tcltest::test test1 {init, eval and close} \
-setup $setup \
-body {
set id [::duktape::init]
set value [::duktape::eval $id {1 + 2 * 3}]
::duktape::close $id
return $value
} -result 7
tcltest::test test2 {call, types} \
-setup $setup \
-body {
set result {}
set id [::duktape::init]
lappend result [::duktape::call $id Math.abs -5]
lappend result [::duktape::call $id Math.abs {-5 boolean}]
lappend result [::duktape::call $id Math.abs {-5 nan}]
lappend result [::duktape::call $id Math.abs {-5 null}]
lappend result [::duktape::call $id Math.abs {-5 number}]
lappend result [::duktape::call $id Math.abs {-5 undefined}]
lappend result [::duktape::call $id Math.abs {-5 string}]
catch {
lappend result [::duktape::call $id Math.abs {-5 foo}]
}
catch {
lappend result [::duktape::call $id Math.abs {-5 hello world}]
}
::duktape::close $id
return $result
} -result [list \
5 1 NaN 0 5 NaN 5]
# (no type) boolean nan null number undefined string
tcltest::test test3 {js-proc} \
-setup $setup \
-body {
set result {}
set id [::duktape::init]
::duktape::js-proc $id foo {{a 1 num} {b 2 num}} {
return Math.sin(a) + b;
}
catch {
::duktape::js-proc $id foo {} {
return -1;
}
}
lappend result [foo 0 0]
lappend result [foo 1 2]
rename foo {}
::duktape::close $id
return $result
} -result {0 2.8414709848078967}
tcltest::test test4.1 {oo} \
-setup $setup \
-constraints tcloo \
-body {
package require duktape::oo
set result {}
set duktapeInterp [::duktape::oo::Duktape new]
$duktapeInterp js-proc foo {{a 1 num} {b 2 num}} {
return Math.sin(a) + b;
}
lappend result catch:[catch {
$duktapeInterp js-proc foo {} {
return -1;
}
}]
lappend result [foo 0 0]
lappend result [foo 1 2]
rename foo {}
$duktapeInterp js-method sin {{deg 0 number}} {
return Math.sin(deg * Math.PI / 180);
}
lappend result [$duktapeInterp sin 90]
$duktapeInterp destroy
return $result
} -result {catch:1 0 2.8414709848078967 1}
tcltest::test test4.2 {oo token} \
-setup $setup \
-constraints tcloo \
-body {
package require duktape::oo
set result {}
set duktapeInterp [::duktape::oo::Duktape new]
$duktapeInterp eval {
var n = 42 + 108;
}
set result [::duktape::eval [$duktapeInterp token] n]
$duktapeInterp destroy
return $result
} -result 150
tcltest::test test5 {JSON object} \
-setup $setup \
-constraints tcloo \
-body {
package require duktape::oo
set result {}
set duktapeInterp [::duktape::oo::Duktape new]
set json1 [::duktape::oo::JSON new $duktapeInterp {{"a":[1,2,3]}}]
set json2 [::duktape::oo::JSON new $duktapeInterp {{}}]
lappend result [$json1 get a 2]
$json1 set b "Hello, world!\"'"
lappend result [$json1 get b]
$json1 set-json c {["foo", {"bar": "baz"}]}
lappend result [$json1 get-json c]
$json1 set-json {"test1"}
lappend result [$json1 get-json]
$json1 parse {"test2"}
lappend result [$json1 stringify]
$json1 destroy
$json2 destroy
$duktapeInterp destroy
return $result
} -result [list \
3 \
"Hello, world!\"'" \
{["foo",{"bar":"baz"}]} \
{"test1"} \
{"test2"} \
]
tcltest::test test6 {Cleanup} \
-setup $setup \
-body {
set interp [interp create]
$interp eval $setup
set result [$interp eval {
set dt [::duktape::init]
set dt [::duktape::init]
::duktape::close $dt
set dt [::duktape::init]
::duktape::eval $dt {1 + 2 + 3 + 4 + 5}
}]
interp delete $interp
return $result
} -result 15
tcltest::test test7 {Tcl Functions} -setup $setup -body {
set dt [::duktape::init -safe true]
::duktape::tcl-function $dt test {args} {
return [join $args {}]
}
set result [::duktape::eval $dt {
test('P', 'A', 'S', 'S');
}]
::duktape::close $dt
return $result
} -result PASS
tcltest::test test8 {Tcl Eval} -setup $setup -body {
set dt [::duktape::init]
::duktape::make-unsafe $dt
set result [::duktape::eval $dt {
Duktape.tcl.eval('join', 'P A S S', '');
}]
::duktape::close $dt
return $result
} -result PASS
tcltest::test test9 {Tcl Safe} -setup $setup -body {
set dt [::duktape::init -safe true]
catch {
set result [::duktape::eval $dt {
Duktape.tcl.eval('expr', '1+1');
}]
} result
::duktape::close $dt
return $result
# XXX:TODO: More stable error ?
} -result {TypeError: cannot read property 'eval' of undefined}
tcltest::test test10 {To JSON} -setup $setup -body {
set dt [::duktape::init]
::duktape::tcl-function $dt test json {result} {
return "\{\"test\":\"$result\"\}"
}
set result [::duktape::eval $dt {
check = test("verified");
check.test
}]
::duktape::close $dt
return $result
} -result verified
tcltest::test test10 {To array} -setup $setup -body {
set dt [::duktape::init]
::duktape::tcl-function $dt test {array json} {result} {
set retval [list]
lappend retval "\{\"test\":\"failed\"\}"
lappend retval "\{\"test\":\"$result\"\}"
return $retval
}
set result [::duktape::eval $dt {
check = test("verified");
check[1].test
}]
::duktape::close $dt
return $result
} -result verified
tcltest::test test11 {From array} -setup $setup -body {
set dt [::duktape::init]
::duktape::tcl-function $dt test {input} {
return [lindex $input 1 1]
}
set result [::duktape::eval $dt {
test([["1a","1b","1c"],["2a","PASS","2c"]])
}]
::duktape::close $dt
return $result
} -result PASS
tcltest::test test12 {JavaScript Closure} -setup $setup -body {
set dt [::duktape::init]
::duktape::tcl-function $dt tcleval {args} {
return [::eval {*}$args]
}
set result [::duktape::eval $dt {
function generator() {
var count = 0;
return(function() {
count++;
return(count);
});
}
counter1 = generator();
counter2 = generator();
counter1(); /* 1 */
counter2(); /* 1 */
counter1(); /* 2 */
tcleval(counter2); /* 2 */
tcleval(counter1); /* 3 */
tcleval(counter1); /* 4 */
tcleval(counter2); /* 3 */
counter1(); /* 5 */
counter1() + counter2(); /* 6 + 4 = 10 */
}]
::duktape::close $dt
return $result
} -result 10
tcltest::cleanupTests
# Exit with nonzero status if there are failed tests.
if {$::tcltest::numTests(Failed) > 0} {
exit 1
}
}