OILS / spec / case_.test.sh View on Github | oilshell.org

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