rubytest 0.9.0 → 0.10.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -43,34 +43,6 @@ module Test
43
43
  def begin_test(test)
44
44
  end
45
45
 
46
- #
47
- def skip_case(test_case)
48
- end
49
-
50
- #
51
- def skip_test(test)
52
- end
53
-
54
- #
55
- #def test(test)
56
- #end
57
-
58
- #
59
- def pass(test)
60
- end
61
-
62
- #
63
- def fail(test, exception)
64
- end
65
-
66
- # Report a test error.
67
- def error(test, exception)
68
- end
69
-
70
- # Report a pending test.
71
- def todo(test, exception)
72
- end
73
-
74
46
  #
75
47
  def end_test(test)
76
48
  end
@@ -79,21 +51,8 @@ module Test
79
51
  def end_case(test_case)
80
52
  end
81
53
 
82
- #
83
- def end_suite(test_suite)
84
- end
85
-
86
54
  protected
87
55
 
88
- def record
89
- runner.recorder
90
- end
91
-
92
- # Is coverage information requested?
93
- #def cover?
94
- # runner.cover?
95
- #end
96
-
97
56
  # Count up the total number of tests.
98
57
  def total_count(suite)
99
58
  c = 0
@@ -107,73 +66,14 @@ module Test
107
66
  return c
108
67
  end
109
68
 
110
- # Common timestamp any reporter can use.
111
- def timestamp
112
- seconds = Time.now - @start_time
113
-
114
- "Finished in %.5fs, %.2f tests/s." % [seconds, total/seconds]
115
- end
116
-
117
- #
118
- def total
119
- @total ||= subtotal
120
- end
121
-
122
- #
123
- def subtotal
124
- [:todo, :pass, :fail, :error, :omit, :skip].inject(0) do |s,r|
125
- s += record[r.to_sym].size; s
126
- end
127
- end
128
-
129
- # TODO: lump skipped and omitted into one group ?
130
-
131
69
  TITLES = {
132
70
  :pass => 'passing',
133
71
  :fail => 'failures',
134
72
  :error => 'errors',
135
73
  :todo => 'pending',
136
- :omit => 'omissions',
137
74
  :skip => 'skipped'
138
75
  }
139
76
 
140
- # TODO: Add assertion counts (if reasonably possible).
141
-
142
- # Common tally stamp any reporter can use.
143
- #
144
- # @return [String] tally stamp
145
- def tally
146
- sizes = {}
147
- names = %w{pass error fail todo omit skip}.map{ |n| n.to_sym }
148
- names.each do |r|
149
- sizes[r] = record[r].size
150
- end
151
-
152
- #names.unshift(:tests)
153
- #sizes[:tests] = total
154
-
155
- s = []
156
- names.each do |n|
157
- next unless sizes[n] > 0
158
- s << tally_item(n, sizes)
159
- end
160
-
161
- 'Executed ' + "#{total}".ansi(:bold) + ' tests with ' + s.join(', ') + '.'
162
- end
163
-
164
- #
165
- def tally_item(name, sizes)
166
- x = []
167
- x << "%s" % sizes[name].to_s.ansi(:bold)
168
- x << " %s" % TITLES[name].downcase
169
- x << " (%.1f%%)" % ((sizes[name].to_f/total*100)) if runner.verbose?
170
- x.join('')
171
- end
172
-
173
- #--
174
- # TODO: Matching `bin/ruby-test` is not robust.
175
- #++
176
-
177
77
  # Remove reference to lemon library from backtrace.
178
78
  #
179
79
  # @param [Exception] exception
@@ -2,11 +2,34 @@
2
2
 
3
3
  module Test::Reporters
4
4
 
5
- # Hash Abstract is a base class for the TAP-Y
6
- # and TAP-J reporters.
5
+ # Structured records used by the internal test reporter.
7
6
  #
8
7
  class AbstractHash < Abstract
9
8
 
9
+ def capture_output?
10
+ true
11
+ end
12
+
13
+ # Return a structured record of the completed result.
14
+ def record(result)
15
+ @current_result = result
16
+ if result.status == :skip
17
+ method = result.kind == :case ? :skip_case : :skip_test
18
+ public_send(method, result.test, result.reason)
19
+ elsif result.status == :pass
20
+ pass(result.test)
21
+ else
22
+ public_send(result.status, result.test, result.exception)
23
+ end
24
+ ensure
25
+ @current_result = nil if result
26
+ end
27
+
28
+ def finish(summary)
29
+ @summary = summary
30
+ end_suite(summary.suite)
31
+ end
32
+
10
33
  #
