adding all of botlist, initial add
[botlist.git] / openbotlist / WEB-INF / lib / ruby / rspec / spec / spec / example / example_group_methods_spec.rb
blob2b6d660fe12d919e32e91e1358cda9a1c9cac633
1 require File.dirname(__FILE__) + '/../../spec_helper'
3 module Spec
4   module Example
5     describe 'ExampleGroupMethods' do
6       it_should_behave_like "sandboxed rspec_options"
7       attr_reader :example_group, :result, :reporter
8       before(:each) do
9         options.formatters << mock("formatter", :null_object => true)
10         options.backtrace_tweaker = mock("backtrace_tweaker", :null_object => true)
11         @reporter = FakeReporter.new(@options)
12         options.reporter = reporter
13         @example_group = Class.new(ExampleGroup) do
14           describe("ExampleGroup")
15           it "does nothing"
16         end
17         class << example_group
18           public :include
19         end
20         @result = nil
21       end
23       after(:each) do
24         ExampleGroup.reset
25       end
27       describe "#describe" do
28         attr_reader :child_example_group
29         before do
30           @child_example_group = @example_group.describe("Another ExampleGroup") do
31             it "should pass" do
32               true.should be_true
33             end
34           end
35         end
37         it "should create a subclass of the ExampleGroup when passed a block" do
38           child_example_group.superclass.should == @example_group
39           @options.example_groups.should include(child_example_group)
40         end
42         it "should not inherit examples" do
43           child_example_group.examples.length.should == 1
44         end
45       end
47       describe "#it" do
48         it "should should create an example instance" do
49           lambda {
50             @example_group.it("")
51           }.should change { @example_group.examples.length }.by(1)
52         end
53       end
55       describe "#xit" do
56         before(:each) do
57           Kernel.stub!(:warn)
58         end
60         it "should NOT  should create an example instance" do
61           lambda {
62             @example_group.xit("")
63           }.should_not change(@example_group.examples, :length)
64         end
66         it "should warn that it is disabled" do
67           Kernel.should_receive(:warn).with("Example disabled: foo")
68           @example_group.xit("foo")
69         end
70       end
72       describe "#examples" do
73         it "should have Examples" do
74           example_group = Class.new(ExampleGroup) do
75             describe('example')
76             it "should pass" do
77               1.should == 1
78             end
79           end
80           example_group.examples.length.should == 1
81           example_group.examples.first.description.should == "should pass"
82         end
84         it "should not include methods that begin with test (only when TU interop is loaded)" do
85           example_group = Class.new(ExampleGroup) do
86             describe('example')
87             def test_any_args(*args)
88               true.should be_true
89             end
90             def test_something
91               1.should == 1
92             end
93             def test
94               raise "This is not a real test"
95             end
96             def testify
97               raise "This is not a real test"
98             end
99           end
100           example_group.examples.length.should == 0
101           example_group.run.should be_true
102         end
104         it "should include methods that begin with should and has an arity of 0 in suite" do
105           example_group = Class.new(ExampleGroup) do
106             describe('example')
107             def shouldCamelCase
108               true.should be_true
109             end
110             def should_any_args(*args)
111               true.should be_true
112             end
113             def should_something
114               1.should == 1
115             end
116             def should_not_something
117               1.should_not == 2
118             end
119             def should
120               raise "This is not a real example"
121             end
122             def should_not
123               raise "This is not a real example"
124             end
125           end
126           example_group = example_group.dup
127           example_group.examples.length.should == 4
128           descriptions = example_group.examples.collect {|example| example.description}.sort
129           descriptions.should include("shouldCamelCase")
130           descriptions.should include("should_any_args")
131           descriptions.should include("should_something")
132           descriptions.should include("should_not_something")
133         end
135         it "should not include methods that begin with test_ and has an arity > 0 in suite" do
136           example_group = Class.new(ExampleGroup) do
137             describe('example')
138             def test_invalid(foo)
139               1.should == 1
140             end
141             def testInvalidCamelCase(foo)
142               1.should == 1
143             end
144           end
145           example_group.examples.length.should == 0
146         end
148         it "should not include methods that begin with should_ and has an arity > 0 in suite" do
149           example_group = Class.new(ExampleGroup) do
150             describe('example')
151             def should_invalid(foo)
152               1.should == 2
153             end
154             def shouldInvalidCamelCase(foo)
155               1.should == 3
156             end
157             def should_not_invalid(foo)
158               1.should == 4
159             end
160             def should_valid
161               1.should == 1
162             end
163           end
164           example_group.examples.length.should == 1
165           example_group.run.should be_true
166         end
168         it "should run should_methods" do
169           example_group = Class.new(ExampleGroup) do
170             def should_valid
171               1.should == 2
172             end
173           end
174           example_group.examples.length.should == 1
175           example_group.run.should be_false
176         end
177       end
179       describe "#set_description" do
180         attr_reader :example_group
181         before do
182           class << example_group
183             public :set_description
184           end
185         end
187         describe "#set_description(String)" do
188           before(:each) do
189             example_group.set_description("abc")
190           end
192           specify ".description should return the String passed into .set_description" do
193             example_group.description.should == "abc"
194           end
196           specify ".described_type should provide nil as its type" do
197             example_group.described_type.should be_nil
198           end
199         end
201         describe "#set_description(Type)" do
202           before(:each) do
203             example_group.set_description(ExampleGroup)
204           end
206           specify ".description should return a String representation of that type (fully qualified) as its name" do
207             example_group.description.should == "Spec::Example::ExampleGroup"
208           end
210           specify ".described_type should return the passed in type" do
211             example_group.described_type.should == Spec::Example::ExampleGroup
212           end
213         end
215         describe "#set_description(String, Type)" do
216           before(:each) do
217             example_group.set_description("behaving", ExampleGroup)
218           end
220           specify ".description should return String then space then Type" do
221             example_group.description.should == "behaving Spec::Example::ExampleGroup"
222           end
224           specify ".described_type should return the passed in type" do
225             example_group.described_type.should == Spec::Example::ExampleGroup
226           end
227         end
229         describe "#set_description(Type, String not starting with a space)" do
230           before(:each) do
231             example_group.set_description(ExampleGroup, "behaving")
232           end
234           specify ".description should return the Type then space then String" do
235             example_group.description.should == "Spec::Example::ExampleGroup behaving"
236           end
237         end
239         describe "#set_description(Type, String starting with .)" do
240           before(:each) do
241             example_group.set_description(ExampleGroup, ".behaving")
242           end
244           specify ".description should return the Type then String" do
245             example_group.description.should == "Spec::Example::ExampleGroup.behaving"
246           end
247         end
249         describe "#set_description(Type, String containing .)" do
250           before(:each) do
251             example_group.set_description(ExampleGroup, "calling a.b")
252           end
254           specify ".description should return the Type then space then String" do
255             example_group.description.should == "Spec::Example::ExampleGroup calling a.b"
256           end
257         end
259         describe "#set_description(Type, String starting with .)" do
260           before(:each) do
261             example_group.set_description(ExampleGroup, ".behaving")
262           end
264           specify "should return the Type then String" do
265             example_group.description.should == "Spec::Example::ExampleGroup.behaving"
266           end
267         end
269         describe "#set_description(Type, String containing .)" do
270           before(:each) do
271             example_group.set_description(ExampleGroup, "is #1")
272           end
274           specify ".description should return the Type then space then String" do
275             example_group.description.should == "Spec::Example::ExampleGroup is #1"
276           end
277         end
279         describe "#set_description(String, Type, String)" do
280           before(:each) do
281             example_group.set_description("A", Hash, "with one entry")
282           end
284           specify ".description should return the first String then space then Type then second String" do
285             example_group.description.should == "A Hash with one entry"
286           end
287         end
289         describe "#set_description(Hash representing options)" do
290           before(:each) do
291             example_group.set_description(:a => "b", :spec_path => "blah")
292           end
294           it ".spec_path should expand the passed in :spec_path option passed into the constructor" do
295             example_group.spec_path.should == File.expand_path("blah")
296           end
298           it ".description_options should return all the options passed in" do
299             example_group.description_options.should == {:a => "b", :spec_path => "blah"}
300           end
302         end
303       end
305       describe "#description" do
306         it "should return the same description instance for each call" do
307           example_group.description.should eql(example_group.description)
308         end
310         it "should not add a space when description_text begins with #" do
311           child_example_group = Class.new(example_group) do
312             describe("#foobar", "Does something")
313           end
314           child_example_group.description.should == "ExampleGroup#foobar Does something"
315         end
317         it "should not add a space when description_text begins with ." do
318           child_example_group = Class.new(example_group) do
319             describe(".foobar", "Does something")
320           end
321           child_example_group.description.should == "ExampleGroup.foobar Does something"
322         end
323         
324         it "should return the class name if nil" do
325           example_group.set_description(nil)
326           example_group.description.should =~ /Class:/
327         end
328         
329         it "should return the class name if nil" do
330           example_group.set_description("")
331           example_group.description.should =~ /Class:/
332         end
333       end
335       describe "#description_parts" do
336         it "should return an Array of the current class description args" do
337           example_group.description_parts.should == [example_group.description]
338         end
340         it "should return an Array of the description args from each class in the hierarchy" do
341           child_example_group = Class.new(example_group)
342           child_example_group.describe("Child", ExampleGroup)
343           child_example_group.description.should_not be_empty
345           grand_child_example_group = Class.new(child_example_group)
346           grand_child_example_group.describe("GrandChild", ExampleGroup)
347           grand_child_example_group.description.should_not be_empty
349           grand_child_example_group.description_parts.should == [
350             "ExampleGroup",
351             "Child",
352             Spec::Example::ExampleGroup,
353             "GrandChild",
354             Spec::Example::ExampleGroup
355           ]
356         end
357       end
359       describe "#described_type" do
360         it "should return passed in type" do
361           child_example_group = Class.new(example_group) do
362             describe Object
363           end
364           child_example_group.described_type.should == Object
365         end
367         it "should return #described_type of superclass when no passed in type" do
368           parent_example_group = Class.new(ExampleGroup) do
369             describe Object, "#foobar"
370           end
371           child_example_group = Class.new(parent_example_group) do
372             describe "not a type"
373           end
374           child_example_group.described_type.should == Object
375         end
376       end
378       describe "#remove_after" do
379         it "should unregister a given after(:each) block" do
380           after_all_ran = false
381           @example_group.it("example") {}
382           proc = Proc.new { after_all_ran = true }
383           ExampleGroup.after(:each, &proc)
384           @example_group.run
385           after_all_ran.should be_true
387           after_all_ran = false
388           ExampleGroup.remove_after(:each, &proc)
389           @example_group.run
390           after_all_ran.should be_false
391         end
392       end
394       describe "#include" do
395         it "should have accessible class methods from included module" do
396           mod1_method_called = false
397           mod1 = Module.new do
398             class_methods = Module.new do
399               define_method :mod1_method do
400                 mod1_method_called = true
401               end
402             end
404             metaclass.class_eval do
405               define_method(:included) do |receiver|
406                 receiver.extend class_methods
407               end
408             end
409           end
411           mod2_method_called = false
412           mod2 = Module.new do
413             class_methods = Module.new do
414               define_method :mod2_method do
415                 mod2_method_called = true
416               end
417             end
419             metaclass.class_eval do
420               define_method(:included) do |receiver|
421                 receiver.extend class_methods
422               end
423             end
424           end
426           @example_group.include mod1, mod2
428           @example_group.mod1_method
429           @example_group.mod2_method
430           mod1_method_called.should be_true
431           mod2_method_called.should be_true
432         end
433       end
435       describe "#number_of_examples" do
436         it "should count number of specs" do
437           proc do
438             @example_group.it("one") {}
439             @example_group.it("two") {}
440             @example_group.it("three") {}
441             @example_group.it("four") {}
442           end.should change {@example_group.number_of_examples}.by(4)
443         end
444       end
446       describe "#class_eval" do
447         it "should allow constants to be defined" do
448           example_group = Class.new(ExampleGroup) do
449             describe('example')
450             FOO = 1
451             it "should reference FOO" do
452               FOO.should == 1
453             end
454           end
455           example_group.run
456           Object.const_defined?(:FOO).should == false
457         end
458       end
460       describe '#register' do
461         it "should add ExampleGroup to set of ExampleGroups to be run" do
462           options.example_groups.delete(example_group)
463           options.example_groups.should_not include(example_group)
464           
465           example_group.register {}
466           options.example_groups.should include(example_group)
467         end
468       end
470       describe '#unregister' do
471         before do
472           options.example_groups.should include(example_group)
473         end
475         it "should remove ExampleGroup from set of ExampleGroups to be run" do
476           example_group.unregister
477           options.example_groups.should_not include(example_group)
478         end
479       end
481       describe "#registration_backtrace" do
482         it "returns the backtrace of where the ExampleGroup was registered" do
483           example_group = Class.new(ExampleGroup)
484           example_group.registration_backtrace.join("\n").should include("#{__FILE__}:#{__LINE__-1}")
485         end
486       end
487     end
488   end