diff options
| author | Laurenz <laurmaedje@gmail.com> | 2021-02-24 21:29:32 +0100 |
|---|---|---|
| committer | Laurenz <laurmaedje@gmail.com> | 2021-02-24 21:29:32 +0100 |
| commit | f084165eabbb8ad1b8e8969078fce89070ab4d96 (patch) | |
| tree | 6003822cc646ecf0ba6a3070c87ab283503a4c3b /tests | |
| parent | dae3dad5407e49715736a2a3d8735e65027e6c11 (diff) | |
While loops 🔁
Diffstat (limited to 'tests')
22 files changed, 260 insertions, 177 deletions
diff --git a/tests/ref/control/for-invalid.png b/tests/ref/control/for-invalid.png Binary files differdeleted file mode 100644 index d758aa95..00000000 --- a/tests/ref/control/for-invalid.png +++ /dev/null diff --git a/tests/ref/control/for-value.png b/tests/ref/control/for-value.png Binary files differdeleted file mode 100644 index fa323edc..00000000 --- a/tests/ref/control/for-value.png +++ /dev/null diff --git a/tests/ref/control/for.png b/tests/ref/control/for.png Binary files differindex 2f13985a..cfbc8d08 100644 --- a/tests/ref/control/for.png +++ b/tests/ref/control/for.png diff --git a/tests/ref/control/if-invalid.png b/tests/ref/control/if-invalid.png Binary files differdeleted file mode 100644 index 319fbdbd..00000000 --- a/tests/ref/control/if-invalid.png +++ /dev/null diff --git a/tests/ref/control/if.png b/tests/ref/control/if.png Binary files differindex 75a20d00..7db3a8ae 100644 --- a/tests/ref/control/if.png +++ b/tests/ref/control/if.png diff --git a/tests/ref/control/invalid.png b/tests/ref/control/invalid.png Binary files differnew file mode 100644 index 00000000..9a119088 --- /dev/null +++ b/tests/ref/control/invalid.png diff --git a/tests/ref/control/let-invalid.png b/tests/ref/control/let-invalid.png Binary files differdeleted file mode 100644 index 19d4d545..00000000 --- a/tests/ref/control/let-invalid.png +++ /dev/null diff --git a/tests/ref/control/let-terminated.png b/tests/ref/control/let.png Binary files differindex 24f20c69..24f20c69 100644 --- a/tests/ref/control/let-terminated.png +++ b/tests/ref/control/let.png diff --git a/tests/ref/control/while.png b/tests/ref/control/while.png Binary files differnew file mode 100644 index 00000000..f0baf0af --- /dev/null +++ b/tests/ref/control/while.png diff --git a/tests/ref/spacing.png b/tests/ref/spacing.png Binary files differindex 5c3acf9b..fb102e66 100644 --- a/tests/ref/spacing.png +++ b/tests/ref/spacing.png diff --git a/tests/typ/control/for-invalid.typ b/tests/typ/control/for-invalid.typ deleted file mode 100644 index c8bdebdd..00000000 --- a/tests/typ/control/for-invalid.typ +++ /dev/null @@ -1,32 +0,0 @@ -// Test invalid for loop syntax. - ---- -// Error: 5-5 expected identifier -#for - -// Error: 7-7 expected keyword `in` -#for v - -// Error: 10-10 expected expression -#for v in - -// Error: 15-15 expected body -#for v in iter - ---- -// Should output `v in iter`. -// Error: 5 expected identifier -#for -v in iter {} - -// Should output `A thing`. -// Error: 7-10 expected identifier, found string -A#for "v" thing. - -// Should output `in iter`. -// Error: 6-9 expected identifier, found string -#for "v" in iter {} - -// Should output `+ b in iter`. -// Error: 7 expected keyword `in` -#for a + b in iter {} diff --git a/tests/typ/control/for-value.typ b/tests/typ/control/for-value.typ deleted file mode 100644 index 3ab80716..00000000 --- a/tests/typ/control/for-value.typ +++ /dev/null @@ -1,20 +0,0 @@ -// Test return value of for loops. - ---- -// Template body yields template. -// Should output `234`. -#for v in (1, 2, 3, 4) [#if v >= 2 [{v}]] - ---- -// Block body yields template. -// Should output `[1st, 2nd, 3rd, 4th, 5th, 6th]`. -{ - "[" + for v in (1, 2, 3, 4, 5, 6) { - (if v > 1 [, ] - + [{v}] - + if v == 1 [st] - + if v == 2 [nd] - + if v == 3 [rd] - + if v >= 4 [th]) - } + "]" -} diff --git a/tests/typ/control/for.typ b/tests/typ/control/for.typ index 294345b5..36bce447 100644 --- a/tests/typ/control/for.typ +++ b/tests/typ/control/for.typ @@ -1,11 +1,10 @@ // Test for loops. -// Ref: false --- -// Array. - -#for x in () {} +// Empty array. +#for x in () [Nope] +// Array. #let sum = 0 #for x in (1, 2, 3, 4, 5) { sum += x @@ -13,14 +12,12 @@ #test(sum, 15) ---- -// Dictionary. -// Ref: true -(\ #for k, v in (name: "Typst", age: 2) [ - #h(0.5cm) {k}: {v}, \ -]) +// Dictionary is not traversed in insertion order. +// Should output `age: 1, name: Typst,`. +#for k, v in (name: "Typst", age: 2) [ + {k}: {v}, \ +] ---- // String. { let out = "" @@ -36,6 +33,33 @@ } --- +// Block body. +// Should output `[1st, 2nd, 3rd, 4th, 5th, 6th]`. +{ + "[" + for v in (1, 2, 3, 4, 5, 6) { + (if v > 1 [, ] + + [{v}] + + if v == 1 [st] + + if v == 2 [nd] + + if v == 3 [rd] + + if v >= 4 [th]) + } + "]" +} + +// Template body. +// Should output `234`. +#for v in (1, 2, 3, 4, 5, 6, 7) [#if v >= 2 and v <= 5 { repr(v) }] + +--- +// Value of for loops. +// Ref: false +#test(type(for v in () {}), "template") +#test(type(for v in () []), "template") + +--- +// Error: 14-19 unknown variable +#let error = error + // Uniterable expression. // Error: 11-15 cannot loop over boolean #for v in true {} @@ -44,9 +68,7 @@ // Error: 11-18 cannot add integer and string #for v in 1 + "2" {} -// Error: 14-17 cannot apply '-' to string -#let error = -"" -#let result = for v in (1, 2, 3) { +// A single error stops iteration. +#test(error, for v in (1, 2, 3) { if v < 2 [Ok] else {error} -} -#test(result, error) +}) diff --git a/tests/typ/control/if-invalid.typ b/tests/typ/control/if-invalid.typ deleted file mode 100644 index 6d2deab1..00000000 --- a/tests/typ/control/if-invalid.typ +++ /dev/null @@ -1,28 +0,0 @@ -// Test invalid if syntax. - ---- -// Error: 4 expected expression -#if - -// Error: 4 expected expression -{if} - -// Error: 6 expected body -#if x - -// Error: 1-6 unexpected keyword `else` -#else {} - ---- -// Should output `x`. -// Error: 4 expected expression -#if -x {} - -// Should output `something`. -// Error: 6 expected body -#if x something - -// Should output `A thing.` -// Error: 20 expected body -A#if false {} #else thing diff --git a/tests/typ/control/if-value.typ b/tests/typ/control/if-value.typ deleted file mode 100644 index d7124255..00000000 --- a/tests/typ/control/if-value.typ +++ /dev/null @@ -1,21 +0,0 @@ -// Test return value of if expressions. -// Ref: false - ---- -{ - let x = 1 - let y = 2 - let z - - // Returns if branch. - z = if x < y { "ok" } - test(z, "ok") - - // Returns else branch. - z = if x > y { "bad" } else { "ok" } - test(z, "ok") - - // Missing else evaluates to none. - z = if x > y { "bad" } - test(z, none) -} diff --git a/tests/typ/control/if.typ b/tests/typ/control/if.typ index 4ed6b649..8d07e9b8 100644 --- a/tests/typ/control/if.typ +++ b/tests/typ/control/if.typ @@ -3,35 +3,61 @@ --- // Test condition evaluation. #if 1 < 2 [ - Ok. + One. ] #if true == false [ - Bad, but we {dont-care}! + {Bad}, but we {dont-care}! ] --- -// Brace in condition. +// Braced condition. #if {true} [ - Ok. + One. +] + +// Template in condition. +#if [] != none [ + Two. ] // Multi-line condition with parens. #if ( 1 + 1 == 1 -) { - nope -} #else { - "Ok." +) [ + Nope. +] #else { + "Three." } // Multiline. #if false [ Bad. ] #else { - let pt = "." - "Ok" + pt + let point = "." + "Four" + point +} + +--- +// Value of if expressions. +// Ref: false +{ + let x = 1 + let y = 2 + let z + + // Returns if branch. + z = if x < y { "ok" } + test(z, "ok") + + // Returns else branch. + z = if x > y { "bad" } else { "ok" } + test(z, "ok") + + // Missing else evaluates to none. + z = if x > y { "bad" } + test(z, none) } --- diff --git a/tests/typ/control/invalid.typ b/tests/typ/control/invalid.typ new file mode 100644 index 00000000..49158a68 --- /dev/null +++ b/tests/typ/control/invalid.typ @@ -0,0 +1,100 @@ +// Test invalid control syntax. + +--- +// Error: 5 expected identifier +#let + +// Error: 5 expected identifier +{let} + +// Error: 6-9 expected identifier, found string +#let "v" + +// Should output `1`. +// Error: 7 expected semicolon or line break +#let v 1 + +// Error: 9 expected expression +#let v = + +// Should output `= 1`. +// Error: 6-9 expected identifier, found string +#let "v" = 1 + +--- +// Error: 4 expected expression +#if + +// Error: 4 expected expression +{if} + +// Error: 6 expected body +#if x + +// Error: 1-6 unexpected keyword `else` +#else {} + +// Should output `x`. +// Error: 4 expected expression +#if +x {} + +// Should output `something`. +// Error: 6 expected body +#if x something + +// Should output `A thing.` +// Error: 20 expected body +A#if false {} #else thing + +--- +// Error: 7 expected expression +#while + +// Error: 7 expected expression +{while} + +// Error: 9 expected body +#while x + +// Should output `x`. +// Error: 7 expected expression +#while +x {} + +// Should output `something`. +// Error: 9 expected body +#while x something + +--- +// Error: 5 expected identifier +#for + +// Error: 5 expected identifier +{for} + +// Error: 7 expected keyword `in` +#for v + +// Error: 10 expected expression +#for v in + +// Error: 15 expected body +#for v in iter + +// Should output `v in iter`. +// Error: 5 expected identifier +#for +v in iter {} + +// Should output `A thing`. +// Error: 7-10 expected identifier, found string +A#for "v" thing + +// Should output `in iter`. +// Error: 6-9 expected identifier, found string +#for "v" in iter {} + +// Should output `+ b in iter`. +// Error: 7 expected keyword `in` +#for a + b in iter {} diff --git a/tests/typ/control/let-invalid.typ b/tests/typ/control/let-invalid.typ deleted file mode 100644 index f29353ed..00000000 --- a/tests/typ/control/let-invalid.typ +++ /dev/null @@ -1,20 +0,0 @@ -// Test invalid let binding syntax. - ---- -// Error: 5 expected identifier -#let - -// Error: 6-9 expected identifier, found string -#let "v" - -// Should output `1`. -// Error: 7 expected semicolon or line break -#let v 1 - -// Error: 9 expected expression -#let v = - ---- -// Should output `= 1`. -// Error: 6-9 expected identifier, found string -#let "v" = 1 diff --git a/tests/typ/control/let-terminated.typ b/tests/typ/control/let-terminated.typ deleted file mode 100644 index 623265e0..00000000 --- a/tests/typ/control/let-terminated.typ +++ /dev/null @@ -1,28 +0,0 @@ -// Test termination of let statements. - ---- -// Terminated by line break. -#let v1 = 1 -One - -// Terminated by semicolon. -#let v2 = 2; Two - -// Terminated by semicolon and line break. -#let v3 = 3; -Three - -// Terminated because expression ends. -// Error: 12 expected semicolon or line break -#let v4 = 4 Four - -// Terminated by semicolon even though we are in a paren group. -// Error: 2:19 expected expression -// Error: 1:19 expected closing paren -#let v5 = (1, 2 + ; Five - -#test(v1, 1) -#test(v2, 2) -#test(v3, 3) -#test(v4, 4) -#test(v5, (1, 2)) diff --git a/tests/typ/control/let.typ b/tests/typ/control/let.typ index e609d3a9..8df29b11 100644 --- a/tests/typ/control/let.typ +++ b/tests/typ/control/let.typ @@ -1,7 +1,8 @@ // Test let bindings. -// Ref: false --- +// Ref: false + // Automatically initialized with none. #let x #test(x, none) @@ -9,3 +10,32 @@ // Manually initialized with one. #let x = 1 #test(x, 1) + +--- +// Termination. + +// Terminated by line break. +#let v1 = 1 +One + +// Terminated by semicolon. +#let v2 = 2; Two + +// Terminated by semicolon and line break. +#let v3 = 3; +Three + +// Terminated because expression ends. +// Error: 12 expected semicolon or line break +#let v4 = 4 Four + +// Terminated by semicolon even though we are in a paren group. +// Error: 2:19 expected expression +// Error: 1:19 expected closing paren +#let v5 = (1, 2 + ; Five + +#test(v1, 1) +#test(v2, 2) +#test(v3, 3) +#test(v4, 4) +#test(v5, (1, 2)) diff --git a/tests/typ/control/while.typ b/tests/typ/control/while.typ new file mode 100644 index 00000000..7ad70372 --- /dev/null +++ b/tests/typ/control/while.typ @@ -0,0 +1,46 @@ +// Test while expressions. + +--- +// Should output `2 4 6 8 10`. +#let i = 0 +#while i < 10 [ + { i += 2 } + #i +] + +// Should output `Hi`. +#let iter = true +#while iter { + iter = false + "Hi." +} + +#while false { + dont-care +} + +--- +// Value of while loops. +// Ref: false +#test(type(while false {}), "template") +#test(type(while false []), "template") + +--- +// Error: 14-19 unknown variable +#let error = error + +// Condition must be boolean. +// Error: 8-14 expected boolean, found template +#while [nope] [nope] + +// Make sure that we don't complain twice. +// Error: 8-15 unknown variable +#while nothing {} + +// A single error stops iteration. +#let i = 0 +#test(error, while i < 10 { + i += 1 + if i < 5 [nope] else { error } +}) +#test(i, 5) diff --git a/tests/typ/spacing.typ b/tests/typ/spacing.typ index d44cd84c..77dac53c 100644 --- a/tests/typ/spacing.typ +++ b/tests/typ/spacing.typ @@ -20,8 +20,16 @@ A#if false [] #else [B]C \ A#if true [B] #else [] C \ --- +// Spacing around while loop. + +#let c = true; A#while c [{c = false}B]C \ +#let c = true; A#while c [{c = false}B] C \ +#let c = true; A #while c { c = false; "B" }C \ +#let c = true; A #while c { c = false; "B" } C \ + +--- // Spacing around for loop. A#for _ in (none,) [B]C \ A#for _ in (none,) [B] C \ -A #for _ in (none,) [B]C \ +A #for _ in (none,) {"B"}C \ |
