diff options
| author | Laurenz <laurmaedje@gmail.com> | 2021-01-26 21:57:56 +0100 |
|---|---|---|
| committer | Laurenz <laurmaedje@gmail.com> | 2021-01-26 21:57:56 +0100 |
| commit | 010ddc4795123987bdef3b5a60240203e7f11d01 (patch) | |
| tree | fa27e7def6d229ba1347ab2cb7d2d06462f46613 /src/syntax/mod.rs | |
| parent | f006636dd2af065a149e3aa1d67fcbe889c53b87 (diff) | |
More straightforward pretty printing tests 🧹
Diffstat (limited to 'src/syntax/mod.rs')
| -rw-r--r-- | src/syntax/mod.rs | 89 |
1 files changed, 88 insertions, 1 deletions
diff --git a/src/syntax/mod.rs b/src/syntax/mod.rs index 22f51d82..fa1308de 100644 --- a/src/syntax/mod.rs +++ b/src/syntax/mod.rs @@ -31,7 +31,7 @@ mod tests { use crate::pretty::pretty; #[track_caller] - pub fn test_pretty(src: &str, exp: &str) { + fn test(src: &str, exp: &str) { let tree = parse(src).output; let found = pretty(&tree); if exp != found { @@ -41,4 +41,91 @@ mod tests { panic!("test failed"); } } + + #[track_caller] + fn roundtrip(src: &str) { + test(src, src); + } + + #[test] + fn test_pretty_print_node() { + // Basic text and markup. + roundtrip("*"); + roundtrip("_"); + roundtrip(" "); + roundtrip("\\ "); + roundtrip("\n\n"); + roundtrip("hi"); + + // Heading. + roundtrip("# *Ok*"); + + // Raw. + roundtrip("`lang 1`"); + test("`` hi``", "`hi`"); + test("`` ` ``", "```"); + } + + #[test] + fn test_pretty_print_expr() { + // Basic expressions. + roundtrip("{none}"); + roundtrip("{hi}"); + roundtrip("{true}"); + roundtrip("{10}"); + roundtrip("{3.14}"); + roundtrip("{10pt}"); + roundtrip("{14.1deg}"); + roundtrip("{20%}"); + roundtrip("{#abcdef}"); + roundtrip(r#"{"hi"}"#); + test(r#"{"let's go"}"#, r#"{"let\'s go"}"#); + + // Arrays. + roundtrip("{()}"); + roundtrip("{(1)}"); + roundtrip("{(1, 2, 3)}"); + + // Dictionaries. + roundtrip("{(:)}"); + roundtrip("{(key: value)}"); + roundtrip("{(a: 1, b: 2)}"); + + // Templates. + roundtrip("{[]}"); + roundtrip("{[*Ok*]}"); + roundtrip("{[[f]]}"); + + // Groups. + roundtrip("{(1)}"); + + // Blocks. + roundtrip("{}"); + roundtrip("{1}"); + roundtrip("{ #let x = 1; x += 2; x + 1 }"); + + // Operators. + roundtrip("{-x}"); + roundtrip("{not true}"); + roundtrip("{1 + 3}"); + + // Parenthesized calls. + roundtrip("{v()}"); + roundtrip("{v(1)}"); + roundtrip("{v(a: 1, b)}"); + + // Bracket calls. + roundtrip("[v]"); + roundtrip("[v 1]"); + roundtrip("[v 1, 2][*Ok*]"); + roundtrip("[v 1 | f 2]"); + roundtrip("{[[v]]}"); + test("[v 1, [[f 2]]]", "[v 1 | f 2]"); + test("[v 1, 2][[f 3]]", "[v 1, 2 | f 3]"); + + // Keywords. + roundtrip("#let x = 1 + 2"); + roundtrip("#if x [y] #else [z]"); + roundtrip("#for x #in y {z}"); + } } |
