moonscript/test2.moon

149 lines
3.2 KiB
Plaintext
Raw Normal View History

2012-09-08 17:24:46 +00:00
require "lfs"
require "busted"
2012-11-28 08:24:58 +00:00
parse = require "moonscript.parse"
compile = require "moonscript.compile"
util = require "moonscript.util"
2012-09-08 17:24:46 +00:00
pattern = ...
2013-01-03 01:55:43 +00:00
import unpack from util
2012-09-08 17:24:46 +00:00
options = {
in_dir: "tests/inputs",
out_dir: "tests/outputs",
input_pattern: "(.*)%.moon$",
output_ext: ".lua"
show_timings: os.getenv "TIME"
diff: {
tool: "git diff --no-index --color" --color-words"
2012-09-08 17:24:46 +00:00
filter: (str) ->
-- strip the first four lines
table.concat [line for line in *util.split(str, "\n")[5,]], "\n"
}
}
timings = {}
gettime = nil
pcall ->
require "socket"
gettime = socket.gettime
gettime or= os.clock
2012-09-08 17:24:46 +00:00
benchmark = (fn) ->
if gettime
start = gettime!
res = {fn!}
gettime! - start, unpack res
else
nil, fn!
read_all = (fname) ->
2012-10-31 16:57:07 +00:00
if f = io.open(fname, "r")
with f\read "*a"
f\close!
2012-09-08 17:24:46 +00:00
diff_file = (a_fname, b_fname) ->
out = io.popen(options.diff.tool .. " ".. a_fname .. " " .. b_fname, "r")\read "*a"
if options.diff.filter
out = options.diff.filter out
out
diff_str = (expected, got) ->
a_tmp = os.tmpname! .. ".expected"
b_tmp = os.tmpname! .. ".got"
with io.open(a_tmp, "w")
\write expected
\close!
with io.open(b_tmp, "w")
\write got
\close!
with diff_file a_tmp, b_tmp
os.remove a_tmp
os.remove b_tmp
string_assert = (expected, got) ->
if expected != got
diff = diff_str expected, got
error "string equality assert failed:\n" .. diff
input_fname = (base) ->
options.in_dir .. "/" .. base .. ".moon"
output_fname = (base) ->
options.out_dir .. "/" .. base .. options.output_ext
describe "input tests", ->
inputs = for file in lfs.dir options.in_dir
with match = file\match options.input_pattern
continue unless match
2012-11-29 06:47:52 +00:00
table.sort inputs
2012-09-08 17:24:46 +00:00
for name in *inputs
input = input_fname name
fn = if pattern and not input\match pattern
pending
else
it
fn input .. " #input", ->
file_str = read_all input_fname name
parse_time, tree, err = benchmark -> parse.string file_str
2012-11-01 03:13:26 +00:00
error err if err
2012-09-08 17:24:46 +00:00
compile_time, code, err, pos = benchmark -> compile.tree tree
2012-11-01 03:13:26 +00:00
error compile.format_error err, pos, file_str unless code
2012-09-08 17:24:46 +00:00
table.insert timings, {name, parse_time, compile_time}
if os.getenv "BUILD"
with io.open output_fname(name), "w"
\write code
\close!
else
expected_str = read_all output_fname name
2012-10-31 16:57:07 +00:00
error "Test not built: " .. input_fname(name) unless expected_str
2012-09-08 17:24:46 +00:00
string_assert expected_str, code
nil
busted.options = {
output: require'busted.output.utf_terminal'!
suppress_pending: true
}
print busted!
if options.show_timings
format_time = (sec) -> ("%.3fms")\format(sec*1000)
col_width = math.max unpack [#t[1] for t in *timings]
print "\nTimings:"
total_parse, total_compile = 0, 0
for tuple in *timings
name, parse_time, compile_time = unpack tuple
name = name .. (" ")\rep col_width - #name
total_parse += parse_time
total_compile += compile_time
print " * " .. name,
"p: " .. format_time(parse_time),
"c: " .. format_time(compile_time)
print "\nTotal:"
print " parse:", format_time total_parse
print " compile:", format_time total_compile