-
Notifications
You must be signed in to change notification settings - Fork 197
Expand file tree
/
Copy pathcoverage.moon
More file actions
82 lines (60 loc) · 1.68 KB
/
Copy pathcoverage.moon
File metadata and controls
82 lines (60 loc) · 1.68 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
log = (str="") ->
io.stderr\write str .. "\n"
create_counter = ->
setmetatable {}, __index: (name) =>
with tbl = setmetatable {}, __index: => 0
@[name] = tbl
position_to_lines = (file_content, positions) ->
lines = {}
current_pos = 0
line_no = 1
for char in file_content\gmatch "."
if count = rawget positions, current_pos
lines[line_no] = count
if char == "\n"
line_no += 1
current_pos += 1
lines
format_file = (fname, positions) ->
-- sources have @ in front of file names
fname = fname\gsub "^@", ""
file = assert io.open fname
content = file\read "*a"
file\close!
lines = position_to_lines content, positions
log "------| @#{fname}"
line_no = 1
for line in (content .. "\n")\gmatch "(.-)\n"
foramtted_no = "% 5d"\format(line_no)
sym = lines[line_no] and "*" or " "
log "#{sym}#{foramtted_no}| #{line}"
line_no += 1
log!
class CodeCoverage
new: =>
@reset!
reset: =>
@line_counts = create_counter!
start: =>
debug.sethook @\process_line, "l"
stop: =>
debug.sethook!
print_results: =>
@format_results!
process_line: (_, line_no) =>
debug_data = debug.getinfo 2, "S"
source = debug_data.source
@line_counts[source][line_no] += 1
format_results: =>
line_table = require "moonscript.line_tables"
positions = create_counter!
for file, lines in pairs @line_counts
file_table = line_table[file]
continue unless file_table
for line, count in pairs lines
position = file_table[line]
continue unless position
positions[file][position] += count
for file, ps in pairs positions
format_file file, ps
{ :CodeCoverage }