mspec 1.1.1 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -2,7 +2,7 @@ require File.dirname(__FILE__) + '/../../spec_helper'
2
2
  require 'mspec/expectations/expectations'
3
3
  require 'mspec/runner/actions/tally'
4
4
  require 'mspec/runner/mspec'
5
- require 'mspec/runner/state'
5
+ require 'mspec/runner/example'
6
6
 
7
7
  describe Tally do
8
8
  before :each do
@@ -56,9 +56,7 @@ end
56
56
  describe TallyAction do
57
57
  before :each do
58
58
  @tally = TallyAction.new
59
- @state = SpecState.new("describe", "it")
60
- @state.exceptions << ["msg", Exception.new("it broke")]
61
- @state.exceptions << ["msg", ExpectationNotMetError.new("disappointment")]
59
+ @state = ExampleState.new("describe", "it")
62
60
  end
63
61
 
64
62
  it "responds to #counter by returning the Tally object" do
@@ -75,11 +73,29 @@ describe TallyAction do
75
73
  @tally.counter.expectations.should == 1
76
74
  end
77
75
 
78
- it "responds to #after by incrementing counts returned by Tally#examples, #failures, #errors" do
76
+ it "responds to #after by incrementing counts returned by Tally#examples" do
79
77
  @tally.after @state
80
78
  @tally.counter.examples.should == 1
81
79
  @tally.counter.expectations.should == 0
80
+ @tally.counter.failures.should == 0
81
+ @tally.counter.errors.should == 0
82
+ end
83
+
84
+ it "responds to #exception by incrementing counts returned by Tally#failures" do
85
+ exc = ExceptionState.new nil, nil, ExpectationNotMetError.new("Failed!")
86
+ @tally.exception exc
87
+ @tally.counter.examples.should == 0
88
+ @tally.counter.expectations.should == 0
82
89
  @tally.counter.failures.should == 1
90
+ @tally.counter.errors.should == 0
91
+ end
92
+
93
+ it "responds to #exception by incrementing counts returned by Tally#errors" do
94
+ exc = ExceptionState.new nil, nil, Exception.new("Error!")
95
+ @tally.exception exc
96
+ @tally.counter.examples.should == 0
97
+ @tally.counter.expectations.should == 0
98
+ @tally.counter.failures.should == 0
83
99
  @tally.counter.errors.should == 1
84
100
  end
85
101
 
@@ -88,18 +104,22 @@ describe TallyAction do
88
104
  @tally.after @state
89
105
  @tally.expectation @state
90
106
  @tally.expectation @state
91
- @tally.format.should == "1 file, 1 example, 2 expectations, 1 failure, 1 error"
107
+ exc = ExceptionState.new nil, nil, ExpectationNotMetError.new("Failed!")
108
+ @tally.exception exc
109
+ @tally.format.should == "1 file, 1 example, 2 expectations, 1 failure, 0 errors"
92
110
  end
93
111
 
94
112
  it "responds to #register by registering itself with MSpec for appropriate actions" do
95
113
  MSpec.should_receive(:register).with(:load, @tally)
96
114
  MSpec.should_receive(:register).with(:after, @tally)
115
+ MSpec.should_receive(:register).with(:exception, @tally)
97
116
  MSpec.should_receive(:register).with(:expectation, @tally)
98
117
  @tally.register
99
118
  end
100
119
 
101
120
  it "responds to #unregister by unregistering itself with MSpec for appropriate actions" do
102
121
  MSpec.should_receive(:unregister).with(:load, @tally)
122
+ MSpec.should_receive(:unregister).with(:exception, @tally)
103
123
  MSpec.should_receive(:unregister).with(:after, @tally)
104
124
  MSpec.should_receive(:unregister).with(:expectation, @tally)
105
125
  @tally.unregister
@@ -2,11 +2,11 @@ require File.dirname(__FILE__) + '/../spec_helper'
2
2
  require 'mspec/matchers/base'
3
3
  require 'mspec/runner/mspec'
