~ubuntu-branches/ubuntu/oneiric/puppet/oneiric-security

« back to all changes in this revision

Viewing changes to vendor/gems/rspec/spec/spec/runner/options_spec.rb

  • Committer: Bazaar Package Importer
  • Author(s): Micah Anderson
  • Date: 2008-07-26 15:43:45 UTC
  • mto: (3.1.1 lenny) (1.3.1 upstream)
  • mto: This revision was merged to the branch mainline in revision 16.
  • Revision ID: james.westby@ubuntu.com-20080726154345-1fmgo76b4l72ulvc
ImportĀ upstreamĀ versionĀ 0.24.5

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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 "#include_pattern" do
 
23
        it "should default to '**/*_spec.rb'" do
 
24
          @options.filename_pattern.should == "**/*_spec.rb"
 
25
        end
 
26
      end
 
27
      
 
28
      describe "#files_to_load" do
 
29
        
 
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)
 
34
        end
 
35
        
 
36
        describe "with default --pattern" do
 
37
          it "should load files named _spec.rb" do
 
38
            dir = File.expand_path(File.dirname(__FILE__) + "/resources/")
 
39
            @options.files << dir
 
40
            @options.files_to_load.should == ["#{dir}/a_spec.rb"]
 
41
          end
 
42
        end
 
43
        
 
44
        describe "with explicit pattern (single)" do
 
45
          before(:each) do
 
46
            @options.filename_pattern = "**/*_foo.rb"
 
47
          end
 
48
        
 
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)
 
53
          end
 
54
        
 
55
          it "should load files in directories following pattern" do
 
56
            dir = File.expand_path(File.dirname(__FILE__) + "/resources")
 
57
            @options.files << dir
 
58
            @options.files_to_load.should include("#{dir}/a_foo.rb")
 
59
          end
 
60
        
 
61
          it "should not load files in directories not following pattern" do
 
62
            dir = File.expand_path(File.dirname(__FILE__) + "/resources")
 
63
            @options.files << dir
 
64
            @options.files_to_load.should_not include("#{dir}/a_bar.rb")
 
65
          end
 
66
        end
 
67
        
 
68
        describe "with explicit pattern (comma,separated,values)" do
 
69
          
 
70
          before(:each) do
 
71
            @options.filename_pattern = "**/*_foo.rb,**/*_bar.rb"
 
72
          end
 
73
 
 
74
          it "should support comma separated values" do
 
75
            dir = File.expand_path(File.dirname(__FILE__) + "/resources")
 
76
            @options.files << dir
 
77
            @options.files_to_load.should include("#{dir}/a_foo.rb")
 
78
            @options.files_to_load.should include("#{dir}/a_bar.rb")
 
79
          end
 
80
        
 
81
          it "should support comma separated values with spaces" do
 
82
            dir = File.expand_path(File.dirname(__FILE__) + "/resources")
 
83
            @options.files << dir
 
84
            @options.files_to_load.should include("#{dir}/a_foo.rb")
 
85
            @options.files_to_load.should include("#{dir}/a_bar.rb")
 
86
          end
 
87
        
 
88
        end
 
89
      
 
90
      end
 
91
 
 
92
      describe "#backtrace_tweaker" do
 
93
        it "should default to QuietBacktraceTweaker" do
 
94
          @options.backtrace_tweaker.class.should == QuietBacktraceTweaker
 
95
        end
 
96
      end
 
97
 
 
98
      describe "#dry_run" do
 
99
        it "should default to false" do
 
100
          @options.dry_run.should == false
 
101
        end
 
102
      end
 
103
 
 
104
      describe "#context_lines" do
 
105
        it "should default to 3" do
 
106
          @options.context_lines.should == 3
 
107
        end
 
108
      end
 
109
 
 
110
      describe "#parse_diff with nil" do
 
111
        before(:each) do
 
112
          @options.parse_diff nil
 
113
        end
 
114
 
 
115
        it "should make diff_format unified" do
 
116
          @options.diff_format.should == :unified
 
117
        end
 
118
 
 
119
        it "should set Spec::Expectations.differ to be a default differ" do
 
120
          Spec::Expectations.differ.class.should ==
 
121
            ::Spec::Expectations::Differs::Default
 
122
        end
 
123
      end
 
124
 
 
125
      describe "#parse_diff with 'unified'" do
 
126
        before(:each) do
 
127
          @options.parse_diff 'unified'
 
128
        end
 
129
 
 
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)
 
133
        end
 
134
 
 
135
        it "should set Spec::Expectations.differ to be a default differ" do
 
136
          Spec::Expectations.differ.class.should ==
 
137
            ::Spec::Expectations::Differs::Default
 
