/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

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