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

230 lines, 71 significant
1## compare_shells: bash mksh zsh ash
2
3# dash doesn't have echo -e, $'', etc.
4
5# TODO: fix J8 bug causing failure
6
7# Cross-cutting test of serialization formats. That is, what J8 Notation
8# should fix.
9#
10# TODO: Also see spec/xtrace for another use case.
11
12#### printf %q newline
13case $SH in (ash) return ;; esac # yash and ash don't implement this
14
15newline=$'one\ntwo'
16printf '%q\n' "$newline"
17
18quoted="$(printf '%q\n' "$newline")"
19restored=$(eval "echo $quoted")
20test "$newline" = "$restored" && echo roundtrip-ok
21
22## STDOUT:
23$'one\ntwo'
24roundtrip-ok
25## END
26## OK mksh STDOUT:
27'one'$'\n''two'
28roundtrip-ok
29## END
30## OK zsh STDOUT:
31one$'\n'two
32roundtrip-ok
33## END
34## N-I ash stdout-json: ""
35
36#### printf %q spaces
37case $SH in (ash) return ;; esac # yash and ash don't implement this
38
39# bash does a weird thing and uses \
40
41spaces='one two'
42printf '%q\n' "$spaces"
43
44## STDOUT:
45'one two'
46## END
47## OK bash/zsh STDOUT:
48one\ two
49## END
50## N-I ash stdout-json: ""
51
52#### printf %q quotes
53case $SH in (ash) return ;; esac # yash and ash don't implement %q
54
55quotes=\'\"
56printf '%q\n' "$quotes"
57
58quoted="$(printf '%q\n' "$quotes")"
59restored=$(eval "echo $quoted")
60test "$quotes" = "$restored" && echo roundtrip-ok
61
62## STDOUT:
63\'\"
64roundtrip-ok
65## END
66## OK osh STDOUT:
67$'\'"'
68roundtrip-ok
69## END
70## BUG mksh STDOUT:
71''\''"'
72roundtrip-ok
73## END
74## N-I ash stdout-json: ""
75
76#### printf %q unprintable
77case $SH in (ash) return ;; esac # yash and ash don't implement this
78
79unprintable=$'\xff'
80printf '%q\n' "$unprintable"
81
82# bash and zsh agree
83## STDOUT:
84$'\377'
85## END
86## OK osh STDOUT:
87$'\xff'
88## END
89## BUG mksh STDOUT:
90''$'\377'
91## END
92## N-I ash stdout-json: ""
93
94#### printf %q unicode
95case $SH in (ash) return ;; esac # yash and ash don't implement this
96
97unicode=$'\u03bc'
98unicode=$'\xce\xbc' # does the same thing
99
100printf '%q\n' "$unicode"
101
102# Oil issue: we have quotes. Isn't that OK?
103## STDOUT:
104μ
105## END
106## OK osh STDOUT:
107'μ'
108## END
109## N-I ash stdout-json: ""
110
111#### printf %q invalid unicode
112case $SH in (ash) return ;; esac
113
114# Hm bash/mksh/zsh understand these. They are doing decoding and error
115# recovery! inspecting the bash source seems to confirm this.
116unicode=$'\xce'
117printf '%q\n' "$unicode"
118
119unicode=$'\xce\xce\xbc'
120printf '%q\n' "$unicode"
121
122unicode=$'\xce\xbc\xce'
123printf '%q\n' "$unicode"
124
125case $SH in (mksh) return ;; esac # it prints unprintable chars here!
126
127unicode=$'\xcea'
128printf '%q\n' "$unicode"
129unicode=$'a\xce'
130printf '%q\n' "$unicode"
131## STDOUT:
132$'\xce'
133$'\xceμ'
134$'μ\xce'
135$'\xcea'
136$'a\xce'
137## END
138## OK bash STDOUT:
139$'\316'
140$'\316μ'
141$'μ\316'
142$'\316a'
143$'a\316'
144## END
145## BUG mksh STDOUT:
146''$'\316'
147''$'\316''μ'
148'μ'$'\316'
149## END
150## OK zsh STDOUT:
151$'\316'
152$'\316'μ
153μ$'\316'
154$'\316'a
155a$'\316'
156## END
157## N-I ash stdout-json: ""
158
159#### set
160case $SH in (zsh) return ;; esac # zsh doesn't make much sense
161
162zz=$'one\ntwo'
163
164set | grep zz
165## STDOUT:
166zz=$'one\ntwo'
167## END
168## OK ash stdout-json: "zz='one\n"
169## BUG zsh stdout-json: ""
170
171
172#### declare
173case $SH in (ash|zsh) return ;; esac # zsh doesn't make much sense
174
175zz=$'one\ntwo'
176
177typeset | grep zz
178typeset -p zz
179
180## STDOUT:
181zz=$'one\ntwo'
182declare -- zz=$'one\ntwo'
183## END
184
185## OK mksh STDOUT:
186typeset zz
187typeset zz=$'one\ntwo'
188## BUG zsh stdout-json: ""
189## N-I ash stdout-json: ""
190
191#### ${var@Q}
192case $SH in (zsh|ash) exit ;; esac
193
194zz=$'one\ntwo \u03bc'
195
196# weirdly, quoted and unquoted aren't different
197echo ${zz@Q}
198echo "${zz@Q}"
199## STDOUT:
200$'one\ntwo μ'
201$'one\ntwo μ'
202## END
203## OK mksh STDOUT:
204$'one
205two μ'
206$'one
207two μ'
208## END
209## N-I ash/zsh stdout-json: ""
210
211#### xtrace
212zz=$'one\ntwo'
213set -x
214echo "$zz"
215## STDOUT:
216one
217two
218## END
219## STDERR:
220+ echo $'one\ntwo'
221## END
222## OK bash/ash STDERR:
223+ echo 'one
224two'
225## END
226## OK zsh STDERR:
227+zsh:3> echo 'one
228two'
229## END
230