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

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