11
34
  # @return [Hash]
12
35
  #
@@ -51,9 +74,6 @@ module Test::Reporters
51
74
  #
52
75
  def begin_test(test)
53
76
  @test_index += 1
54
-
55
- @stdout, @stderr = $stdout, $stderr
56
- $stdout, $stderr = StringIO.new, StringIO.new
57
77
  end
58
78
 
59
79
  # Ruby Test use the term "skip", where as TAP-Y/J uses "omit".
@@ -62,22 +82,16 @@ module Test::Reporters
62
82
  #
63
83
  # @return [Hash]
64
84
  #
65
- def skip_test(test)
66
- h = {}
67
- h['type' ] = 'test'
68
- h['status'] = 'omit'
69
-
70
- merge_subtype h, test
71
- merge_setup h, test
72
- merge_label h, test
73
- #merge_comparison h, test, exception
74
- #merge_coverage h, test
75
- merge_source h, test
76
- #merge_exception h, test, exception
77
- merge_output h
78
- merge_time h
85
+ def skip_test(test, reason=nil)
86
+ skip_record(test, reason)
87
+ end
79
88
 
80
- return h
89
+ # A skipped case is reported like a skipped test, as a case.
90
+ #
91
+ # @return [Hash]
92
+ #
93
+ def skip_case(test_case, reason=nil)
94
+ skip_record(test_case, reason).merge('type' => 'case')
81
95
  end
82
96
 
83
97
  #
@@ -169,7 +183,6 @@ module Test::Reporters
169
183
  #
170
184
  def end_test(test)
171
185
  super(test)
172
- $stdout, $stderr = @stdout, @stderr
173
186
  end
174
187
 
175
188
  #
@@ -183,14 +196,14 @@ module Test::Reporters
183
196
  def end_suite(suite)
184
197
  h = {
185
198
  'type' => 'final',
186
- 'time' => Time.now - @start_time,
199
+ 'time' => @summary.elapsed,
187
200
  'counts' => {
188
- 'total' => total,
189
- 'pass' => record[:pass].size,
190
- 'fail' => record[:fail].size,
191
- 'error' => record[:error].size,
192
- 'omit' => record[:omit].size,
193
- 'todo' => record[:todo].size
201
+ 'total' => @summary.total,
202
+ 'pass' => @summary.counts[:pass],
203
+ 'fail' => @summary.counts[:fail],
204
+ 'error' => @summary.counts[:error],
205
+ 'omit' => @summary.counts[:skip],
206
+ 'todo' => @summary.counts[:todo]
194
207
  }
195
208
  }
196
209
  return h
@@ -198,6 +211,23 @@ module Test::Reporters
198
211
 
199
212
  private
200
213
 
214
+ # The record of a skipped test or case.
215
+ def skip_record(test, reason)
216
+ h = {}
217
+ h['type' ] = 'test'
218
+ h['status'] = 'omit'
219
+ h['reason'] = reason if reason
220
+
221
+ merge_subtype h, test
222
+ merge_setup h, test
223
+ merge_label h, test
224
+ merge_source h, test
225
+ merge_output h
226
+ merge_time h
227
+
228
+ return h
229
+ end
230
+
201
231
  # For todo entries in particulr, i.e. NotImplementedError
202
232
  # exception, the return value represents the "todo" priority
203
233
  # level. The `Exception#priority` method returns an Integer
@@ -206,7 +236,7 @@ module Test::Reporters
206
236
  #
207
237
  def merge_priority(hash, test, exception)
208
238
  level = exception.priority
209
- h['priority'] = level.to_i
239
+ hash['priority'] = level.to_i
210
240
  end
211
241
 
212
242
  #
@@ -233,8 +263,8 @@ module Test::Reporters
233
263
 
234
264
  # Add source location information to hash.
235
265
  def merge_source(hash, test)
236
- if test.respond_to?('source_location')
237
- file, line = source_location
266
+ if test.respond_to?(:source_location)
267
+ file, line = test.source_location
238
268
  hash['file' ] = file
239
269
  hash['line' ] = line
240
270
  hash['source' ] = code(file, line).to_str
@@ -251,6 +281,12 @@ module Test::Reporters
251
281
  hash['exception']['snippet' ] = code(exception).to_omap
252
282
  hash['exception']['message' ] = exception.message
253
283
  hash['exception']['backtrace'] = clean_backtrace(exception) if bt
