summaryrefslogtreecommitdiffhomepage
path: root/tests/custom/00_syntax
diff options
context:
space:
mode:
authorJo-Philipp Wich <jo@mein.io>2022-04-12 23:03:32 +0200
committerJo-Philipp Wich <jo@mein.io>2022-04-13 15:55:22 +0200
commite14b0993b101839d2d40b5c4f184e6b0c2083b65 (patch)
treec23ec0351ac1f08917604bfa824fa83d0ab5668c /tests/custom/00_syntax
parent23ddf91d6380da392c8eea7b7fe12c2cd687b6de (diff)
syntax: implement support for ES6 template literals
Implement support for ECMAScript 6 template literals which allow simple interpolation of variable values into strings without resorting to `sprintf()` or manual string concatenation. Signed-off-by: Jo-Philipp Wich <jo@mein.io>
Diffstat (limited to 'tests/custom/00_syntax')
-rw-r--r--tests/custom/00_syntax/27_template_literals102
1 files changed, 102 insertions, 0 deletions
diff --git a/tests/custom/00_syntax/27_template_literals b/tests/custom/00_syntax/27_template_literals
new file mode 100644
index 0000000..40fa9ce
--- /dev/null
+++ b/tests/custom/00_syntax/27_template_literals
@@ -0,0 +1,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 --