1 |
# oil-builtin-funcs.test.sh |
2 |
|
3 |
# TODO: Test that there are exceptions when there are too many args, etc. |
4 |
|
5 |
#### Bool() |
6 |
var a = Bool( @() ) |
7 |
var b = Bool( @(foo) ) |
8 |
write $a $b |
9 |
## STDOUT: |
10 |
False |
11 |
True |
12 |
## END |
13 |
|
14 |
#### Int() |
15 |
var a = Int("3") |
16 |
var b = Int("-35") |
17 |
write $a $b |
18 |
## STDOUT: |
19 |
3 |
20 |
-35 |
21 |
## END |
22 |
|
23 |
#### Float() |
24 |
# TODO: This needs a lot more testing, for precision, etc. |
25 |
var a = Float("1.2") |
26 |
var b = Float("3.4") |
27 |
write $a $b |
28 |
## STDOUT: |
29 |
1.2 |
30 |
3.4 |
31 |
## END |
32 |
|
33 |
#### Str() |
34 |
# TODO: more testing |
35 |
var a = Str(5) |
36 |
var b = Str(42) |
37 |
write $a $b |
38 |
## STDOUT: |
39 |
5 |
40 |
42 |
41 |
## END |
42 |
|
43 |
#### Tuple() |
44 |
# TODO: more testing |
45 |
var a = Tuple() |
46 |
write $a |
47 |
## STDOUT: |
48 |
() |
49 |
## END |
50 |
|
51 |
#### List() |
52 |
# TODO: more testing |
53 |
var a = List(range(3)) |
54 |
write $a |
55 |
## STDOUT: |
56 |
[0, 1, 2] |
57 |
## END |
58 |
|
59 |
#### Dict() |
60 |
# TODO: more testing |
61 |
var a = Dict() |
62 |
#repr a |
63 |
write $len(a) |
64 |
## STDOUT: |
65 |
0 |
66 |
## END |
67 |
|
68 |
#### join() |
69 |
var x = @(a b 'c d') |
70 |
|
71 |
var y = join(x) |
72 |
argv.py $y |
73 |
|
74 |
var z = join(x, ":") |
75 |
argv.py $z |
76 |
## STDOUT: |
77 |
['abc d'] |
78 |
['a:b:c d'] |
79 |
## END |
80 |
|
81 |
#### abs |
82 |
|
83 |
# Also test smooshing |
84 |
write $abs(-5)$abs(-0)$abs(5) |
85 |
write $abs(-5) $abs(-0) $abs(5) |
86 |
## STDOUT: |
87 |
505 |
88 |
5 |
89 |
0 |
90 |
5 |
91 |
## END |
92 |
|
93 |
#### any() and all() |
94 |
var a1 = all( @(yes yes) ) |
95 |
var a2 = all( @(yes '') ) |
96 |
var a3 = all( @('' '') ) |
97 |
# This should be true and false or what? |
98 |
write $a1 $a2 $a3 |
99 |
write __ |
100 |
|
101 |
var x1 = any( @(yes yes) ) |
102 |
var x2 = any( @(yes '') ) |
103 |
var x3 = any( @('' '') ) |
104 |
write $x1 $x2 $x3 |
105 |
|
106 |
## STDOUT: |
107 |
True |
108 |
False |
109 |
False |
110 |
__ |
111 |
True |
112 |
True |
113 |
False |
114 |
## END |
115 |
|
116 |
#### sum() |
117 |
var start = 42 |
118 |
|
119 |
write $sum( range(3) ) |
120 |
write $sum( range(3), start) |
121 |
write $sum( range(0), start) |
122 |
## STDOUT: |
123 |
3 |
124 |
45 |
125 |
42 |
126 |
## END |
127 |
|
128 |
#### sorted() |
129 |
var x = sorted(range(3)) |
130 |
write @x |
131 |
## STDOUT: |
132 |
0 |
133 |
1 |
134 |
2 |
135 |
## END |
136 |
|
137 |
#### reversed() |
138 |
var x = reversed(range(3)) |
139 |
write @x |
140 |
## STDOUT: |
141 |
2 |
142 |
1 |
143 |
0 |
144 |
## END |
145 |
|
146 |
#### enumerate() |
147 |
write $enumerate |
148 |
# TODO: need new for loop syntax |
149 |
for (i, a in enumerate( @(a b c) )) { |
150 |
write $i $a |
151 |
} |
152 |
## STDOUT: |
153 |
0 a |
154 |
1 b |
155 |
2 c |
156 |
## END |
157 |
|
158 |
#### zip() |
159 |
write $zip |
160 |
var a = @(1 2 3) |
161 |
var b = @(a b c) |
162 |
for (item in zip(a, b)) { |
163 |
write $item |
164 |
} |
165 |
## STDOUT: |
166 |
1 a |
167 |
2 b |
168 |
3 c |
169 |
## END |
170 |
|
171 |
#### @split(x) respects IFS |
172 |
setvar IFS = ":" |
173 |
var x = "one:two:three" |
174 |
argv.py @split(x) |
175 |
## STDOUT: |
176 |
['one', 'two', 'three'] |
177 |
## END |
178 |
|
179 |
#### @maybe(x) |
180 |
setvar empty = '' |
181 |
setvar x = 'X' |
182 |
argv.py a @maybe(empty) @maybe(x) b |
183 |
|
184 |
setvar n = null |
185 |
argv.py a @maybe(n) b |
186 |
|
187 |
## STDOUT: |
188 |
['a', 'X', 'b'] |
189 |
['a', 'b'] |
190 |
## END |
191 |
|
192 |
#### @maybe on invalid type is fatal error |
193 |
|
194 |
# not allowed |
195 |
setvar marray = @() |
196 |
argv.py a @maybe(marray) b |
197 |
echo done |
198 |
## status: 1 |
199 |
## STDOUT: |
200 |
## END |
201 |
|
202 |
|
203 |
#### @glob(x) |
204 |
touch -- a.z b.z -.z |
205 |
write -- @glob('?.z') |
206 |
echo ___ |
207 |
|
208 |
# add it back |
209 |
shopt -s dashglob |
210 |
write -- @glob('?.z') |
211 |
|
212 |
## STDOUT: |
213 |
a.z |
214 |
b.z |
215 |
___ |
216 |
-.z |
217 |
a.z |
218 |
b.z |
219 |
## END |