OILS / spec / serialize.test.sh View on Github | oils.pub

232 lines, 72 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:
169zz='one
170## END
171## BUG zsh stdout-json: ""
172
173
174#### declare
175case $SH in (ash|zsh) return ;; esac # zsh doesn't make much sense
176
177zz=$'one\ntwo'
178
179typeset | grep zz
180typeset -p zz
181
182## STDOUT:
183zz=$'one\ntwo'
184declare -- zz=$'one\ntwo'
185## END
186
187## OK mksh STDOUT:
188typeset zz
189typeset zz=$'one\ntwo'
190## BUG zsh stdout-json: ""
191## N-I ash stdout-json: ""
192
193#### ${var@Q}
194case $SH in (zsh|ash) exit ;; esac
195
196zz=$'one\ntwo \u03bc'
197
198# weirdly, quoted and unquoted aren't different
199echo ${zz@Q}
200echo "${zz@Q}"
201## STDOUT:
202$'one\ntwo μ'
203$'one\ntwo μ'
204## END
205## OK mksh STDOUT:
206$'one
207two μ'
208$'one
209two μ'
210## END
211## N-I ash/zsh stdout-json: ""
212
213#### xtrace
214zz=$'one\ntwo'
215set -x
216echo "$zz"
217## STDOUT:
218one
219two
220## END
221## STDERR:
222+ echo $'one\ntwo'
223## END
224## OK bash/ash STDERR:
225+ echo 'one
226two'
227## END
228## OK zsh STDERR:
229+zsh:3> echo 'one
230two'
231## END
232