mspec 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (162) hide show
  1. data/LICENSE +22 -0
  2. data/README +101 -0
  3. data/Rakefile +44 -0
  4. data/bin/mkspec +7 -0
  5. data/bin/mspec +7 -0
  6. data/bin/mspec-ci +8 -0
  7. data/bin/mspec-run +8 -0
  8. data/bin/mspec-tag +8 -0
  9. data/lib/mspec.rb +6 -0
  10. data/lib/mspec/commands/mkspec.rb +147 -0
  11. data/lib/mspec/commands/mspec-ci.rb +71 -0
  12. data/lib/mspec/commands/mspec-run.rb +80 -0
  13. data/lib/mspec/commands/mspec-tag.rb +87 -0
  14. data/lib/mspec/commands/mspec.rb +143 -0
  15. data/lib/mspec/expectations.rb +2 -0
  16. data/lib/mspec/expectations/expectations.rb +12 -0
  17. data/lib/mspec/expectations/should.rb +23 -0
  18. data/lib/mspec/guards.rb +13 -0
  19. data/lib/mspec/guards/bug.rb +27 -0
  20. data/lib/mspec/guards/compliance.rb +18 -0
  21. data/lib/mspec/guards/conflict.rb +16 -0
  22. data/lib/mspec/guards/endian.rb +40 -0
  23. data/lib/mspec/guards/extensions.rb +12 -0
  24. data/lib/mspec/guards/guard.rb +120 -0
  25. data/lib/mspec/guards/noncompliance.rb +12 -0
  26. data/lib/mspec/guards/platform.rb +38 -0
  27. data/lib/mspec/guards/quarantine.rb +15 -0
  28. data/lib/mspec/guards/runner.rb +30 -0
  29. data/lib/mspec/guards/superuser.rb +15 -0
  30. data/lib/mspec/guards/support.rb +12 -0
  31. data/lib/mspec/guards/version.rb +40 -0
  32. data/lib/mspec/helpers.rb +6 -0
  33. data/lib/mspec/helpers/bignum.rb +5 -0
  34. data/lib/mspec/helpers/const_lookup.rb +5 -0
  35. data/lib/mspec/helpers/flunk.rb +5 -0
  36. data/lib/mspec/helpers/io.rb +13 -0
  37. data/lib/mspec/helpers/scratch.rb +17 -0
  38. data/lib/mspec/helpers/tmp.rb +32 -0
  39. data/lib/mspec/matchers.rb +16 -0
  40. data/lib/mspec/matchers/base.rb +95 -0
  41. data/lib/mspec/matchers/be_ancestor_of.rb +24 -0
  42. data/lib/mspec/matchers/be_close.rb +27 -0
  43. data/lib/mspec/matchers/be_empty.rb +20 -0
  44. data/lib/mspec/matchers/be_false.rb +20 -0
  45. data/lib/mspec/matchers/be_kind_of.rb +24 -0
  46. data/lib/mspec/matchers/be_nil.rb +20 -0
  47. data/lib/mspec/matchers/be_true.rb +20 -0
  48. data/lib/mspec/matchers/complain.rb +56 -0
  49. data/lib/mspec/matchers/eql.rb +26 -0
  50. data/lib/mspec/matchers/equal.rb +26 -0
  51. data/lib/mspec/matchers/equal_utf16.rb +34 -0
  52. data/lib/mspec/matchers/include.rb +32 -0
  53. data/lib/mspec/matchers/output.rb +67 -0
  54. data/lib/mspec/matchers/output_to_fd.rb +71 -0
  55. data/lib/mspec/matchers/raise_error.rb +48 -0
  56. data/lib/mspec/mocks.rb +3 -0
  57. data/lib/mspec/mocks/mock.rb +123 -0
  58. data/lib/mspec/mocks/object.rb +28 -0
  59. data/lib/mspec/mocks/proxy.rb +112 -0
  60. data/lib/mspec/runner.rb +13 -0
  61. data/lib/mspec/runner/actions.rb +6 -0
  62. data/lib/mspec/runner/actions/debug.rb +17 -0
  63. data/lib/mspec/runner/actions/filter.rb +40 -0
  64. data/lib/mspec/runner/actions/gdb.rb +17 -0
  65. data/lib/mspec/runner/actions/tag.rb +97 -0
  66. data/lib/mspec/runner/actions/tally.rb +80 -0
  67. data/lib/mspec/runner/actions/timer.rb +22 -0
  68. data/lib/mspec/runner/filters.rb +4 -0
  69. data/lib/mspec/runner/filters/match.rb +22 -0
  70. data/lib/mspec/runner/filters/profile.rb +54 -0
  71. data/lib/mspec/runner/filters/regexp.rb +7 -0
  72. data/lib/mspec/runner/filters/tag.rb +29 -0
  73. data/lib/mspec/runner/formatters.rb +7 -0
  74. data/lib/mspec/runner/formatters/dotted.rb +81 -0
  75. data/lib/mspec/runner/formatters/html.rb +87 -0
  76. data/lib/mspec/runner/formatters/specdoc.rb +27 -0
  77. data/lib/mspec/runner/formatters/spinner.rb +89 -0
  78. data/lib/mspec/runner/formatters/summary.rb +8 -0
  79. data/lib/mspec/runner/formatters/unit.rb +25 -0
  80. data/lib/mspec/runner/formatters/yaml.rb +43 -0
  81. data/lib/mspec/runner/mspec.rb +232 -0
  82. data/lib/mspec/runner/object.rb +20 -0
  83. data/lib/mspec/runner/shared.rb +12 -0
  84. data/lib/mspec/runner/state.rb +116 -0
  85. data/lib/mspec/runner/tag.rb +20 -0
  86. data/lib/mspec/utils/name_map.rb +130 -0
  87. data/lib/mspec/utils/options.rb +344 -0
  88. data/lib/mspec/utils/script.rb +77 -0
  89. data/lib/mspec/version.rb +3 -0
  90. data/spec/commands/mkspec_spec.rb +321 -0
  91. data/spec/commands/mspec_ci_spec.rb +139 -0
  92. data/spec/commands/mspec_run_spec.rb +146 -0
  93. data/spec/commands/mspec_spec.rb +359 -0
  94. data/spec/commands/mspec_tag_spec.rb +131 -0
  95. data/spec/expectations/expectations_spec.rb +16 -0
  96. data/spec/expectations/should_spec.rb +99 -0
  97. data/spec/guards/bug_spec.rb +137 -0
  98. data/spec/guards/compliance_spec.rb +70 -0
  99. data/spec/guards/conflict_spec.rb +20 -0
  100. data/spec/guards/endian_spec.rb +42 -0
  101. data/spec/guards/extensions_spec.rb +36 -0
  102. data/spec/guards/guard_spec.rb +355 -0
  103. data/spec/guards/noncompliance_spec.rb +36 -0
  104. data/spec/guards/platform_spec.rb +84 -0
  105. data/spec/guards/quarantine_spec.rb +19 -0
  106. data/spec/guards/runner_spec.rb +75 -0
  107. data/spec/guards/superuser_spec.rb +22 -0
  108. data/spec/guards/support_spec.rb +22 -0
  109. data/spec/guards/version_spec.rb +133 -0
  110. data/spec/helpers/bignum_spec.rb +11 -0
  111. data/spec/helpers/const_lookup_spec.rb +19 -0
  112. data/spec/helpers/flunk_spec.rb +15 -0
  113. data/spec/helpers/io_spec.rb +34 -0
  114. data/spec/helpers/scratch_spec.rb +22 -0
  115. data/spec/helpers/tmp_spec.rb +72 -0
  116. data/spec/matchers/base_spec.rb +180 -0
  117. data/spec/matchers/be_ancestor_of_spec.rb +28 -0
  118. data/spec/matchers/be_close_spec.rb +46 -0
  119. data/spec/matchers/be_empty_spec.rb +26 -0
  120. data/spec/matchers/be_false_spec.rb +28 -0
  121. data/spec/matchers/be_kind_of_spec.rb +29 -0
  122. data/spec/matchers/be_nil_spec.rb +27 -0
  123. data/spec/matchers/be_true_spec.rb +28 -0
  124. data/spec/matchers/complain_spec.rb +52 -0
  125. data/spec/matchers/eql_spec.rb +33 -0
  126. data/spec/matchers/equal_spec.rb +33 -0
  127. data/spec/matchers/equal_utf16_spec.rb +47 -0
  128. data/spec/matchers/include_spec.rb +37 -0
  129. data/spec/matchers/output_spec.rb +74 -0
  130. data/spec/matchers/output_to_fd_spec.rb +33 -0
  131. data/spec/matchers/raise_error_spec.rb +56 -0
  132. data/spec/mocks/mock_spec.rb +272 -0
  133. data/spec/mocks/proxy_spec.rb +259 -0
  134. data/spec/runner/actions/debug_spec.rb +61 -0
  135. data/spec/runner/actions/filter_spec.rb +84 -0
  136. data/spec/runner/actions/gdb_spec.rb +61 -0
  137. data/spec/runner/actions/tag_spec.rb +253 -0
  138. data/spec/runner/actions/tally_spec.rb +107 -0
  139. data/spec/runner/actions/timer_spec.rb +42 -0
  140. data/spec/runner/filters/a.yaml +4 -0
  141. data/spec/runner/filters/b.yaml +11 -0
  142. data/spec/runner/filters/match_spec.rb +44 -0
  143. data/spec/runner/filters/profile_spec.rb +117 -0
  144. data/spec/runner/filters/regexp_spec.rb +13 -0
  145. data/spec/runner/filters/tag_spec.rb +77 -0
  146. data/spec/runner/formatters/dotted_spec.rb +184 -0
  147. data/spec/runner/formatters/html_spec.rb +191 -0
  148. data/spec/runner/formatters/specdoc_spec.rb +57 -0
  149. data/spec/runner/formatters/spinner_spec.rb +78 -0
  150. data/spec/runner/formatters/summary_spec.rb +29 -0
  151. data/spec/runner/formatters/unit_spec.rb +71 -0
  152. data/spec/runner/formatters/yaml_spec.rb +123 -0
  153. data/spec/runner/mspec_spec.rb +393 -0
  154. data/spec/runner/shared_spec.rb +41 -0
  155. data/spec/runner/state_spec.rb +535 -0
  156. data/spec/runner/tag_spec.rb +93 -0
  157. data/spec/runner/tags.txt +3 -0
  158. data/spec/spec_helper.rb +46 -0
  159. data/spec/utils/name_map_spec.rb +178 -0
  160. data/spec/utils/options_spec.rb +862 -0
  161. data/spec/utils/script_spec.rb +240 -0
  162. metadata +217 -0
