OILS / spec / ysh-string.test.sh View on Github | oilshell.org

429 lines, 162 significant
1## our_shell: ysh
2## oils_failures_allowed: 0
3
4#### single quoted -- implicit and explicit raw
5var x = 'foo bar'
6echo $x
7setvar x = r'foo bar' # Same string
8echo $x
9setvar x = r'\t\n' # This is raw
10echo $x
11## STDOUT:
12foo bar
13foo bar
14\t\n
15## END
16
17#### Implicit raw single quote with backslash is a syntax error
18var x = '\t\n'
19echo $x
20## status: 2
21## stdout-json: ""
22
23#### single quoted C strings: $'foo\n'
24
25# expression mode
26var x = $'foo\nbar'
27echo "$x"
28
29# command mode
30if test "$x" = $'foo\nbar'; then
31 echo equal
32fi
33
34## STDOUT:
35foo
36bar
37equal
38## END
39
40#### raw strings and J8 strings don't work in OSH
41shopt --unset ysh:all
42
43echo r'hello \'
44echo u'mu \u{3bc}'
45echo b'byte \yff'
46
47echo --
48
49echo r'''
50raw multi
51'''
52
53echo u'''
54u multi
55'''
56
57echo b'''
58b multi
59'''
60
61## STDOUT:
62rhello \
63umu \u{3bc}
64bbyte \yff
65--
66r
67raw multi
68
69u
70u multi
71
72b
73b multi
74
75## END
76
77#### J8-style u'' and b'' strings in expression mode
78
79var x = u'\u{3bc}'
80var y = b'\yff'
81
82
83write --end '' -- $x | od -A n -t x1
84write --end '' -- $y | od -A n -t x1
85
86## STDOUT:
87 ce bc
88 ff
89## END
90
91#### J8-style u'' and b'' strings in command mode
92
93write --end '' -- u'\u{3bc}' | od -A n -t x1
94write --end '' -- b'\yff' | od -A n -t x1
95
96# TODO: make this be illegal
97# echo u'hello \u03bc'
98
99## STDOUT:
100 ce bc
101 ff
102## END
103
104#### J8-style multi-line strings u''' b''' in command mode
105
106write --end '' -- u'''
107 --
108 \u{61}
109 --
110 '''
111write --end '' -- b'''
112--
113\y62
114--
115'''
116
117# Should be illegal?
118#echo u'hello \u03bc'
119
120## STDOUT:
121--
122a
123--
124--
125b
126--
127## END
128
129#### Double Quoted
130var name = 'World'
131var g = "Hello $name"
132
133echo "Hello $name"
134echo $g
135## STDOUT:
136Hello World
137Hello World
138## END
139
140#### Multiline strings with '' and ""
141
142var single = '
143 single
144'
145
146var x = 42
147var double = "
148 double $x
149"
150
151echo $single
152echo $double
153
154## STDOUT:
155
156 single
157
158
159 double 42
160
161## END
162
163#### C strings in %() array literals
164shopt -s oil:upgrade
165
166var lines=%($'aa\tbb' $'cc\tdd')
167write @lines
168
169## STDOUT:
170aa bb
171cc dd
172## END
173
174#### shopt parse_ysh_string
175
176# Ignored prefix
177echo r'\'
178
179# space
180write r '' end
181
182# These use shell rules!
183echo ra'\'
184echo raw'\'
185
186echo r"\\"
187
188# Now it's a regular r
189shopt --unset parse_ysh_string
190write unset r'\'
191
192## STDOUT:
193\
194r
195
196end
197ra\
198raw\
199r\
200unset
201r\
202## END
203
204#### Triple Double Quotes, Expression Mode
205
206var line1 = """line1"""
207echo line1=$line1
208var line2 = """
209line2"""
210echo line2=$line2
211
212var two = 2
213var three = 3
214var x = """
215 one "
216 two = $two ""
217 three = $three
218 """
219echo "[$x]"
220
221var x = """
222 good
223 bad
224 """
225echo "[$x]"
226
227## STDOUT:
228line1=line1
229line2=line2
230[one "
231two = 2 ""
232 three = 3
233]
234[good
235 bad
236]
237## END
238
239#### Triple Single Quotes, Expression Mode
240
241var two = 2
242var three = 2
243
244var x = '''
245 two = $two '
246 three = $three ''
247 \u{61}
248 '''
249echo "[$x]"
250
251var x = u'''
252 two = $two '
253 three = $three ''
254 \u{61}
255 '''
256echo "[$x]"
257
258var x = b'''
259 two = $two '
260 three = $three ''
261 \u{61} \y61
262 '''
263echo "[$x]"
264
265## STDOUT:
266[two = $two '
267three = $three ''
268 \u{61}
269]
270[two = $two '
271three = $three ''
272 a
273]
274[two = $two '
275three = $three ''
276 a a
277]
278## END
279
280
281#### Triple Double Quotes, Command Mode
282
283var two=2
284var three=3
285
286echo ""a # test lookahead
287
288echo --
289echo """
290 one "
291 two = $two ""
292 three = $three
293 """
294
295echo --
296tac <<< """
297 one "
298 two = $two ""
299 three = $three
300 """
301
302shopt --unset parse_triple_quote
303
304echo --
305echo """
306 one
307 two = $two
308 three = $three
309 """
310
311
312## STDOUT:
313a
314--
315one "
316two = 2 ""
317three = 3
318
319--
320
321three = 3
322two = 2 ""
323one "
324--
325
326 one
327 two = 2
328 three = 3
329
330## END
331
332#### raw strings and triple quotes
333
334echo r'''a'''
335
336shopt --unset parse_ysh_string
337
338echo r'''a'''
339
340## STDOUT:
341a
342ra
343## END
344
345
346#### Triple Single Quotes, Command Mode
347
348echo ''a # make sure lookahead doesn't mess up
349
350echo --
351echo '''
352 two = $two
353 '
354 '' '
355 \u{61}
356 '''
357## STDOUT:
358a
359--
360two = $two
361'
362'' '
363\u{61}
364
365## END
366
367#### Triple Single Quotes, Here Doc
368
369tac <<< '''
370 two = $two
371 '
372 '' '
373 \u{61}
374 '''
375
376## STDOUT:
377
378\u{61}
379'' '
380'
381two = $two
382## END
383
384#### Triple Single Quotes without parse_triple_quote
385
386shopt --unset parse_triple_quote
387
388echo '''
389 two = $two
390 \u{61}
391 '''
392
393## STDOUT:
394
395 two = $two
396 \u{61}
397
398## END
399
400#### here doc with quotes
401
402# This has 3 right double quotes
403
404cat <<EOF
405"hello"
406""
407"""
408EOF
409
410
411## STDOUT:
412"hello"
413""
414"""
415## END
416
417#### triple quoted and implicit concatenation
418
419# Should we allow this? Or I think it's possible to make it a syntax error
420
421echo '''
422single
423'''zz
424
425echo """
426double
427"""zz
428## status: 2
429## stdout-json: ""