1
2 ## compare_shells: bash dash mksh zsh
3 ## oils_failures_allowed: 1
4
5 # Note: zsh passes most of these tests too
6
7 #### Case statement
8 case a in
9 a) echo A ;;
10 *) echo star ;;
11 esac
12
13 for x in a b; do
14 case $x in
15 # the pattern is DYNAMIC and evaluated on every iteration
16 $x) echo loop ;;
17 *) echo star ;;
18 esac
19 done
20 ## STDOUT:
21 A
22 loop
23 loop
24 ## END
25
26 #### Case statement with ;;&
27 # ;;& keeps testing conditions
28 # NOTE: ;& and ;;& are bash 4 only, not on Mac
29 case a in
30 a) echo A ;;&
31 *) echo star ;;&
32 *) echo star2 ;;
33 esac
34 ## status: 0
35 ## STDOUT:
36 A
37 star
38 star2
39 ## END
40 ## N-I dash stdout-json: ""
41 ## N-I dash status: 2
42 ## N-I zsh stdout-json: ""
43 ## N-I zsh status: 1
44
45 #### Case statement with ;&
46 # ;& ignores the next condition. Why would that be useful?
47
48 for x in aa bb cc dd zz; do
49 case $x in
50 aa) echo aa ;&
51 bb) echo bb ;&
52 cc) echo cc ;;
53 dd) echo dd ;;
54 esac
55 echo --
56 done
57
58 ## status: 0
59 ## STDOUT:
60 aa
61 bb
62 cc
63 --
64 bb
65 cc
66 --
67 cc
68 --
69 dd
70 --
71 --
72 ## END
73 ## N-I dash stdout-json: ""
74 ## N-I dash status: 2
75
76 #### Case with empty condition
77 case $empty in
78 ''|foo) echo match ;;
79 *) echo no ;;
80 esac
81 ## stdout: match
82
83 #### Match a literal with a glob character
84 x='*.py'
85 case "$x" in
86 '*.py') echo match ;;
87 esac
88 ## stdout: match
89
90 #### Match a literal with a glob character with a dynamic pattern
91 x='b.py'
92 pat='[ab].py'
93 case "$x" in
94 $pat) echo match ;;
95 esac
96 ## stdout: match
97 ## BUG zsh stdout-json: ""
98
99 #### Quoted literal in glob pattern
100 x='[ab].py'
101 pat='[ab].py'
102 case "$x" in
103 "$pat") echo match ;;
104 esac
105 ## stdout: match
106
107 #### Multiple Patterns Match
108 x=foo
109 result='-'
110 case "$x" in
111 f*|*o) result="$result X"
112 esac
113 echo $result
114 ## stdout: - X
115
116 #### Pattern ? matches 1 code point (many bytes), but not multiple code points
117
118 # These two code points form a single character.
119 two_code_points="__$(echo $'\u0061\u0300')__"
120
121 # U+0061 is A, and U+0300 is an accent.
122 #
123 # (Example taken from # https://blog.golang.org/strings)
124 #
125 # However ? in bash/zsh only counts CODE POINTS. They do NOT take into account
126 # this case.
127
128 for s in '__a__' '__μ__' "$two_code_points"; do
129 case $s in
130 __?__)
131 echo yes
132 ;;
133 *)
134 echo no
135 esac
136 done
137 ## STDOUT:
138 yes
139 yes
140 no
141 ## END
142 ## BUG dash/mksh STDOUT:
143 yes
144 no
145 no
146 ## END
147
148 #### case with single byte LC_ALL=C
149
150 LC_ALL=C
151
152 c=$(printf \\377)
153
154 # OSH prints -1 here
155 #echo "${#c}"
156
157 case $c in
158 '') echo a ;;
159 "$c") echo b ;;
160 esac
161
162 ## STDOUT:
163 b
164 ## END
165
166 #### \(\) in pattern (regression)
167 s='foo()'
168
169 case $s in
170 *\(\)) echo 'match'
171 esac
172
173 case $SH in (dash) exit;; esac # not implemented
174
175 shopt -s extglob
176
177 case $s in
178 *(foo|bar)'()') echo 'extglob'
179 esac
180 ## STDOUT:
181 match
182 extglob
183 ## END
184 ## N-I dash STDOUT:
185 match
186 ## END
187
188
189 #### case \n bug regression
190
191 case
192 in esac
193
194 ## STDOUT:
195 ## END
196 ## status: 2
197 ## OK mksh status: 1
198 ## OK zsh status: 127
199