@@ -0,0 +1,393 @@
1
+ require File.dirname(__FILE__) + '/../spec_helper'
2
+ require 'mspec/helpers/tmp'
3
+ require 'mspec/matchers/base'
4
+ require 'mspec/runner/mspec'
5
+
6
+ describe MSpec, ".register_files" do
7
+ it "records which spec files to run" do
8
+ MSpec.register_files [:one, :two, :three]
9
+ MSpec.retrieve(:files).should == [:one, :two, :three]
10
+ end
11
+ end
12
+
13
+ describe MSpec, ".register_mode" do
14
+ it "sets execution mode flags" do
15
+ MSpec.register_mode :verify
16
+ MSpec.retrieve(:mode).should == :verify
17
+ end
18
+ end
19
+
20
+ describe MSpec, ".register_tags_path" do
21
+ it "records the path to tag files" do
22
+ MSpec.register_tags_path "path/to/tags"
23
+ MSpec.retrieve(:tags_path).should == "path/to/tags"
24
+ end
25
+ end
26
+
27
+ describe MSpec, ".register_exit" do
28
+ before :each do
29
+ MSpec.store :exit, 0
30
+ end
31
+
32
+ it "records the exit code" do
33
+ MSpec.exit_code.should == 0
34
+ MSpec.register_exit 1
35
+ MSpec.exit_code.should == 1
36
+ end
37
+ end
38
+
39
+ describe MSpec, ".exit_code" do
40
+ it "retrieves the code set with .register_exit" do
41
+ MSpec.register_exit 99
42
+ MSpec.exit_code.should == 99
43
+ end
44
+ end
45
+
46
+ describe MSpec, ".store" do
47
+ it "records data for MSpec settings" do
48
+ MSpec.store :anything, :value
49
+ MSpec.retrieve(:anything).should == :value
50
+ end
51
+ end
52
+
53
+ describe MSpec, ".retrieve" do
54
+ it "accesses .store'd data" do
55
+ MSpec.register :action, :first
56
+ MSpec.retrieve(:action).should == [:first]
57
+ end
58
+ end
59
+
60
+ describe MSpec, ".randomize" do
61
+ it "sets the flag to randomize spec execution order" do
62
+ MSpec.randomize?.should == false
63
+ MSpec.randomize
64
+ MSpec.randomize?.should == true
65
+ MSpec.randomize false
66
+ MSpec.randomize?.should == false
67
+ end
68
+ end
69
+
70
+ describe MSpec, ".register" do
71
+ it "is the gateway behind the register(symbol, action) facility" do
72
+ MSpec.register :bonus, :first
73
+ MSpec.register :bonus, :second
74
+ MSpec.register :bonus, :second
75
+ MSpec.retrieve(:bonus).should == [:first, :second]
76
+ end
77
+ end
78
+
79
+ describe MSpec, ".unregister" do
80
+ it "is the gateway behind the unregister(symbol, actions) facility" do
81
+ MSpec.register :unregister, :first
82
+ MSpec.register :unregister, :second
83
+ MSpec.unregister :unregister, :second
84
+ MSpec.retrieve(:unregister).should == [:first]
85
+ end
86
+ end
87
+
88
+ describe MSpec, ".protect" do
89
+ before :each do
90
+ @ss = mock('SpecState')
91
+ @ss.stub!(:exceptions).and_return([])
92
+ @rs = mock('RunState')
93
+ @rs.stub!(:state).and_return(@ss)
94
+ @exception = Exception.new("Sharp!")
95
+ ScratchPad.record @exception
96
+ end
97
+
98
+ it "rescues any exceptions raised when executing the block argument" do
99
+ MSpec.stack.push @rs
100
+ lambda {
101
+ MSpec.protect("") { raise Exception, "Now you see me..." }
102
+ }.should_not raise_error
103
+ end
104
+
105
+ it "records the exception in the current.state object's exceptions" do
106
+ MSpec.stack.push @rs
107
+ MSpec.protect("testing") { raise ScratchPad.recorded }
108
+ @ss.exceptions.should == [["testing", ScratchPad.recorded]]
109
+ end
110
+
111
+ it "writes a message to STDERR if current is nil" do
112
+ STDERR.stub!(:write)
113
+ STDERR.should_receive(:write).with("\nAn exception occurred in testing:\nException: \"Sharp!\"\n")
114
+ MSpec.stack.clear
115
+ MSpec.protect("testing") { raise ScratchPad.recorded }
116
+ end
117
+
118
+ it "writes a message to STDERR if current.state is nil" do
119
+ STDERR.stub!(:write)
120
+ STDERR.should_receive(:write).with("\nAn exception occurred in testing:\nException: \"Sharp!\"\n")
121
+ @rs.stub!(:state).and_return(nil)
122
+ MSpec.stack.push @rs
123
+ MSpec.protect("testing") { raise ScratchPad.recorded }
124
+ end
125
+ end
126
+
127
+ describe MSpec, ".stack" do
128
+ it "returns an array" do
129
+ MSpec.stack.should be_kind_of(Array)
130
+ end
131
+ end
132
+
133
+ describe MSpec, ".current" do
134
+ it "returns the top of the execution stack" do
135
+ MSpec.stack.clear
136
+ MSpec.stack.push :a
137
+ MSpec.stack.push :b
138
+ MSpec.current.should == :b
139
+ end
140
+ end
141
+
142
+ describe MSpec, ".actions" do
143
+ before :each do
144
+ MSpec.store :start, []
145
+ ScratchPad.record []
146
+ start_one = mock("one")
147
+ start_one.stub!(:start).and_return { ScratchPad << :one }
148
+ start_two = mock("two")
149
+ start_two.stub!(:start).and_return { ScratchPad << :two }
150
+ MSpec.register :start, start_one
151
+ MSpec.register :start, start_two
152
+ end
153
+
154
+ it "does not attempt to run any actions if none have been registered" do
155
+ MSpec.store :finish, nil
156
+ lambda { MSpec.actions :finish }.should_not raise_error
157
+ end
158
+
159
+ it "runs each action registered as a start action" do
160
+ MSpec.actions :start
161
+ ScratchPad.recorded.should == [:one, :two]
162
+ end
163
+ end
164
+
165
+ describe MSpec, ".verify_mode?" do
166
+ before :each do
167
+ MSpec.store :mode, nil
168
+ end
169
+
170
+ it "returns true if register_mode(:verify) is called" do
171
+ MSpec.verify_mode?.should == false
172
+ MSpec.register_mode :verify
173
+ MSpec.verify_mode?.should == true
174
+ end
175
+ end
176
+
177
+ describe MSpec, ".report_mode?" do
178
+ before :each do
179
+ MSpec.store :mode, nil
180
+ end
181
+
182
+ it "returns true if register_mode(:report) is called" do
183
+ MSpec.report_mode?.should == false
184
+ MSpec.register_mode :report
185
+ MSpec.report_mode?.should == true
186
+ end
187
+ end
188
+
189
+ describe MSpec, ".describe" do
190
+ before :each do
191
+ MSpec.stack.clear
192
+ end
193
+
194
+ it "accepts one argument" do
195
+ MSpec.describe(Object) { ScratchPad.record MSpec.current }
196
+ ScratchPad.recorded.should be_kind_of(RunState)
197
+ end
198
+
199
+ it "pushes a new RunState instance on the stack" do
200
+ MSpec.describe(Object, "msg") { ScratchPad.record MSpec.current }
201
+ ScratchPad.recorded.should be_kind_of(RunState)
202
+ end
203
+
204
+ it "pops the RunState instance off the stack when finished" do
205
+ MSpec.describe(Object, "msg") { ScratchPad.record MSpec.current }
206
+ ScratchPad.recorded.should be_kind_of(RunState)
207
+ MSpec.stack.should == []
208
+ end
209
+ end
210
+
211
+ describe MSpec, ".process" do
212
+ before :each do
213
+ MSpec.stub!(:files)
214
+ MSpec.store :start, []
215
+ MSpec.store :finish, []
216
+ end
217
+
218
+ it "calls all start actions" do
219
+ start = mock("start")
220
+ start.stub!(:start).and_return { ScratchPad.record :start }
221
+ MSpec.register :start, start
222
+ MSpec.process
223
+ ScratchPad.recorded.should == :start
224
+ end
225
+
226
+ it "calls all finish actions" do
227
+ finish = mock("finish")
228
+ finish.stub!(:finish).and_return { ScratchPad.record :finish }
229
+ MSpec.register :finish, finish
230
+ MSpec.process
231
+ ScratchPad.recorded.should == :finish
232
+ end
233
+
234
+ it "calls the files method" do
235
+ MSpec.should_receive(:files)
236
+ MSpec.process
237
+ end
238
+ end
239
+
240
+ describe MSpec, ".files" do
241
+ before :each do
242
+ MSpec.store :load, []
243
+ MSpec.store :unload, []
244
+ MSpec.register_files [:one, :two, :three]
245
+ Kernel.stub!(:load)
246
+ end
247
+
248
+ it "calls load actions before each file" do
249
+ load = mock("load")
250
+ load.stub!(:load).and_return { ScratchPad.record :load }
251
+ MSpec.register :load, load
252
+ MSpec.files
253
+ ScratchPad.recorded.should == :load
254
+ end
255
+
256
+ it "shuffles the file list if .randomize? is true" do
257
+ MSpec.randomize
258
+ MSpec.should_receive(:shuffle)
259
+ MSpec.files
260
+ MSpec.randomize false
261
+ end
262
+
263
+ it "registers the current file" do
264
+ MSpec.should_receive(:store).with(:file, :one)
265
+ MSpec.should_receive(:store).with(:file, :two)
266
+ MSpec.should_receive(:store).with(:file, :three)
267
+ MSpec.files
268
+ end
269
+ end
270
+
271
+ describe MSpec, ".shuffle" do
272
+ before :each do
273
+ @base = (0..100).to_a
274
+ @list = @base.clone
275
+ MSpec.shuffle @list
276
+ end
277
+
278
+ it "does not alter the elements in the list" do
279
+ @base.each do |elt|
280
+ @list.should include(elt)
281
+ end
282
+ end
283
+
284
+ it "changes the order of the list" do
285
+ # obviously, this spec has a certain probability
286
+ # of failing. If it fails, run it again.
287
+ @list.should_not == @base
288
+ end
289
+ end
290
+
291
+ describe MSpec, ".tags_path" do
292
+ before :each do
293
+ MSpec.store :tags_path, nil
294
+ end
295
+
296
+ it "returns 'spec/tags' if no tags path has been registered" do
297
+ MSpec.tags_path.should == "spec/tags"
298
+ end
299
+
300
+ it "returns the registered tags path" do
301
+ MSpec.register_tags_path "/path/to/tags"
302
+ MSpec.tags_path.should == "/path/to/tags"
303
+ end
304
+ end
305
+
306
+ describe MSpec, ".tags_file" do
307
+ before :each do
308
+ MSpec.store :file, "/path/to/spec/something/some_spec.rb"
309
+ MSpec.store :tags_path, nil
310
+ end
311
+
312
+ it "returns the tags file for the current spec file with default tags_path" do
313
+ MSpec.tags_file.should == "spec/tags/something/some_tags.txt"
314
+ end
315
+
316
+ it "returns the tags file for the current spec file with custom tags_path" do
317
+ MSpec.register_tags_path "/path/to/tags"
318
+ MSpec.tags_file.should == "/path/to/tags/something/some_tags.txt"
319
+ end
320
+ end
321
+
322
+ describe MSpec, ".read_tags" do
323
+ before :each do
324
+ MSpec.stub!(:tags_file).and_return(File.dirname(__FILE__) + '/tags.txt')
325
+ end
326
+
327
+ it "returns a list of tag instances for matching tag names found" do
328
+ one = SpecTag.new "fail(broken):Some#method? works"
329
+ MSpec.read_tags("fail", "pass").should == [one]
330
+ end
331
+
332
+ it "returns [] if no tags names match" do
333
+ MSpec.read_tags("super").should == []
334
+ end
335
+ end
336
+
337
+ describe MSpec, ".write_tag" do
338
+ before :each do
339
+ FileUtils.stub!(:mkdir_p)
340
+ MSpec.stub!(:tags_file).and_return(tmp("tags.txt"))
341
+ @tag = SpecTag.new "fail(broken):Some#method works"
342
+ end
343
+
344
+ after :all do
345
+ File.delete tmp("tags.txt") rescue nil
346
+ end
347
+
348
+ it "writes a tag to the tags file for the current spec file" do
349
+ MSpec.write_tag @tag
350
+ IO.read(tmp("tags.txt")).should == "fail(broken):Some#method works\n"
351
+ end
352
+
353
+ it "does not write a duplicate tag" do
354
+ File.open(tmp("tags.txt"), "w") { |f| f.puts @tag }
355
+ MSpec.write_tag @tag
356
+ IO.read(tmp("tags.txt")).should == "fail(broken):Some#method works\n"
357
+ end
358
+ end
359
+
360
+ describe MSpec, ".delete_tag" do
361
+ before :each do
362
+ FileUtils.cp File.dirname(__FILE__) + "/tags.txt", tmp("tags.txt")
363
+ MSpec.stub!(:tags_file).and_return(tmp("tags.txt"))
364
+ @tag = SpecTag.new "fail(Comments don't matter):Some#method? works"
365
+ end
366
+
367
+ after :each do
368
+ File.delete tmp("tags.txt") rescue nil
369
+ end
370
+
371
+ it "deletes the tag if it exists" do
372
+ MSpec.delete_tag(@tag).should == true
373
+ IO.read(tmp("tags.txt")).should == %[incomplete(20%):The#best method ever
374
+ benchmark(0.01825):The#fastest method today
375
+ ]
376
+ end
377
+
378
+ it "does not change the tags file contents if the tag doesn't exist" do
379
+ @tag.tag = "failed"
380
+ MSpec.delete_tag(@tag).should == false
381
+ IO.read(tmp("tags.txt")).should == %[fail(broken):Some#method? works
382
+ incomplete(20%):The#best method ever
383
+ benchmark(0.01825):The#fastest method today
384
+ ]
385
+ end
386
+
387
+ it "deletes the tag file if it is empty" do
388
+ MSpec.delete_tag(@tag).should == true
389
+ MSpec.delete_tag(SpecTag.new("incomplete:The#best method ever")).should == true
390
+ MSpec.delete_tag(SpecTag.new("benchmark:The#fastest method today")).should == true
391
+ File.exist?(tmp("tags.txt")).should == false
392
+ end
393
+ end
@@ -0,0 +1,41 @@
1
+ require File.dirname(__FILE__) + '/../spec_helper'
2
+ require 'mspec/runner/shared'
3
+
4
+ describe Object, "#shared" do
5
+ it "stores the passed block in the MSpec module" do
6
+ proc = lambda { :shared }
7
+ shared :shared, &proc
8
+ MSpec.retrieve(:shared).should == proc
9
+ end
10
+ end
11
+
12
+ describe Object, "#it_behaves_like" do
13
+ before :each do
14
+ end
15
+
16
+ it "retrieves the instance variable set on Object and calls the proc" do
17
+ proc = lambda { |a| raise Exception, "visited with #{a.inspect}" }
18
+ shared :shared, &proc
19
+ lambda {
20
+ it_behaves_like(:shared, nil)
21
+ }.should raise_error(Exception, "visited with nil")
22
+ end
23
+
24
+ it "accepts an optional argument to specify the class/module" do
25
+ proc = lambda { |a, b| raise Exception, "visited with #{a.inspect}, #{b.inspect}" }
26
+ shared :shared, &proc
27
+ lambda {
28
+ it_behaves_like(:shared, :method, :klass)
29
+ }.should raise_error(Exception, "visited with :method, :klass")
30
+ end
31
+
32
+ it "accepts an optional argument to specify the class/module name" do
33
+ proc = lambda { |a, b, c|
34
+ raise Exception, "visited with #{a.inspect}, #{b.inspect}, #{c.inspect}"
35
+ }
36
+ shared :shared, &proc
37
+ lambda {
38
+ it_behaves_like(:shared, :method, :klass, :name)
39
+ }.should raise_error(Exception, "visited with :method, :klass, :name")
40
+ end
41
+ end
@@ -0,0 +1,535 @@
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/state'
6
+
7
+ describe RunState do
8
+ before :each do
9
+ @state = RunState.new
10
+ @proc = lambda { }
11
+ end
12
+
13
+ it "records before(:all) blocks" do
14
+ @state.before(:all, &@proc)
15
+ @state.instance_variable_get(:@start).should == [@proc]
16
+ end
17
+
18
+ it "records before(:each) blocks" do
19
+ @state.before(:each, &@proc)
20
+ @state.instance_variable_get(:@before).should == [@proc]
21
+ end
22
+
23
+ it "records after(:all) blocks" do
24
+ @state.after(:all, &@proc)
25
+ @state.instance_variable_get(:@finish).should == [@proc]
26
+ end
27
+
28
+ it "records after(:each) blocks" do
29
+ @state.after(:each, &@proc)
30
+ @state.instance_variable_get(:@after).should == [@proc]
31
+ end
32
+
33
+ it "records it blocks" do
34
+ @state.it("message", &@proc)
35
+ msg, proc = @state.instance_variable_get(:@spec)[0]
36
+ msg.should == "message"
37
+ proc.should == @proc
38
+ end
39
+
40
+ it "records describe blocks" do
41
+ @state.describe(Object, "message", &@proc)
42
+ @state.instance_variable_get(:@describe).should == "Object message"
43
+ @state.instance_variable_get(:@block).should == @proc
44
+ end
45
+ end
46
+
47
+ describe RunState, "#protect" do
48
+ it "calls MSpec.protect" do
49
+ ScratchPad.record []
50
+ a = lambda { ScratchPad << :a }
51
+ b = lambda { ScratchPad << :b }
52
+ RunState.new.protect("message", [a, b])
53
+ ScratchPad.recorded.should == [:a, :b]
54
+ end
55
+ end
56
+
57
+ describe RunState, "#state" do
58
+ before :each do
59
+ MSpec.store :before, []
60
+ MSpec.store :after, []
61
+
62
+ @state = RunState.new
63
+ end
64
+
65
+ it "returns nil if no spec is being executed" do
66
+ @state.state.should == nil
67
+ end
68
+
69
+ it "returns a SpecState instance if a spec is being executed" do
70
+ ScratchPad.record @state
71
+ @state.describe("") { }
72
+ @state.it("") { ScratchPad.record ScratchPad.recorded.state }
73
+ @state.process
74
+ @state.state.should == nil
75
+ ScratchPad.recorded.should be_kind_of(SpecState)
76
+ end
77
+ end
78
+
79
+ describe RunState, "#process" do
80
+ before :each do
81
+ MSpec.store :before, []
82
+ MSpec.store :after, []
83
+
84
+ @state = RunState.new
85
+ @state.describe("") { }
86
+
87
+ @a = lambda { ScratchPad << :a }
88
+ @b = lambda { ScratchPad << :b }
89
+ ScratchPad.record []
90
+ end
91
+
92
+ it "calls each before(:all) block" do
93
+ @state.before(:all, &@a)
94
+ @state.before(:all, &@b)
95
+ @state.it("") { }
96
+ @state.process
97
+ ScratchPad.recorded.should == [:a, :b]
98
+ end
99
+
100
+ it "calls each after(:all) block" do
101
+ @state.after(:all, &@a)
102
+ @state.after(:all, &@b)
103
+ @state.it("") { }
104
+ @state.process
105
+ ScratchPad.recorded.should == [:a, :b]
106
+ end
107
+
108
+ it "calls each it block" do
109
+ @state.it("one", &@a)
110
+ @state.it("two", &@b)
111
+ @state.process
112
+ ScratchPad.recorded.should == [:a, :b]
113
+ end
114
+
115
+ it "calls each before(:each) block" do
116
+ @state.before(:each, &@a)
117
+ @state.before(:each, &@b)
118
+ @state.it("") { }
119
+ @state.process
120
+ ScratchPad.recorded.should == [:a, :b]
121
+ end
122
+
123
+ it "calls each after(:each) block" do
124
+ @state.after(:each, &@a)
125
+ @state.after(:each, &@b)
126
+ @state.it("") { }
127
+ @state.process
128
+ ScratchPad.recorded.should == [:a, :b]
129
+ end
130
+
131
+ it "calls Mock.cleanup for each it block" do
132
+ @state.it("") { }
133
+ @state.it("") { }
134
+ Mock.should_receive(:cleanup).twice
135
+ @state.process
136
+ end
137
+
138
+ it "calls Mock.verify_count for each it block" do
139
+ @state.it("") { }
140
+ @state.it("") { }
141
+ Mock.should_receive(:verify_count).twice
142
+ @state.process
143
+ end
144
+
145
+ it "calls the describe block" do
146
+ ScratchPad.record []
147
+ @state.describe(Object, "msg") { ScratchPad << :a }
148
+ @state.process
149
+ ScratchPad.recorded.should == [:a]
150
+ end
151
+
152
+ it "creates a new SpecState instance for each spec" do
153
+ ScratchPad.record @state
154
+ @state.describe("desc") { }
155
+ @state.it("it") { ScratchPad.record ScratchPad.recorded.state }
156
+ @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]]
169
+ end
170
+
171
+ it "shuffles the spec list if MSpec.randomize? is true" do
172
+ MSpec.randomize
173
+ MSpec.should_receive(:shuffle)
174
+ @state.it("") { }
175
+ @state.process
176
+ MSpec.randomize false
177
+ end
178
+ end
179
+
180
+ describe RunState, "#process in pretend mode" do
181
+ before :all do
182
+ MSpec.register_mode :pretend
183
+ end
184
+
185
+ after :all do
186
+ MSpec.register_mode nil
187
+ end
188
+
189
+ before :each do
190
+ MSpec.store :before, []
191
+ MSpec.store :after, []
192
+
193
+ @state = RunState.new
194
+ @state.describe("") { }
195
+
196
+ @a = lambda { ScratchPad << :a }
197
+ @b = lambda { ScratchPad << :b }
198
+ ScratchPad.record []
199
+ end
200
+
201
+ it "does not call any before(:all) block" do
202
+ @state.before(:all, &@a)
203
+ @state.before(:all, &@b)
204
+ @state.it("") { }
205
+ @state.process
206
+ ScratchPad.recorded.should == []
207
+ end
208
+
209
+ it "does not call any after(:all) block" do
210
+ @state.after(:all, &@a)
211
+ @state.after(:all, &@b)
212
+ @state.it("") { }
213
+ @state.process
214
+ ScratchPad.recorded.should == []
215
+ end
216
+
217
+ it "does not call any it block" do
218
+ @state.it("one", &@a)
219
+ @state.it("two", &@b)
220
+ @state.process
221
+ ScratchPad.recorded.should == []
222
+ end
223
+
224
+ it "does not call any before(:each) block" do
225
+ @state.before(:each, &@a)
226
+ @state.before(:each, &@b)
227
+ @state.it("") { }
228
+ @state.process
229
+ ScratchPad.recorded.should == []
230
+ end
231
+
232
+ it "does not call any after(:each) block" do
233
+ @state.after(:each, &@a)
234
+ @state.after(:each, &@b)
235
+ @state.it("") { }
236
+ @state.process
237
+ ScratchPad.recorded.should == []
238
+ end
239
+
240
+ it "does not call Mock.cleanup" do
241
+ @state.it("") { }
242
+ @state.it("") { }
243
+ Mock.should_not_receive(:cleanup)
244
+ @state.process
245
+ end
246
+
247
+ it "calls the describe block" do
248
+ ScratchPad.record []
249
+ @state.describe(Object, "msg") { ScratchPad << :a }
250
+ @state.process
251
+ ScratchPad.recorded.should == [:a]
252
+ end
253
+ end
254
+
255
+ describe RunState, "#process" do
256
+ before :each do
257
+ MSpec.store :before, []
258
+ MSpec.store :after, []
259
+
260
+ @state = RunState.new
261
+ @state.describe("") { }
262
+ @state.it("") { }
263
+ end
264
+
265
+ after :each do
266
+ MSpec.store :before, nil
267
+ MSpec.store :after, nil
268
+ end
269
+
270
+ it "calls registered before actions with the current SpecState instance" do
271
+ before = mock("before")
272
+ before.should_receive(:before).and_return {
273
+ ScratchPad.record :before
274
+ @spec_state = @state.state
275
+ }
276
+ MSpec.register :before, before
277
+ @state.process
278
+ ScratchPad.recorded.should == :before
279
+ @spec_state.should be_kind_of(SpecState)
280
+ end
281
+
282
+ it "calls registered after actions with the current SpecState instance" do
283
+ after = mock("after")
284
+ after.should_receive(:after).and_return {
285
+ ScratchPad.record :after
286
+ @spec_state = @state.state
287
+ }
288
+ MSpec.register :after, after
289
+ @state.process
290
+ ScratchPad.recorded.should == :after
291
+ @spec_state.should be_kind_of(SpecState)
292
+ end
293
+ end
294
+
295
+ describe RunState, "#process in pretend mode" do
296
+ before :all do
297
+ MSpec.register_mode :pretend
298
+ end
299
+
300
+ after :all do
301
+ MSpec.register_mode nil
302
+ end
303
+
304
+ before :each do
305
+ MSpec.store :before, []
306
+ MSpec.store :after, []
307
+
308
+ @state = RunState.new
309
+ @state.describe("") { }
310
+ @state.it("") { }
311
+ end
312
+
313
+ after :each do
314
+ MSpec.store :before, nil
315
+ MSpec.store :after, nil
316
+ end
317
+
318
+ it "calls registered before actions with the current SpecState instance" do
319
+ before = mock("before")
320
+ before.should_receive(:before).and_return {
321
+ ScratchPad.record :before
322
+ @spec_state = @state.state
323
+ }
324
+ MSpec.register :before, before
325
+ @state.process
326
+ ScratchPad.recorded.should == :before
327
+ @spec_state.should be_kind_of(SpecState)
328
+ end
329
+
330
+ it "calls registered after actions with the current SpecState instance" do
331
+ after = mock("after")
332
+ after.should_receive(:after).and_return {
333
+ ScratchPad.record :after
334
+ @spec_state = @state.state
335
+ }
336
+ MSpec.register :after, after
337
+ @state.process
338
+ ScratchPad.recorded.should == :after
339
+ @spec_state.should be_kind_of(SpecState)
340
+ end
341
+ end
342
+
343
+ describe RunState, "#process" do
344
+ before :each do
345
+ MSpec.store :enter, []
346
+ MSpec.store :leave, []
347
+
348
+ @state = RunState.new
349
+ @state.describe("") { }
350
+ @state.it("") { }
351
+ end
352
+
353
+ after :each do
354
+ MSpec.store :enter, nil
355
+ MSpec.store :leave, nil
356
+ end
357
+
358
+ it "calls registered enter actions with the current #describe string" do
359
+ enter = mock("enter")
360
+ enter.should_receive(:enter).and_return { ScratchPad.record :enter }
361
+ MSpec.register :enter, enter
362
+ @state.process
363
+ ScratchPad.recorded.should == :enter
364
+ end
365
+
366
+ it "calls registered leave actions" do
367
+ leave = mock("leave")
368
+ leave.should_receive(:leave).and_return { ScratchPad.record :leave }
369
+ MSpec.register :leave, leave
370
+ @state.process
371
+ ScratchPad.recorded.should == :leave
372
+ end
373
+ end
374
+
375
+ describe RunState, "#process in pretend mode" do
376
+ before :all do
377
+ MSpec.register_mode :pretend
378
+ end
379
+
380
+ after :all do
381
+ MSpec.register_mode nil
382
+ end
383
+
384
+ before :each do
385
+ MSpec.store :enter, []
386
+ MSpec.store :leave, []
387
+
388
+ @state = RunState.new
389
+ @state.describe("") { }
390
+ @state.it("") { }
391
+ end
392
+
393
+ after :each do
394
+ MSpec.store :enter, nil
395
+ MSpec.store :leave, nil
396
+ end
397
+
398
+ it "calls registered enter actions with the current #describe string" do
399
+ enter = mock("enter")
400
+ enter.should_receive(:enter).and_return { ScratchPad.record :enter }
401
+ MSpec.register :enter, enter
402
+ @state.process
403
+ ScratchPad.recorded.should == :enter
404
+ end
405
+
406
+ it "calls registered leave actions" do
407
+ leave = mock("leave")
408
+ leave.should_receive(:leave).and_return { ScratchPad.record :leave }
409
+ MSpec.register :leave, leave
410
+ @state.process
411
+ ScratchPad.recorded.should == :leave
412
+ end
413
+ 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