rspec-unit 0.9.22

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,63 @@
1
+ require 'rubygems'
2
+ require 'bundler/setup'
3
+
4
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
5
+
6
+ require 'rspec-unit'
7
+
8
+ def mock_example_group_instance(example_group)
9
+ eg_inst = example_group.new
10
+ example_group.stub(:new).and_return(eg_inst)
11
+ eg_inst
12
+ end
13
+
14
+ class NullObject
15
+ def method_missing(method, *args, &block)
16
+ # ignore
17
+ end
18
+ end
19
+
20
+ class RSpec::Core::ExampleGroup
21
+ def self.run_all(reporter=nil)
22
+ @orig_space = RSpec::Mocks.space
23
+ RSpec::Mocks.space = RSpec::Mocks::Space.new
24
+ run(reporter || NullObject.new)
25
+ ensure
26
+ RSpec::Mocks.space = @orig_space
27
+ end
28
+ end
29
+
30
+ def sandboxed(&block)
31
+ begin
32
+ @orig_config = RSpec.configuration
33
+ @orig_world = RSpec.world
34
+ new_config = RSpec::Core::Configuration.new
35
+ new_world = RSpec::Core::World.new(new_config)
36
+ RSpec.instance_variable_set(:@configuration, new_config)
37
+ RSpec.instance_variable_set(:@world, new_world)
38
+ object = Object.new
39
+ object.extend(RSpec::Core::ObjectExtensions)
40
+ object.extend(RSpec::Core::SharedExampleGroup)
41
+
42
+ object.instance_eval(&block)
43
+ ensure
44
+ RSpec.instance_variable_set(:@configuration, @orig_config)
45
+ RSpec.instance_variable_set(:@world, @orig_world)
46
+ end
47
+ end
48
+
49
+ def in_editor?
50
+ ENV.has_key?('TM_MODE') || ENV.has_key?('EMACS') || ENV.has_key?('VIM')
51
+ end
52
+
53
+ RSpec.configure do |c|
54
+ c.color_enabled = !in_editor?
55
+ c.filter_run :focused => true
56
+ c.run_all_when_everything_filtered = true
57
+ c.alias_example_to :fit, :focused => true
58
+ c.formatter = :documentation
59
+ c.around do |example|
60
+ sandboxed { example.run }
61
+ end
62
+ end
63
+
@@ -0,0 +1,403 @@
1
+ require File.expand_path(File.dirname(__FILE__) + "/spec_helper")
2
+
3
+ describe RSpec::Core::ExampleGroup do
4
+ it "supports using assertions in examples" do
5
+ lambda {assert_equal 1, 1}.should_not raise_error
6
+ end
7
+ end
8
+
9
+ describe "RSpec::Unit::TestCase" do
10
+ before do
11
+ @foo = Class.new(RSpec::Unit::TestCase)
12
+ @foo_definition_line = __LINE__ - 1
13
+ @caller_at_foo_definition = caller
14
+ @formatter = RSpec::Core::Formatters::BaseFormatter.new('')
15
+ end
16
+
17
+ describe "identifying test methods" do
18
+ it "ignores methods that don't begin with 'test_'" do
19
+ @foo.class_eval do
20
+ def bar; end
21
+ end
22
+ @foo.examples.should be_empty
23
+ end
24
+
25
+ it "notices methods that begin with 'test_'" do
26
+ @foo.class_eval do
27
+ def test_bar; end
28
+ end
29
+ @foo.examples.size.should == 1
30
+ @foo.examples.first.metadata[:description].should == 'test_bar'
31
+ end
32
+
33
+ it "ignores non-public test methods" do
34
+ @foo.class_eval do
35
+ protected
36
+ def test_foo; end
37
+ private
38
+ def test_bar; end
39
+ end
40
+ @foo.examples.should be_empty
41
+ end
42
+
43
+ it "ignores methods with good names but requiring parameters" do
44
+ @foo.class_eval do
45
+ def test_foo(a); end
46
+ def test_bar(a, *b); end
47
+ end
48
+ @foo.examples.should be_empty
49
+ end
50
+
51
+ it "notices methods that have only optional parameters" do
52
+ @foo.class_eval do
53
+ def test_foo(*a); end
54
+ end
55
+ @foo.examples.size.should == 1
56
+ @foo.examples.first.metadata[:description].should == 'test_foo'
57
+ end
58
+
59
+ it "creates an example to represent a test method" do
60
+ @foo.class_eval do
61
+ def test_bar; end
62
+ end
63
+ @foo.examples.size.should == 1
64
+ @foo.examples.first.metadata[:description].should == 'test_bar'
65
+ end
66
+
67
+ it "creates examples for inherited methods" do
68
+ @foo.class_eval do
69
+ def test_bar; end
70
+ end
71
+
72
+ bar = Class.new(@foo)
73
+ bar.examples.size.should == 1
74
+ bar.examples.first.metadata[:description].should == 'test_bar'
75
+ end
76
+
77
+ it "creates examples for methods newly added to superclasses" do
78
+ bar = Class.new(@foo)
79
+ @foo.class_eval do
80
+ def test_bar; end
81
+ end
82
+ bar.examples.size.should == 1
83
+ bar.examples.first.metadata[:description].should == 'test_bar'
84
+ end
85
+
86
+ it "creates examples for methods added by inclusion of a module" do
87
+ bar = Module.new do
88
+ def test_bar; end
89
+ end
90
+ @foo.send(:include, bar)
91
+ @foo.examples.size.should == 1
92
+ @foo.examples.first.metadata[:description].should == 'test_bar'
93
+ end
94
+ end
95
+
96
+ describe "running test methods" do
97
+ it "runs the test methods as examples" do
98
+ @foo.class_eval do
99
+ def test_bar; end
100
+ end
101
+
102
+ eg_inst = mock_example_group_instance(@foo)
103
+ eg_inst.should_receive(:test_bar).once
104
+
105
+ @foo.run_all
106
+ end
107
+
108
+ it "brackets test methods with setup/teardown" do
109
+ @foo.class_eval do
110
+ def test_bar; end
111
+ def test_baz; end
112
+ end
113
+
114
+ eg_inst = mock_example_group_instance(@foo)
115
+ eg_inst.should_receive(:setup) .once.ordered
116
+ eg_inst.should_receive(:test_bar).once.ordered
117
+ eg_inst.should_receive(:teardown).once.ordered
118
+ eg_inst.should_receive(:setup) .once.ordered
119
+ eg_inst.should_receive(:test_baz).once.ordered
120
+ eg_inst.should_receive(:teardown).once.ordered
121
+
122
+ @foo.run_all
123
+ end
124
+
125
+ it "only calls setup/teardown once per test in subclasses" do
126
+ @foo.class_eval do
127
+ def test_baz; end
128
+ end
129
+ bar = Class.new(@foo)
130
+ bar.class_eval do
131
+ def test_quux; end
132
+ end
133
+
134
+ eg_inst = mock_example_group_instance(bar)
135
+ eg_inst.should_receive(:setup) .once.ordered
136
+ eg_inst.should_receive(:test_baz) .once.ordered
137
+ eg_inst.should_receive(:teardown) .once.ordered
138
+ eg_inst.should_receive(:setup) .once.ordered
139
+ eg_inst.should_receive(:test_quux).once.ordered
140
+ eg_inst.should_receive(:teardown) .once.ordered
141
+
142
+ bar.run_all
143
+ end
144
+
145
+ it "records failed tests in RSpec style" do
146
+ @foo.class_eval do
147
+ def test_bar; flunk; end
148
+ end
149
+ @foo.run_all(@formatter)
150
+ @formatter.failed_examples.size.should == 1
151
+ end
152
+
153
+ it "indicates failed tests in test/unit style" do
154
+ @foo.class_eval do
155
+ class <<self; attr_accessor :_passed; end
156
+ def test_bar; flunk; end
157
+ def teardown; self.class._passed = passed?; end
158
+ end
159
+ @foo.run_all
160
+ @foo._passed.should == false
161
+ end
162
+
163
+ it "records passed tests in RSpec style" do
164
+ @foo.class_eval do
165
+ def test_bar; assert true; end
166
+ end
167
+ @foo.run_all(@formatter)
168
+ @formatter.failed_examples.should be_empty
169
+ end
170
+
171
+ it "indicates passed tests in test/unit style" do
172
+ @foo.class_eval do
173
+ class <<self; attr_accessor :_passed; end
174
+ def test_bar; assert true; end
175
+ def teardown; self.class._passed = passed?; end
176
+ end
177
+ @foo.run_all
178
+ @foo._passed.should == true
179
+ end
180
+ end
181
+
182
+ describe "inherited" do
183
+ it "adds the new subclass to RSpec.world.example_groups" do
184
+ class SampleTestCase < RSpec::Unit::TestCase
185
+ end
186
+ RSpec.world.example_groups.should == [@foo, SampleTestCase]
187
+ end
188
+ end
189
+
190
+ describe "ancestors" do
191
+ before do
192
+ @bar = Class.new(@foo)
193
+ end
194
+
195
+ it "removes TestCase from the end" do
196
+ @bar.ancestors.should == [@bar, @foo]
197
+ end
198
+ end
199
+
200
+ describe "test class metadata" do
201
+ before do
202
+ class SampleTestCaseForName < RSpec::Unit::TestCase
203
+ end
204
+ end
205
+
206
+ it "sets :description to the class name if the class has a name" do
207
+ SampleTestCaseForName.metadata[:example_group][:description].should == "SampleTestCaseForName"
208
+ end
209
+
210
+ it "sets :description to '<Anonymous TestCase>' for anonymous test classes" do
211
+ @foo.metadata[:example_group][:description].should == "<Anonymous TestCase>"
212
+ end
213
+
214
+ it "adds :test_unit => true" do
215
+ @foo.metadata[:example_group][:test_unit].should be_true
216
+ end
217
+
218
+ it "sets :file_path to the file in which the class is first defined" do
219
+ @foo.metadata[:example_group][:file_path].should == __FILE__
220
+ end
221
+
222
+ it "sets :line_number to the line where the class definition begins" do
223
+ @foo.metadata[:example_group][:line_number].should == @foo_definition_line
224
+ end
225
+
226
+ it "sets :location to file_path and line_number" do
227
+ @foo.metadata[:example_group][:location].should == "#{__FILE__}:#{@foo_definition_line}"
228
+ end
229
+
230
+ it "sets :caller" do
231
+ @foo.metadata[:example_group][:caller].first.should =~ Regexp.new(Regexp.escape(@foo.metadata[:example_group][:location]))
232
+ @foo.metadata[:example_group][:caller].size.should == @caller_at_foo_definition.size + 3
233
+ end
234
+
235
+ it "has nil for :block and :describes" do
236
+ @foo.metadata[:example_group][:block].should be_nil
237
+ @foo.metadata[:example_group][:describes].should be_nil
238
+ end
239
+
240
+ it "records test_case_info metadata" do
241
+ @foo.class_eval do
242
+ test_case_info :foo => :bar
243
+ end
244
+ @foo.metadata[:example_group][:foo].should == :bar
245
+ end
246
+ end
247
+
248
+ describe "test method metadata" do
249
+ def find_example(example_group, name)
250
+ example_group.examples.find{|e|e.description == name}
251
+ end
252
+
253
+ def test_baz_metadata
254
+ find_example(@foo, 'test_baz').metadata
255
+ end
256
+
257
+ it "uses a test method's name as its :description" do
258
+ @foo.class_eval do
259
+ def test_baz; end
260
+ end
261
+ @foo.examples.first.metadata[:description].should == 'test_baz'
262
+ end
263
+
264
+ it "sets the test method's :full_description to ClassName#method_name" do
265
+ @foo.class_eval do
266
+ def test_baz; end
267
+ end
268
+ test_baz_metadata[:full_description].should == "#{@foo.metadata[:example_group][:description]}#test_baz"
269
+ end
270
+
271
+ it "adds :test_unit => true" do
272
+ @foo.class_eval do
273
+ def test_baz; end
274
+ end
275
+ test_baz_metadata[:test_unit].should be_true
276
+ end
277
+
278
+ it "sets :file_path to the file where the method is defined" do
279
+ @foo.class_eval do
280
+ def test_baz; end
281
+ end
282
+ test_baz_metadata[:file_path].should == __FILE__
283
+ end
284
+
285
+ it "sets :line_number to the line where the method definition begins" do
286
+ @foo.class_eval do
287
+ def test_baz
288
+ end
289
+ end
290
+ test_baz_metadata[:line_number].should == (__LINE__ - 3)
291
+ end
292
+
293
+ it "sets :location to file path and line number" do
294
+ @foo.class_eval do
295
+ def test_baz; end
296
+ end
297
+ test_baz_metadata[:location].should == "#{__FILE__}:#{__LINE__-2}"
298
+ end
299
+
300
+ it "sets :example_group and :behaviour to the test case class's metadata" do
301
+ @foo.class_eval do
302
+ def test_baz; end
303
+ end
304
+ test_baz_metadata[:example_group].should == @foo.metadata[:example_group]
305
+ test_baz_metadata[:behaviour].should == @foo.metadata[:example_group]
306
+ end
307
+
308
+ it "sets :caller" do
309
+ @foo.class_eval do
310
+ def test_baz; end
311
+ end
312
+ test_baz_metadata[:caller].first.should == @foo.examples.first.metadata[:location]
313
+ test_baz_metadata[:caller].size.should == caller.size + 3
314
+ end
315
+
316
+ it "records test_info metadata for next test method" do
317
+ @foo.class_eval do
318
+ test_info :foo => :bar
319
+ def test_baz; end
320
+ end
321
+ test_baz_metadata[:foo].should == :bar
322
+ end
323
+
324
+ it "records test_info metadata *only* for next test method" do
325
+ @foo.class_eval do
326
+ test_info :foo => :bar
327
+ def test_baz; end
328
+ def test_quux; end
329
+ end
330
+ find_example(@foo, 'test_quux').metadata[:foo].should be_nil
331
+ end
332
+ end
333
+
334
+ describe "examples within a test case" do
335
+ it "allows 'example' to create an example" do
336
+ @foo.class_eval do
337
+ example "should bar" do end
338
+ end
339
+ @foo.examples.size.should == 1
340
+ @foo.examples.first.metadata[:description].should == 'should bar'
341
+ end
342
+
343
+ it "supports 'test' as an alias of example" do
344
+ @foo.class_eval do
345
+ test "should bar" do end
346
+ end
347
+ @foo.examples.size.should == 1
348
+ @foo.examples.first.metadata[:description].should == 'should bar'
349
+ @foo.examples.first.metadata[:test_unit].should be_true
350
+ end
351
+
352
+ it "heeds 'alias_example_to'" do
353
+ @foo.class_eval do
354
+ alias_example_to :make_test
355
+ make_test "should bar" do end
356
+ end
357
+ @foo.examples.size.should == 1
358
+ @foo.examples.first.metadata[:description].should == 'should bar'
359
+ end
360
+
361
+ it "allows defining 'before' blocks" do
362
+ @foo.class_eval do
363
+ before {bar}
364
+ def test_bar; end
365
+ end
366
+
367
+ eg_inst = mock_example_group_instance(@foo)
368
+ eg_inst.should_receive(:bar).once
369
+
370
+ @foo.run_all
371
+ end
372
+
373
+ it "allows defining 'after' blocks" do
374
+ @foo.class_eval do
375
+ after {bar}
376
+ def test_bar; end
377
+ end
378
+
379
+ eg_inst = mock_example_group_instance(@foo)
380
+ eg_inst.should_receive(:bar).once
381
+ @foo.run_all
382
+ end
383
+
384
+ it "allows examples to use instance variables created in 'setup'" do
385
+ @foo.class_eval do
386
+ def setup; super; @quux = 42; end
387
+ it "quux" do @quux.should == 42 end
388
+ end
389
+ @foo.run_all(@formatter)
390
+ @formatter.failed_examples.should be_empty
391
+ end
392
+
393
+ it "allows test methods to use instance variables created in 'before' blocks" do
394
+ @foo.class_eval do
395
+ before { @quux = 42 }
396
+ def test_quux; assert_equal 42, @quux; end
397
+ end
398
+ @foo.run_all(@formatter)
399
+ @formatter.failed_examples.should be_empty
400
+ end
401
+ end
402
+
403
+ end