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

199 lines, 103 significant
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
8case a in
9 a) echo A ;;
10 *) echo star ;;
11esac
12
13for 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
19done
20## STDOUT:
21A
22loop
23loop
24## END
25
26#### Case statement with ;;&
27# ;;& keeps testing conditions
28# NOTE: ;& and ;;& are bash 4 only, not on Mac
29case a in
30 a) echo A ;;&
31 *) echo star ;;&
32 *) echo star2 ;;
33esac
34## status: 0
35## STDOUT:
36A
37star
38star2
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
48for 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 --
56done
57
58## status: 0
59## STDOUT:
60aa
61bb
62cc
63--
64bb
65cc
66--
67cc
68--
69dd
70--
71--
72## END
73## N-I dash stdout-json: ""
74## N-I dash status: 2
75
76#### Case with empty condition
77case $empty in
78 ''|foo) echo match ;;
79 *) echo no ;;
80esac
81## stdout: match
82
83#### Match a literal with a glob character
84x='*.py'
85case "$x" in
86 '*.py') echo match ;;
87esac
88## stdout: match
89
90#### Match a literal with a glob character with a dynamic pattern
91x='b.py'
92pat='[ab].py'
93case "$x" in
94 $pat) echo match ;;
95esac
96## stdout: match
97## BUG zsh stdout-json: ""
98
99#### Quoted literal in glob pattern
100x='[ab].py'
101pat='[ab].py'
102case "$x" in
103 "$pat") echo match ;;
104esac
105## stdout: match
106
107#### Multiple Patterns Match
108x=foo
109result='-'
110case "$x" in
111 f*|*o) result="$result X"
112esac
113echo $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.
119two_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
128for s in '__a__' '__μ__' "$two_code_points"; do
129 case $s in
130 __?__)
131 echo yes
132 ;;
133 *)
134 echo no
135 esac
136done
137## STDOUT:
138yes
139yes
140no
141## END
142## BUG dash/mksh STDOUT:
143yes
144no
145no
146## END
147
148#### case with single byte LC_ALL=C
149
150LC_ALL=C
151
152c=$(printf \\377)
153
154# OSH prints -1 here
155#echo "${#c}"
156
157case $c in
158 '') echo a ;;
159 "$c") echo b ;;
160esac
161
162## STDOUT:
163b
164## END
165
166#### \(\) in pattern (regression)
167s='foo()'
168
169case $s in
170 *\(\)) echo 'match'
171esac
172
173case $SH in (dash) exit;; esac # not implemented
174
175shopt -s extglob
176
177case $s in
178 *(foo|bar)'()') echo 'extglob'
179esac
180## STDOUT:
181match
182extglob
183## END
184## N-I dash STDOUT:
185match
186## END
187
188
189#### case \n bug regression
190
191case
192in esac
193
194## STDOUT:
195## END
196## status: 2
197## OK mksh status: 1
198## OK zsh status: 127
199