4
4
  require 'mspec/mocks/mock'
5
- require 'mspec/runner/state'
5
+ require 'mspec/runner/context'
6
6
 
7
- describe RunState do
7
+ describe ContextState do
8
8
  before :each do
9
- @state = RunState.new
9
+ @state = ContextState.new
10
10
  @proc = lambda { }
11
11
  end
12
12
 
@@ -44,44 +44,68 @@ describe RunState do
44
44
  end
45
45
  end
46
46
 
47
- describe RunState, "#protect" do
48
- it "calls MSpec.protect" do
47
+ describe ContextState, "#protect" do
48
+ before :each do
49
49
  ScratchPad.record []
50
- a = lambda { ScratchPad << :a }
51
- b = lambda { ScratchPad << :b }
52
- RunState.new.protect("message", [a, b])
50
+ @a = lambda { ScratchPad << :a }
51
+ @b = lambda { ScratchPad << :b }
52
+ @c = lambda { raise Exception, "Fail!" }
53
+ end
54
+
55
+ it "returns false and does execute any blocks if check is true and MSpec.pretend_mode? is true" do
56
+ MSpec.stub!(:pretend_mode?).and_return(true)
57
+ ContextState.new.protect("message", [@a, @b]).should be_false
58
+ ScratchPad.recorded.should == []
59
+ end
60
+
61
+ it "executes the blocks if MSpec.pretend_mode? is false" do
62
+ MSpec.stub!(:pretend_mode?).and_return(false)
63
+ ContextState.new.protect("message", [@a, @b])
64
+ ScratchPad.recorded.should == [:a, :b]
65
+ end
66
+
67
+ it "executes the blocks if check is false" do
68
+ ContextState.new.protect("message", [@a, @b], false)
53
69
  ScratchPad.recorded.should == [:a, :b]
54
70
  end
71
+
72
+ it "returns true if none of the blocks raise an exception" do
73
+ ContextState.new.protect("message", [@a, @b]).should be_true
74
+ end
75
+
76
+ it "returns false if any of the blocks raise an exception" do
77
+ ContextState.new.protect("message", [@a, @c, @b]).should be_false
78
+ end
55
79
  end
56
80
 
57
- describe RunState, "#state" do
81
+ describe ContextState, "#state" do
58
82
  before :each do
59
83
  MSpec.store :before, []
60
84
  MSpec.store :after, []
61
85
 
62
- @state = RunState.new
86
+ @state = ContextState.new
63
87
  end
64
88
 
65
89
  it "returns nil if no spec is being executed" do
66
90
  @state.state.should == nil
67
91
  end
68
92
 
69
- it "returns a SpecState instance if a spec is being executed" do
93
+ it "returns a ExampleState instance if an example is being executed" do
70
94
  ScratchPad.record @state
71
95
  @state.describe("") { }
72
96
  @state.it("") { ScratchPad.record ScratchPad.recorded.state }
73
97
  @state.process
74
98
  @state.state.should == nil
75
- ScratchPad.recorded.should be_kind_of(SpecState)
99
+ ScratchPad.recorded.should be_kind_of(ExampleState)
76
100
  end
77
101
  end
78
102
 
79
- describe RunState, "#process" do
103
+ describe ContextState, "#process" do
80
104
  before :each do
81
105
  MSpec.store :before, []
82
106
  MSpec.store :after, []
83
107
 
84
- @state = RunState.new
108
+ @state = ContextState.new
85
109
  @state.describe("") { }
86
110
 
87
111
  @a = lambda { ScratchPad << :a }
@@ -112,6 +136,13 @@ describe RunState, "#process" do
112
136
  ScratchPad.recorded.should == [:a, :b]
113
137
  end
114
138
 
139
+ it "does not call the it block if an exception is raised in before(:each)" do
140
+ @state.before(:each) { raise Exception, "Fail!" }
141
+ @state.it("one", &@a)
142
+ @state.process
143
+ ScratchPad.recorded.should == []
144
+ end
145
+
115
146
  it "calls each before(:each) block" do
