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

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