mspec 1.1.1 → 1.2.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.
@@ -0,0 +1,96 @@
1
+ require File.dirname(__FILE__) + '/../spec_helper'
2
+ require 'mspec/matchers/base'
3
+ require 'mspec/runner/mspec'
4
+ require 'mspec/mocks/mock'
5
+ require 'mspec/runner/example'
6
+
7
+ describe ExampleState do
8
+ it "is initialized with the describe and it strings" do
9
+ ExampleState.new("This", "does").should be_kind_of(ExampleState)
10
+ end
11
+ end
12
+
13
+ describe ExampleState, "#describe" do
14
+ before :each do
15
+ @state = ExampleState.new("describe", "it")
16
+ end
17
+
18
+ it "returns the arguments to the #describe block stringified and concatenated" do
19
+ @state.describe.should == "describe"
20
+ end
21
+ end
22
+
23
+ describe ExampleState, "#it" do
24
+ before :each do
25
+ @state = ExampleState.new("describe", "it")
26
+ end
27
+
28
+ it "returns the argument to the #it block" do
29
+ @state.it.should == "it"
30
+ end
31
+ end
32
+
33
+ describe ExampleState, "#unfiltered?" do
34
+ before :each do
35
+ MSpec.store :include, nil
36
+ MSpec.store :exclude, nil
37
+
38
+ @state = ExampleState.new("describe", "it")
39
+ @filter = mock("filter")
40
+ end
41
+
42
+ it "returns true if MSpec include filters list is empty" do
43
+ @state.unfiltered?.should == true
44
+ end
45
+
46
+ it "returns true if MSpec include filters match this spec" do
47
+ @filter.should_receive(:===).and_return(true)
48
+ MSpec.register :include, @filter
49
+ @state.unfiltered?.should == true
50
+ end
51
+
52
+ it "returns false if MSpec include filters do not match this spec" do
53
+ @filter.should_receive(:===).and_return(false)
54
+ MSpec.register :include, @filter
55
+ @state.unfiltered?.should == false
56
+ end
57
+
58
+ it "returns true if MSpec exclude filters list is empty" do
59
+ @state.unfiltered?.should == true
60
+ end
61
+
62
+ it "returns true if MSpec exclude filters do not match this spec" do
63
+ @filter.should_receive(:===).and_return(false)
64
+ MSpec.register :exclude, @filter
65
+ @state.unfiltered?.should == true
66
+ end
67
+
68
+ it "returns false if MSpec exclude filters match this spec" do
69
+ @filter.should_receive(:===).and_return(true)
70
+ MSpec.register :exclude, @filter
71
+ @state.unfiltered?.should == false
72
+ end
73
+
74
+ it "returns false if MSpec include and exclude filters match this spec" do
75
+ @filter.should_receive(:===).twice.and_return(true)
76
+ MSpec.register :include, @filter
77
+ MSpec.register :exclude, @filter
78
+ @state.unfiltered?.should == false
79
+ end
80
+ end
81
+
82
+ describe ExampleState, "#filtered?" do
83
+ before :each do
84
+ @state = ExampleState.new("describe", "it")
85
+ end
86
+
87
+ it "returns true if #unfiltered returns false" do
88
+ @state.should_receive(:unfiltered?).and_return(false)
89
+ @state.filtered?.should == true
90
+ end
91
+
92
+ it "returns false if #unfiltered returns true" do
93
+ @state.should_receive(:unfiltered?).and_return(true)
94
+ @state.filtered?.should == false
95
+ end
96
+ end
@@ -0,0 +1,110 @@
1
+ require File.dirname(__FILE__) + '/../spec_helper'
2
+ require 'mspec/expectations/expectations'
3
+ require 'mspec/runner/example'
4
+ require 'mspec/runner/exception'
5
+
6
+ describe ExceptionState, "#initialize" do
7
+ it "takes a state, location (e.g. before :each), and exception" do
8
+ state = ExampleState.new "Class#method", "does something"
9
+ exc = Exception.new "Fail!"
10
+ ExceptionState.new(state, "location", exc).should be_kind_of(ExceptionState)
11
+ end
12
+ end
13
+
14
+ describe ExceptionState, "#description" do
15
+ before :each do
16
+ @state = ExampleState.new "Class#method", "does something"
17
+ end
18
+
19
+ it "returns the state description if state was not nil" do
20
+ exc = ExceptionState.new(@state, nil, nil)
21
+ exc.description.should == "Class#method does something"
22
+ end
23
+
24
+ it "returns the location if it is not nil and description is nil" do
25
+ exc = ExceptionState.new(nil, "location", nil)
26
+ exc.description.should == "An exception occurred during: location"
27
+ end
28
+
29
+ it "returns both description and location if neither are nil" do
30
+ exc = ExceptionState.new(@state, "location", nil)
31
+ exc.description.should == "An exception occurred during: location\nClass#method does something"
32
+ end
33
+ end
34
+
35
+ describe ExceptionState, "#describe" do
36
+ before :each do
37
+ @state = ExampleState.new "Class#method", "does something"
38
+ end
39
+
40
+ it "returns the ExampleState#describe string if created with a non-nil state" do
41
+ ExceptionState.new(@state, nil, nil).describe.should == @state.describe
42
+ end
43
+
44
+ it "returns an empty string if created with a nil state" do
45
+ ExceptionState.new(nil, nil, nil).describe.should == ""
46
+ end
47
+ end
48
+
49
+ describe ExceptionState, "#it" do
50
+ before :each do
51
+ @state = ExampleState.new "Class#method", "does something"
52
+ end
53
+
54
+ it "returns the ExampleState#it string if created with a non-nil state" do
55
+ ExceptionState.new(@state, nil, nil).it.should == @state.it
56
+ end
57
+
58
+ it "returns an empty string if created with a nil state" do
59
+ ExceptionState.new(nil, nil, nil).it.should == ""
60
+ end
61
+ end
62
+
63
+ describe ExceptionState, "#failure?" do
64
+ before :each do
65
+ @state = ExampleState.new "C#m", "works"
66
+ end
67
+
68
+ it "returns true if the exception is an ExpectationNotMetError" do
69
+ exc = ExceptionState.new @state, "", ExpectationNotMetError.new("Fail!")
70
+ exc.failure?.should be_true
71
+ end
72
+
73
+ it "returns false if the exception is not an ExpectationNotMetError" do
74
+ exc = ExceptionState.new @state, "", Exception.new("Fail!")
75
+ exc.failure?.should be_false
76
+ end
77
+ end
78
+
79
+ describe ExceptionState, "#message" do
80
+ it "returns <No message> if the exception message is empty" do
81
+ exc = ExceptionState.new @state, "", Exception.new("")
82
+ exc.message.should == "<No message>"
83
+ end
84
+
85
+ it "returns the message without exception class when the exception is ExpectationNotMetError" do
86
+ exc = ExceptionState.new @state, "", ExpectationNotMetError.new("Fail!")
87
+ exc.message.should == "Fail!"
88
+ end
89
+
90
+ it "returns the message with exception class when the exception is not ExpectationNotMetError" do
91
+ exc = ExceptionState.new @state, "", Exception.new("Fail!")
92
+ exc.message.should == "Exception: Fail!"
93
+ end
94
+ end
95
+
96
+ describe ExceptionState, "#backtrace" do
97
+ before :each do
98
+ begin
99
+ raise Exception, "mock backtrace"
100
+ rescue Exception => @exception
101
+ end
102
+ end
103
+
104
+ it "returns a string representation of the exception backtrace" do
105
+ exc = ExceptionState.new @state, "", @exception
106
+ exc.backtrace.should be_kind_of(String)
107
+ end
108
+
109
+ # TODO: spec the filtering of the backtrace so mspec files don't display
110
+ end
@@ -1,7 +1,7 @@
1
1
  require File.dirname(__FILE__) + '/../../spec_helper'
