summaryrefslogtreecommitdiff
path: root/spec/mspec/spec/runner/formatters/dotted_spec.rb
blob: 1e9b06f6e1f32011ebee59336da7d7640d286942 (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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
require File.dirname(__FILE__) + '/../../spec_helper'
require 'mspec/runner/formatters/dotted'
require 'mspec/runner/mspec'
require 'mspec/runner/example'
require 'mspec/utils/script'

describe DottedFormatter, "#initialize" do
  it "permits zero arguments" do
    DottedFormatter.new
  end

  it "accepts one argument" do
    DottedFormatter.new nil
  end
end

describe DottedFormatter, "#register" do
  before :each do
    @formatter = DottedFormatter.new
    MSpec.stub(:register)
  end

  it "registers self with MSpec for appropriate actions" do
    MSpec.should_receive(:register).with(:exception, @formatter)
    MSpec.should_receive(:register).with(:before, @formatter)
    MSpec.should_receive(:register).with(:after, @formatter)
    MSpec.should_receive(:register).with(:finish, @formatter)
    @formatter.register
  end

  it "creates TimerAction and TallyAction" do
    timer = double("timer")
    tally = double("tally")
    timer.should_receive(:register)
    tally.should_receive(:register)
    tally.should_receive(:counter)
    TimerAction.should_receive(:new).and_return(timer)
    TallyAction.should_receive(:new).and_return(tally)
    @formatter.register
  end
end

describe DottedFormatter, "#print" do
  before :each do
    $stdout = IOStub.new
  end

  after :each do
    $stdout = STDOUT
  end

  it "writes to $stdout by default" do
    formatter = DottedFormatter.new
    formatter.print "begonias"
    $stdout.should == "begonias"
  end

  it "writes to the file specified when the formatter was created" do
    out = IOStub.new
    File.should_receive(:open).with("some/file", "w").and_return(out)
    formatter = DottedFormatter.new "some/file"
    formatter.print "begonias"
    out.should == "begonias"
  end

  it "flushes the IO output" do
    $stdout.should_receive(:flush)
    formatter = DottedFormatter.new
    formatter.print "begonias"
  end
end

describe DottedFormatter, "#exception" do
  before :each do
    @formatter = DottedFormatter.new
    @failure = ExceptionState.new nil, nil, SpecExpectationNotMetError.new("failed")
    @error = ExceptionState.new nil, nil, MSpecExampleError.new("boom!")
  end

  it "sets the #failure? flag" do
    @formatter.exception @failure
    @formatter.failure?.should be_true
    @formatter.exception @error
    @formatter.failure?.should be_false
  end

  it "sets the #exception? flag" do
    @formatter.exception @error
    @formatter.exception?.should be_true
    @formatter.exception @failure
    @formatter.exception?.should be_true
  end

  it "addes the exception to the list of exceptions" do
    @formatter.exceptions.should == []
    @formatter.exception @error
    @formatter.exception @failure
    @formatter.exceptions.should == [@error, @failure]
  end
end

describe DottedFormatter, "#exception?" do
  before :each do
    @formatter = DottedFormatter.new
    @failure = ExceptionState.new nil, nil, SpecExpectationNotMetError.new("failed")
    @error = ExceptionState.new nil, nil, MSpecExampleError.new("boom!")
  end

  it "returns false if there have been no exceptions" do
    @formatter.exception?.should be_false
  end

  it "returns true if any exceptions are errors" do
    @formatter.exception @failure
    @formatter.exception @error
    @formatter.exception?.should be_true
  end

  it "returns true if all exceptions are failures" do
    @formatter.exception @failure
    @formatter.exception @failure
    @formatter.exception?.should be_true
  end

  it "returns true if all exceptions are errors" do
    @formatter.exception @error
    @formatter.exception @error
    @formatter.exception?.should be_true
  end
end

describe DottedFormatter, "#failure?" do
  before :each do
    @formatter = DottedFormatter.new
    @failure = ExceptionState.new nil, nil, SpecExpectationNotMetError.new("failed")
    @error = ExceptionState.new nil, nil, MSpecExampleError.new("boom!")
  end

  it "returns false if there have been no exceptions" do
    @formatter.failure?.should be_false
  end

  it "returns false if any exceptions are errors" do
    @formatter.exception @failure
    @formatter.exception @error
    @formatter.failure?.should be_false
  end

  it "returns true if all exceptions are failures" do
    @formatter.exception @failure
    @formatter.exception @failure
    @formatter.failure?.should be_true
  end
end

describe DottedFormatter, "#before" do
  before :each do
    @state = ExampleState.new ContextState.new("describe"), "it"
    @formatter = DottedFormatter.new
    @formatter.exception ExceptionState.new(nil, nil, SpecExpectationNotMetError.new("Failed!"))
  end

  it "resets the #failure? flag to false" do
    @formatter.failure?.should be_true
    @formatter.before @state
    @formatter.failure?.should be_false
  end

  it "resets the #exception? flag to false" do
    @formatter.exception?.should be_true
    @formatter.before @state
    @formatter.exception?.should be_false
  end
end

describe DottedFormatter, "#after" do
  before :each do
    $stdout = @out = IOStub.new
    @formatter = DottedFormatter.new
    @state = ExampleState.new ContextState.new("describe"), "it"
  end

  after :each do
    $stdout = STDOUT
  end

  it "prints a '.' if there was no exception raised" do
    @formatter.after(@state)
    @out.should == "."
  end

  it "prints an 'F' if there was an expectation failure" do
    exc = SpecExpectationNotMetError.new "failed"
    @formatter.exception ExceptionState.new(@state, nil, exc)
    @formatter.after(@state)
    @out.should == "F"
  end

  it "prints an 'E' if there was an exception other than expectation failure" do
    exc = MSpecExampleError.new("boom!")
    @formatter.exception ExceptionState.new(@state, nil, exc)
    @formatter.after(@state)
    @out.should == "E"
  end

  it "prints an 'E' if there are mixed exceptions and exepctation failures" do
    exc = SpecExpectationNotMetError.new "failed"
    @formatter.exception ExceptionState.new(@state, nil, exc)
    exc = MSpecExampleError.new("boom!")
    @formatter.exception ExceptionState.new(@state, nil, exc)
    @formatter.after(@state)
    @out.should == "E"
  end
end

describe DottedFormatter, "#finish" do
  before :each do
    @tally = double("tally").as_null_object
    TallyAction.stub(:new).and_return(@tally)
    @timer = double("timer").as_null_object
    TimerAction.stub(:new).and_return(@timer)

    $stdout = @out = IOStub.new
    context = ContextState.new "Class#method"
    @state = ExampleState.new(context, "runs")
    MSpec.stub(:register)
    @formatter = DottedFormatter.new
    @formatter.register
  end

  after :each do
    $stdout = STDOUT
  end

  it "prints a failure message for an exception" do
    exc = ExceptionState.new @state, nil, MSpecExampleError.new("broken")
    @formatter.exception exc
    @formatter.after @state
    @formatter.finish
    @out.should =~ /^1\)\nClass#method runs ERROR$/
  end

  it "prints a backtrace for an exception" do
    exc = ExceptionState.new @state, nil, MSpecExampleError.new("broken")
    exc.stub(:backtrace).and_return("path/to/some/file.rb:35:in method")
    @formatter.exception exc
    @formatter.after @state
    @formatter.finish
    @out.should =~ %r[path/to/some/file.rb:35:in method$]
  end

  it "prints a summary of elapsed time" do
    @timer.should_receive(:format).and_return("Finished in 2.0 seconds")
    @formatter.finish
    @out.should =~ /^Finished in 2.0 seconds$/
  end

  it "prints a tally of counts" do
    @tally.should_receive(:format).and_return("1 example, 0 failures")
    @formatter.finish
    @out.should =~ /^1 example, 0 failures$/
  end

  it "prints errors, backtraces, elapsed time, and tallies" do
    exc = ExceptionState.new @state, nil, MSpecExampleError.new("broken")
    exc.stub(:backtrace).and_return("path/to/some/file.rb:35:in method")
    @formatter.exception exc
    @timer.should_receive(:format).and_return("Finished in 2.0 seconds")
    @tally.should_receive(:format).and_return("1 example, 1 failure")
    @formatter.after @state
    @formatter.finish
    @out.should ==
%[E

1)
Class#method runs ERROR
MSpecExampleError: broken
path/to/some/file.rb:35:in method

Finished in 2.0 seconds

1 example, 1 failure
]
  end
end