1 require File.dirname(__FILE__) + '/../../spec_helper.rb'
7 @err = StringIO.new('')
8 @out = StringIO.new('')
9 @options = Options.new(@err, @out)
13 Spec::Expectations.differ = nil
16 describe "#examples" do
17 it "should default to empty array" do
18 @options.examples.should == []
22 describe "#backtrace_tweaker" do
23 it "should default to QuietBacktraceTweaker" do
24 @options.backtrace_tweaker.class.should == QuietBacktraceTweaker
28 describe "#dry_run" do
29 it "should default to false" do
30 @options.dry_run.should == false
34 describe "#context_lines" do
35 it "should default to 3" do
36 @options.context_lines.should == 3
40 describe "#parse_diff with nil" do
42 @options.parse_diff nil
45 it "should make diff_format unified" do
46 @options.diff_format.should == :unified
49 it "should set Spec::Expectations.differ to be a default differ" do
50 Spec::Expectations.differ.class.should ==
51 ::Spec::Expectations::Differs::Default
55 describe "#parse_diff with 'unified'" do
57 @options.parse_diff 'unified'
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)
65 it "should set Spec::Expectations.differ to be a default differ" do
66 Spec::Expectations.differ.class.should ==
67 ::Spec::Expectations::Differs::Default
71 describe "#parse_diff with 'context'" do
73 @options.parse_diff 'context'
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
81 it "should set Spec::Expectations.differ to be a default differ" do
82 Spec::Expectations.differ.class.should ==
83 ::Spec::Expectations::Differs::Default
87 describe "#parse_diff with Custom::Differ" do
89 @options.parse_diff 'Custom::Differ'
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)
98 it "should set Spec::Expectations.differ to be a default differ" do
99 Spec::Expectations.differ.class.should ==
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)
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"])
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)
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."
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)
138 @options.run_examples.should be_true
142 describe "#load_class" do
143 it "should raise error when not class name" do
145 @options.send(:load_class, 'foo', 'fruit', '--food')
146 end.should raise_error('"foo" is not a valid class name')
150 describe "#reporter" do
151 it "returns a Reporter" do
152 @options.reporter.should be_instance_of(Reporter)
153 @options.reporter.options.should === @options
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
163 example_1_has_run = true
166 example_2_has_run = true
170 @options.examples = nil
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
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
183 example_1_has_run = true
186 example_2_has_run = true
190 @options.examples = []
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
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
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
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
217 @options.add_example_group @example_group
218 @options.example_groups.should include(@example_group)
220 @options.remove_example_group @example_group
221 @options.example_groups.should_not include(@example_group)
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).
231 @options.user_input_for_runner = nil
233 @options.run_examples
236 it "should use a custom runner when given" do
237 runner = Custom::ExampleGroupRunner.new(@options, nil)
238 Custom::ExampleGroupRunner.should_receive(:new).
241 @options.user_input_for_runner = "Custom::ExampleGroupRunner"
243 @options.run_examples
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').
251 @options.user_input_for_runner = "Custom::ExampleGroupRunner:something"
253 @options.run_examples
256 describe "#run_examples when there are example_group" do
258 @options.add_example_group Class.new(::Spec::Example::ExampleGroup)
259 @options.formatters << Formatter::BaseTextFormatter.new(@options, @out)
262 it "runs the Examples and outputs the result" do
263 @options.run_examples
264 @out.string.should include("0 examples, 0 failures")
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
274 describe "#run_examples when there are no example_group" do
276 @options.formatters << Formatter::BaseTextFormatter.new(@options, @out)
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")
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