diff options
Diffstat (limited to 'tests/control_flow.nom')
| -rw-r--r-- | tests/control_flow.nom | 255 |
1 files changed, 112 insertions, 143 deletions
diff --git a/tests/control_flow.nom b/tests/control_flow.nom index 62b59fa..1d223a4 100644 --- a/tests/control_flow.nom +++ b/tests/control_flow.nom @@ -1,192 +1,161 @@ -#!/usr/bin/env nomsu -V1 +#!/usr/bin/env nomsu -V2.3.4.3 # Tests for the stuff defined in core/control_flow.nom use "core" - do nothing - -action [test conditionals] - if: yes - %loc1 <- (yes) - if: no - barf "entered if 'no' conditional" - - unless: yes - barf "entered unless 'yes' conditional" - - if: yes - %loc2 <- (yes) - ..else - barf "entered if 'yes' else conditional" - - unless: no - %loc3 <- (yes) - ..else - barf "entered unless 'no' else conditional" - -assume (all of [%loc1 = (nil), %loc2 = (nil), %loc3 = (nil)]) or barf "conditionals leaking locals" - -assume ((5 if (yes) else 1) = 5) -assume ((5 if (no) else 1) = 1) -action [return nil]: return (nil) -assume (((return nil) if (yes) else 99) = (nil)) - +action [test conditionals]: + if (yes): %loc1 = (yes) + if (no): barf "entered if 'no' conditional" + unless (yes): barf "entered unless 'yes' conditional" + if (yes): %loc2 = (yes) + ..else: barf "entered if 'yes' else conditional" + + unless (no) (%loc3 = (yes)) else (barf "entered unless 'no' else conditional") + +assume (all of [%loc1 == (nil), %loc2 == (nil), %loc3 == (nil)]) or barf "conditionals leaking locals" +assume ((5 if (yes) else 1) == 5) +assume ((5 if (no) else 1) == 1) +action [return nil] (return (nil)) +assume (((return nil) if (yes) else 99) == (nil)) go to %skip barf "go-to failed." --- %skip --- - -%tot <- 0 -for %x in [1,2,3] - %tot +<- %x -assume (%tot = 6) or barf "for-loop failed" - -%x <- 0 -repeat - %x +<- 1 - if (%x = 3): stop repeating +%tot = 0 +for %x in [1, 2, 3]: %tot += %x +assume (%tot == 6) or barf "for-loop failed" +%x = 0 +repeat: + %x += 1 + if (%x == 3): stop repeating if (%x > 3): barf "Failed to stop repeat loop" -assume (%x = 3) or barf "Failed to repeat" - -%x <- 0 -repeat 5 times - %x +<- 1 -assume (%x = 5) or barf "Failed to repeat 5 times" -<- {%x:0,%y:0} -for % in [1,2,3] - repeat 5 times +assume (%x == 3) or barf "Failed to repeat" +%x = 0 +repeat 5 times: %x += 1 +assume (%x == 5) or barf "Failed to repeat 5 times" +set {%x: 0, %y: 0} +for % in [1, 2, 3]: + repeat 5 times: do next repeat - %x +<- 1 - %y +<- 1 -assume ([%x,%y] = [0,3]) or barf "Failed to continue repeat" - -<- {%x:0,%y:0} -for % in [1,2,3] - repeat 5 times + %x += 1 + + %y += 1 + +assume ([%x, %y] == [0, 3]) or barf "Failed to continue repeat" +set {%x: 0, %y: 0} +for % in [1, 2, 3]: + repeat 5 times: do next % - %x +<- 1 - %y +<- 1 -assume ([%x,%y] = [0,0]) or barf "Failed to continue for" - -<- {%x:0,%y:0} -for % in [1,2,3] - repeat 5 times + %x += 1 + + %y += 1 + +assume ([%x, %y] == [0, 0]) or barf "Failed to continue for" +set {%x: 0, %y: 0} +for % in [1, 2, 3]: + repeat 5 times: stop repeating - %x +<- 1 - %y +<- 1 -assume ([%x,%y] = [0,3]) or barf "Failed to stop repeat" - -<- {%x:0,%y:0} -for % in [1,2,3] - repeat 5 times + %x += 1 + + %y += 1 + +assume ([%x, %y] == [0, 3]) or barf "Failed to stop repeat" +set {%x: 0, %y: 0} +for % in [1, 2, 3]: + repeat 5 times: stop % - %x +<- 1 - %y +<- 1 -assume ([%x,%y] = [0,0]) or barf "Failed to stop for" - -%x <- 0 -repeat while: %x < 10 - %x +<- 1 -assume (%x = 10) or barf "repeat-while failed" - -%x <- 0 -repeat until: %x = 10 - %x +<- 1 -assume (%x = 10) or barf "repeat-until failed" - -%x <- 0 -for %i in 1 to 3: %x +<- %i -assume (%x = 6) or barf "Numeric for range failed" - -%x <- 0 -for %i in 3 to 1 via -1: %x +<- %i -assume (%x = 6) or barf "backwards numeric for range failed" - -%result <- {} -for %key = %value in {x:1,y:2} - %result.("\%key\%key") <- (%value * 11) -assume (%result = {xx:11,yy:22}) or barf "key/value iteration failed" - -for %key = %value in {x:1,y:2} + %x += 1 + + %y += 1 + +assume ([%x, %y] == [0, 0]) or barf "Failed to stop for" +%x = 0 +repeat while (%x < 10): %x += 1 +assume (%x == 10) or barf "repeat-while failed" +%x = 0 +repeat until (%x == 10): %x += 1 +assume (%x == 10) or barf "repeat-until failed" +%x = 0 +for %i in 1 to 3: %x += %i +assume (%x == 6) or barf "Numeric for range failed" +%x = 0 +for %i in 3 to 1 via -1: %x += %i +assume (%x == 6) or barf "backwards numeric for range failed" +%result = {} +for %key = %value in {x: 1, y: 2}: %result."\%key\%key" = (%value * 11) +assume (%result == {xx: 11, yy: 22}) or barf "key/value iteration failed" +for %key = %value in {x: 1, y: 2}: stop %key barf "stopping key failed" -for %key = %value in {x:1,y:2} +for %key = %value in {x: 1, y: 2}: stop %value barf "stopping value failed" -for %key = %value in {x:1} +for %key = %value in {x: 1}: stop %key stop %value do next %key do next %value -for %key = %value in {x:1,y:2} +for %key = %value in {x: 1, y: 2}: do next %key barf "skipping key failed" -for %key = %value in {x:1,y:2} +for %key = %value in {x: 1, y: 2}: do next %value barf "skipping value failed" -action [barfer]: barf "this should never be reached" -when - * (no): barf "'when' fail" +action [barfer] (barf "this should never be reached") +when: + * (no) (barf "'when' fail") * (no) - * (3 > 4): barf "'when' fail 2" + * (3 > 4) (barf "'when' fail 2") * (yes) - * (barfer): do nothing - * (99 > 1): barf "Fell through incorrectly" + * (barfer) (do nothing) + * (99 > 1) (barf "Fell through incorrectly") -%else_worked <- (no) -when - * (no): barf - * else: %else_worked <- (yes) -assume %else_worked or barf "when..else failed" +%else_worked = (no) +when: + * (no) (barf) + *else (%else_worked = (yes)) -action [test when scope] - when - * (yes): %leaked <- (yes) +assume %else_worked or barf "when..else failed" +action [test when scope] (when (* (yes) (%leaked = (yes)))) test when scope assume (not %leaked) or barf "'when' is leaking locals" - -%when_worked <- (no) -when 4 = ? +%when_worked = (no) +when 4 = ?: * 1 - * 2: barf "'when = ?' fail" + * 2 (barf "'when = ?' fail") * 3 * 4 - * (barfer): %when_worked <- (yes) -assume %when_worked + * (barfer) (%when_worked = (yes)) -%when_worked <- (no) -when 5 = ? - * 6: barf - * else: %when_worked <- (yes) assume %when_worked +%when_worked = (no) +when 5 = ?: + * 6 (barf) + *else (%when_worked = (yes)) -%x <- 1 -do - %x <- 2 -assume (%x = 2) or barf "'do' is redefining locals" - -assume +assume %when_worked +%x = 1 +do: %x = 2 +assume (%x == 2) or barf "'do' is redefining locals" +assume (..) (..) - result of - %n <- 0 - for % in [1,2,3]: %n +<- % + result of: + %n = 0 + for % in [1, 2, 3]: %n += % return %n - ..= 6 -..or barf "'result of %' failed" - + ..== 6 -%t <- [1,[2,[[3],4],5,[[[6]]]]] -%flat <- [] -for % in recursive %t - if: (type of %) is "table" - for %2 in %: recurse % on %2 +%t = [1, [2, [[3], 4], 5, [[[6]]]]] +%flat = [] +for % in recursive %t: + if ((type of %) is "table"): for %2 in %: recurse % on %2 ..else: add % to %flat -assume: (sorted %flat) = [1,2,3,4,5,6] +assume ((sorted %flat) == [1, 2, 3, 4, 5, 6]) say "Control flow test passed." |