2
2
  require 'mspec/runner/formatters/dotted'
3
3
  require 'mspec/runner/mspec'
4
- require 'mspec/runner/state'
4
+ require 'mspec/runner/example'
5
5
 
6
6
  describe DottedFormatter, "#initialize" do
7
7
  it "permits zero arguments" do
@@ -20,6 +20,8 @@ describe DottedFormatter, "#register" do
20
20
 
21
21
  it "registers self with MSpec for appropriate actions" do
22
22
  MSpec.stub!(:register)
23
+ MSpec.should_receive(:register).with(:exception, @formatter)
24
+ MSpec.should_receive(:register).with(:before, @formatter)
23
25
  MSpec.should_receive(:register).with(:after, @formatter)
24
26
  MSpec.should_receive(:register).with(:finish, @formatter)
25
27
  @formatter.register
@@ -58,24 +60,106 @@ describe DottedFormatter, "#print" do
58
60
  end
59
61
  end
60
62
 
61
- describe DottedFormatter, "#message" do
63
+ describe DottedFormatter, "#exception" do
62
64
  before :each do
63
65
  @formatter = DottedFormatter.new
66
+ @failure = ExceptionState.new nil, nil, ExpectationNotMetError.new("failed")
67
+ @error = ExceptionState.new nil, nil, MSpecExampleError.new("boom!")
64
68
  end
