diff options
| author | Laurenz <laurmaedje@gmail.com> | 2021-01-22 17:16:42 +0100 |
|---|---|---|
| committer | Laurenz <laurmaedje@gmail.com> | 2021-01-22 17:16:42 +0100 |
| commit | ac788f2082711161ec8208eede04d9a2bae02241 (patch) | |
| tree | b139e41d327af906163c0b177d402b855c04507e /tests | |
| parent | 0de4f3ed7bb20a94fd58f93b0793d3b5a8e13972 (diff) | |
Many more expressions 🥗
Boolean, equality, comparison and assignment expression parsing and evaluation.
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/lang/ref/bracket-call.png | bin | 22257 -> 22101 bytes | |||
| -rw-r--r-- | tests/lang/ref/if.png | bin | 4352 -> 3970 bytes | |||
| -rw-r--r-- | tests/lang/ref/let.png | bin | 3347 -> 3542 bytes | |||
| -rw-r--r-- | tests/lang/typ/bracket-call.typ | 12 | ||||
| -rw-r--r-- | tests/lang/typ/expressions.typ | 124 | ||||
| -rw-r--r-- | tests/lang/typ/let.typ | 24 | ||||
| -rw-r--r-- | tests/typeset.rs | 24 |
7 files changed, 136 insertions, 48 deletions
diff --git a/tests/lang/ref/bracket-call.png b/tests/lang/ref/bracket-call.png Binary files differindex 16afb187..e8924484 100644 --- a/tests/lang/ref/bracket-call.png +++ b/tests/lang/ref/bracket-call.png diff --git a/tests/lang/ref/if.png b/tests/lang/ref/if.png Binary files differindex da99e185..4c390c54 100644 --- a/tests/lang/ref/if.png +++ b/tests/lang/ref/if.png diff --git a/tests/lang/ref/let.png b/tests/lang/ref/let.png Binary files differindex 4b8a1131..c555a9a0 100644 --- a/tests/lang/ref/let.png +++ b/tests/lang/ref/let.png 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 diff --git a/tests/typeset.rs b/tests/typeset.rs index fd5f4c81..dbcb6517 100644 --- a/tests/typeset.rs +++ b/tests/typeset.rs @@ -142,10 +142,12 @@ fn test( let mut ok = true; let mut frames = vec![]; + let mut lines = 0; for (i, part) in src.split("---").enumerate() { - let (part_ok, part_frames) = test_part(i, part, env); + let (part_ok, part_frames) = test_part(part, i, lines, env); ok &= part_ok; frames.extend(part_frames); + lines += part.lines().count() as u32; } if !frames.is_empty() { @@ -177,7 +179,7 @@ fn test( ok } -fn test_part(i: usize, src: &str, env: &mut Env) -> (bool, Vec<Frame>) { +fn test_part(src: &str, i: usize, lines: u32, env: &mut Env) -> (bool, Vec<Frame>) { let map = LineMap::new(src); let (compare_ref, ref_diags) = parse_metadata(src, &map); @@ -215,14 +217,14 @@ fn test_part(i: usize, src: &str, env: &mut Env) -> (bool, Vec<Frame>) { for diag in &diags { if !ref_diags.contains(diag) { print!(" Not annotated | "); - print_diag(diag, &map); + print_diag(diag, &map, lines); } } for diag in &ref_diags { if !diags.contains(diag) { print!(" Not emitted | "); - print_diag(diag, &map); + print_diag(diag, &map, lines); } } } @@ -288,7 +290,7 @@ fn register_helpers(scope: &mut Scope, panicked: Rc<RefCell<bool>>) { Value::Str(p.finish()) } - let eq = move |ctx: &mut EvalContext, args: &mut Args| -> Value { + let test = move |ctx: &mut EvalContext, args: &mut Args| -> Value { let lhs = args.require::<Value>(ctx, "left-hand side"); let rhs = args.require::<Value>(ctx, "right-hand side"); if lhs != rhs { @@ -302,13 +304,15 @@ fn register_helpers(scope: &mut Scope, panicked: Rc<RefCell<bool>>) { } }; - scope.set("f", ValueFunc::new("f", f)); - scope.set("eq", ValueFunc::new("eq", eq)); + scope.define("f", ValueFunc::new("f", f)); + scope.define("test", ValueFunc::new("test", test)); } -fn print_diag(diag: &Spanned<Diag>, map: &LineMap) { - let start = map.location(diag.span.start).unwrap(); - let end = map.location(diag.span.end).unwrap(); +fn print_diag(diag: &Spanned<Diag>, map: &LineMap, lines: u32) { + let mut start = map.location(diag.span.start).unwrap(); + let mut end = map.location(diag.span.end).unwrap(); + start.line += lines; + end.line += lines; println!("{}: {}-{}: {}", diag.v.level, start, end, diag.v.message); } |
