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

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