65
69
 
66
- it "returns <No message> if the exception message is empty" do
67
- exc = Exception.new ""
68
- @formatter.message(exc).should == "<No message>"
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
69
75
  end
70
76
 
71
- it "returns the message without exception class when the exception is ExpectationNotMetError" do
72
- exc = Exception.new "message"
73
- @formatter.message(exc).should == "Exception: message"
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
74
82
  end
75
83
 
76
- it "returns the message with exception class when the exception is not ExpectationNotMetError" do
77
- exc = ExpectationNotMetError.new "message"
78
- @formatter.message(exc).should == "message"
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]
89
+ end
90
+ end
91
+
92
+ describe DottedFormatter, "#exception?" do
93
+ before :each do
94
+ @formatter = DottedFormatter.new
95
+ @failure = ExceptionState.new nil, nil, ExpectationNotMetError.new("failed")
96
+ @error = ExceptionState.new nil, nil, MSpecExampleError.new("boom!")
97
+ end
98
+
99
+ it "returns false if there have been no exceptions" do
100
+ @formatter.exception?.should be_false
101
+ end
102
+
103
+ it "returns true if any exceptions are errors" do
104
+ @formatter.exception @failure
105
+ @formatter.exception @error
106
+ @formatter.exception?.should be_true
107
+ end
108
+
109
+ it "returns true if all exceptions are failures" do
110
+ @formatter.exception @failure
111
+ @formatter.exception @failure
112
+ @formatter.exception?.should be_true
113
+ end
114
+
115
+ it "returns true if all exceptions are errors" do
116
+ @formatter.exception @error
117
+ @formatter.exception @error
118
+ @formatter.exception?.should be_true
119
+ end
120
+ end
121
+
122
+ describe DottedFormatter, "#failure?" do
123
+ before :each do
124
+ @formatter = DottedFormatter.new
125
+ @failure = ExceptionState.new nil, nil, ExpectationNotMetError.new("failed")
126
+ @error = ExceptionState.new nil, nil, MSpecExampleError.new("boom!")
127
+ end
128
+
129
+ it "returns false if there have been no exceptions" do
130
+ @formatter.failure?.should be_false
131
+ end
132
+
133
+ it "returns false if any exceptions are errors" do
134
+ @formatter.exception @failure
135
+ @formatter.exception @error
136
+ @formatter.failure?.should be_false
137
+ end
138
+
139
+ it "returns true if all exceptions are failures" do
140
+ @formatter.exception @failure
141
+ @formatter.exception @failure
142
+ @formatter.failure?.should be_true
143
+ end
144
+ end
145
+
146
+ describe DottedFormatter, "#before" do
147
+ before :each do
148
+ @state = ExampleState.new("describe", "it")
149
+ @formatter = DottedFormatter.new
150
+ @formatter.exception ExceptionState.new(nil, nil, ExpectationNotMetError.new("Failed!"))
151
+ end
152
+
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
157
+ end
158
+
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
79
163
  end
80
164
  end
81
165
 
@@ -83,7 +167,7 @@ describe DottedFormatter, "#after" do
83
167
  before :each do
84
168
  $stdout = @out = IOStub.new
85
169
  @formatter = DottedFormatter.new
86
- @state = SpecState.new("describe", "it")
170
+ @state = ExampleState.new("describe", "it")
87
171
  end
88
172
 
89
173
  after :each do
@@ -96,20 +180,24 @@ describe DottedFormatter, "#after" do
96
180
  end
97
181
 
98
182
  it "prints an 'F' if there was an expectation failure" do
99
- @state.exceptions << ["msg", ExpectationNotMetError.new("failed")]
183
+ exc = ExpectationNotMetError.new "failed"
184
+ @formatter.exception ExceptionState.new(@state, nil, exc)
100
185
  @formatter.after(@state)
101
186
  @out.should == "F"
102
187
  end
103
188
 
104
189
  it "prints an 'E' if there was an exception other than expectation failure" do
105
- @state.exceptions << ["msg", MSpecExampleError.new("boom!")]
190
+ exc = MSpecExampleError.new("boom!")
191
+ @formatter.exception ExceptionState.new(@state, nil, exc)
106
192
  @formatter.after(@state)
107
193
  @out.should == "E"
108
194
  end
109
195
 
110
196
  it "prints an 'E' if there are mixed exceptions and exepctation failures" do
