PageRenderTime 83ms CodeModel.GetById 17ms app.highlight 63ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/bundle/jruby/2.1/gems/rspec-core-2.14.8/spec/rspec/core/option_parser_spec.rb

https://github.com/delowong/logstash
Ruby | 242 lines | 203 code | 39 blank | 0 comment | 4 complexity | bd3d1d4ec1d8e58365ac93f1851d724f MD5 | raw file
  1require "spec_helper"
  2
  3module RSpec::Core
  4  describe OptionParser do
  5    let(:output_file){ mock File }
  6
  7    before do
  8      RSpec.stub(:deprecate)
  9      File.stub(:open).with("foo.txt",'w') { (output_file) }
 10    end
 11
 12    it "does not parse empty args" do
 13      parser = Parser.new
 14      OptionParser.should_not_receive(:new)
 15      parser.parse!([])
 16    end
 17
 18    it "proposes you to use --help and returns an error on incorrect argument" do
 19      parser = Parser.new
 20      option = "--my_wrong_arg"
 21
 22      parser.should_receive(:abort) do |msg|
 23        expect(msg).to include('use --help', option)
 24      end
 25
 26      parser.parse!([option])
 27    end
 28
 29    describe "--formatter" do
 30      it "is deprecated" do
 31        RSpec.should_receive(:deprecate)
 32        Parser.parse!(%w[--formatter doc])
 33      end
 34
 35      it "gets converted to --format" do
 36        options = Parser.parse!(%w[--formatter doc])
 37        expect(options[:formatters].first).to eq(["doc"])
 38      end
 39    end
 40
 41    describe "--default_path" do
 42      it "gets converted to --default-path" do
 43        options = Parser.parse!(%w[--default_path foo])
 44        expect(options[:default_path]).to eq "foo"
 45      end
 46    end
 47
 48    describe "--line_number" do
 49      it "gets converted to --line-number" do
 50        options = Parser.parse!(%w[--line_number 3])
 51        expect(options[:line_numbers]).to eq ["3"]
 52      end
 53    end
 54
 55
 56    describe "--default-path" do
 57      it "sets the default path where RSpec looks for examples" do
 58        options = Parser.parse!(%w[--default-path foo])
 59        expect(options[:default_path]).to eq "foo"
 60      end
 61    end
 62
 63    %w[--line-number -l].each do |option|
 64      describe option do
 65        it "sets the line number of an example to run" do
 66          options = Parser.parse!([option, "3"])
 67          expect(options[:line_numbers]).to eq ["3"]
 68        end
 69      end
 70    end
 71
 72    %w[--format -f].each do |option|
 73      describe option do
 74        it "defines the formatter" do
 75          options = Parser.parse!([option, 'doc'])
 76          expect(options[:formatters].first).to eq(["doc"])
 77        end
 78      end
 79    end
 80
 81    %w[--out -o].each do |option|
 82      describe option do
 83        let(:options) { Parser.parse!([option, 'out.txt']) }
 84
 85        it "sets the output stream for the formatter" do
 86          expect(options[:formatters].last).to eq(['progress', 'out.txt'])
 87        end
 88
 89        context "with multiple formatters" do
 90          context "after last formatter" do
 91            it "sets the output stream for the last formatter" do
 92              options = Parser.parse!(['-f', 'progress', '-f', 'doc', option, 'out.txt'])
 93              expect(options[:formatters][0]).to eq(['progress'])
 94              expect(options[:formatters][1]).to eq(['doc', 'out.txt'])
 95            end
 96          end
 97
 98          context "after first formatter" do
 99            it "sets the output stream for the first formatter" do
