summaryrefslogtreecommitdiffhomepage
path: root/tests/custom/00_syntax/18_if_condition
diff options
context:
space:
mode:
authorPetr Štetiar <ynezz@true.cz>2021-03-19 16:54:55 +0100
committerJo-Philipp Wich <jo@mein.io>2021-04-23 00:42:30 +0200
commit2b59097c3f61fa901e91ac4cea48940760439578 (patch)
tree958d739a78f959dfcd55b3d76e6e970ca53fa1c6 /tests/custom/00_syntax/18_if_condition
parent80393611fb6634abcc0da1dee2da7c4418dbde8d (diff)
tests: create custom tests from current tests cases
Signed-off-by: Petr Štetiar <ynezz@true.cz>
Diffstat (limited to 'tests/custom/00_syntax/18_if_condition')
-rw-r--r--tests/custom/00_syntax/18_if_condition121
1 files changed, 121 insertions, 0 deletions
diff --git a/tests/custom/00_syntax/18_if_condition b/tests/custom/00_syntax/18_if_condition
new file mode 100644
index 0000000..9e02767
--- /dev/null
+++ b/tests/custom/00_syntax/18_if_condition
@@ -0,0 +1,121 @@
+Utpl implements C-style if/else conditions and ?: ternary statements.
+
+Like with for- and while-loops, an alternative syntax form suitable
+for template blocks is supported.
+
+
+-- Expect stdout --
+This should print "one":
+one
+
+This should print "two":
+two
+
+Multiple conditions can be used by chaining if/else statements:
+three
+
+If the conditional block consists of only one statement, the curly
+braces may be omitted:
+two
+
+An if-condition using the alternative syntax:
+Variable x has another value.
+
+
+An if-condition using the special "elif" keyword in alternative syntax mode:
+Variable x was set to five.
+
+
+Ternary expressions function similar to if/else statements but
+only allow for a single expression in the true and false branches:
+Variable x is one
+-- End --
+
+-- Testcase --
+This should print "one":
+{%
+ x = 0;
+
+ if (x == 0) {
+ print("one");
+ }
+ else {
+ print("two");
+ }
+%}
+
+
+This should print "two":
+{%
+ x = 1;
+
+ if (x == 0) {
+ print("one");
+ }
+ else {
+ print("two");
+ }
+%}
+
+
+Multiple conditions can be used by chaining if/else statements:
+{%
+ x = 2;
+
+ if (x == 0) {
+ print("one");
+ }
+ else if (x == 1) {
+ print("two");
+ }
+ else if (x == 2) {
+ print("three");
+ }
+ else {
+ print("four");
+ }
+%}
+
+
+If the conditional block consists of only one statement, the curly
+braces may be omitted:
+{%
+ x = 5;
+
+ if (x == 0)
+ print("one");
+ else
+ print("two");
+%}
+
+
+An if-condition using the alternative syntax:
+{% if (x == 1): -%}
+Variable x was set to one.
+{% else -%}
+Variable x has another value.
+{% endif %}
+
+
+An if-condition using the special "elif" keyword in alternative syntax mode:
+{% if (x == 0): -%}
+Variable x was set to zero.
+{% elif (x == 1): -%}
+Variable x was set to one.
+{% elif (x == 5): -%}
+Variable x was set to five.
+{% else -%}
+Variable x has another value.
+{% endif %}
+
+
+Ternary expressions function similar to if/else statements but
+only allow for a single expression in the true and false branches:
+{%
+ x = 1;
+ s = (x == 1) ? "Variable x is one" : "Variable x has another value";
+
+ print(s);
+%}
+
+-- End --