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

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