summaryrefslogtreecommitdiff
path: root/tests/lang
diff options
context:
space:
mode:
authorLaurenz <laurmaedje@gmail.com>2021-01-22 17:16:42 +0100
committerLaurenz <laurmaedje@gmail.com>2021-01-22 17:16:42 +0100
commitac788f2082711161ec8208eede04d9a2bae02241 (patch)
treeb139e41d327af906163c0b177d402b855c04507e /tests/lang
parent0de4f3ed7bb20a94fd58f93b0793d3b5a8e13972 (diff)
Many more expressions 🥗
Boolean, equality, comparison and assignment expression parsing and evaluation.
Diffstat (limited to 'tests/lang')
-rw-r--r--tests/lang/ref/bracket-call.pngbin22257 -> 22101 bytes
-rw-r--r--tests/lang/ref/if.pngbin4352 -> 3970 bytes
-rw-r--r--tests/lang/ref/let.pngbin3347 -> 3542 bytes
-rw-r--r--tests/lang/typ/bracket-call.typ12
-rw-r--r--tests/lang/typ/expressions.typ124
-rw-r--r--tests/lang/typ/let.typ24
6 files changed, 122 insertions, 38 deletions
diff --git a/tests/lang/ref/bracket-call.png b/tests/lang/ref/bracket-call.png
index 16afb187..e8924484 100644
--- a/tests/lang/ref/bracket-call.png
+++ b/tests/lang/ref/bracket-call.png
Binary files differ
diff --git a/tests/lang/ref/if.png b/tests/lang/ref/if.png
index da99e185..4c390c54 100644
--- a/tests/lang/ref/if.png
+++ b/tests/lang/ref/if.png
Binary files differ
diff --git a/tests/lang/ref/let.png b/tests/lang/ref/let.png
index 4b8a1131..c555a9a0 100644
--- a/tests/lang/ref/let.png
+++ b/tests/lang/ref/let.png
Binary files differ
diff --git a/tests/lang/typ/bracket-call.typ b/tests/lang/typ/bracket-call.typ
index 4b92a4b8..73250e08 100644
--- a/tests/lang/typ/bracket-call.typ
+++ b/tests/lang/typ/bracket-call.typ
@@ -33,14 +33,20 @@
[f|f|f]
// With body.
-[f | box][💕]
+// Error: 1:6-1:7 expected function name, found integer
+[f | 1 | box][💕]
-// Error: 1:2-1:2 expected function name
-[|f true]
+// Error: 2:2-2:2 expected function name
+// Error: 1:3-1:3 expected function name
+[||f true]
// Error: 1:6-1:6 expected function name
[f 1|]
+// Error: 2:2-2:2 expected function name
+// Error: 1:3-1:3 expected function name
+[|][Nope]
+
// Error: 2:5-2:5 expected closing paren
// Error: 1:8-1:9 expected expression, found closing paren
[f (|f )]
diff --git a/tests/lang/typ/expressions.typ b/tests/lang/typ/expressions.typ
index 97aa8d81..49b8910d 100644
--- a/tests/lang/typ/expressions.typ
+++ b/tests/lang/typ/expressions.typ
@@ -3,44 +3,118 @@
#let a = 2
#let b = 4
+// Error: 1:14-1:17 cannot apply '+' to string
+#let error = +""
+
// Paren call.
-[eq f(1), "f(1)"]
-[eq type(1), "integer"]
+[test f(1), "f(1)"]
+[test type(1), "integer"]
// Unary operations.
-[eq +1, 1]
-[eq -1, 1-2]
-[eq --1, 1]
+[test +1, 1]
+[test -1, 1-2]
+[test --1, 1]
-// Binary operations.
-[eq "a" + "b", "ab"]
-[eq 1-4, 3*-1]
-[eq a * b, 8]
-[eq 12pt/.4, 30pt]
+// Math operations.
+[test "a" + "b", "ab"]
+[test 1-4, 3*-1]
+[test a * b, 8]
+[test 12pt/.4, 30pt]
+[test 1e+2-1e-2, 99.99]
// Associativity.
-[eq 1+2+3, 6]
-[eq 1/2*3, 1.5]
+[test 1+2+3, 6]
+[test 1/2*3, 1.5]
// Precedence.
-[eq 1+2*-3, -5]
+[test 1+2*-3, -5]
-// Parentheses.
-[eq (a), 2]
-[eq (2), 2]
-[eq (1+2)*3, 9]
+// Short-circuiting logical operators.
+[test not "a" == "b", true]
+[test not 7 < 4 and 10 == 10, true]
+[test 3 < 2 or 4 < 5, true]
+[test false and false or true, true]
-// Error: 1:3-1:10 cannot add integer and string
-{(1 + "2")}
+// Right-hand side not even evaluated.
+[test false and dont-care, false]
+[test true or dont-care, true]
+
+// Equality and inequality.
+[test "ab" == "a" + "b", true]
+[test [*Hi*] == [*Hi*], true]
+[test "a" != "a", false]
+[test [*] != [_], true]
+[test (1, 2, 3) == (1, 2) + (3,), true]
+[test () == (1,), false]
+[test (a: 1, b: 2) == (b: 2, a: 1), true]
+[test (:) == (a: 1), false]
+[test 1 == "hi", false]
+[test 1 == 1.0, true]
+[test 30% == 30% + 0cm, true]
+[test 1in == 0% + 72pt, true]
+[test 30% == 30% + 1cm, false]
+
+// Comparisons.
+[test 13 * 3 < 14 * 4, true]
+[test 5 < 10, true]
+[test 5 > 5, false]
+[test 5 <= 5, true]
+[test 5 <= 4, false]
+[test 45deg < 1rad, true]
+
+// Assignment.
+#let x = "some"
+#let y = "some"
+[test (x = y = "") == none and x == none and y == "", true]
+
+// Modify-assign operators.
+#let x = 0
+{ x = 10 } [test x, 10]
+{ x -= 5 } [test x, 5]
+{ x += 1 } [test x, 6]
+{ x *= x } [test x, 36]
+{ x /= 2.0 } [test x, 18.0]
+{ x = "some" } [test x, "some"]
+{ x += "thing" } [test x, "something"]
+
+// Error: 1:3-1:4 unknown variable
+{ z = 1 }
+
+// Error: 1:3-1:6 cannot assign to this expression
+{ (x) = "" }
-// Confusion with floating-point literal.
-[eq 1e+2-1e-2, 99.99]
+// Error: 1:3-1:8 cannot assign to this expression
+{ 1 + 2 = 3}
+
+// Error: 1:3-1:6 cannot assign to constant
+{ box = "hi" }
+
+// Works if we define box before (since then it doesn't resolve to the standard
+// library version anymore).
+#let box = ""; { box = "hi" }
+
+// Parentheses.
+[test (a), 2]
+[test (2), 2]
+[test (1+2)*3, 9]
// Error: 1:3-1:3 expected expression
{-}
-// Error: 1:8-1:8 expected expression
-[eq {1+}, 1]
+// Error: 1:10-1:10 expected expression
+[test {1+}, 1]
+
+// Error: 1:10-1:10 expected expression
+[test {2*}, 2]
+
+// Error: 1:7-1:16 cannot apply '-' to boolean
+[test -not true, error]
+
+// Error: 1:2-1:8 cannot apply 'not' to array
+{not ()}
+
+// Error: 1:3-1:10 cannot apply '+' to integer and string
+{(1 + "2")}
-// Error: 1:8-1:8 expected expression
-[eq {2*}, 2]
+// Error: 1:2-1:12 cannot apply '<=' to relative and relative
+{30% <= 40%}
diff --git a/tests/lang/typ/let.typ b/tests/lang/typ/let.typ
index 3f8f5e0f..7e9246bb 100644
--- a/tests/lang/typ/let.typ
+++ b/tests/lang/typ/let.typ
@@ -1,10 +1,10 @@
// Automatically initialized with `none`.
#let x
-[eq x, none]
+[test x, none]
// Initialized with `1`.
#let y = 1
-[eq y, 1]
+[test y, 1]
// Initialize with template, not terminated by semicolon in template.
#let v = [Hello; there]
@@ -15,15 +15,19 @@
2,
3,
)
-[eq x, (1, 2, 3)]
+[test x, (1, 2, 3)]
// Multiple bindings in one line.
-#let x = "a"; #let y = "b"; [eq x + y, "ab"]
+#let x = "a"; #let y = "b"; [test x + y, "ab"]
// Invalid name.
// Error: 1:6-1:7 expected identifier, found integer
#let 1
+// Invalid name.
+// Error: 1:6-1:7 expected identifier, found integer
+#let 1 = 2
+
// Terminated by end of line before binding name.
// Error: 1:5-1:5 expected identifier
#let
@@ -35,24 +39,24 @@ The Fi#let;rst
// Terminated with just a line break.
#let v = "a"
-The Second [eq v, "a"]
+The Second [test v, "a"]
// Terminated with semicolon + line break.
#let v = "a";
-The Third [eq v, "a"]
+The Third [test v, "a"]
// Terminated with just a semicolon.
-The#let v = "a"; Fourth [eq v, "a"]
+The#let v = "a"; Fourth [test v, "a"]
// Terminated by semicolon even though we are in a paren group.
// Error: 2:25-2:25 expected expression
// Error: 1:25-1:25 expected closing paren
-The#let array = (1, 2 + ;Fifth [eq array, (1, 2)]
+The#let array = (1, 2 + ;Fifth [test array, (1, 2)]
// Not terminated.
// Error: 1:16-1:16 expected semicolon or line break
-The#let v = "a"Sixth [eq v, "a"]
+The#let v = "a"Sixth [test v, "a"]
// Not terminated.
// Error: 1:16-1:16 expected semicolon or line break
-The#let v = "a" [eq v, "a"] Seventh
+The#let v = "a" [test v, "a"] Seventh