284
+ if @current_result && @current_result.exceptions.size > 1
285
+ hash['exceptions'] = @current_result.exceptions.map do |error|
286
+ {'class' => error.class.to_s, 'message' => error.message,
287
+ 'backtrace' => clean_backtrace(error)}
288
+ end
289
+ end
254
290
  end
255
291
 
256
292
  # TODO: This is still an "idea in progress" for both RybyTest and Tap-Y/J.
@@ -280,13 +316,13 @@ module Test::Reporters
280
316
 
281
317
  #
282
318
  def merge_output(hash)
283
- hash['stdout'] = $stdout.string
284
- hash['stderr'] = $stderr.string
319
+ hash['stdout'] = @current_result.stdout
320
+ hash['stderr'] = @current_result.stderr
285
321
  end
286
322
 
287
323
  #
288
324
  def merge_time(hash)
289
- hash['time'] = Time.now - @start_time
325
+ hash['time'] = @current_result.elapsed
290
326
  end
291
327
 
292
328
  end
@@ -5,82 +5,108 @@ module Test::Reporters
5
5
  # Simple Dot-Progress Reporter
6
6
  class Dotprogress < Abstract
7
7
 
8
- def skip_test(unit, reason)
9
- print "S".ansi(:cyan) if runner.verbose?
8
+ def initialize(runner, output: $stdout)
9
+ super(runner)
10
+ @output = output
11
+ @verbose = runner.verbose?
10
12
  end
11
13
 
12
- def pass(unit)
13
- print "."
14
- $stdout.flush
14
+ def record(result)
15
+ if result.status == :skip
16
+ method = result.kind == :case ? :skip_case : :skip_test
17
+ public_send(method, result.test, result.reason)
18
+ elsif result.status == :pass
19
+ pass(result.test)
20
+ else
21
+ public_send(result.status, result.test, result.exception)
22
+ end
23
+ end
24
+
25
+ def finish(summary)
26
+ @summary = summary
27
+ end_suite(summary.suite)
28
+ end
29
+
30
+ def skip_test(test, reason)
31
+ emit('S'.ansi(:cyan)) if @verbose
15
32
  end
16
33
 
17
- def fail(unit, exception)
18
- print "F".ansi(:red)
19
- $stdout.flush
34
+ def skip_case(test_case, reason)
35
+ emit('S'.ansi(:cyan)) if @verbose
20
36
  end
21
37
 
22
- def error(unit, exception)
23
- print "E".ansi(:red, :bold)
24
- $stdout.flush
38
+ def pass(test)
39
+ emit('.')
25
40
  end
26
41
 
27
- def todo(unit, exception)
28
- print "P".ansi(:yellow)
29
- $stdout.flush
42
+ def fail(test, exception)
43
+ emit('F'.ansi(:red))
44
+ end
45
+
46
+ def error(test, exception)
47
+ emit('E'.ansi(:red, :bold))
48
+ end
49
+
50
+ def todo(test, exception)
51
+ emit('P'.ansi(:yellow))
30
52
  end
31
53
 
32
54
  def end_suite(suite)
33
- puts; puts
34
- puts timestamp
35
- puts
36
-
37
- if runner.verbose?
38
- unless record[:omit].empty?
39
- puts "SKIPPED\n\n"
40
- record[:skip].each do |test, reason|
41
- puts " #{test}".ansi(:bold)
42
- puts " #{reason}" if String===reason
43
- puts
44
- end
45
- end
46
- end
55
+ summary = @summary
56
+ @output.puts
57
+ @output.puts
58
+ rate = summary.elapsed.zero? ? 0.0 : summary.total / summary.elapsed
59
+ @output.puts "Finished in %.5fs, %.2f tests/s." % [summary.elapsed, rate]
60
+ @output.puts
47
61
 
48
- unless record[:todo].empty?
49
- puts "PENDING\n\n"
50
- record[:todo].each do |test, exception|
51
- puts " #{test}".ansi(:bold) unless test.to_s.empty?
52
- puts " #{exception}"
53
- puts " #{file_and_line(exception)}"
54
- puts code(exception)
55
- puts
62
+ if @verbose && summary.counts[:skip].positive?
63
+ @output.puts "SKIPPED\n\n"
64
+ summary.results_for(:skip).each do |result|
65
+ @output.puts " #{result.test}".ansi(:bold)
66
+ @output.puts " #{result.reason}" if String === result.reason
67
+ @output.puts
56
68
  end
