1 require File.dirname(__FILE__) + '/../../spec_helper'
2 require 'mspec/runner/formatters/dotted'
3 require 'mspec/runner/mspec'
4 require 'mspec/runner/example'
6 describe DottedFormatter, "#initialize" do
7 it "permits zero arguments" do
11 it "accepts one argument" do
12 DottedFormatter.new nil
16 describe DottedFormatter, "#register" do
18 @formatter = DottedFormatter.new
21 it "registers self with MSpec for appropriate actions" do
22 MSpec.stub!(:register)
23 MSpec.should_receive(:register).with(:exception, @formatter)
24 MSpec.should_receive(:register).with(:before, @formatter)
25 MSpec.should_receive(:register).with(:after, @formatter)
26 MSpec.should_receive(:register).with(:finish, @formatter)
30 it "creates TimerAction and TallyAction" do
33 timer.should_receive(:register)
34 tally.should_receive(:register)
35 tally.should_receive(:counter)
36 TimerAction.should_receive(:new).and_return(timer)
37 TallyAction.should_receive(:new).and_return(tally)
42 describe DottedFormatter, "#print" do
47 it "writes to $stdout by default" do
49 formatter = DottedFormatter.new
50 formatter.print "begonias"
51 $stdout.should == "begonias"
54 it "writes to the file specified when the formatter was created" do
56 File.should_receive(:open).with("some/file", "w").and_return(out)
57 formatter = DottedFormatter.new "some/file"
58 formatter.print "begonias"
59 out.should == "begonias"
63 describe DottedFormatter, "#exception" do
65 @formatter = DottedFormatter.new
66 @failure = ExceptionState.new nil, nil, ExpectationNotMetError.new("failed")
67 @error = ExceptionState.new nil, nil, MSpecExampleError.new("boom!")
70 it "sets the #failure? flag" do
71 @formatter.exception @failure
72 @formatter.failure?.should be_true
73 @formatter.exception @error
74 @formatter.failure?.should be_false
77 it "sets the #exception? flag" do
78 @formatter.exception @error
79 @formatter.exception?.should be_true
80 @formatter.exception @failure
81 @formatter.exception?.should be_true
84 it "addes the exception to the list of exceptions" do
85 @formatter.exceptions.should == []
86 @formatter.exception @error
87 @formatter.exception @failure
88 @formatter.exceptions.should == [@error, @failure]
92 describe DottedFormatter, "#exception?" do
94 @formatter = DottedFormatter.new
95 @failure = ExceptionState.new nil, nil, ExpectationNotMetError.new("failed")
96 @error = ExceptionState.new nil, nil, MSpecExampleError.new("boom!")
99 it "returns false if there have been no exceptions" do
100 @formatter.exception?.should be_false
103 it "returns true if any exceptions are errors" do
104 @formatter.exception @failure
105 @formatter.exception @error
106 @formatter.exception?.should be_true
109 it "returns true if all exceptions are failures" do
110 @formatter.exception @failure
111 @formatter.exception @failure
112 @formatter.exception?.should be_true
115 it "returns true if all exceptions are errors" do
116 @formatter.exception @error
117 @formatter.exception @error
118 @formatter.exception?.should be_true
122 describe DottedFormatter, "#failure?" do
124 @formatter = DottedFormatter.new
125 @failure = ExceptionState.new nil, nil, ExpectationNotMetError.new("failed")
126 @error = ExceptionState.new nil, nil, MSpecExampleError.new("boom!")
129 it "returns false if there have been no exceptions" do
130 @formatter.failure?.should be_false
133 it "returns false if any exceptions are errors" do
134 @formatter.exception @failure
135 @formatter.exception @error
136 @formatter.failure?.should be_false
139 it "returns true if all exceptions are failures" do
140 @formatter.exception @failure
141 @formatter.exception @failure
142 @formatter.failure?.should be_true
146 describe DottedFormatter, "#before" do
148 @state = ExampleState.new ContextState.new("describe"), "it"
149 @formatter = DottedFormatter.new
150 @formatter.exception ExceptionState.new(nil, nil, ExpectationNotMetError.new("Failed!"))
153 it "resets the #failure? flag to false" do
154 @formatter.failure?.should be_true
155 @formatter.before @state
156 @formatter.failure?.should be_false
159 it "resets the #exception? flag to false" do
160 @formatter.exception?.should be_true
161 @formatter.before @state
162 @formatter.exception?.should be_false
166 describe DottedFormatter, "#after" do
168 $stdout = @out = IOStub.new
169 @formatter = DottedFormatter.new
170 @state = ExampleState.new ContextState.new("describe"), "it"
177 it "prints a '.' if there was no exception raised" do
178 @formatter.after(@state)
182 it "prints an 'F' if there was an expectation failure" do
183 exc = ExpectationNotMetError.new "failed"
184 @formatter.exception ExceptionState.new(@state, nil, exc)
185 @formatter.after(@state)
189 it "prints an 'E' if there was an exception other than expectation failure" do
190 exc = MSpecExampleError.new("boom!")
191 @formatter.exception ExceptionState.new(@state, nil, exc)
192 @formatter.after(@state)
196 it "prints an 'E' if there are mixed exceptions and exepctation failures" do
197 exc = ExpectationNotMetError.new "failed"
198 @formatter.exception ExceptionState.new(@state, nil, exc)
199 exc = MSpecExampleError.new("boom!")
200 @formatter.exception ExceptionState.new(@state, nil, exc)
201 @formatter.after(@state)
206 describe DottedFormatter, "#finish" do
208 @tally = mock("tally", :null_object => true)
209 TallyAction.stub!(:new).and_return(@tally)
210 @timer = mock("timer", :null_object => true)
211 TimerAction.stub!(:new).and_return(@timer)
213 $stdout = @out = IOStub.new
214 context = ContextState.new "Class#method"
215 @state = ExampleState.new(context, "runs")
216 MSpec.stub!(:register)
217 @formatter = DottedFormatter.new
225 it "prints a failure message for an exception" do
226 exc = ExceptionState.new @state, nil, MSpecExampleError.new("broken")
227 @formatter.exception exc
228 @formatter.after @state
230 @out.should =~ /^1\)\nClass#method runs ERROR$/
233 it "prints a backtrace for an exception" do
234 exc = ExceptionState.new @state, nil, MSpecExampleError.new("broken")
235 exc.stub!(:backtrace).and_return("path/to/some/file.rb:35:in method")
236 @formatter.exception exc
237 @formatter.after @state
239 @out.should =~ %r[path/to/some/file.rb:35:in method$]
242 it "prints a summary of elapsed time" do
243 @timer.should_receive(:format).and_return("Finished in 2.0 seconds")
245 @out.should =~ /^Finished in 2.0 seconds$/
248 it "prints a tally of counts" do
249 @tally.should_receive(:format).and_return("1 example, 0 failures")
251 @out.should =~ /^1 example, 0 failures$/
254 it "prints errors, backtraces, elapsed time, and tallies" do
255 exc = ExceptionState.new @state, nil, MSpecExampleError.new("broken")
256 exc.stub!(:backtrace).and_return("path/to/some/file.rb:35:in method")
257 @formatter.exception exc
258 @timer.should_receive(:format).and_return("Finished in 2.0 seconds")
259 @tally.should_receive(:format).and_return("1 example, 1 failure")
260 @formatter.after @state
266 Class#method runs ERROR
267 MSpecExampleError: broken
268 path/to/some/file.rb:35:in method
270 Finished in 2.0 seconds