100              options = Parser.parse!(['-f', 'progress', option, 'out.txt', '-f', 'doc'])
101              expect(options[:formatters][0]).to eq(['progress', 'out.txt'])
102              expect(options[:formatters][1]).to eq(['doc'])
103            end
104          end
105        end
106      end
107    end
108
109    %w[--example -e].each do |option|
110      describe option do
111        it "escapes the arg" do
112          options = Parser.parse!([option, "this (and that)"])
113          expect(options[:full_description].length).to eq(1)
114          expect("this (and that)").to match(options[:full_description].first)
115        end
116      end
117    end
118
119    %w[--pattern -P].each do |option|
120      describe option do
121        it "sets the filename pattern" do
122          options = Parser.parse!([option, 'spec/**/*.spec'])
123          expect(options[:pattern]).to eq('spec/**/*.spec')
124        end
125      end
126    end
127
128    %w[--tag -t].each do |option|
129      describe option do
130        context "without ~" do
131          it "treats no value as true" do
132            options = Parser.parse!([option, 'foo'])
133            expect(options[:inclusion_filter]).to eq(:foo => true)
134          end
135
136          it "treats 'true' as true" do
137            options = Parser.parse!([option, 'foo:true'])
138            expect(options[:inclusion_filter]).to eq(:foo => true)
139          end
140
141          it "treats 'nil' as nil" do
142            options = Parser.parse!([option, 'foo:nil'])
143            expect(options[:inclusion_filter]).to eq(:foo => nil)
144          end
145
146          it "treats 'false' as false" do
147            options = Parser.parse!([option, 'foo:false'])
148            expect(options[:inclusion_filter]).to eq(:foo => false)
149          end
150
151          it "merges muliple invocations" do
152            options = Parser.parse!([option, 'foo:false', option, 'bar:true', option, 'foo:true'])
153            expect(options[:inclusion_filter]).to eq(:foo => true, :bar => true)
154          end
155
156          it "treats 'any_string' as 'any_string'" do
157            options = Parser.parse!([option, 'foo:any_string'])
158            expect(options[:inclusion_filter]).to eq(:foo => 'any_string')
159          end
160        end
161
162        context "with ~" do
163          it "treats no value as true" do
164            options = Parser.parse!([option, '~foo'])
165            expect(options[:exclusion_filter]).to eq(:foo => true)
166          end
167
168          it "treats 'true' as true" do
169            options = Parser.parse!([option, '~foo:true'])
170            expect(options[:exclusion_filter]).to eq(:foo => true)
171          end
172
173          it "treats 'nil' as nil" do
174            options = Parser.parse!([option, '~foo:nil'])
175            expect(options[:exclusion_filter]).to eq(:foo => nil)
176          end
177
178          it "treats 'false' as false" do
179            options = Parser.parse!([option, '~foo:false'])
180            expect(options[:exclusion_filter]).to eq(:foo => false)
181          end
182        end
183      end
184    end
185
186    describe "--order" do
187      it "is nil by default" do
188        expect(Parser.parse!([])[:order]).to be_nil
189      end
190
191      %w[rand random].each do |option|
192        context "with #{option}" do
193          it "defines the order as random" do
194            options = Parser.parse!(['--order', option])
195            expect(options[:order]).to eq(option)
196          end
197        end
198      end
199    end
200
201    describe "--seed" do
202      it "sets the order to rand:SEED" do
203        options = Parser.parse!(%w[--seed 123])
204        expect(options[:order]).to eq("rand:123")
205      end
206    end
207
208    describe '--profile' do
209      it 'sets profile_examples to true by default' do
210        options = Parser.parse!(%w[--profile])
211        expect(options[:profile_examples]).to eq true
212      end
213
214      it 'sets profile_examples to supplied int' do
215        options = Parser.parse!(%w[--profile 10])
216        expect(options[:profile_examples]).to eq 10
217      end
218
219      it 'sets profile_examples to true when accidentally combined with path' do
220        allow(Kernel).to receive(:warn)
221        options = Parser.parse!(%w[--profile some/path])
222        expect(options[:profile_examples]).to eq true
223      end
224
225      it 'warns when accidentally combined with path' do
226        expect(Kernel).to receive(:warn) do |msg|
227          expect(msg).to match "Non integer specified as profile count"
228        end
229        options = Parser.parse!(%w[--profile some/path])
230        expect(options[:profile_examples]).to eq true
231      end
232    end
233
234    describe '--warning' do
235      it 'enables warnings' do
236        options = Parser.parse!(%w[--warnings])
237        expect(options[:warnings]).to eq true
238      end
239    end
240
241  end
242end