111
- @state.exceptions << ["msg", ExpectationNotMetError.new("failed")]
112
- @state.exceptions << ["msg", MSpecExampleError.new("boom!")]
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)
113
201
  @formatter.after(@state)
114
202
  @out.should == "E"
115
203
  end
@@ -123,7 +211,7 @@ describe DottedFormatter, "#finish" do
123
211
  TimerAction.stub!(:new).and_return(@timer)
124
212
 
125
213
  $stdout = @out = IOStub.new
126
- @state = SpecState.new("describe", "it")
214
+ @state = ExampleState.new("Class#method", "runs")
127
215
  MSpec.stub!(:register)
128
216
  @formatter = DottedFormatter.new
129
217
  @formatter.register
@@ -134,15 +222,17 @@ describe DottedFormatter, "#finish" do
134
222
  end
135
223
 
136
224
  it "prints a failure message for an exception" do
137
- @state.exceptions << ["msg", MSpecExampleError.new("broken")]
225
+ exc = ExceptionState.new @state, nil, MSpecExampleError.new("broken")
226
+ @formatter.exception exc
138
227
  @formatter.after @state
139
228
  @formatter.finish
140
- @out.should =~ /^1\)\ndescribe it ERROR$/
229
+ @out.should =~ /^1\)\nClass#method runs ERROR$/
141
230
  end
142
231
 
143
232
  it "prints a backtrace for an exception" do
144
- @formatter.stub!(:backtrace).and_return("path/to/some/file.rb:35:in method")
145
- @state.exceptions << ["msg", MSpecExampleError.new("broken")]
233
+ exc = ExceptionState.new @state, nil, MSpecExampleError.new("broken")
234
+ exc.stub!(:backtrace).and_return("path/to/some/file.rb:35:in method")
235
+ @formatter.exception exc
146
236
  @formatter.after @state
147
237
  @formatter.finish
148
238
  @out.should =~ %r[path/to/some/file.rb:35:in method$]
@@ -161,24 +251,24 @@ describe DottedFormatter, "#finish" do
161
251
  end
162
252
 
163
253
  it "prints errors, backtraces, elapsed time, and tallies" do
164
- @state.exceptions << ["msg", MSpecExampleError.new("broken")]
165
- @formatter.stub!(:backtrace).and_return("path/to/some/file.rb:35:in method")
254
+ exc = ExceptionState.new @state, nil, MSpecExampleError.new("broken")
255
+ exc.stub!(:backtrace).and_return("path/to/some/file.rb:35:in method")
256
+ @formatter.exception exc
166
257
  @timer.should_receive(:format).and_return("Finished in 2.0 seconds")
167
- @tally.should_receive(:format).and_return("1 example, 0 failures")
258
+ @tally.should_receive(:format).and_return("1 example, 1 failure")
168
259
  @formatter.after @state
169
260
  @formatter.finish
170
261
  @out.should ==
171
262
  %[E
172
263
 
173
264
  1)
174
- describe it ERROR
175
- MSpecExampleError occurred during: msg
265
+ Class#method runs ERROR
176
266
  MSpecExampleError: broken
177
267
  path/to/some/file.rb:35:in method
178
268
 
179
269
  Finished in 2.0 seconds
180
270
 
181
- 1 example, 0 failures
271
+ 1 example, 1 failure
182
272
  ]
183
273
  end
184
274
  end
@@ -2,7 +2,7 @@ require File.dirname(__FILE__) + '/../../spec_helper'
2
2
  require 'mspec/guards/guard'
3
3
  require 'mspec/runner/formatters/html'
4
4
  require 'mspec/runner/mspec'
5
- require 'mspec/runner/state'
5
+ require 'mspec/runner/example'
6
6
 
7
7
  describe HtmlFormatter do
8
8
  before :each do
@@ -90,11 +90,36 @@ describe HtmlFormatter, "#leave" do
90
90
  end
91
91
  end
92
92
 
93
+ describe HtmlFormatter, "#exception" do
94
+ before :each do
95
+ $stdout = @out = IOStub.new
96
+ @formatter = HtmlFormatter.new
97
+ @formatter.register
98
+ @state = ExampleState.new("describe", "it")
99
+ end
100
+
101
+ after :each do
102
+ $stdout = STDOUT
103
+ end
104
+
105
+ it "prints the #it string once for each exception raised" do
106
+ exc = ExceptionState.new @state, nil, ExpectationNotMetError.new("disappointing")
107
+ @formatter.exception exc
108
+ exc = ExceptionState.new @state, nil, MSpecExampleError.new("painful")
109
+ @formatter.exception exc
110
+ @out.should ==
111
+ %[<li class="fail">- it (<a href="#details-1">FAILED - 1</a>)</li>
112
+ <li class="fail">- it (<a href="#details-2">ERROR - 2</a>)</li>
113
+ ]
114
+ end
115
+ end
116
+
93
117
  describe HtmlFormatter, "#after" do