57
69
  end
58
70
 
59
- unless record[:fail].empty?
60
- puts "FAILURES\n\n"
61
- record[:fail].each do |test_unit, exception|
62
- puts " #{test_unit}".ansi(:bold)
63
- puts " #{exception}"
64
- puts " #{file_and_line(exception)}"
65
- puts code(exception)
66
- puts " " + clean_backtrace(exception).join("\n ")
67
- puts
71
+ {todo: 'PENDING', fail: 'FAILURES', error: 'ERRORS'}.each do |status, title|
72
+ next if summary.counts[status].zero?
73
+ @output.puts "#{title}\n\n"
74
+ summary.results_for(status).each do |result|
75
+ @output.puts " #{result.test}".ansi(:bold) unless status == :todo && result.test.to_s.empty?
76
+ result.exceptions.each_with_index do |exception, index|
77
+ prefix = result.exceptions.size > 1 ? "#{index + 1}. " : ''
78
+ @output.puts " #{prefix}#{exception.class}: #{exception.message}"
79
+ @output.puts " #{file_and_line(exception)}"
80
+ @output.puts code(exception)
81
+ @output.puts " " + clean_backtrace(exception).join("\n ") unless status == :todo
82
+ end
83
+ @output.puts
68
84
  end
69
85
  end
70
86
 
71
- unless record[:error].empty?
72
- puts "ERRORS\n\n"
73
- record[:error].each do |test_unit, exception|
74
- puts " #{test_unit}".ansi(:bold)
75
- puts " #{exception}"
76
- puts " #{file_and_line(exception)}"
77
- puts code(exception)
78
- puts " " + clean_backtrace(exception).join("\n ")
79
- puts
80
- end
87
+ if summary.total.zero?
88
+ @output.puts 'No tests were run.'
89
+ else
90
+ @output.puts tally_for(summary)
81
91
  end
92
+ end
82
93
 
83
- puts tally
94
+ private
95
+
96
+ def emit(symbol)
97
+ @output.print(symbol)
98
+ @output.flush
99
+ end
100
+
101
+ def tally_for(summary)
102
+ items = [:pass, :error, :fail, :todo, :skip].filter_map do |status|
103
+ count = summary.counts[status]
104
+ next if count.zero?
105
+ title = TITLES[status].downcase
106
+ percentage = @verbose ? " (%.1f%%)" % (count.to_f / summary.total * 100) : ''
107
+ "#{count.to_s.ansi(:bold)} #{title}#{percentage}"
108
+ end
109
+ "Executed #{summary.total.to_s.ansi(:bold)} tests with #{items.join(', ')}."
84
110
  end
85
111
 
86
112
  end
