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