94
118
  before :each do
95
119
  $stdout = @out = IOStub.new
96
120
  @formatter = HtmlFormatter.new
97
- @state = SpecState.new("describe", "it")
121
+ @formatter.register
122
+ @state = ExampleState.new("describe", "it")
98
123
  end
99
124
 
100
125
  after :each do
@@ -106,16 +131,12 @@ describe HtmlFormatter, "#after" do
106
131
  @out.should == %[<li class="pass">- it</li>\n]
107
132
  end
108
133
 
109
- it "prints the #it string once for each exception raised" do
110
- @formatter.register
111
- @state.exceptions << ["msg", ExpectationNotMetError.new("disappointing")]
112
- @state.exceptions << ["msg", MSpecExampleError.new("painful")]
113
- @formatter.tally.after @state
134
+ it "does not print any output if an exception is raised" do
135
+ exc = ExceptionState.new @state, nil, ExpectationNotMetError.new("disappointing")
136
+ @formatter.exception exc
137
+ out = @out.dup
114
138
  @formatter.after @state
115
- @out.should ==
116
- %[<li class="fail">- it (<a href="#details-1">FAILED - 1</a>)</li>
117
- <li class="fail">- it (<a href="#details-2">ERROR - 2</a>)</li>
118
- ]
139
+ @out.should == out
119
140
  end
120
141
  end
121
142
 
@@ -127,7 +148,7 @@ describe HtmlFormatter, "#finish" do
127
148
  TimerAction.stub!(:new).and_return(@timer)
128
149
 
129
150
  $stdout = @out = IOStub.new
130
- @state = SpecState.new("describe", "it")
151
+ @state = ExampleState.new("describe", "it")
131
152
  MSpec.stub!(:register)
132
153
  @formatter = HtmlFormatter.new
133
154
  @formatter.register
@@ -140,16 +161,16 @@ describe HtmlFormatter, "#finish" do
140
161
  end
141
162
 
142
163
  it "prints a failure message for an exception" do
143
- @state.exceptions << ["msg", @exception]
144
- @formatter.instance_variable_set :@states, [@state]
164
+ exc = ExceptionState.new @state, nil, @exception
165
+ @formatter.exception exc
145
166
  @formatter.finish
146
167
  @out.should =~ %r[<p>describe it ERROR</p>]
147
168
  end
148
169
 
149
170
  it "prints a backtrace for an exception" do
150
- @formatter.stub!(:backtrace).and_return("path/to/some/file.rb:35:in method")
151
- @state.exceptions << ["msg", @exception]
152
- @formatter.instance_variable_set :@states, [@state]
171
+ exc = ExceptionState.new @state, nil, @exception
172
+ exc.stub!(:backtrace).and_return("path/to/some/file.rb:35:in method")
173
+ @formatter.exception exc
153
174
  @formatter.finish
154
175
  @out.should =~ %r[<pre>.*path/to/some/file.rb:35:in method.*</pre>]m
155
176
  end
@@ -167,14 +188,16 @@ describe HtmlFormatter, "#finish" do
167
188
  end
168
189
 
169
190
  it "prints errors, backtraces, elapsed time, and tallies" do
170
- @state.exceptions << ["msg", @exception]
171
- @formatter.stub!(:backtrace).and_return("path/to/some/file.rb:35:in method")
191
+ exc = ExceptionState.new @state, nil, @exception
192
+ exc.stub!(:backtrace).and_return("path/to/some/file.rb:35:in method")
193
+ @formatter.exception exc
194
+
172
195
  @timer.should_receive(:format).and_return("Finished in 2.0 seconds")
173
196
  @tally.should_receive(:format).and_return("1 example, 1 failures")
174
- @formatter.instance_variable_set :@states, [@state]
175
197
  @formatter.finish
176
198
  @out.should ==
177
- %[<hr>
199
+ %[<li class=\"fail\">- it (<a href=\"#details-1\">ERROR - 1</a>)</li>
200
+ <hr>
178
201
  <ol id="details">
179
202
  <li id="details-1"><p>describe it ERROR</p>
180
203
  <p>MSpecExampleError: broken</p>