-
Notifications
You must be signed in to change notification settings - Fork 1
/
oc_test.tcl
executable file
·308 lines (236 loc) · 8.76 KB
/
oc_test.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
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
#!/usr/bin/tclsh8.6
package require oclib::oclib
set v "hello"
✅ equal [length $v] 5
🚫 empty $v
⊢ [: 7 is integer]
✓ [: 7 is integer]
❌ [: 7 is space]
test equal 5 [: "hello" | bytelength]
test equal 5 [. "hello" bytelength]
test equal 5 [bytelength "hello"]
test equal 5 [string bytelength "hello"]
test equal 0 [: "hello" compare "hello"]
test equal 0 [. "hello" compare "hello"]
test equal 0 [compare "hello" "hello"]
test equal 0 [string compare "hello" "hello"]
test equal 1 [: 2 compare 1]
test equal -1 [: 1 compare 2]
test equal 0 [: "yes" equal "no"]
test [: "same" equal "same"]
test equal 5 [: "hello" length]
test equal 0 [: "hello" is integer]
test [. 7 is integer]
test [is integer 7]
test [string is integer 7]
test [: 7 is integer]
test equal 2 [: "hello" first l]
test equal 2 [. "hello" first l]
test equal 2 [first l "hello"]
test equal 2 [string first l "hello"]
test equal 3 [: "hello" last l]
test equal "l" [: "hello" index 3]
test equal "Hello" [: "hello" map {h H}]
test equal "Hello" [: "hello" map {h H}]
test [: "hello" match h*]
test equal "el" [: "hello" range 1 2]
test equal "hehehe" [: he repeat 3]
test equal "hehehe" [repeat he 3]
test equal "hejjo" [: "hello" replace 2 3 jj]
test equal "hejjo" [: "hello" | replace 2 3 jj]
test equal "olleh" [: "hello" reverse]
test equal "olleh" [. "hello" reverse]
test equal "hello" [: "HELLO" tolower]
test equal "HELLO" [: "hello" toupper]
test equal "Hello" [: "hello" totitle]
test equal "hello" [: " hello " trim]
test equal "hello" [. " hello " trim]
test equal "hello" [trim " hello "]
test equal "hello" [string trim " hello "]
test equal " hello" [: " hello " trimright]
test equal "hello " [: " hello " trimleft]
test equal [get "Hello World!"] [dict get "Hello World!"]
test equal [get {1 a 2 b 3 c}] [dict get {1 a 2 b 3 c}]
test equal [get {1 a 2 b 3 c} 2] [dict get {1 a 2 b 3 c} 2]
test equal [dget {1 a 2 b 3 c} 2] [dict get {1 a 2 b 3 c} 2]
test equal [: {1 a 2 b 3 c} get 2] [dict get {1 a 2 b 3 c} 2]
test equal [: {1 a 2 b 3 c} | get 2] [dict get {1 a 2 b 3 c} 2]
set ::env(FOO) BAR ; test equal [exec bash -c "echo \$FOO"] "BAR"
set a foo ; test equal $a "foo"
dset b Foo bar ; test equal $b {Foo bar}
dset b x y z ; test equal $b {Foo bar x {y z}}
: b dset k v ; test equal $b {Foo bar x {y z} k v}
: b dunset k ; test equal $b {Foo bar x {y z}}
. b dset k v ; test equal $b {Foo bar x {y z} k v}
. b dunset k ; test equal $b {Foo bar x {y z}}
test equal [get foo] "foo"
test equal [get {foo bar}] {foo bar}
test equal [get $b] [dict get $b]
test equal [get $b Foo] [dict get $b Foo]
test equal [get $b x y] [dict get $b x y]
test equal [get $b] [: $b get]
test equal [get $b Foo] [: $b get Foo]
test equal [get $b x y] [: $b get x y]
test equal [. $b get] [: $b get]
test equal [. $b get Foo] [: $b get Foo]
test equal [. $b get x y] [: $b get x y]
test equal [keys $b] {Foo x}
test equal [values $b] {bar {y z}}
test equal [exists ?] [info exists ?]
test equal [exists b] [info exists b]
test equal [exists $b Foo] [dict exists $b Foo]
test equal [exists $b bar] [dict exists $b bar]
test equal [∃ ?] [info exists ?]
test equal [∃ b] [info exists b]
test equal [∃ $b Foo] [dict exists $b Foo]
test equal [∃ $b bar] [dict exists $b bar]
test equal [exists $b Foo] [: $b exists Foo]
test equal [exists $b bar] [: $b exists bar]
test equal [rfc_2822 $b] "Foo: bar\r\nx: y z\r\n\r\n"
dset b body body
test equal [rfc_2822 $b] "Foo: bar\r\nx: y z\r\n\r\nbody"
test equal [parse rfc_2822 [rfc_2822 $b]] $b
test equal [json $b] \
{{
"Foo": "bar",
"x": [
"y",
"z"
],
"body": "body"
}
}
test equal [parse json [json $b]] $b
set c {1 a 2 b 3 c}
test equal [csv $c] "1,a\r\n2,b\r\n3,c\r\n"
test equal [parse csv_as_dict [csv $c]] $c
test equal [lines "a\nb"] {a b}
test equal [lines "a\nb\n"] {a b}
test equal [lines "a\nb\n"] [lines "a\r\nb\r\n"]
test equal [lines "a\n\nb\n"] {a {} b}
test equal [lines "a\nb\n\n"] {a b}
test equal [: "a\nb\n\n" lines] {a b}
test equal [: "a\nb\n\n" | lines] {a b}
close [file tempfile f]
file set $f "Hello!\n"
test equal [file get $f] "Hello!\n"
test equal [💾 get $f] "Hello!\n"
file set $f "a\nb\nc\n"
test equal [file lines $f] {a b c}
file set $f "1,a\r\n2,b\r\n3,c\r\n"
test equal [file csv $f] {{1 a} {2 b} {3 c}}
file delete $f
set s World!
test equal [prepend s "Hello "] "Hello World!"
set l {}
for x in {1 2 3 4 5} {lappend l $x}
test equal $l {1 2 3 4 5}
set l {}
for {x y} in {1 2 3 4 5} {lappend l [list $x $y]}
test equal $l {{1 2} {3 4} {5 {}}}
set l {}
for {x y} in {a 1 a 1 a 1} {lappend l [list $x $y]}
test equal $l {{a 1} {a 1} {a 1}}
set l {}
dict for {x y} {a 1 a 1 a 1} {lappend l [list $x $y]}
test equal $l {{a 1}}
#FIXME assign
proc foo {a b} {
test only
} require {
is integer $a
is integer $b
{$a > $b}
} do {
return [expr {$a + $b}]
}
test equal [foo 10 5] 15
test equal [errorcode {foo 5 10}] {assert 5 > 10}
test equal [errorcode {foo a b}] {assert is integer a}
set d {a 1 b 2}
test equal [errorcode {dict get $d c}] {TCL LOOKUP DICT c}
test equal [errorcode {dict get $novar c}] {TCL LOOKUP VARNAME novar}
test equal [errorcode {dict get d c}] {TCL VALUE DICTIONARY}
set l {0 1 2 3 4 5}
test equal 3 [. $l lindex 3]
test equal {3 4} [. $l lrange 3 4]
. l lappend 6 7
test equal 7 [. $l lindex 7]
test equal 8 [. $l llength]
. $l lassign a b c
test equal [list $a $b $c] {0 1 2}
set a xxx
append a yyy ; test equal $a xxxyyy
prepend a yyy ; test equal $a yyyxxxyyy
set a xxx
string append a yyy ; test equal $a xxxyyy
string prepend a yyy ; test equal $a yyyxxxyyy
set a xxx
: a append yyy ; test equal $a xxxyyy
: a prepend yyy ; test equal $a yyyxxxyyy
set a xxx
. a append yyy ; test equal $a xxxyyy
. a prepend yyy ; test equal $a yyyxxxyyy
set a xxx
test equal [binary encode base64 $a] [string base64 $a]
test equal [binary encode base64 $a] [base64 $a]
test equal [binary encode base64 $a] [. $a base64]
test equal [binary encode base64 $a] [: $a | base64]
test equal [base64 {}] {}
test equal [parse base64 {}] {}
test equal 0A [format "%02X" 10]
test equal [string format "%02X" 10] [format "%02X" 10]
test equal [. "%02X" format 10] [format "%02X" 10]
scan "Foo Bar" "%s %s" a b ; test equal $a Foo ; test equal $b Bar
string scan "Foo Bar" "%s %s" a b ; test equal $a Foo ; test equal $b Bar
: "Foo Bar" scan "%s %s" a b ; test equal $a Foo ; test equal $b Bar
. "Foo Bar" scan "%s %s" a b ; test equal $a Foo ; test equal $b Bar
test equal [. {a b c} join -] a-b-c
set l {} ; . l lappend a b c ; test equal $l {a b c}
set a {}
set b {}
. {Foo Bar} lassign a b ; test equal $a Foo ; test equal $b Bar
test equal [lindex {0 1 2 3} 1] 1
test equal [. {0 1 2 3} lindex 1] 1
test equal [. {0 1 2 3} llength] 4
test equal [. {0 1 2 3} lrange 1 2] {1 2}
test equal [. {0 1 2 3} lreplace 1 2 a b] {0 a b 3}
test equal [: {0 1 2 3} | lreplace 1 2 a b] {0 a b 3}
test equal [: {0 1 2 3} | lreplace 1 2 a b | lreverse] {3 b a 0}
test equal [with {0 1 2 3} {lreplace 1 2 a b | lreverse}] {3 b a 0}
test equal [. {0 1 2 3} lreverse] {3 2 1 0}
set a {0 1 2 3 4} ; . a lset 3 x ; test equal $a {0 1 2 x 4}
test equal [string split "1,2,3" ,] [. "1,2,3" split ,]
set x 7
test equal [subst {$x}] 7
test equal [subst {$x}] [string subst {$x}]
test equal [subst -nocommands {$x}] [string subst -nocommands {$x}]
test equal [subst {$x}] [. {$x} subst]
test equal [subst -nocommands {$x}] [. {$x} subst -nocommands]
test equal [zlib gzip Hello] [. Hello gzip]
test equal [parse gzip [. Hello gzip]] Hello
test equal [. [bexec md5 [file get oc_list-1.0.tm]] trim] \
[string md5 [file get oc_list-1.0.tm]]
test equal [. [bexec md5 [file get oc_list-1.0.tm]] trim] \
[. [file get oc_list-1.0.tm] md5]
test equal [errorcode {exec bash -c "exit [get $::oc::ex_codes EX_DATAERR]"}] \
EX_DATAERR
set id [dict create Name "Sam O'Connor" DOB "22 Jan 1977" Tick ✓]
dset id Box "\[-\]"
set qs "Name=Sam%20O%27Connor&DOB=22%20Jan%201977&Tick=%E2%9C%93&Box=%5B-%5D"
test equal [. $id qstring] $qs
test equal [qstring {*}$id] $qs
test equal [qstring $id] $qs
test equal [parse qstring $qs] $id
test equal [: $qs parse qstring] $id
✅ {[chars "Hello!"] eq {H e l l o !}}
✅ {[cat {*}[chars "Hello!"]] eq "Hello!"}
source oc_ostring.tcl
namespace import ::oc::ostring::*
✅ {[ocompare "abc" to "abC" -case no -length 3] == 0}
✅ [oequal "abc" to "abC" -case no -length 3]
🚫 [oequal "abc" to "abC"]
✅ {[ofirst "b" in "abC"] == 1}
✅ {[ofirst "b" in "abCb" -after 2] == 3}
✅ {[ochar_at 3 in "012345"] == 3}
puts pass