aboutsummaryrefslogtreecommitdiffstats
path: root/spec/mspec/lib/mspec/runner/actions/tally.rb
blob: 33f937293c1a5a4ba0dc2b8d80a852eacbd94147 (plain)
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
class Tally
  attr_accessor :files, :examples, :expectations, :failures, :errors, :guards, :tagged

  def initialize
    @files = @examples = @expectations = @failures = @errors = @guards = @tagged = 0
  end

  def files!(add=1)
    @files += add
  end

  def examples!(add=1)
    @examples += add
  end

  def expectations!(add=1)
    @expectations += add
  end

  def failures!(add=1)
    @failures += add
  end

  def errors!(add=1)
    @errors += add
  end

  def guards!(add=1)
    @guards += add
  end

  def tagged!(add=1)
    @tagged += add
  end

  def file
    pluralize files, "file"
  end

  def example
    pluralize examples, "example"
  end

  def expectation
    pluralize expectations, "expectation"
  end

  def failure
    pluralize failures, "failure"
  end

  def error
    pluralize errors, "error"
  end

  def guard
    pluralize guards, "guard"
  end

  def tag
    "#{tagged} tagged"
  end

  def format
    results = [ file, example, expectation, failure, error, tag ]
    if [:report, :report_on, :verify].any? { |m| MSpec.mode? m }
      results << guard
    end
    results.join(", ")
  end

  alias_method :to_s, :format

  def pluralize(count, singular)
    "#{count} #{singular}#{'s' unless count == 1}"
  end
  private :pluralize
end

class TallyAction
  attr_reader :counter

  def initialize
    @counter = Tally.new
  end

  def register
    MSpec.register :load,        self
    MSpec.register :exception,   self
    MSpec.register :example,     self
    MSpec.register :tagged,      self
    MSpec.register :expectation, self
  end

  def unregister
    MSpec.unregister :load,        self
    MSpec.unregister :exception,   self
    MSpec.unregister :example,     self
    MSpec.unregister :tagged,      self
    MSpec.unregister :expectation, self
  end

  def load
    @counter.files!
  end

  # Callback for the MSpec :expectation event. Increments the
  # tally of expectations (e.g. #should, #should_receive, etc.).
  def expectation(state)
    @counter.expectations!
  end

  # Callback for the MSpec :exception event. Increments the
  # tally of errors and failures.
  def exception(exception)
    exception.failure? ? @counter.failures! : @counter.errors!
  end

  # Callback for the MSpec :example event. Increments the tally
  # of examples.
  def example(state, block)
    @counter.examples!
  end

  def tagged(state)
    @counter.examples!
    @counter.tagged!
  end

  def format
    @counter.format
  end
end