OILS / spec / ysh-for.test.sh View on Github | oils.pub

262 lines, 140 significant
1## our_shell: ysh
2
3#### For loop over expression: List
4var mylist = [1, 2, 3]
5for item in (mylist) {
6 echo "item $item"
7}
8
9## STDOUT:
10item 1
11item 2
12item 3
13## END
14
15#### For loop over expression: Dict, not BashAssoc
16
17var mydict = {name: 'bob', age: 40}
18for key in (mydict) {
19 echo "key $key"
20}
21
22declare -A A=([name]=bob)
23for key in (A) {
24 echo "key $key"
25}
26
27## status: 3
28## STDOUT:
29key name
30key age
31## END
32
33
34#### For loop over range
35var myrange = 0 ..< 3
36for i in (myrange) {
37 echo "i $i"
38}
39
40## STDOUT:
41i 0
42i 1
43i 2
44## END
45
46#### Shell for loop with index (equivalent of enumerate())
47for i, item in a b c {
48 echo "$i $item"
49}
50## STDOUT:
510 a
521 b
532 c
54## END
55
56#### 3 indices with (mylist) is a runtime error
57for i, item bad in (['foo', 'bar']) {
58 echo "$i $item $bad"
59}
60## status: 2
61
62#### Shell for loop can't have 3 indices
63for i, bad, bad in a b c {
64 echo $i $item
65}
66## status: 2
67
68#### Any for loop can't have 4 indices
69for a, b, c, d in (['foo']) {
70 echo $i $item
71}
72## status: 2
73
74#### Expression for loop with index: List
75for i, item in (['spam', 'eggs']) {
76 echo "$i $item"
77}
78## STDOUT:
790 spam
801 eggs
81## END
82
83#### Expression for loop with index: Dict (TODO: define dict iter order)
84for key, value in ({name: 'bob', age: 40}) {
85 echo "pair $key $value"
86}
87## STDOUT:
88pair name bob
89pair age 40
90## END
91
92#### Dict: index key value loop (TODO: define dict iter order)
93for i, key, value in ({name: 'bob', age: 40}) {
94 echo "entry $i $key $value"
95}
96## STDOUT:
97entry 0 name bob
98entry 1 age 40
99## END
100
101
102#### Equivalent of zip()
103
104var array = :| d e f |
105
106for i, item in a b c {
107 echo "$i $item $[array[i]]"
108}
109
110## STDOUT:
1110 a d
1121 b e
1132 c f
114## END
115
116#### parse_bare_word eliminates confusion
117
118shopt --unset parse_bare_word
119
120for x in mylist { # THIS FAILS
121 echo "BAD $x"
122}
123
124## status: 2
125## STDOUT:
126## END
127
128
129#### Object that's not iterable
130
131echo hi
132for x in (42) {
133 echo $x
134}
135
136## status: 3
137## STDOUT:
138hi
139## END
140
141#### YSH for with brace substitution and glob
142
143touch {foo,bar}.py
144for i, file in *.py {README,foo}.md {
145 echo "$i $file"
146}
147## STDOUT:
1480 bar.py
1491 foo.py
1502 README.md
1513 foo.md
152## END
153
154#### for x in (io.stdin) {
155
156# to avoid stdin conflict
157
158$[ENV.SH] $[ENV.REPO_ROOT]/spec/testdata/ysh-for-stdin.ysh
159
160## STDOUT:
161-1-
162-2-
163-3-
164
1650 1
1661 2
1672 3
168
169empty
170done
171
172empty2
173done2
174
175space
176hi
177## END
178
179#### I/O error in for x in (stdin) {
180
181set +o errexit
182
183# EISDIR - stdin descriptor is dir
184$[ENV.SH] -c 'for x in (io.stdin) { echo $x }' < /
185if test $? -ne 0; then
186 echo pass
187fi
188
189## STDOUT:
190pass
191## END
192
193#### Append to List in loop extends the loop (matches JS)
194
195# see demo/survey-loop
196
197var mylist = [1,2,3]
198for x in (mylist) {
199 if (x === 2) {
200 call mylist->append(99)
201 }
202 echo $x
203}
204## STDOUT:
2051
2062
2073
20899
209## END
210
211#### Remove from List in loop shortens it (matches JS)
212
213# see demo/survey-loop
214
215var mylist = [1,2,3,4]
216for x in (mylist) {
217 if (x === 2) {
218 call mylist->pop()
219 }
220 echo $x
221}
222## STDOUT:
2231
2242
2253
226## END
227
228#### Adding to Dict in loop does NOT extend the loop (matches JS)
229
230# see demo/survey-loop
231
232var mydict = {"1": null, "2": null, "3": null}
233for x in (mydict) {
234 if (x === "2") {
235 setvar mydict["99"] = null
236 }
237 echo $x
238}
239## STDOUT:
2401
2412
2423
243## END
244
245#### Removing from Dict in loop does NOT change the loop (does NOT match JS)
246
247# see demo/survey-loop
248
249var mydict = {"1": null, "2": null, "3": null, "4": null}
250for x in (mydict) {
251 if (x === "2") {
252 call mydict->erase("1")
253 call mydict->erase("3")
254 }
255 echo $x
256}
257## STDOUT:
2581
2592
2603
2614
262## END