OILS / spec / ysh-for.test.sh View on Github | oilshell.org

192 lines, 97 significant
1## our_shell: ysh
2## oils_failures_allowed: 0
3
4#### For loop over expression: List
5var mylist = [1, 2, 3]
6for item in (mylist) {
7 echo "item $item"
8}
9
10## STDOUT:
11item 1
12item 2
13item 3
14## END
15
16#### For loop over expression: Dict, not BashAssoc
17
18var mydict = {name: 'bob', age: 40}
19for key in (mydict) {
20 echo "key $key"
21}
22
23declare -A A=([name]=bob)
24for key in (A) {
25 echo "key $key"
26}
27
28## status: 3
29## STDOUT:
30key name
31key age
32## END
33
34
35#### For loop over range
36var myrange = 0 .. 3
37for i in (myrange) {
38 echo "i $i"
39}
40
41## STDOUT:
42i 0
43i 1
44i 2
45## END
46
47#### Shell for loop with index (equivalent of enumerate())
48for i, item in a b c {
49 echo "$i $item"
50}
51## STDOUT:
520 a
531 b
542 c
55## END
56
57#### 3 indices with (mylist) is a runtime error
58for i, item bad in (['foo', 'bar']) {
59 echo "$i $item $bad"
60}
61## status: 2
62
63#### Shell for loop can't have 3 indices
64for i, bad, bad in a b c {
65 echo $i $item
66}
67## status: 2
68
69#### Any for loop can't have 4 indiecs
70for a, b, c, d in (['foo']) {
71 echo $i $item
72}
73## status: 2
74
75#### Expression for loop with index: List
76for i, item in (['spam', 'eggs']) {
77 echo "$i $item"
78}
79## STDOUT:
800 spam
811 eggs
82## END
83
84#### Expression for loop with index: Dict (TODO: define dict iter order)
85for key, value in ({name: 'bob', age: 40}) {
86 echo "pair $key $value"
87}
88## STDOUT:
89pair name bob
90pair age 40
91## END
92
93#### Dict: index key value loop (TODO: define dict iter order)
94for i, key, value in ({name: 'bob', age: 40}) {
95 echo "entry $i $key $value"
96}
97## STDOUT:
98entry 0 name bob
99entry 1 age 40
100## END
101
102
103#### Equivalent of zip()
104
105var array = :| d e f |
106
107for i, item in a b c {
108 echo "$i $item $[array[i]]"
109}
110
111## STDOUT:
1120 a d
1131 b e
1142 c f
115## END
116
117#### parse_bare_word eliminates confusion
118
119shopt --unset parse_bare_word
120
121for x in mylist { # THIS FAILS
122 echo "BAD $x"
123}
124
125## status: 2
126## STDOUT:
127## END
128
129
130#### Object that's not iterable
131
132echo hi
133for x in (42) {
134 echo $x
135}
136
137## status: 3
138## STDOUT:
139hi
140## END
141
142#### YSH for with brace substitution and glob
143
144touch {foo,bar}.py
145for i, file in *.py {README,foo}.md {
146 echo "$i $file"
147}
148## STDOUT:
1490 bar.py
1501 foo.py
1512 README.md
1523 foo.md
153## END
154
155#### for x in <> {
156
157# to avoid stdin conflict
158
159$SH $REPO_ROOT/spec/testdata/ysh-for-stdin.ysh
160
161## STDOUT:
162-1-
163-2-
164-3-
165
1660 1
1671 2
1682 3
169
170empty
171done
172
173empty2
174done2
175
176space
177hi
178## END
179
180#### I/O error in for x in <> {
181
182set +o errexit
183
184# EISDIR - stdin descriptor is dir
185$SH -c 'for x in <> { echo $x }' < /
186if test $? -ne 0; then
187 echo pass
188fi
189
190## STDOUT:
191pass
192## END