@@ -0,0 +1,44 @@
1
+ require_relative 'dotprogress'
2
+
3
+ module Test::Reporters
4
+ # Nest case headings and show each result at its place in the suite.
5
+ class Outline < Dotprogress
6
+ SYMBOLS = {
7
+ pass: '.', fail: 'F', error: 'E', todo: 'P', skip: 'S'
8
+ }.freeze
9
+
10
+ def begin_suite(suite)
11
+ @depth = 0
12
+ end
13
+
14
+ def begin_case(test_case)
15
+ @output.puts "#{' ' * @depth}#{test_case.to_s.lines.first.to_s.strip}"
16
+ @depth += 1
17
+ end
18
+
19
+ def end_case(test_case)
20
+ @depth -= 1
21
+ end
22
+
23
+ def record(result)
24
+ depth = result.kind == :case && @depth.positive? ? @depth - 1 : @depth
25
+ indent = ' ' * depth
26
+ @output.puts "#{indent}#{SYMBOLS.fetch(result.status)} #{result.test.to_s.lines.first.to_s.strip}"
27
+ if [:fail, :error].include?(result.status)
28
+ result.exceptions.each do |exception|
29
+ @output.puts "#{indent} #{exception.class}: #{exception.message}"
30
+ location = file_and_line(exception)
31
+ @output.puts "#{indent} #{location}" unless location.empty?
32
+ end
33
+ end
34
+ end
35
+
36
+ def end_suite(suite)
37
+ summary = @summary
38
+ @output.puts
39
+ rate = summary.elapsed.zero? ? 0.0 : summary.total / summary.elapsed
40
+ @output.puts "Finished in %.5fs, %.2f tests/s." % [summary.elapsed, rate]
41
+ @output.puts summary.total.zero? ? 'No tests were run.' : tally_for(summary)
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,35 @@
1
+ require_relative 'dotprogress'
2
+
3
+ module Test::Reporters
4
+ # Show completed results and timing without enumerating the suite in advance.
5
+ class Progress < Dotprogress
6
+ SYMBOLS = {
7
+ pass: '.', fail: 'F', error: 'E', todo: 'P', skip: 'S'
8
+ }.freeze
9
+
10
+ def begin_suite(suite)
11
+ @completed = 0
12
+ @started_at = Process.clock_gettime(Process::CLOCK_MONOTONIC)
13
+ @depth = 0
14
+ end
15
+
16
+ def begin_case(test_case)
17
+ @output.puts "#{' ' * @depth}#{test_case.to_s.lines.first.to_s.strip}"
18
+ @depth += 1
19
+ end
20
+
21
+ def end_case(test_case)
22
+ @depth -= 1
23
+ end
24
+
25
+ def record(result)
26
+ @completed += 1
27
+ elapsed = Process.clock_gettime(Process::CLOCK_MONOTONIC) - @started_at
28
+ depth = result.kind == :case && @depth.positive? ? @depth - 1 : @depth
29
+ label = "#{' ' * depth}#{result.test.to_s.lines.first.to_s.strip}"
30
+ @output.puts "%4d %8.3fs %8.5fs %s %s" % [
31
+ @completed, elapsed, result.elapsed, SYMBOLS.fetch(result.status), label
32
+ ]
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,31 @@
1
+ require_relative 'dotprogress'
2
+
3
+ module Test::Reporters
4
+ # One status line per result, followed by failure details and totals.
5
+ class Summary < Dotprogress
6
+ LABELS = {
7
+ pass: 'PASS', fail: 'FAIL', error: 'ERROR',
8
+ todo: 'TODO', skip: 'SKIP'
9
+ }.freeze
10
+
11
+ def begin_suite(suite)
12
+ @case_path = []
13
+ end
14
+
15
+ def begin_case(test_case)
16
+ @case_path << test_case.to_s.lines.first.to_s.strip
17
+ end
18
+
19
+ def end_case(test_case)
20
+ @case_path.pop
21
+ end
22
+
23
+ def record(result)
24
+ label = result.test.to_s.lines.first.to_s.strip
25
+ path = @case_path.dup
26
+ path.pop if result.kind == :case && path.last == label
27
+ path << label unless label.empty?
28
+ @output.puts "#{LABELS.fetch(result.status).ljust(5)} #{path.join(' ')}"
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,54 @@
1
+ module Test::Reporters
2
+ # TAP version 13 stream. The plan follows execution so case outcomes count.
3
+ class Tap < Abstract
4
+ def initialize(runner, output: $stdout)
5
+ super(runner)
6
+ @output = output
7
+ end
8
+
9
+ def capture_output?
10
+ true
11
+ end
12
+
13
+ def begin_suite(suite)
14
+ @index = 0
15
+ @output.puts 'TAP version 13'
16
+ end
17
+
18
+ def record(result)
19
+ @index += 1
20
+ label = single_line(result.test.to_s)
21
+ line = "#{[:pass, :skip].include?(result.status) ? 'ok' : 'not ok'} #{@index} - #{label}"
22
+ case result.status
23
+ when :skip
24
+ line << " # SKIP #{single_line(result.reason)}"
25
+ when :todo
26
+ line << " # TODO #{single_line(result.exception&.message)}"
27
+ end
28
+ @output.puts line.rstrip
29
+
30
+ if [:fail, :error].include?(result.status)
31
+ result.exceptions.each do |exception|
32
+ diagnostic("#{exception.class}: #{exception.message}")
33
+ diagnostic(exception.backtrace&.first) if exception.backtrace&.first
34
+ end
35
+ end
36
+ result.stdout.each_line { |line| diagnostic("stdout: #{line}") }
37
+ result.stderr.each_line { |line| diagnostic("stderr: #{line}") }
38
+ end
39
+
40
+ def finish(summary)
41
+ @output.puts summary.total.zero? ? '1..0 # SKIP No tests were run' : "1..#{summary.total}"
42
+ end
43
+
44
+ private
45
+
46
+ def single_line(value)
47
+ value.to_s.gsub(/\s+/, ' ').strip.gsub('#', '#')
48
+ end
49
+
50
+ def diagnostic(message)
51
+ message.to_s.each_line { |line| @output.puts "# #{line.chomp}" }
52
+ end
53
+ end
54
+ end