]> git.openstreetmap.org Git - rails.git/blob - vendor/gems/rspec-1.1.2/spec/spec/runner/options_spec.rb
e4d01b05cf430617b7e7d119d9b29fd8fb3ff43e
[rails.git] / vendor / gems / rspec-1.1.2 / spec / spec / runner / options_spec.rb
1 require File.dirname(__FILE__) + '/../../spec_helper.rb'
2
3 module Spec
4   module Runner
5     describe Options do
6       before(:each) do
7         @err = StringIO.new('')
8         @out = StringIO.new('')
9         @options = Options.new(@err, @out)
10       end
11
12       after(:each) do
13         Spec::Expectations.differ = nil
14       end
15
16       describe "#examples" do
17         it "should default to empty array" do
18           @options.examples.should == []
19         end
20       end
21
22       describe "#backtrace_tweaker" do
23         it "should default to QuietBacktraceTweaker" do
24           @options.backtrace_tweaker.class.should == QuietBacktraceTweaker
25         end
26       end
27
28       describe "#dry_run" do
29         it "should default to false" do
30           @options.dry_run.should == false
31         end
32       end
33
34       describe "#context_lines" do
35         it "should default to 3" do
36           @options.context_lines.should == 3
37         end
38       end
39
40       describe "#parse_diff with nil" do
41         before(:each) do
42           @options.parse_diff nil
43         end
44
45         it "should make diff_format unified" do
46           @options.diff_format.should == :unified
47         end
48
49         it "should set Spec::Expectations.differ to be a default differ" do
50           Spec::Expectations.differ.class.should ==
51             ::Spec::Expectations::Differs::Default
52         end
53       end
54
55       describe "#parse_diff with 'unified'" do
56         before(:each) do
57           @options.parse_diff 'unified'
58         end
59
60         it "should make diff_format unified and uses default differ_class" do
61           @options.diff_format.should == :unified
62           @options.differ_class.should equal(Spec::Expectations::Differs::Default)
63         end
64
65         it "should set Spec::Expectations.differ to be a default differ" do
66           Spec::Expectations.differ.class.should ==
67             ::Spec::Expectations::Differs::Default
68         end
69       end
70
71       describe "#parse_diff with 'context'" do
72         before(:each) do
73           @options.parse_diff 'context'
74         end
75
76         it "should make diff_format context and uses default differ_class" do
77           @options.diff_format.should == :context
78           @options.differ_class.should == Spec::Expectations::Differs::Default
79         end
80
81         it "should set Spec::Expectations.differ to be a default differ" do
82           Spec::Expectations.differ.class.should ==
83             ::Spec::Expectations::Differs::Default
84         end
85       end
86
87       describe "#parse_diff with Custom::Differ" do
88         before(:each) do
89           @options.parse_diff 'Custom::Differ'
90         end
91
92         it "should use custom differ_class" do
93           @options.diff_format.should == :custom
94           @options.differ_class.should == Custom::Differ
95           Spec::Expectations.differ.should be_instance_of(Custom::Differ)
96         end
97
98         it "should set Spec::Expectations.differ to be a default differ" do
99           Spec::Expectations.differ.class.should ==
100             ::Custom::Differ
101         end
102       end
103
104       describe "#parse_diff with missing class name" do
105         it "should raise error" do
106           lambda { @options.parse_diff "Custom::MissingDiffer" }.should raise_error(NameError)
107           @err.string.should match(/Couldn't find differ class Custom::MissingDiffer/n)
108         end
109       end
110
111       describe "#parse_example" do
112         it "with argument thats not a file path, sets argument as the example" do
113           example = "something or other"
114           File.file?(example).should == false
115           @options.parse_example example
116           @options.examples.should eql(["something or other"])
117         end
118
119         it "with argument that is a file path, sets examples to contents of the file" do
120           example = "#{File.dirname(__FILE__)}/examples.txt"
121           File.should_receive(:file?).with(example).and_return(true)
122           file = StringIO.new("Sir, if you were my husband, I would poison your drink.\nMadam, if you were my wife, I would drink it.")
123           File.should_receive(:open).with(example).and_return(file)
124
125           @options.parse_example example
126           @options.examples.should eql([
127             "Sir, if you were my husband, I would poison your drink.",
128               "Madam, if you were my wife, I would drink it."
129           ])
130         end
131       end
132
133       describe "#examples_should_not_be_run" do
134         it "should cause #run_examples to return true and do nothing" do
135           @options.examples_should_not_be_run
136           ExampleGroupRunner.should_not_receive(:new)
137
138           @options.run_examples.should be_true
139         end
140       end
141
142       describe "#load_class" do
143         it "should raise error when not class name" do
144           lambda do
145             @options.send(:load_class, 'foo', 'fruit', '--food')
146           end.should raise_error('"foo" is not a valid class name')
147         end
148       end
149
150       describe "#reporter" do
151         it "returns a Reporter" do
152           @options.reporter.should be_instance_of(Reporter)
153           @options.reporter.options.should === @options
154         end
155       end
156
157       describe "#add_example_group affecting passed in example_group" do
158         it "runs all examples when options.examples is nil" do
159           example_1_has_run = false
160           example_2_has_run = false
161           @example_group = Class.new(::Spec::Example::ExampleGroup).describe("Some Examples") do
162             it "runs 1" do
163               example_1_has_run = true
164             end
165             it "runs 2" do
166               example_2_has_run = true
167             end
168           end
169
170           @options.examples = nil
171
172           @options.add_example_group @example_group
173           @options.run_examples
174           example_1_has_run.should be_true
175           example_2_has_run.should be_true
176         end
177
178         it "keeps all example_definitions when options.examples is empty" do
179           example_1_has_run = false
180           example_2_has_run = false
181           @example_group = Class.new(::Spec::Example::ExampleGroup).describe("Some Examples") do
182             it "runs 1" do
183               example_1_has_run = true
184             end
185             it "runs 2" do
186               example_2_has_run = true
187             end
188           end
189
190           @options.examples = []
191
192           @options.add_example_group @example_group
193           @options.run_examples
194           example_1_has_run.should be_true
195           example_2_has_run.should be_true
196         end
197       end
198
199       describe "#add_example_group affecting example_group" do
200         it "adds example_group when example_group has example_definitions and is not shared" do
201           @example_group = Class.new(::Spec::Example::ExampleGroup).describe("Some Examples") do
202             it "uses this example_group" do
203             end
204           end
205
206           @options.number_of_examples.should == 0
207           @options.add_example_group @example_group
208           @options.number_of_examples.should == 1
209           @options.example_groups.length.should == 1
210         end
211       end
212
213       describe "#remove_example_group" do
214         it "should remove the ExampleGroup from the list of ExampleGroups" do
215           @example_group = Class.new(::Spec::Example::ExampleGroup).describe("Some Examples") do
216           end
217           @options.add_example_group @example_group
218           @options.example_groups.should include(@example_group)
219
220           @options.remove_example_group @example_group
221           @options.example_groups.should_not include(@example_group)
222         end
223       end
224
225       describe "#run_examples" do
226         it "should use the standard runner by default" do
227           runner = ::Spec::Runner::ExampleGroupRunner.new(@options)
228           ::Spec::Runner::ExampleGroupRunner.should_receive(:new).
229             with(@options).
230             and_return(runner)
231           @options.user_input_for_runner = nil
232
233           @options.run_examples
234         end
235
236         it "should use a custom runner when given" do
237           runner = Custom::ExampleGroupRunner.new(@options, nil)
238           Custom::ExampleGroupRunner.should_receive(:new).
239             with(@options, nil).
240             and_return(runner)
241           @options.user_input_for_runner = "Custom::ExampleGroupRunner"
242
243           @options.run_examples
244         end
245
246         it "should use a custom runner with extra options" do
247           runner = Custom::ExampleGroupRunner.new(@options, 'something')
248           Custom::ExampleGroupRunner.should_receive(:new).
249             with(@options, 'something').
250             and_return(runner)
251           @options.user_input_for_runner = "Custom::ExampleGroupRunner:something"
252
253           @options.run_examples
254         end
255
256         describe "#run_examples when there are example_group" do
257           before(:each) do
258             @options.add_example_group Class.new(::Spec::Example::ExampleGroup)
259             @options.formatters << Formatter::BaseTextFormatter.new(@options, @out)
260           end
261
262           it "runs the Examples and outputs the result" do
263             @options.run_examples
264             @out.string.should include("0 examples, 0 failures")
265           end
266
267           it "sets #examples_run? to true" do
268             @options.examples_run?.should be_false
269             @options.run_examples
270             @options.examples_run?.should be_true
271           end
272         end
273
274         describe "#run_examples when there are no example_group" do
275           before(:each) do
276             @options.formatters << Formatter::BaseTextFormatter.new(@options, @out)
277           end
278
279           it "does not run Examples and does not output a result" do
280             @options.run_examples
281             @out.string.should_not include("examples")
282             @out.string.should_not include("failures")
283           end
284
285           it "sets #examples_run? to false" do
286             @options.examples_run?.should be_false
287             @options.run_examples
288             @options.examples_run?.should be_false
289           end
290         end
291       end
292     end
293   end
294 end