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
19 var c = Int("bad")
20 echo 'should not get here'
21
22 ## status: 3
23 ## STDOUT:
24 3
25 -35
26 ## END
27
28 #### Float()
29 # TODO: This needs a lot more testing, for precision, etc.
30 var a = Float("1.2")
31 var b = Float("3.4")
32 write $a $b
33 ## STDOUT:
34 1.2
35 3.4
36 ## END
37
38 #### Str()
39 # TODO: more testing
40 var a = Str(5)
41 var b = Str(42)
42 write $a $b
43 ## STDOUT:
44 5
45 42
46 ## END
47
48 #### Dict()
49 # TODO: more testing
50 var a = Dict()
51 write $len(a)
52 ## STDOUT:
53 0
54 ## END
55
56 #### join()
57 var x = %(a b 'c d')
58
59 var y = join(x)
60 argv.py $y
61
62 var z = join(x, ":")
63 argv.py $z
64 ## STDOUT:
65 ['abc d']
66 ['a:b:c d']
67 ## END
68
69 #### abs
70
71 # Also test smooshing
72 write $abs(-5)$abs(-0)$abs(5)
73 write $abs(-5) $abs(-0) $abs(5)
74 ## STDOUT:
75 505
76 5
77 0
78 5
79 ## END
80
81 #### any() and all()
82 var a1 = all( %(yes yes) )
83 var a2 = all( %(yes '') )
84 var a3 = all( %('' '') )
85 # This should be true and false or what?
86 write $a1 $a2 $a3
87 write __
88
89 var x1 = any( %(yes yes) )
90 var x2 = any( %(yes '') )
91 var x3 = any( %('' '') )
92 write $x1 $x2 $x3
93
94 ## STDOUT:
95 true
96 false
97 false
98 __
99 true
100 true
101 false
102 ## END
103
104 #### sum()
105 var start = 42
106
107 write $sum( range(3) )
108 write $sum( range(3), start)
109 write $sum( range(0), start)
110 ## STDOUT:
111 3
112 45
113 42
114 ## END
115
116 #### sorted()
117 var x = sorted(range(3))
118 write @x
119 ## STDOUT:
120 0
121 1
122 2
123 ## END
124
125 #### reversed()
126 var x = reversed(range(3))
127 write @x
128 ## STDOUT:
129 2
130 1
131 0
132 ## END
133
134 #### @split(x) respects IFS
135 setvar IFS = ":"
136 var x = "one:two:three"
137 argv.py @split(x)
138 ## STDOUT:
139 ['one', 'two', 'three']
140 ## END
141
142 #### @maybe(x)
143 setvar empty = ''
144 setvar x = 'X'
145 argv.py a @maybe(empty) @maybe(x) b
146
147 setvar n = null
148 argv.py a @maybe(n) b
149
150 ## STDOUT:
151 ['a', 'X', 'b']
152 ['a', 'b']
153 ## END
154
155 #### maybe() on invalid type is fatal error
156
157 # not allowed
158 setvar marray = %()
159 argv.py a @maybe(marray) b
160 echo done
161 ## status: 1
162 ## STDOUT:
163 ## END
164
165 #### split() on invalid type is fatal error
166 var myarray = %( --all --long )
167 write -- @myarray
168 write -- @split(myarray)
169 ## status: 1
170 ## STDOUT:
171 --all
172 --long
173 ## END
174
175 #### @glob(x)
176
177 # empty glob
178 write -- A @glob('__nope__') B
179 echo ___
180
181 touch -- a.z b.z -.z
182 write -- @glob('?.z')
183 echo ___
184
185 # add it back
186 shopt -s dashglob
187 write -- @glob('?.z')
188
189 ## STDOUT:
190 A
191 B
192 ___
193 a.z
194 b.z
195 ___
196 -.z
197 a.z
198 b.z
199 ## END
200