Uh oh!
There was an error while loading. Please reload this page.
This repository was archived by the owner on Nov 20, 2020. It is now read-only.
forked from leafo/moonscript
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.lua
More file actions
Latest commit
executable file
·193 lines (159 loc) · 4.14 KB
/
Copy pathtest.lua
File metadata and controls
executable file
·193 lines (159 loc) · 4.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#!/usr/bin/env lua
require"lfs"
require"alt_getopt"
localgettime=nil
pcall(function()
require"socket"
gettime=socket.gettime
end)
require"moonscript.parse"
require"moonscript.compile"
localparse, compile=moonscript.parse, moonscript.compile
localopts, ind=alt_getopt.get_opts(arg, "qd:", { })
localargv= {}
fori=ind, #argdotable.insert(argv, arg[i]) end
localaction=table.remove(argv, 1) or"run"
localdiff_tool=opts.dor"diff"
localquiet=opts.q
localopts= {
in_dir="tests/inputs",
out_dir="tests/outputs",
input_pattern="(.*)%.moon$",
output_ext=".lua"
}
localtotal_time= {
parse=0,
compile=0
}
localfunctionformat_time(sec)
return ("%.3fms"):format(sec*1000)
end
localfunctiondiff(a_fname, b_fname)
returnio.popen(diff_tool..""..a_fname..""..b_fname, "r"):read("*a")
end
localfunctioninput_name(name) returnopts.in_dir.."/"..nameend
localfunctionoutput_name(name)
returnopts.out_dir.."/"..name:match(opts.input_pattern)..opts.output_ext
end
localfunctionrun_file(name, benchmark)
name=input_name(name)
file_str=io.open(name):read("*a")
localstart_parse
ifbenchmarkthenstart_parse=gettime() end
localtree, err=parse.string(file_str)
localparse_time=0
ifbenchmarkthenparse_time=gettime() -start_parseend
ifnottreethen
error("Parse error in "..name.."\n"..err)
end
localstart_compile
ifbenchmarkthenstart_compile=gettime() end
localcode, err, pos=compile.tree(tree)
ifnotcodethen
print()
print(("Failed to compile: %s"):format(name))
print(compile.format_error(err, pos, file_str))
os.exit()
end
-- local success, code = pcall(compile.tree, tree)
-- if not success then
-- error("Compile error in"..name..":\n"..code)
-- end
ifbenchmarkthen
localcompile_time=gettime() -start_compile
returncode, parse_time, compile_time
end
returncode
end
localfunctioninputs(pattern)
returncoroutine.wrap(function()
forfileinlfs.dir(opts.in_dir) do
localbody=file:match(opts.input_pattern)
ifbodythen
ifnotpatternorbody:match(pattern) then
coroutine.yield(file)
end
end
end
end)
end
localactions= {
build=function(pattern)
forfileininputs(pattern) do
localout_fname=output_name(file)
print("Building: ", file, out_fname)
localresult=run_file(file)
ifresultthen
io.open(out_fname, "w"):write(result)
end
end
end,
run=function(pattern)
localfailed=false
localtests_run, result=0
forfileininputs(pattern) do
tests_run=tests_run+1
localcorrect_fname=output_name(file)
result, parse_time, compile_time=run_file(file, gettime)
localhandle=io.open(correct_fname)
ifnothandlethen
print("Test not built yet:", correct_fname)
else
localcorrect=handle:read("*a")
ifresult~=correctthen
print("Test", file, "failed")
localtmp_name=os.tmpname()
localtmp=io.open(tmp_name, "w")
tmp:write(result)
tmp:close()
ifnotquietthen
print(diff(correct_fname, tmp_name))
end
os.remove(tmp_name)
-- break
else
ifparse_timethen
total_time.parse=total_time.parse+parse_time
total_time.compile=total_time.compile+compile_time
parse_time=format_time(parse_time)
compile_time=format_time(compile_time)
print("Test", file, "passed", "",
("p: %s, c: %s"):format(parse_time, compile_time))
else
print("Test", file, "passed")
end
end
end
end
ifgettimethen
print""
print"total:"
print(" parse time", format_time(total_time.parse))
print(" compile time", format_time(total_time.compile))
end
iftests_run==0then
ifnotpatternthen
print("No tests found")
else
print("No tests matching pattern:", pattern)
end
elseiftests_run==1then
-- print(result)
end
end,
list=function(pattern)
localcount=0
forfileininputs(pattern) do
count=count+1
print(file)
end
ifcount>0thenprint("") end
print("Count:", count)
end
}
localfn=actions[action]
iffnthen
fn(unpack(argv))
else
print("Unknown action:", action)
end