rubytest 0.8.1 → 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.
@@ -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
@@ -0,0 +1,76 @@
1
+ # encoding: UTF-8
2
+
3
+ module Test::Reporters
4
+
5
+ # TAP-J Reporter
6
+ #
7
+ # Writes the run as a TAP-J stream (revision 5): a line of JSON for
8
+ # the suite, each case, each test and the final tally.
9
+ #
10
+ class Tapj < AbstractHash
11
+
12
+ REVISION = 5
13
+
14
+ #
15
+ def initialize(runner, output: $stdout)
16
+ require 'json'
17
+ super(runner)
18
+ @output = output
19
+ end
20
+
21
+ #
22
+ def begin_suite(suite)
23
+ emit super(suite).merge('rev' => REVISION)
24
+ end
25
+
26
+ #
27
+ def begin_case(test_case)
28
+ emit super(test_case)
29
+ end
30
+
31
+ #
32
+ def pass(test)
33
+ emit super(test)
34
+ end
35
+
36
+ #
37
+ def fail(test, exception)
38
+ emit super(test, exception)
39
+ end
40
+
41
+ #
42
+ def error(test, exception)
43
+ emit super(test, exception)
44
+ end
45
+
46
+ #
47
+ def todo(test, exception)
48
+ emit super(test, exception)
49
+ end
50
+
51
+ #
52
+ def skip_test(test, reason=nil)
53
+ emit super(test, reason)
54
+ end
55
+
56
+ #
57
+ def skip_case(test_case, reason=nil)
58
+ emit super(test_case, reason)
59
+ end
60
+
61
+ #
62
+ def end_suite(suite)
63
+ emit super(suite)
64
+ end
65
+
66
+ private
67
+
68
+ # Write one record to the stream.
69
+ def emit(hash)
70
+ @output.puts hash.to_json
71
+ hash
72
+ end
73
+
74
+ end
75
+
76
+ end
@@ -0,0 +1,77 @@
1
+ # encoding: UTF-8
2
+
3
+ module Test::Reporters
4
+
5
+ # TAP-Y Reporter
6
+ #
7
+ # Writes the run as a TAP-Y stream (revision 5): a YAML document for
8
+ # the suite, each case, each test and the final tally.
9
+ #
10
+ class Tapy < AbstractHash
11
+
12
+ REVISION = 5
13
+
14
+ #
15
+ def initialize(runner, output: $stdout)
16
+ require 'yaml'
17
+ super(runner)
18
+ @output = output
19
+ end
20
+
21
+ #
22
+ def begin_suite(suite)
23
+ emit super(suite).merge('rev' => REVISION)
24
+ end
25
+
26
+ #
27
+ def begin_case(test_case)
28
+ emit super(test_case)
29
+ end
30
+
31
+ #
32
+ def pass(test)
33
+ emit super(test)
34
+ end
35
+
36
+ #
37
+ def fail(test, exception)
38
+ emit super(test, exception)
39
+ end
40
+
41
+ #
42
+ def error(test, exception)
43
+ emit super(test, exception)
44
+ end
45
+
46
+ #
47
+ def todo(test, exception)
48
+ emit super(test, exception)
49
+ end
50
+
51
+ #
52
+ def skip_test(test, reason=nil)
53
+ emit super(test, reason)
54
+ end
55
+
56
+ #
57
+ def skip_case(test_case, reason=nil)
58
+ emit super(test_case, reason)
59
+ end
60
+
61
+ #
62
+ def end_suite(suite)
63
+ emit super(suite)
64
+ @output.puts '...'
65
+ end
66
+
67
+ private
68
+
69
+ # Write one record to the stream.
70
+ def emit(hash)
71
+ @output.puts hash.to_yaml
72
+ hash
73
+ end
74
+
75
+ end
76
+
77
+ end
@@ -1,53 +1,60 @@
1
1
  module Test
2
-
3
- # Recorder class is an observer that tracks all tests
4
- # that are run and categorizes them according to their
5
- # test status.
2
+ # Owns run results and controls when they reach the reporter.
6
3
  class Recorder
4
+ attr_reader :results, :reporter, :summary
7
5
 
8
- def initialize
9
- @table = Hash.new{ |h,k| h[k] = [] }
6
+ def initialize(reporter = nil)
7
+ @reporter = reporter
8
+ @results = []
10
9
  end
11
10
 
12
- def [](key)
13
- @table[key.to_sym]
11
+ def begin_suite(suite)
12
+ @suite = suite
13
+ @started_at = Process.clock_gettime(Process::CLOCK_MONOTONIC)
14
+ reporter.begin_suite(suite) if reporter&.respond_to?(:begin_suite)
14
15
  end
