Class: MiniSpec::Reporter

Inherits:
Object
  • Object
show all
Defined in:
lib/minispec/reporter.rb

Constant Summary collapse

@@indent =
" ".freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(stdout = STDOUT) ⇒ Reporter

Returns a new instance of Reporter.



7
8
9
10
# File 'lib/minispec/reporter.rb', line 7

def initialize stdout = STDOUT
  @stdout = stdout
  @failed_specs, @failed_tests, @skipped_tests = [], {}, {}
end

Instance Attribute Details

#failed_specsObject (readonly)

Returns the value of attribute failed_specs.



5
6
7
# File 'lib/minispec/reporter.rb', line 5

def failed_specs
  @failed_specs
end

#failed_testsObject (readonly)

Returns the value of attribute failed_tests.



5
6
7
# File 'lib/minispec/reporter.rb', line 5

def failed_tests
  @failed_tests
end

#skipped_testsObject (readonly)

Returns the value of attribute skipped_tests.



5
6
7
# File 'lib/minispec/reporter.rb', line 5

def skipped_tests
  @skipped_tests
end

Instance Method Details

#exception_details(spec, test, exception) ⇒ Object



100
101
102
103
104
105
# File 'lib/minispec/reporter.rb', line 100

def exception_details spec, test, exception
  puts(info([spec, test]*' / '))
  puts(error(exception.message), indent: 2)
  exception.backtrace.each {|l| puts(info(MiniSpec::Utils.shorten_source(l)), indent: 2)}
  puts('---', '')
end

#failure_details(spec, test, failure) ⇒ Object



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
# File 'lib/minispec/reporter.rb', line 107

def failure_details spec, test, failure
  puts(info([spec, test]*' / '))
  puts(error(callerline(failure[:callers][0])), indent: 2)
  callers(failure[:callers]).each {|l| puts(info(l), indent: 2)}
  puts
  return puts(*failure[:message].split("\n"), '', indent: 2) if failure[:message]
  return if failure[:right_object] == :__ms__right_object

  expected, actual = [:right_object, :left_object].map do |obj|
    str = stringify_object(failure[obj])
    [str =~ /\n/ ? :puts : :print, str]
  end

  send(expected.first, info('           Expected: '))
  print('NOT ') if failure[:negation]
  puts(expected.last)

  send(actual.first, info('             Actual: '))
  puts(actual.last)

  print(info('     Compared using: '))
  puts(failure[:right_method])

  diff = diff(actual.last, expected.last)
  puts(info('               Diff: '), diff) unless diff.empty?
  puts('---', '')
end

#failures?Boolean

Returns:

  • (Boolean)


161
162
163
# File 'lib/minispec/reporter.rb', line 161

def failures?
  @failed_specs.any? || @failed_tests.any?
end

#mark_as_failed(spec, test, verb, proc, failures) ⇒ Object



144
145
146
147
# File 'lib/minispec/reporter.rb', line 144

def mark_as_failed spec, test, verb, proc, failures
  puts(error("FAILED"))
  (@failed_tests[spec] ||= []).push([test, verb, proc, failures])
end

#mark_as_passed(spec, test) ⇒ Object



135
136
137
# File 'lib/minispec/reporter.rb', line 135

def mark_as_passed spec, test
  puts(success("OK"))
end

#mark_as_skipped(spec, test, source_location) ⇒ Object



139
140
141
142
# File 'lib/minispec/reporter.rb', line 139

def mark_as_skipped spec, test, source_location
  puts(warn("Skipped"))
  (@skipped_tests[spec] ||= []).push([test, source_location])
end


149
# File 'lib/minispec/reporter.rb', line 149

def print(*args); @stdout.print(*indent_lines(*args)) end

#puts(*args) ⇒ Object



150
# File 'lib/minispec/reporter.rb', line 150

def puts(*args);  @stdout.puts(*indent_lines(*args))  end

#summaryObject



12
13
14
15
16
17
# File 'lib/minispec/reporter.rb', line 12

def summary
  summary__failed_specs
  summary__failed_tests
  summary__skipped_tests
  totals
end

#summary__failed_specsObject



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/minispec/reporter.rb', line 19

def summary__failed_specs
  return if @failed_specs.empty?
  puts
  puts(error('--- Failed Specs ---'))
  last_ex = nil
  @failed_specs.each do |(spec,proc,ex)|
    puts(info(spec))
    puts(info('defined at ' + proc.source_location.join(':')), indent: 2) if proc.is_a?(Proc)
    if last_ex && ex.backtrace == last_ex.backtrace
      puts('see exception above', indent: 2)
      next
    end
    last_ex = ex
    puts(error(ex.message), indent: 2)
    ex.backtrace.each {|l| puts(l, indent: 2)}
    puts
  end
end

#summary__failed_testsObject



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/minispec/reporter.rb', line 53

def summary__failed_tests
  return if @failed_tests.empty?
  puts
  puts(error('--- Failed Tests ---'), '')
  @failed_tests.each_pair do |spec, failures|
    @failed_specs.push(spec) # to be used on #totals__specs
    failures.each do |(test,verb,proc,errors)|
      errors.each do |error|
        error.is_a?(Exception) ?
          exception_details(spec, test, error) :
          failure_details(spec, test, error)
      end
    end
  end
end

#summary__skipped_testsObject



38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/minispec/reporter.rb', line 38

def summary__skipped_tests
  return if @skipped_tests.empty?
  puts
  puts(warn('--- Skipped Tests ---'))
  @skipped_tests.each_pair do |spec,tests|
    puts(info(spec))
    tests.each do |(test,source_location)|
      puts(warn(test), indent: 2)
      puts(info(MiniSpec::Utils.shorten_source(source_location)), indent: 2)
      puts
    end
    puts
  end
end

#totalsObject



69
70
71
72
73
74
75
# File 'lib/minispec/reporter.rb', line 69

def totals
  puts
  puts('---')
  totals__specs
  totals__tests
  totals__assertions
end

#totals__assertionsObject



93
94
95
96
97
98
# File 'lib/minispec/reporter.rb', line 93

def totals__assertions
  print(info('  Assertions: '))
  return puts(success(Minispec.assertions)) if @failed_tests.empty?
  print(info(Minispec.assertions))
  puts(error('  (%s failed)' % @failed_tests.values.map(&:size).reduce(:+)))
end

#totals__specsObject



77
78
79
80
81
82
# File 'lib/minispec/reporter.rb', line 77

def totals__specs
  print(info('       Specs: '))
  return puts(success(Minispec.specs.size)) if @failed_specs.empty?
  print(info(Minispec.specs.size))
  puts(error('  (%s failed)' % @failed_specs.size))
end

#totals__testsObject



84
85
86
87
88
89
90
91
# File 'lib/minispec/reporter.rb', line 84

def totals__tests
  print(info('       Tests: '))
  print(send(@failed_tests.any? ? :info : :success, Minispec.tests))
  failed  = error('  (%s failed)' % @failed_tests.values.map(&:size).reduce(:+)) if @failed_tests.any?
  skipped = warn('  (%s skipped)' % @skipped_tests.size) if @skipped_tests.any?
  report  = [failed, skipped].compact.join(', ')
  puts(report)
end