aboutsummaryrefslogtreecommitdiff
path: root/lib/testing.nom
diff options
context:
space:
mode:
authorBruce Hill <bitbucket@bruce-hill.com>2017-09-24 20:20:27 -0700
committerBruce Hill <bitbucket@bruce-hill.com>2017-09-24 20:20:27 -0700
commitaf3274ca9237a08093009e87955e30ab5f473f12 (patch)
tree46c249d6b42ac51111c7e198a10b8dad09b17923 /lib/testing.nom
parente4660b169c14d24c3ec373b197e8b9469d200d50 (diff)
massive overhaul, compiler kinda works.
Diffstat (limited to 'lib/testing.nom')
-rw-r--r--lib/testing.nom69
1 files changed, 69 insertions, 0 deletions
diff --git a/lib/testing.nom b/lib/testing.nom
index 87a7711..83d0108 100644
--- a/lib/testing.nom
+++ b/lib/testing.nom
@@ -2,6 +2,75 @@ require "lib/metaprogramming.nom"
# For unit testing
macro block [test %code yields %expected] =:
+
+
+ _yield_tree: (tree, indent_level=0)=>
+ ind = (s) -> INDENT\rep(indent_level)..s
+ switch tree.type
+ when "File"
+ coroutine.yield(ind"File:")
+ @_yield_tree(tree.value.body, indent_level+1)
+ when "Errors" then coroutine.yield(ind"Error:\n#{tree.value}")
+ when "Block"
+ for chunk in *tree.value
+ @_yield_tree(chunk, indent_level)
+ when "Thunk"
+ coroutine.yield(ind"Thunk:")
+ @_yield_tree(tree.value, indent_level+1)
+ when "Statement" then @_yield_tree(tree.value, indent_level)
+ when "FunctionCall"
+ alias = @get_alias tree
+ args = [a for a in *tree.value when a.type != "Word"]
+ if #args == 0
+ coroutine.yield(ind"Call [#{alias}]!")
+ else
+ coroutine.yield(ind"Call [#{alias}]:")
+ for a in *args
+ @_yield_tree(a, indent_level+1)
+ when "String" then coroutine.yield(ind(repr(tree.value)))
+ when "Longstring" then coroutine.yield(ind(repr(tree.value)))
+ when "Number" then coroutine.yield(ind(tree.value))
+ when "Var" then coroutine.yield ind"Var[#{repr(tree.value)}]"
+ when "List"
+ if #tree.value == 0
+ coroutine.yield(ind("<Empty List>"))
+ else
+ coroutine.yield(ind"List:")
+ for item in *tree.value
+ @_yield_tree(item, indent_level+1)
+ else error("Unknown/unimplemented thingy: #{tree.type}")
+
+ print_tree:(tree)=>
+ for line in coroutine.wrap(-> @_yield_tree(tree))
+ @writeln(line)
+
+ stringify_tree:(tree)=>
+ result = {}
+ for line in coroutine.wrap(-> @_yield_tree(tree))
+ insert(result, line)
+ return concat result, "\n"
+
+ test: (src, filename, expected)=>
+ i = 1
+ while i != nil
+ start,stop = src\find("\n\n", i)
+
+ test = src\sub(i,start)
+ i = stop
+ start,stop = test\find"==="
+ if not start or not stop then
+ @error("WHERE'S THE ===? in:\n#{test}")
+ test_src, expected = test\sub(1,start-1), test\sub(stop+1,-1)
+ expected = expected\match'[\n]*(.*[^\n])'
+ tree = @parse(test_src, filename)
+ got = @stringify_tree(tree.value.body)
+ if got != expected
+ @error"TEST FAILED!\nSource:\n#{test_src}\nExpected:\n#{expected}\n\nGot:\n#{got}"
+
+
+
+
+
%generated =: repr (nomsu "stringify_tree" [%code's "value"])
%expected =: %expected as lua
if (%generated != %expected):