116
147
  @state.before(:each, &@a)
117
148
  @state.before(:each, &@b)
@@ -128,6 +159,14 @@ describe RunState, "#process" do
128
159
  ScratchPad.recorded.should == [:a, :b]
129
160
  end
130
161
 
162
+ it "does not call after(:each) if an exception is raised in before(:each)" do
163
+ @state.before(:each) { raise Exception, "Fail!" }
164
+ @state.after(:each, &@a)
165
+ @state.it("") { }
166
+ @state.process
167
+ ScratchPad.recorded.should == []
168
+ end
169
+
131
170
  it "calls Mock.cleanup for each it block" do
132
171
  @state.it("") { }
133
172
  @state.it("") { }
@@ -142,6 +181,13 @@ describe RunState, "#process" do
142
181
  @state.process
143
182
  end
144
183
 
184
+ it "does not call Mock.verify_count if an exception is raised in before(:each)" do
185
+ @state.before(:each) { raise Exception, "Fail!" }
186
+ @state.it("") { }
187
+ Mock.should_not_receive(:verify_count)
188
+ @state.process
189
+ end
190
+
145
191
  it "calls the describe block" do
146
192
  ScratchPad.record []
147
193
  @state.describe(Object, "msg") { ScratchPad << :a }
@@ -149,23 +195,12 @@ describe RunState, "#process" do
149
195
  ScratchPad.recorded.should == [:a]
150
196
  end
151
197
 
152
- it "creates a new SpecState instance for each spec" do
198
+ it "creates a new ExampleState instance for each example" do
153
199
  ScratchPad.record @state
154
200
  @state.describe("desc") { }
155
201
  @state.it("it") { ScratchPad.record ScratchPad.recorded.state }
156
202
  @state.process
157
- ScratchPad.recorded.should be_kind_of(SpecState)
158
- end
159
-
160
- it "records exceptions that occur while running the spec" do
161
- ScratchPad.record @state
162
- exception = Exception.new("bump!")
163
- MSpec.stack.push @state
164
- @state.describe("describe") { }
165
- @state.it("it") { raise exception }
166
- @state.after(:each) { ScratchPad.record ScratchPad.recorded.state.exceptions }
167
- @state.process
168
- ScratchPad.recorded.should == [[nil, exception]]
203
+ ScratchPad.recorded.should be_kind_of(ExampleState)
169
204
  end
170
205
 
171
206
  it "shuffles the spec list if MSpec.randomize? is true" do
@@ -177,7 +212,7 @@ describe RunState, "#process" do
177
212
  end
178
213
  end
179
214
 
180
- describe RunState, "#process in pretend mode" do
215
+ describe ContextState, "#process in pretend mode" do
181
216
  before :all do
182
217
  MSpec.register_mode :pretend
183
218
  end
@@ -190,7 +225,7 @@ describe RunState, "#process in pretend mode" do
190
225
  MSpec.store :before, []
191
226
  MSpec.store :after, []
192
227
 
193
- @state = RunState.new
228
+ @state = ContextState.new
194
229
  @state.describe("") { }
195
230
 
196
231
  @a = lambda { ScratchPad << :a }
@@ -252,12 +287,12 @@ describe RunState, "#process in pretend mode" do
252
287
  end
253
288
  end
254
289
 
255
- describe RunState, "#process" do
290
+ describe ContextState, "#process" do
256
291
  before :each do
257
292
  MSpec.store :before, []
258
293
  MSpec.store :after, []
259
294
 
260
- @state = RunState.new
295
+ @state = ContextState.new
261
296
  @state.describe("") { }
262
297
  @state.it("") { }
263
298
  end
@@ -267,7 +302,7 @@ describe RunState, "#process" do
267
302
  MSpec.store :after, nil
268
303
  end
269
304
 
270
- it "calls registered before actions with the current SpecState instance" do
305
+ it "calls registered before actions with the current ExampleState instance" do
271
306
  before = mock("before")
