-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathexercism_reporter.rb
141 lines (111 loc) · 3.05 KB
/
exercism_reporter.rb
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
module Minitest
class ExercismReporter < AbstractReporter
class << self
attr_reader :instance
end
def self.init(*args)
raise "Instance already defined" if @instance
@instance = new(*args)
end
def start; end
def prerecord(*)
reset_user_output
end
def set_metadata(test_file, metadata)
@metadata[test_file] = metadata.each_with_object({}) do |md, hash|
hash[md[:test]] = md
end
end
def record(result)
test_metadata = metadata[result.source_location.first][result.name]
tests << TestResult.new(result, user_output, test_metadata)
end
def report
write_report(status, tests: tests.sort_by(&:index).map(&:to_h))
end
def status
tests.all? { |t| t.status == :pass } ? :pass : :fail
end
def exception_raised!(e)
message =
case e
when SyntaxError
ExtractSyntaxExceptionErrorMessage.(e)
else
ExtractStandardExceptionErrorMessage.(e)
end
write_report(:error, message:)
end
def report_written?
@report_written
end
def record_output(*args)
user_output.puts(*args)
end
private
attr_reader :exercise, :path, :results, :tests, :user_output, :metadata
def initialize(exercise, path)
super()
@exercise = exercise
@path = path
@tests = []
@report_written = false
@metadata = {}
reset_user_output
end
def write_report(status, tests: nil, message: nil)
return if report_written?
TestRunner::WriteReport.(path, status, tests:, message:)
@report_written = true
end
def reset_user_output
@user_output = StringIO.new
end
class TestResult
def initialize(result, output_stream, metadata)
@result = result
@output_stream = output_stream
@metadata = metadata
end
def to_h
{}.tap do |hash|
hash[:name] = metadata[:name]
hash[:test_code] = metadata[:test_code]
hash[:status] = status
hash[:output] = output if attach_output?
hash[:message] = message if attach_message?
hash[:task_id] = metadata[:task_id]
end
end
def status
return :error if result.error?
result.failures.size == 0 ? :pass : :fail
end
def index
metadata[:index]
end
private
attr_reader :result, :output_stream, :metadata
def attach_output?
output_stream.length > 0
end
def attach_message?
status != :pass
end
def message
if result.error?
ExtractFailureErrorMessage.(result.failure)
else
result.failure.to_s
end
end
# Output should be restricted to the first 500 chars
# and an message should be appended if longer.
def output
str = output_stream.string
return str if str.length <= 500
%(#{str[0, 500]}\n\n...Output was truncated. Please limit to 500 chars...)
end
end
end
end