diff options
author | Jo-Philipp Wich <jo@mein.io> | 2022-03-06 23:56:41 +0100 |
---|---|---|
committer | Jo-Philipp Wich <jo@mein.io> | 2022-03-07 23:17:51 +0100 |
commit | efe8a0233bea283765443226340fa732cffc9ca6 (patch) | |
tree | 3532b18c9ada2e4bab1ec7d4e89e960067168c3e /tests | |
parent | 05bd7edd7a101aa09a54371aa34bc22646b75bee (diff) |
syntax: support add new operators
- Support ES2016 exponentiation (**) and exponentiation assignment (**=)
- Support ES2020 nullish coalescing (??) and logical nullish assignment (??=)
- Support ES2021 logical and assignment (&&=) and logical or assignment (||=)
Signed-off-by: Jo-Philipp Wich <jo@mein.io>
Diffstat (limited to 'tests')
-rw-r--r-- | tests/custom/00_syntax/24_null_coalesce | 50 | ||||
-rw-r--r-- | tests/custom/00_syntax/25_and_or_assignment | 55 | ||||
-rw-r--r-- | tests/custom/00_syntax/26_exponentiation | 56 |
3 files changed, 161 insertions, 0 deletions
diff --git a/tests/custom/00_syntax/24_null_coalesce b/tests/custom/00_syntax/24_null_coalesce new file mode 100644 index 0000000..26b89f1 --- /dev/null +++ b/tests/custom/00_syntax/24_null_coalesce @@ -0,0 +1,50 @@ +Null coalescing operators return the right hand side of an expression of +the left hand side is null. + + +1. The `??` operator returns the right hand side of the expression if the +left hand side evaluates to `null`. + +-- Expect stdout -- +is null +false +0 +-- End -- + +-- Testcase -- +{% + x = null; + y = false; + z = 0; + + print(x ?? "is null", "\n"); + print(y ?? "is null", "\n"); + print(z ?? "is null", "\n"); +%} +-- End -- + + +2. The `??=` nullish assignment operator sets the left hand side variable +or value to the right hand side expression if the existing value is null. + +-- Expect stdout -- +is null +false +0 +-- End -- + +-- Testcase -- +{% + x = null; + y = false; + z = 0; + + x ??= "is null"; + y ??= "is null"; + z ??= "is null"; + + print(x, "\n"); + print(y, "\n"); + print(z, "\n"); +%} +-- End -- diff --git a/tests/custom/00_syntax/25_and_or_assignment b/tests/custom/00_syntax/25_and_or_assignment new file mode 100644 index 0000000..4dbc5f3 --- /dev/null +++ b/tests/custom/00_syntax/25_and_or_assignment @@ -0,0 +1,55 @@ +The logical AND and logical OR assignment operators set the left hand side +variable or value to the right hand side expression result depending on +whether the lhs value is truish. + + +1. The `&&=` operator overwrites the lhs variable or field with the rhs +expression result if the lhs is truish. + +-- Expect stdout -- +[ + null, + false, + "is truish" +] +-- End -- + +-- Testcase -- +{% + x = null; + y = false; + z = true; + + x &&= "is truish"; + y &&= "is truish"; + z &&= "is truish"; + + printf("%.J\n", [ x, y, z ]); +%} +-- End -- + + +2. The `||=` operator overwrites the lhs variable or field with the rhs +expression result if the lhs is falsy. + +-- Expect stdout -- +[ + "is falsy", + "is falsy", + true +] +-- End -- + +-- Testcase -- +{% + x = null; + y = false; + z = true; + + x ||= "is falsy"; + y ||= "is falsy"; + z ||= "is falsy"; + + printf("%.J\n", [ x, y, z ]); +%} +-- End -- diff --git a/tests/custom/00_syntax/26_exponentiation b/tests/custom/00_syntax/26_exponentiation new file mode 100644 index 0000000..1aab257 --- /dev/null +++ b/tests/custom/00_syntax/26_exponentiation @@ -0,0 +1,56 @@ +The exponentiation and exponentiation assignment operands allow raising +the base operand value to the given power. + + +1. The `**` operator returns the result of raising the first operand to +the power of the second operand. + +-- Expect stdout -- +[ + 1, + 4, + 9223372036854775808, + -9223372036854775808, + -0.25, + 2.75568 +] +-- End -- + +-- Testcase -- +{% + printf("%.J\n", [ + 2 ** 0, + 2 ** 2, + 2 ** 63, + -2 ** 63, + -2 ** -2, + 1.5 ** 2.5 + ]); +%} +-- End -- + + +2. The `**=` operator raises the lhs variable or field value to the +power value in the rhs expression. + +-- Expect stdout -- +[ + 4, + -0.25, + 2.75568 +] +-- End -- + +-- Testcase -- +{% + x = 2; + y = -2; + z = 1.5; + + x **= 2; + y **= -2; + z **= 2.5; + + printf("%.J\n", [ x, y, z ]); +%} +-- End -- |