272
307
  before.should_receive(:before).and_return {
273
308
  ScratchPad.record :before
@@ -276,10 +311,10 @@ describe RunState, "#process" do
276
311
  MSpec.register :before, before
277
312
  @state.process
278
313
  ScratchPad.recorded.should == :before
279
- @spec_state.should be_kind_of(SpecState)
314
+ @spec_state.should be_kind_of(ExampleState)
280
315
  end
281
316
 
282
- it "calls registered after actions with the current SpecState instance" do
317
+ it "calls registered after actions with the current ExampleState instance" do
283
318
  after = mock("after")
284
319
  after.should_receive(:after).and_return {
285
320
  ScratchPad.record :after
@@ -288,11 +323,11 @@ describe RunState, "#process" do
288
323
  MSpec.register :after, after
289
324
  @state.process
290
325
  ScratchPad.recorded.should == :after
291
- @spec_state.should be_kind_of(SpecState)
326
+ @spec_state.should be_kind_of(ExampleState)
292
327
  end
293
328
  end
294
329
 
295
- describe RunState, "#process in pretend mode" do
330
+ describe ContextState, "#process in pretend mode" do
296
331
  before :all do
297
332
  MSpec.register_mode :pretend
298
333
  end
@@ -305,7 +340,7 @@ describe RunState, "#process in pretend mode" do
305
340
  MSpec.store :before, []
306
341
  MSpec.store :after, []
307
342
 
308
- @state = RunState.new
343
+ @state = ContextState.new
309
344
  @state.describe("") { }
310
345
  @state.it("") { }
311
346
  end
@@ -315,7 +350,7 @@ describe RunState, "#process in pretend mode" do
315
350
  MSpec.store :after, nil
316
351
  end
317
352
 
318
- it "calls registered before actions with the current SpecState instance" do
353
+ it "calls registered before actions with the current ExampleState instance" do
319
354
  before = mock("before")
320
355
  before.should_receive(:before).and_return {
321
356
  ScratchPad.record :before
@@ -324,10 +359,10 @@ describe RunState, "#process in pretend mode" do
324
359
  MSpec.register :before, before
325
360
  @state.process
326
361
  ScratchPad.recorded.should == :before
327
- @spec_state.should be_kind_of(SpecState)
362
+ @spec_state.should be_kind_of(ExampleState)
328
363
  end
329
364
 
330
- it "calls registered after actions with the current SpecState instance" do
365
+ it "calls registered after actions with the current ExampleState instance" do
331
366
  after = mock("after")
332
367
  after.should_receive(:after).and_return {
333
368
  ScratchPad.record :after
@@ -336,16 +371,16 @@ describe RunState, "#process in pretend mode" do
336
371
  MSpec.register :after, after
337
372
  @state.process
338
373
  ScratchPad.recorded.should == :after
339
- @spec_state.should be_kind_of(SpecState)
374
+ @spec_state.should be_kind_of(ExampleState)
340
375
  end
341
376
  end
342
377
 
343
- describe RunState, "#process" do
378
+ describe ContextState, "#process" do
344
379
  before :each do
345
380
  MSpec.store :enter, []
346
381
  MSpec.store :leave, []
347
382
 
348
- @state = RunState.new
383
+ @state = ContextState.new
349
384
  @state.describe("") { }
350
385
  @state.it("") { }
351
386
  end
@@ -372,7 +407,7 @@ describe RunState, "#process" do
372
407
  end
373
408
  end
374
409
 
375
- describe RunState, "#process in pretend mode" do
410
+ describe ContextState, "#process in pretend mode" do
376
411
  before :all do
377
412
  MSpec.register_mode :pretend
378
413
  end
@@ -385,7 +420,7 @@ describe RunState, "#process in pretend mode" do
385
420
  MSpec.store :enter, []
386
421
  MSpec.store :leave, []
387
422
 
388
- @state = RunState.new
423
+ @state = ContextState.new
389
424
  @state.describe("") { }
390
425
  @state.it("") { }
391
426
  end
@@ -411,125 +446,3 @@ describe RunState, "#process in pretend mode" do
411
446
  ScratchPad.recorded.should == :leave
412
447
  end
413
448
  end
414
-
415
- describe SpecState do
416
- it "is initialized with the describe and it strings" do
417
- SpecState.new("This", "does").should be_kind_of(SpecState)
418
- end
419
- end
420
-
421
- describe SpecState, "#describe" do
422
- before :each do
423
- @state = SpecState.new("describe", "it")
424
- end
425
-
426
- it "returns the arguments to the #describe block stringified and concatenated" do
427
- @state.describe.should == "describe"
428
- end
429
- end
430
-
431
- describe SpecState, "#it" do
432
- before :each do
433
- @state = SpecState.new("describe", "it")
434
- end
435
-
436
- it "returns the argument to the #it block" do
437
- @state.it.should == "it"
438
- end
439
- end
440
-
441
- describe SpecState, "#exceptions" do
442
- before :each do
443
- @state = SpecState.new("describe", "it")
444
- end
445
-
446
- it "returns an array" do
447
- @state.exceptions.should be_kind_of(Array)
448
- end
449
- end
450
-
451
- describe SpecState, "#exception?" do
452
- before :each do
453
- @state = SpecState.new("describe", "it")
454
- end
455
-
456
- it "returns false if no exceptions were recorded" do
457
- @state.exception?.should == false
458
- end
459
-
460
- it "returns true if any exceptions were recorded" do
461
- @state.exceptions.push :a
462
- @state.exception?.should == true
463
- end
464
- end
465
-
466
- describe SpecState, "#unfiltered?" do
467
- before :each do
468
- MSpec.store :include, nil
469
- MSpec.store :exclude, nil
470
-
471
- @state = SpecState.new("describe", "it")
472
- @filter = mock("filter")
473
- end
474
-
475
- it "returns true if MSpec include filters list is empty" do
476
- @state.unfiltered?.should == true
477
- end
478
-
479
- it "returns true if MSpec include filters match this spec" do
480
- @filter.should_receive(:===).and_return(true)
481
- MSpec.register :include, @filter
482
- @state.unfiltered?.should == true
483
- end
484
-
485
- it "returns false if MSpec include filters do not match this spec" do
486
- @filter.should_receive(:===).and_return(false)
487
- MSpec.register :include, @filter
488
- @state.unfiltered?.should == false
489
- end
490
-
491
- it "returns true if MSpec exclude filters list is empty" do
492
- @state.unfiltered?.should == true
493
- end
494
-
495
- it "returns true if MSpec exclude filters do not match this spec" do
496
- @filter.should_receive(:===).and_return(false)
497
- MSpec.register :exclude, @filter
498
- @state.unfiltered?.should == true
499
- end
500
-
501
- it "returns false if MSpec exclude filters match this spec" do
502
- @filter.should_receive(:===).and_return(true)
503
- MSpec.register :exclude, @filter
504
- @state.unfiltered?.should == false
505
- end
506
-
507
- it "returns false if MSpec include and exclude filters match this spec" do
508
- @filter.should_receive(:===).twice.and_return(true)
509
- MSpec.register :include, @filter
510
- MSpec.register :exclude, @filter
511
- @state.unfiltered?.should == false
512
- end
513
- end
514
-
515
- describe SpecState, "#filtered?" do
516
- before :each do
517
- @state = SpecState.new("describe", "it")
518
- end
519
-
520
- it "returns true if #unfiltered returns false" do
521
- @state.should_receive(:unfiltered?).and_return(false)
522
- @state.filtered?.should == true
523
- end
524
-
525
- it "returns false if #unfiltered returns true" do
526
- @state.should_receive(:unfiltered?).and_return(true)
527
- @state.filtered?.should == false
528
- end
529
- end
530
-
531
- describe SpecState, "#failure?" do
532
- before :each do
533
- @state = SpecState.new("describe", "it")
534
- end
535
- end