summaryrefslogtreecommitdiffhomepage
path: root/tests/custom/00_syntax/27_template_literals
blob: 40fa9ce0a63af609debc09b0a31103d723015864 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
The ucode language supports ES6 template literals for easy interpolation
of expression results into strings.


1. Simple template literals are equivalent to strings.

-- Testcase --
{{ `foo` === 'foo' }}
-- End --

-- Expect stdout --
true
-- End --


2. Template literals may embed expressions using `${...}` placeholder notation.

-- Testcase --
{%
	let x = 2;
	let y = 4;

	print(`The result of ${x} * ${y} is ${x * y}\n`);
%}
-- End --

-- Expect stdout --
The result of 2 * 4 is 8
-- End --


3. Template literals may be nested.

-- Testcase --
{%
	let isFoo = false;
	let isBar = true;

	print(`Foo is ${isFoo} and ${isBar ? `bar is ${isBar}` : `nothing else`}!\n`);
%}
-- End --

-- Expect stdout --
Foo is false and bar is true!
-- End --


4. Placeholder expression results are implicitly stringified.

-- Testcase --
{%
	let o1 = { foo: true };
	let o2 = proto({ color: "red" }, { tostring: function() { return `I am a ${this.color} object` } });

	print(`The first object is ${o1} and the second says "${o2}".\n`);
%}
-- End --

-- Expect stdout --
The first object is { "foo": true } and the second says "I am a red object".
-- End --


5. Escaping either `$` or `{` prevents interpolation as placeholder, sole `$`
   characters bear no special meaning.

-- Testcase --
{%
	printf("%.J\n", [
		`foo \${bar} baz`,
		`foo $\{bar} baz`,
		`foo $bar baz`
	]);
%}
-- End --

-- Expect stdout --
[
	"foo ${bar} baz",
	"foo ${bar} baz",
	"foo $bar baz"
]
-- End --


6. Unterminated placeholder expressions are a synatax error.

-- Testcase --
{{
	`foo ${ bar`
}}
-- End --

-- Expect stderr --
Syntax error: Unterminated string
In line 2, byte 13:

 `    `foo ${ bar``
  Near here -----^


-- End --