15
16
 
16
- #
17
- def skip_test(test, reason)
18
- self[:skip] << [test, reason]
17
+ def begin_case(test_case)
18
+ reporter.begin_case(test_case) if reporter&.respond_to?(:begin_case)
19
19
  end
20
20
 
21
- # Add `test` to pass set.
22
- def pass(test)
23
- self[:pass] << test
21
+ def begin_test(test)
22
+ reporter.begin_test(test) if reporter&.respond_to?(:begin_test)
24
23
  end
25
24
 
26
- def fail(test, exception)
27
- self[:fail] << [test, exception]
25
+ def capture_output?
26
+ !!(reporter && reporter.respond_to?(:capture_output?) && reporter.capture_output?)
28
27
  end
29
28
 
30
- def error(test, exception)
31
- self[:error] << [test, exception]
29
+ def record(result)
30
+ @results << result
31
+ reporter.record(result) if reporter
32
+ result
32
33
  end
33
34
 
34
- def todo(test, exception)
35
- self[:todo] << [test, exception]
35
+ def end_test(test)
36
+ reporter.end_test(test) if reporter&.respond_to?(:end_test)
36
37
  end
37
38
 
38
- #def omit(test, exception)
39
- # self[:omit] << [test, exception]
40
- #end
39
+ def end_case(test_case)
40
+ reporter.end_case(test_case) if reporter&.respond_to?(:end_case)
41
+ end
41
42
 
42
- # Returns true if their are no test errors or failures.
43
- def success?
44
- self[:error].size + self[:fail].size > 0 ? false : true
43
+ def end_suite(suite)
44
+ @summary = RunSummary.new(suite: suite, results: results, elapsed: elapsed)
45
+ reporter.finish(@summary) if reporter
46
+ @summary
45
47
  end
46
48
 
47
- # Ignore any other signals.
48
- def method_missing(*a)
49
+ def success?
50
+ (@summary || RunSummary.new(suite: @suite, results: results, elapsed: elapsed)).success?
49
51
  end
50
52
 
51
- end
53
+ private
52
54
 
55
+ def elapsed
56
+ return 0.0 unless @started_at
57
+ Process.clock_gettime(Process::CLOCK_MONOTONIC) - @started_at
58
+ end
59
+ end
53
60
  end
@@ -0,0 +1,54 @@
1
+ module Test
2
+ # The outcome of one test, or of a case that was skipped or raised an error.
3
+ class Result
4
+ STATUSES = [:pass, :fail, :error, :todo, :skip].freeze
5
+ KINDS = [:test, :case].freeze
6
+
7
+ attr_reader :test, :kind, :status, :exception, :exceptions, :reason, :elapsed, :stdout, :stderr
8
+
9
+ def initialize(test:, status:, kind: :test, exception: nil, reason: nil,
10
+ elapsed: 0.0, stdout: '', stderr: '', exceptions: nil)
11
+ raise ArgumentError, "unknown result status: #{status.inspect}" unless STATUSES.include?(status)
12
+ raise ArgumentError, "unknown result kind: #{kind.inspect}" unless KINDS.include?(kind)
13
+
14
+ @test = test
15
+ @kind = kind
16
+ @status = status
17
+ @exception = exception
18
+ @exceptions = (exceptions || (exception ? [exception] : [])).dup.freeze
19
+ @reason = reason
20
+ @elapsed = elapsed
21
+ @stdout = stdout.to_s.dup.freeze
22
+ @stderr = stderr.to_s.dup.freeze
23
+ freeze
24
+ end
25
+ end
26
+
27
+ # A snapshot of everything recorded during one run.
28
+ class RunSummary
29
+ attr_reader :suite, :results, :counts, :elapsed
30
+
31
+ def initialize(suite:, results:, elapsed:)
32
+ @suite = suite
33
+ @results = results.dup.freeze
34
+ @elapsed = elapsed
35
+ @counts = Result::STATUSES.each_with_object({}) do |status, counts|
36
+ counts[status] = @results.count { |result| result.status == status }
37
+ end.freeze
38
+ freeze
39
+ end
40
+
41
+ def total
42
+ results.size
43
+ end
44
+
45
+ def results_for(status)
46
+ results.select { |result| result.status == status }
47
+ end
48
+
49
+ def success?
50
+ counts[:fail].zero? && counts[:error].zero? &&
51
+ (counts[:pass] + counts[:todo] + counts[:skip]).positive?
52
+ end
53
+ end
54
+ end