138
        end
 
139
      end
 
140
 
 
141
      describe "#parse_diff with 'context'" do
 
142
        before(:each) do
 
143
          @options.parse_diff 'context'
 
144
        end
 
145
 
 
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
 
149
        end
 
150
 
 
151
        it "should set Spec::Expectations.differ to be a default differ" do
 
152
          Spec::Expectations.differ.class.should ==
 
153
            ::Spec::Expectations::Differs::Default
 
154
        end
 
155
      end
 
156
 
 
157
      describe "#parse_diff with Custom::Differ" do
 
158
        before(:each) do
 
159
          @options.parse_diff 'Custom::Differ'
 
160
        end
 
161
 
 
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)
 
166
        end
 
167
 
 
168
        it "should set Spec::Expectations.differ to be a default differ" do
 
169
          Spec::Expectations.differ.class.should ==
 
170
            ::Custom::Differ
 
171
        end
 
172
      end
 
173
 
 
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)
 
178
        end
 
179
      end
 
180
 
 
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"])
 
187
        end
 
188
 
 
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)
 
194
 
 
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."
 
199
          ])
 
200
        end
 
201
      end
 
202
 
 
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)
 
207
 
 
208
          @options.run_examples.should be_true
 
209
        end
 
210
      end
 
211
 
 
212
      describe "#load_class" do
 
213
        it "should raise error when not class name" do
 
214
          lambda do
 
215
            @options.send(:load_class, 'foo', 'fruit', '--food')
 
216
          end.should raise_error('"foo" is not a valid class name')
 
217
        end
 
218
      end
 
219
 
 
220
      describe "#reporter" do
 
221
        it "returns a Reporter" do
 
222
          @options.reporter.should be_instance_of(Reporter)
 
223
          @options.reporter.options.should === @options
 
224
        end
 
225
      end
 
226
 
 
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
 
232
            it "runs 1" do
 
233
              example_1_has_run = true
 
234
            end
 
235
            it "runs 2" do
 
236
              example_2_has_run = true
 
237
            end
 
238
          end
 
239
 
 
240
          @options.examples = nil
 
241
 
 
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
 
246
        end
 
247
 
 
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
 
252
            it "runs 1" do
 
253
              example_1_has_run = true
 
254
            end
 
255
            it "runs 2" do
 
256
              example_2_has_run = true
 
257
            end
 
258
          end
 
259
 
 
260
          @options.examples = []
 
261
 
 
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
 
266
        end
 
267
      end
 
268
 
 
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
 
273
            end
 
274
          end
 
275
 
 
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
 
280
        end
 
281
      end
 
282
 
 
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
 
286
          end
 
287
          @options.add_example_group @example_group
 
288
          @options.example_groups.should include(@example_group)
 
289
 
 
290
          @options.remove_example_group @example_group
 
291
          @options.example_groups.should_not include(@example_group)
 
292
        end
 
293
      end
 
294
 
 
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).
 
299
            with(@options).
 
300
            and_return(runner)
 
301
          @options.user_input_for_runner = nil
 
302
 
 
303
          @options.run_examples
 
304
        end
 
305
 
 
306
        it "should use a custom runner when given" do
 
307
          runner = Custom::ExampleGroupRunner.new(@options, nil)
 
308
          Custom::ExampleGroupRunner.should_receive(:new).
 
309
            with(@options, nil).
 
310
            and_return(runner)
 
311
          @options.user_input_for_runner = "Custom::ExampleGroupRunner"
 
312
 
 
313
          @options.run_examples
 
314
        end
 
315
 
 
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').
 
320
            and_return(runner)
 
321
          @options.user_input_for_runner = "Custom::ExampleGroupRunner:something"
 
322
 
 
323
          @options.run_examples
 
324
        end
 
325
 
 
326
        describe "when there are examples" do
 
327
          before(:each) do
 
328
            @options.add_example_group Class.new(::Spec::Example::ExampleGroup)
 
329
            @options.formatters << Formatter::BaseTextFormatter.new(@options, @out)
 
330
          end
 
331
 
 
332
          it "runs the Examples and outputs the result" do
 
333
            @options.run_examples
 
334
            @out.string.should include("0 examples, 0 failures")
 
335
          end
 
336
 
 
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
 
341
          end
 
342
        end
 
343
 
 
344
        describe "when there are no examples" do
 
345
          before(:each) do
 
346
            @options.formatters << Formatter::BaseTextFormatter.new(@options, @out)
 
347
          end
 
348
 
 
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")
 
353
          end
 
354
 
 
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
 
359
          end
 
360
        end
 
361
      end
 
362
    end
 
363
  end
 
364
end