aboutsummaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
Diffstat (limited to 'tests')
-rw-r--r--tests/all.nom28
-rw-r--r--tests/collections.nom43
-rw-r--r--tests/control_flow.nom191
-rw-r--r--tests/math.nom17
-rw-r--r--tests/metaprogramming.nom55
-rw-r--r--tests/operators.nom76
-rw-r--r--tests/text.nom13
7 files changed, 423 insertions, 0 deletions
diff --git a/tests/all.nom b/tests/all.nom
new file mode 100644
index 0000000..5b95786
--- /dev/null
+++ b/tests/all.nom
@@ -0,0 +1,28 @@
+use "lib/core.nom"
+
+# Prerequisites:
+%var <- 99
+assume (%var = 99) or barf "Var assignment doesn't work."
+
+try: barf "barf"
+..and if it succeeds: barf "try % and if it succeeds failed."
+
+try: do nothing
+..and if it barfs: barf "try % and if it barfs failed."
+
+immediately
+ assume (%ONCE is (nil)) or barf "immediately is executing twice"
+ export %ONCE <- "ONCE"
+ %foo <- "immediately local"
+assume (%ONCE = "ONCE") or barf "Globals don't work."
+assume (%foo is (nil)) or barf "Immediately is leaking locals."
+
+# Run per-file test suites
+run file "tests/metaprogramming.nom"
+run file "tests/text.nom"
+run file "tests/operators.nom"
+run file "tests/control_flow.nom"
+run file "tests/math.nom"
+run file "tests/collections.nom"
+
+say "All tests passed!"
diff --git a/tests/collections.nom b/tests/collections.nom
new file mode 100644
index 0000000..ec79653
--- /dev/null
+++ b/tests/collections.nom
@@ -0,0 +1,43 @@
+#..
+ Tests for the stuff defined in lib/control_flow.nom
+
+use "lib/core.nom"
+
+assume ((2nd to last in [1,2,3,4,5]) = 4)
+assume ((first in [1,2]) = 1)
+assume ((last in [1,2]) = 2)
+assume (3 is in [1,2,3,4,5])
+assume (99 isn't in [1,2,3])
+assume ({x:no} has key "x")
+assume ({x:no} doesn't have key "y")
+assume (not ({x:no} doesn't have key "x"))
+assume ((size of [1,2,3]) = 3)
+%list <- [1,2,3,4,5]
+append 6 to %list
+assume ((last in %list) = 6)
+pop from %list
+assume ((last in %list) = 5)
+remove index 1 from %list
+assume ((first in %list) = 2)
+assume (((% * %) for all [1,2,3]) = [1,4,9])
+assume ((%k = (%v * %v) for %k = %v in {x:1,y:2,z:3}) = {x:1,y:4,z:9})
+assume ((%k for %k = %v in {x:1}) = ["x"])
+assume ((% = (% * %) for all [1,2,3]) = {1:1,2:4,3:9})
+assume (([[1,2],[3,4]] flattened) = [1,2,3,4])
+assume ((entries in {x:1}) = [{key:"x",value:1}])
+assume ((keys in {x:1}) = ["x"])
+assume ((values in {x:1}) = [1])
+assume ((sorted [3,1,2]) = [1,2,3])
+%x <- [3,1,2]
+sort %x
+assume (%x = [1,2,3])
+sort %x by (-%)
+assume (%x = [3,2,1])
+%keys <- {1:999,2:0,3:50}
+sort %x by (% in %keys)
+assume (%x = [2,3,1])
+assume ((unique [1,2,1,3,2,3]) = [1,2,3])
+%c <- (new counter)
+for all ["x","y","x","x","y"]
+ (% in %c) +<- 1
+assume (%c = {x:3,y:2})
diff --git a/tests/control_flow.nom b/tests/control_flow.nom
new file mode 100644
index 0000000..f0fc092
--- /dev/null
+++ b/tests/control_flow.nom
@@ -0,0 +1,191 @@
+#..
+ Tests for the stuff defined in lib/control_flow.nom
+
+use "lib/core.nom"
+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))
+
+go to %skip
+error "go to failed."
+--- %skip ---
+
+%tot <- 0
+for %x in [1,2,3]
+ %tot +<- %x
+assume (%tot = 6) or barf "for-loop failed"
+
+%tot <- 0
+for all [1,2,3]
+ %tot +<- %
+assume (%tot = 6) or barf "for-all-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 all [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 all [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 all [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 all [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 from 1 to 3: %x +<- %i
+assume (%x = 6) or barf "Numeric for range failed"
+
+%x <- 0
+for %i from 3 to 1 via -1: %x +<- %i
+assume (%x = 6) or barf "backwards numeric for range failed"
+
+%x <- 0
+for all 1 to 3: %x +<- %
+assume (%x = 6) or barf "terse numeric for range failed"
+
+%result <- {}
+for %key = %value in {x:1,y:2}
+ (%result's ("\%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}
+ stop %value
+ barf "stopping value failed"
+
+for %key = %value in {x:1,y:2}
+ do next %key
+ barf "skipping key failed"
+
+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"
+ * (no)
+ * (3 > 4): barf "'when' fail 2"
+ * (yes)
+ * (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"
+
+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 = ?
+ * 1
+ * 2: barf "'when = ?' fail"
+ * 3
+ * 4
+ * (barfer): %when_worked <- (yes)
+assume %when_worked
+
+%when_worked <- (no)
+when 5 = ?
+ * 6: barf
+ * else: %when_worked <- (yes)
+assume %when_worked
+
+try: barf
+..and if it succeeds: barf "try failed."
+
+%worked <- (no)
+try: barf
+..and if it barfs: %worked <- (yes)
+assume %worked or barf "try/catch failed"
+
+%x <- 1
+do
+ %x <- 2
+assume (%x = 2) or barf "'do' is redefining locals"
+
+%x <- 1
+try
+ %x <- 2
+ do
+ barf
+ ..then always
+ %x <- 1
+..and if it barfs: do nothing
+assume (%x = 1) or barf "do/then always failed"
diff --git a/tests/math.nom b/tests/math.nom
new file mode 100644
index 0000000..245fe52
--- /dev/null
+++ b/tests/math.nom
@@ -0,0 +1,17 @@
+#..
+ Tests for the stuff defined in lib/control_flow.nom
+
+use "lib/core.nom"
+
+assume (all of [inf, pi, tau, golden ratio, e]) or barf "math constants failed"
+%nan <- (NaN)
+assume (%nan != %nan) or barf "NaN failed"
+assume (("5" as a number) = 5)
+assume
+ all of [..]
+ abs 5, |5|, sqrt 5, √(5), sine 5, cosine 5, tangent 5, arc sine 5, arc cosine 5,
+ arc tangent 5, arc tangent 5/10, hyperbolic sine 5, hyperbolic cosine 5,
+ hyperbolic tangent 5, e^5, ln 5, log_(2) 5, floor 5, ceiling 5, round 5,
+..or barf "math functions failed"
+assume ((463 to the nearest 100) = 500) or barf "rounding failed"
+assume ((2.6 to the nearest 0.25) = 2.5) or barf "rounding failed"
diff --git a/tests/metaprogramming.nom b/tests/metaprogramming.nom
new file mode 100644
index 0000000..37acd3e
--- /dev/null
+++ b/tests/metaprogramming.nom
@@ -0,0 +1,55 @@
+#..
+ Tests for the stuff defined in lib/metaprogramming.nom
+
+use "lib/core.nom"
+
+immediately
+ compile [five] to {expr:"5"}
+assume ((five) = 5) or barf "Compile to expression failed."
+
+immediately
+ compile [loc x] to {statements:"_x = 99", locals:["_x"]}
+lua> "do"
+loc x
+assume (%x is 99) or barf "Compile to statements with locals failed."
+lua> "end"
+assume (%x is (nil)) or barf "Failed to properly localize a variable."
+
+immediately
+ compile [asdf] to
+ %tmp <- ""
+ return {statements:%tmp}
+asdf
+assume (%tmp is (nil)) or barf "compile to is leaking variables"
+
+action [foo %x]
+ %y <- (%x + 1)
+ return %y
+assume ((foo 10) = 11) or barf "Action didn't work."
+assume (%y is (nil)) or barf "Action leaked a local into globals."
+
+immediately
+ parse [baz %] as: foo %
+assume ((baz 10) = 11) or barf "Parse as action failed."
+
+immediately
+ parse [V] as: five
+assume ((V) = 5) or barf "Parse as compile action failed."
+
+remove action "foo %"
+try: foo 99
+..and if it succeeds: barf "Failed to delete action"
+
+assume ((\(5 + 5) as value) = 10) or barf "%tree as value failed."
+
+assume ((\(foo %x)'s source code) = "foo %x") or barf "source code failed."
+
+assume ((repr [1,2]) = "{1, 2}") or barf "repr failed."
+
+assume ((type of {}) = "table") or barf "type of failed."
+
+assume ((nomsu) = (=lua "nomsu")) or barf "nomsu failed"
+
+assume (("x" as lua identifier) = (\%x as lua identifier)) or barf "converting to identifier failed."
+
+assume ((run "return 99") = 99) or barf "run % failed."
diff --git a/tests/operators.nom b/tests/operators.nom
new file mode 100644
index 0000000..e3bd0b9
--- /dev/null
+++ b/tests/operators.nom
@@ -0,0 +1,76 @@
+#..
+ Tests for the stuff defined in lib/operators.nom
+
+use "lib/core.nom"
+
+assume (({x:5}'s "x") = 5) or barf "indexing doesn't work."
+try: % <- ({}'s "[[[\n]]]")
+..and if it barfs: barf "failed to index a table literal with a string containing brackets n stuff"
+
+<-{%x:10,%y:20}
+assume ((%x = 10) and (%y = 20)) or barf "mutli-assignment failed."
+<-{%x:%y, %y:%x}
+assume ((%y = 10) and (%x = 20)) or barf "swapping vars failed."
+
+% <- [%x < %y, %x <= %y, %x > %y, %x >= %y, %x = %y, %x is %y, %x != %y, %x isn't %y, %x is not %y]
+
+assume ({} is {}) or barf "Equality check failed."
+assume (({}'s id) is not ({}'s id)) or barf "Identity check failed."
+
+<-{%x:"outer",%y:"outer"}
+action [set global x local y]
+ export %x <- "inner"
+ %y <- "inner"
+set global x local y
+assume ((%x = "inner") and (%y = "outer")) or barf "export failed."
+
+<-{%x:"outer",%y:"outer"}
+action [set global x local y]
+ exporting [%x]
+ %x <- "inner"
+ %y <- "inner"
+set global x local y
+assume ((%x = "inner") and (%y = "outer")) or barf "export failed."
+
+<-{%x:1,%y:2}
+with [%z, %x<-999]
+ %z <- 999
+ assume (%z = 999) or barf "'with' failed."
+ assume (%x = 999) or barf "'with' assignment failed."
+assume (%x = 1) or barf "'with' scoping failed"
+assume (%z = (nil)) or barf "'with' scoping failed"
+
+assume ((1+2*3-4/2^2) = 6) or barf "math expressions not working properly"
+assume ((5 wrapped around 2) = 1) or barf "mod not working"
+assume (1 <= 2 < 3) or barf "chained operator fail."
+%value <- -999
+action [flipflop]
+ export %value <- (-%value)
+ return %value
+assume (not (1 < (flipflop) < 1)) or barf "3-way inequality evaluated middle term twice"
+assume (((yes) and (yes)) = (yes))
+action [barfer]
+ barf "short circuiting failed"
+assume (((no) and (barfer)) = (no))
+assume ((no) or (yes))
+assume ((yes) or (barfer))
+
+assume ((1 OR 2) = 3)
+assume ((3 XOR 2) = 1)
+assume ((3 AND 2) = 2)
+assume ((NOT (NOT 6)) = 6)
+assume ((1<<1) = 2)
+assume ((2>>1) = 1)
+assume ((2>>>1) = 1)
+#.. Ugh, Lua is stupid when it comes to bitwise arithmetic on negative numbers, so I'm
+ skipping the tests for those.
+
+assume ((-(5)) = -5)
+assume ((not (yes)) = (no))
+%x <- 1
+%x +<- 1
+assume (%x = 2) or barf "+<- failed"
+%x *<- 2
+assume (%x = 4) or barf "*<- failed"
+wrap %x around 3
+assume (%x = 1) or barf "wrap around failed"
diff --git a/tests/text.nom b/tests/text.nom
new file mode 100644
index 0000000..ea18d42
--- /dev/null
+++ b/tests/text.nom
@@ -0,0 +1,13 @@
+#..
+ Tests for the stuff defined in lib/text.nom
+
+use "lib/core.nom"
+
+assume ((["x","y"] joined with ",") = "x,y") or barf "joined with failed"
+assume ((["x","y"] joined) = "xy") or barf "joined failed"
+assume (("asdf" capitalized) = "Asdf") or barf "capitalized failed"
+assume (("asdf" with "X" instead of "s") = "aXdf") or barf "substitution failed"
+# TODO: add tests for indent/dedent
+assume ("\n" = (newline)) or barf "Text literals failed."
+%x <- "\(green)hello\(reset color)"
+assume (("x" + "y") = "xy")