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 "#include_pattern" do
23
it "should default to '**/*_spec.rb'" do
24
@options.filename_pattern.should == "**/*_spec.rb"
28
describe "#files_to_load" do
30
it "should load files not following pattern if named explicitly" do
31
file = File.expand_path(File.dirname(__FILE__) + "/resources/a_bar.rb")
32
@options.files << file
33
@options.files_to_load.should include(file)
36
describe "with default --pattern" do
37
it "should load files named _spec.rb" do
38
dir = File.expand_path(File.dirname(__FILE__) + "/resources/")
40
@options.files_to_load.should == ["#{dir}/a_spec.rb"]
44
describe "with explicit pattern (single)" do
46
@options.filename_pattern = "**/*_foo.rb"
49
it "should load files following pattern" do
50
file = File.expand_path(File.dirname(__FILE__) + "/resources/a_foo.rb")
51
@options.files << file
52
@options.files_to_load.should include(file)
55
it "should load files in directories following pattern" do
56
dir = File.expand_path(File.dirname(__FILE__) + "/resources")
58
@options.files_to_load.should include("#{dir}/a_foo.rb")
61
it "should not load files in directories not following pattern" do
62
dir = File.expand_path(File.dirname(__FILE__) + "/resources")
64
@options.files_to_load.should_not include("#{dir}/a_bar.rb")
68
describe "with explicit pattern (comma,separated,values)" do
71
@options.filename_pattern = "**/*_foo.rb,**/*_bar.rb"
74
it "should support comma separated values" do
75
dir = File.expand_path(File.dirname(__FILE__) + "/resources")
77
@options.files_to_load.should include("#{dir}/a_foo.rb")
78
@options.files_to_load.should include("#{dir}/a_bar.rb")
81
it "should support comma separated values with spaces" do
82
dir = File.expand_path(File.dirname(__FILE__) + "/resources")
84
@options.files_to_load.should include("#{dir}/a_foo.rb")
85
@options.files_to_load.should include("#{dir}/a_bar.rb")
92
describe "#backtrace_tweaker" do
93
it "should default to QuietBacktraceTweaker" do
94
@options.backtrace_tweaker.class.should == QuietBacktraceTweaker
98
describe "#dry_run" do
99
it "should default to false" do
100
@options.dry_run.should == false
104
describe "#context_lines" do
105
it "should default to 3" do
106
@options.context_lines.should == 3
110
describe "#parse_diff with nil" do
112
@options.parse_diff nil
115
it "should make diff_format unified" do
116
@options.diff_format.should == :unified
119
it "should set Spec::Expectations.differ to be a default differ" do
120
Spec::Expectations.differ.class.should ==
121
::Spec::Expectations::Differs::Default
125
describe "#parse_diff with 'unified'" do
127
@options.parse_diff 'unified'
130
it "should make diff_format unified and uses default differ_class" do
131
@options.diff_format.should == :unified
132
@options.differ_class.should equal(Spec::Expectations::Differs::Default)
135
it "should set Spec::Expectations.differ to be a default differ" do
136
Spec::Expectations.differ.class.should ==
137
::Spec::Expectations::Differs::Default
141
describe "#parse_diff with 'context'" do
143
@options.parse_diff 'context'
146
it "should make diff_format context and uses default differ_class" do
147
@options.diff_format.should == :context
148
@options.differ_class.should == Spec::Expectations::Differs::Default
151
it "should set Spec::Expectations.differ to be a default differ" do
152
Spec::Expectations.differ.class.should ==
153
::Spec::Expectations::Differs::Default
157
describe "#parse_diff with Custom::Differ" do
159
@options.parse_diff 'Custom::Differ'
162
it "should use custom differ_class" do
163
@options.diff_format.should == :custom
164
@options.differ_class.should == Custom::Differ
165
Spec::Expectations.differ.should be_instance_of(Custom::Differ)
168
it "should set Spec::Expectations.differ to be a default differ" do
169
Spec::Expectations.differ.class.should ==
174
describe "#parse_diff with missing class name" do
175
it "should raise error" do
176
lambda { @options.parse_diff "Custom::MissingDiffer" }.should raise_error(NameError)
177
@err.string.should match(/Couldn't find differ class Custom::MissingDiffer/n)
181
describe "#parse_example" do
182
it "with argument thats not a file path, sets argument as the example" do
183
example = "something or other"
184
File.file?(example).should == false
185
@options.parse_example example
186
@options.examples.should eql(["something or other"])
189
it "with argument that is a file path, sets examples to contents of the file" do
190
example = "#{File.dirname(__FILE__)}/examples.txt"
191
File.should_receive(:file?).with(example).and_return(true)
192
file = StringIO.new("Sir, if you were my husband, I would poison your drink.\nMadam, if you were my wife, I would drink it.")
193
File.should_receive(:open).with(example).and_return(file)
195
@options.parse_example example
196
@options.examples.should eql([
197
"Sir, if you were my husband, I would poison your drink.",
198
"Madam, if you were my wife, I would drink it."
203
describe "#examples_should_not_be_run" do
204
it "should cause #run_examples to return true and do nothing" do
205
@options.examples_should_not_be_run
206
ExampleGroupRunner.should_not_receive(:new)
208
@options.run_examples.should be_true
212
describe "#load_class" do
213
it "should raise error when not class name" do
215
@options.send(:load_class, 'foo', 'fruit', '--food')
216
end.should raise_error('"foo" is not a valid class name')
220
describe "#reporter" do
221
it "returns a Reporter" do
222
@options.reporter.should be_instance_of(Reporter)
223
@options.reporter.options.should === @options
227
describe "#add_example_group affecting passed in example_group" do
228
it "runs all examples when options.examples is nil" do
229
example_1_has_run = false
230
example_2_has_run = false
231
@example_group = Class.new(::Spec::Example::ExampleGroup).describe("Some Examples") do
233
example_1_has_run = true
236
example_2_has_run = true
240
@options.examples = nil
242
@options.add_example_group @example_group
243
@options.run_examples
244
example_1_has_run.should be_true
245
example_2_has_run.should be_true
248
it "keeps all example_definitions when options.examples is empty" do
249
example_1_has_run = false
250
example_2_has_run = false
251
@example_group = Class.new(::Spec::Example::ExampleGroup).describe("Some Examples") do
253
example_1_has_run = true
256
example_2_has_run = true
260
@options.examples = []
262
@options.add_example_group @example_group
263
@options.run_examples
264
example_1_has_run.should be_true
265
example_2_has_run.should be_true
269
describe "#add_example_group affecting example_group" do
270
it "adds example_group when example_group has example_definitions and is not shared" do
271
@example_group = Class.new(::Spec::Example::ExampleGroup).describe("Some Examples") do
272
it "uses this example_group" do
276
@options.number_of_examples.should == 0
277
@options.add_example_group @example_group
278
@options.number_of_examples.should == 1
279
@options.example_groups.length.should == 1
283
describe "#remove_example_group" do
284
it "should remove the ExampleGroup from the list of ExampleGroups" do
285
@example_group = Class.new(::Spec::Example::ExampleGroup).describe("Some Examples") do
287
@options.add_example_group @example_group
288
@options.example_groups.should include(@example_group)
290
@options.remove_example_group @example_group
291
@options.example_groups.should_not include(@example_group)
295
describe "#run_examples" do
296
it "should use the standard runner by default" do
297
runner = ::Spec::Runner::ExampleGroupRunner.new(@options)
298
::Spec::Runner::ExampleGroupRunner.should_receive(:new).
301
@options.user_input_for_runner = nil
303
@options.run_examples
306
it "should use a custom runner when given" do
307
runner = Custom::ExampleGroupRunner.new(@options, nil)
308
Custom::ExampleGroupRunner.should_receive(:new).
311
@options.user_input_for_runner = "Custom::ExampleGroupRunner"
313
@options.run_examples
316
it "should use a custom runner with extra options" do
317
runner = Custom::ExampleGroupRunner.new(@options, 'something')
318
Custom::ExampleGroupRunner.should_receive(:new).
319
with(@options, 'something').
321
@options.user_input_for_runner = "Custom::ExampleGroupRunner:something"
323
@options.run_examples
326
describe "when there are examples" do
328
@options.add_example_group Class.new(::Spec::Example::ExampleGroup)
329
@options.formatters << Formatter::BaseTextFormatter.new(@options, @out)
332
it "runs the Examples and outputs the result" do
333
@options.run_examples
334
@out.string.should include("0 examples, 0 failures")
337
it "sets #examples_run? to true" do
338
@options.examples_run?.should be_false
339
@options.run_examples
340
@options.examples_run?.should be_true
344
describe "when there are no examples" do
346
@options.formatters << Formatter::BaseTextFormatter.new(@options, @out)
349
it "does not run Examples and does not output a result" do
350
@options.run_examples
351
@out.string.should_not include("examples")
352
@out.string.should_not include("failures")
355
it "sets #examples_run? to false" do
356
@options.examples_run?.should be_false
357
@options.run_examples
358
@options.examples_run?.should be_false