PageRenderTime 58ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://github.com/delowong/logstash
Ruby | 1561 lines | 1447 code | 114 blank | 0 comment | 15 complexity | 1f548e9492452f0999b5ebdc30a16195 MD5 | raw file
Possible License(s): CC-BY-SA-3.0, BSD-3-Clause, LGPL-2.1, 0BSD, MIT, GPL-2.0, Apache-2.0, JSON
  1. require 'spec_helper'
  2. require 'tmpdir'
  3. module RSpec::Core
  4. describe Configuration do
  5. let(:config) { Configuration.new }
  6. describe "RSpec.configuration with a block" do
  7. before { RSpec.stub(:warn_deprecation) }
  8. it "is deprecated" do
  9. RSpec.should_receive(:warn_deprecation)
  10. RSpec.configuration {}
  11. end
  12. end
  13. describe '#deprecation_stream' do
  14. it 'defaults to standard error' do
  15. expect(config.deprecation_stream).to eq $stderr
  16. end
  17. it 'is configurable' do
  18. io = double 'deprecation io'
  19. config.deprecation_stream = io
  20. expect(config.deprecation_stream).to eq io
  21. end
  22. end
  23. describe "#setup_load_path_and_require" do
  24. include_context "isolate load path mutation"
  25. def absolute_path_to(dir)
  26. File.expand_path("../../../../#{dir}", __FILE__)
  27. end
  28. it 'adds `lib` to the load path' do
  29. lib_dir = absolute_path_to("lib")
  30. $LOAD_PATH.delete(lib_dir)
  31. expect($LOAD_PATH).not_to include(lib_dir)
  32. config.setup_load_path_and_require []
  33. expect($LOAD_PATH).to include(lib_dir)
  34. end
  35. it 'adds the configured `default_path` to the load path' do
  36. config.default_path = 'features'
  37. foo_dir = absolute_path_to("features")
  38. expect($LOAD_PATH).not_to include(foo_dir)
  39. config.setup_load_path_and_require []
  40. expect($LOAD_PATH).to include(foo_dir)
  41. end
  42. it 'stores the required files' do
  43. config.should_receive(:require).with('a/path')
  44. config.setup_load_path_and_require ['a/path']
  45. expect(config.requires).to eq ['a/path']
  46. end
  47. context "when `default_path` refers to a file rather than a directory" do
  48. it 'does not add it to the load path' do
  49. config.default_path = 'Rakefile'
  50. config.setup_load_path_and_require []
  51. expect($LOAD_PATH).not_to include(match(/Rakefile/))
  52. end
  53. end
  54. end
  55. describe "#load_spec_files" do
  56. it "loads files using load" do
  57. config.files_to_run = ["foo.bar", "blah_spec.rb"]
  58. config.should_receive(:load).twice
  59. config.load_spec_files
  60. end
  61. it "loads each file once, even if duplicated in list" do
  62. config.files_to_run = ["a_spec.rb", "a_spec.rb"]
  63. config.should_receive(:load).once
  64. config.load_spec_files
  65. end
  66. context "with rspec-1 loaded" do
  67. before { stub_const("Spec::VERSION::MAJOR", 1) }
  68. it "raises with a helpful message" do
  69. expect {
  70. config.load_spec_files
  71. }.to raise_error(/rspec-1 has been loaded/)
  72. end
  73. end
  74. end
  75. describe "#treat_symbols_as_metadata_keys_with_true_values?" do
  76. it 'defaults to false' do
  77. expect(config.treat_symbols_as_metadata_keys_with_true_values?).to be_false
  78. end
  79. it 'can be set to true' do
  80. config.treat_symbols_as_metadata_keys_with_true_values = true
  81. expect(config.treat_symbols_as_metadata_keys_with_true_values?).to be_true
  82. end
  83. end
  84. describe "#mock_framework" do
  85. it "defaults to :rspec" do
  86. config.should_receive(:require).with('rspec/core/mocking/with_rspec')
  87. config.mock_framework
  88. end
  89. end
  90. describe "#mock_framework="do
  91. it "delegates to mock_with" do
  92. config.should_receive(:mock_with).with(:rspec)
  93. config.mock_framework = :rspec
  94. end
  95. end
  96. shared_examples "a configurable framework adapter" do |m|
  97. it "yields a config object if the framework_module supports it" do
  98. custom_config = Struct.new(:custom_setting).new
  99. mod = Module.new
  100. mod.stub(:configuration => custom_config)
  101. config.send m, mod do |mod_config|
  102. mod_config.custom_setting = true
  103. end
  104. expect(custom_config.custom_setting).to be_true
  105. end
  106. it "raises if framework module doesn't support configuration" do
  107. mod = Module.new
  108. expect {
  109. config.send m, mod do |mod_config|
  110. end
  111. }.to raise_error(/must respond to `configuration`/)
  112. end
  113. end
  114. describe "#mock_with" do
  115. before { config.stub(:require) }
  116. it_behaves_like "a configurable framework adapter", :mock_with
  117. [:rspec, :mocha, :rr, :flexmock].each do |framework|
  118. context "with #{framework}" do
  119. it "requires the adapter for #{framework}" do
  120. config.should_receive(:require).with("rspec/core/mocking/with_#{framework}")
  121. config.mock_with framework
  122. end
  123. end
  124. end
  125. it "allows rspec-mocks to be configured with a provided block" do
  126. mod = Module.new
  127. RSpec::Mocks.configuration.should_receive(:add_stub_and_should_receive_to).with(mod)
  128. config.mock_with :rspec do |c|
  129. c.add_stub_and_should_receive_to mod
  130. end
  131. end
  132. context "with a module" do
  133. it "sets the mock_framework_adapter to that module" do
  134. mod = Module.new
  135. config.mock_with mod
  136. expect(config.mock_framework).to eq(mod)
  137. end
  138. end
  139. it "uses the null adapter when set to any unknown key" do
  140. config.should_receive(:require).with('rspec/core/mocking/with_absolutely_nothing')
  141. config.mock_with :crazy_new_mocking_framework_ive_not_yet_heard_of
  142. end
  143. context 'when there are already some example groups defined' do
  144. it 'raises an error since this setting must be applied before any groups are defined' do
  145. RSpec.world.stub(:example_groups).and_return([double.as_null_object])
  146. expect {
  147. config.mock_with :mocha
  148. }.to raise_error(/must be configured before any example groups are defined/)
  149. end
  150. it 'does not raise an error if the default `mock_with :rspec` is re-configured' do
  151. config.mock_framework # called by RSpec when configuring the first example group
  152. RSpec.world.stub(:example_groups).and_return([double.as_null_object])
  153. config.mock_with :rspec
  154. end
  155. it 'does not raise an error if re-setting the same config' do
  156. groups = []
  157. RSpec.world.stub(:example_groups => groups)
  158. config.mock_with :mocha
  159. groups << double.as_null_object
  160. config.mock_with :mocha
  161. end
  162. end
  163. end
  164. describe "#expectation_framework" do
  165. it "defaults to :rspec" do
  166. config.should_receive(:require).with('rspec/expectations')
  167. config.expectation_frameworks
  168. end
  169. end
  170. describe "#expectation_framework=" do
  171. it "delegates to expect_with=" do
  172. config.should_receive(:expect_with).with(:rspec)
  173. config.expectation_framework = :rspec
  174. end
  175. end
  176. describe "#expect_with" do
  177. before do
  178. stub_const("Test::Unit::Assertions", Module.new)
  179. config.stub(:require)
  180. end
  181. it_behaves_like "a configurable framework adapter", :expect_with
  182. [
  183. [:rspec, 'rspec/expectations'],
  184. [:stdlib, 'test/unit/assertions']
  185. ].each do |framework, required_file|
  186. context "with #{framework}" do
  187. it "requires #{required_file}" do
  188. config.should_receive(:require).with(required_file)
  189. config.expect_with framework
  190. end
  191. end
  192. end
  193. it "supports multiple calls" do
  194. config.expect_with :rspec
  195. config.expect_with :stdlib
  196. expect(config.expectation_frameworks).to eq [RSpec::Matchers, Test::Unit::Assertions]
  197. end
  198. it "raises if block given with multiple args" do
  199. expect {
  200. config.expect_with :rspec, :stdlib do |mod_config|
  201. end
  202. }.to raise_error(/expect_with only accepts/)
  203. end
  204. it "raises ArgumentError if framework is not supported" do
  205. expect do
  206. config.expect_with :not_supported
  207. end.to raise_error(ArgumentError)
  208. end
  209. context 'when there are already some example groups defined' do
  210. it 'raises an error since this setting must be applied before any groups are defined' do
  211. RSpec.world.stub(:example_groups).and_return([double.as_null_object])
  212. expect {
  213. config.expect_with :rspec
  214. }.to raise_error(/must be configured before any example groups are defined/)
  215. end
  216. it 'does not raise an error if the default `expect_with :rspec` is re-configured' do
  217. config.expectation_frameworks # called by RSpec when configuring the first example group
  218. RSpec.world.stub(:example_groups).and_return([double.as_null_object])
  219. config.expect_with :rspec
  220. end
  221. it 'does not raise an error if re-setting the same config' do
  222. groups = []
  223. RSpec.world.stub(:example_groups => groups)
  224. config.expect_with :stdlib
  225. groups << double.as_null_object
  226. config.expect_with :stdlib
  227. end
  228. end
  229. end
  230. describe "#expecting_with_rspec?" do
  231. before do
  232. stub_const("Test::Unit::Assertions", Module.new)
  233. config.stub(:require)
  234. end
  235. it "returns false by default" do
  236. expect(config).not_to be_expecting_with_rspec
  237. end
  238. it "returns true when `expect_with :rspec` has been configured" do
  239. config.expect_with :rspec
  240. expect(config).to be_expecting_with_rspec
  241. end
  242. it "returns true when `expect_with :rspec, :stdlib` has been configured" do
  243. config.expect_with :rspec, :stdlib
  244. expect(config).to be_expecting_with_rspec
  245. end
  246. it "returns true when `expect_with :stdlib, :rspec` has been configured" do
  247. config.expect_with :stdlib, :rspec
  248. expect(config).to be_expecting_with_rspec
  249. end
  250. it "returns false when `expect_with :stdlib` has been configured" do
  251. config.expect_with :stdlib
  252. expect(config).not_to be_expecting_with_rspec
  253. end
  254. end
  255. describe "#files_to_run" do
  256. it "loads files not following pattern if named explicitly" do
  257. config.files_or_directories_to_run = "spec/rspec/core/resources/a_bar.rb"
  258. expect(config.files_to_run).to eq([ "spec/rspec/core/resources/a_bar.rb"])
  259. end
  260. it "prevents repetition of dir when start of the pattern" do
  261. config.pattern = "spec/**/a_spec.rb"
  262. config.files_or_directories_to_run = "spec"
  263. expect(config.files_to_run).to eq(["spec/rspec/core/resources/a_spec.rb"])
  264. end
  265. it "does not prevent repetition of dir when later of the pattern" do
  266. config.pattern = "rspec/**/a_spec.rb"
  267. config.files_or_directories_to_run = "spec"
  268. expect(config.files_to_run).to eq(["spec/rspec/core/resources/a_spec.rb"])
  269. end
  270. context "with <path>:<line_number>" do
  271. it "overrides inclusion filters set on config" do
  272. config.filter_run_including :foo => :bar
  273. config.files_or_directories_to_run = "path/to/file.rb:37"
  274. expect(config.inclusion_filter.size).to eq(1)
  275. expect(config.inclusion_filter[:locations].keys.first).to match(/path\/to\/file\.rb$/)
  276. expect(config.inclusion_filter[:locations].values.first).to eq([37])
  277. end
  278. it "overrides inclusion filters set before config" do
  279. config.force(:inclusion_filter => {:foo => :bar})
  280. config.files_or_directories_to_run = "path/to/file.rb:37"
  281. expect(config.inclusion_filter.size).to eq(1)
  282. expect(config.inclusion_filter[:locations].keys.first).to match(/path\/to\/file\.rb$/)
  283. expect(config.inclusion_filter[:locations].values.first).to eq([37])
  284. end
  285. it "clears exclusion filters set on config" do
  286. config.exclusion_filter = { :foo => :bar }
  287. config.files_or_directories_to_run = "path/to/file.rb:37"
  288. expect(config.exclusion_filter).to be_empty,
  289. "expected exclusion filter to be empty:\n#{config.exclusion_filter}"
  290. end
  291. it "clears exclusion filters set before config" do
  292. config.force(:exclusion_filter => { :foo => :bar })
  293. config.files_or_directories_to_run = "path/to/file.rb:37"
  294. expect(config.exclusion_filter).to be_empty,
  295. "expected exclusion filter to be empty:\n#{config.exclusion_filter}"
  296. end
  297. end
  298. context "with default pattern" do
  299. it "loads files named _spec.rb" do
  300. config.files_or_directories_to_run = "spec/rspec/core/resources"
  301. expect(config.files_to_run).to eq([ "spec/rspec/core/resources/a_spec.rb"])
  302. end
  303. it "loads files in Windows", :if => RSpec.windows_os? do
  304. config.files_or_directories_to_run = "C:\\path\\to\\project\\spec\\sub\\foo_spec.rb"
  305. expect(config.files_to_run).to eq([ "C:/path/to/project/spec/sub/foo_spec.rb"])
  306. end
  307. it "loads files in Windows when directory is specified", :if => RSpec.windows_os? do
  308. config.files_or_directories_to_run = "spec\\rspec\\core\\resources"
  309. expect(config.files_to_run).to eq([ "spec/rspec/core/resources/a_spec.rb"])
  310. end
  311. end
  312. context "with default default_path" do
  313. it "loads files in the default path when run by rspec" do
  314. config.stub(:command) { 'rspec' }
  315. config.files_or_directories_to_run = []
  316. expect(config.files_to_run).not_to be_empty
  317. end
  318. it "loads files in the default path when run with DRB (e.g., spork)" do
  319. config.stub(:command) { 'spork' }
  320. RSpec::Core::Runner.stub(:running_in_drb?) { true }
  321. config.files_or_directories_to_run = []
  322. expect(config.files_to_run).not_to be_empty
  323. end
  324. it "does not load files in the default path when run by ruby" do
  325. config.stub(:command) { 'ruby' }
  326. config.files_or_directories_to_run = []
  327. expect(config.files_to_run).to be_empty
  328. end
  329. end
  330. def specify_consistent_ordering_of_files_to_run
  331. File.stub(:directory?).with('a') { true }
  332. orderings = [
  333. %w[ a/1.rb a/2.rb a/3.rb ],
  334. %w[ a/2.rb a/1.rb a/3.rb ],
  335. %w[ a/3.rb a/2.rb a/1.rb ]
  336. ].map do |files|
  337. Dir.should_receive(:[]).with(/^\{?a/) { files }
  338. yield
  339. config.files_to_run
  340. end
  341. expect(orderings.uniq.size).to eq(1)
  342. end
  343. context 'when the given directories match the pattern' do
  344. it 'orders the files in a consistent ordering, regardless of the underlying OS ordering' do
  345. specify_consistent_ordering_of_files_to_run do
  346. config.pattern = 'a/*.rb'
  347. config.files_or_directories_to_run = 'a'
  348. end
  349. end
  350. end
  351. context 'when the pattern is given relative to the given directories' do
  352. it 'orders the files in a consistent ordering, regardless of the underlying OS ordering' do
  353. specify_consistent_ordering_of_files_to_run do
  354. config.pattern = '*.rb'
  355. config.files_or_directories_to_run = 'a'
  356. end
  357. end
  358. end
  359. context 'when given multiple file paths' do
  360. it 'orders the files in a consistent ordering, regardless of the given order' do
  361. File.stub(:directory?) { false } # fake it into thinking these a full file paths
  362. files = ['a/b/c_spec.rb', 'c/b/a_spec.rb']
  363. config.files_or_directories_to_run = *files
  364. ordering_1 = config.files_to_run
  365. config.files_or_directories_to_run = *(files.reverse)
  366. ordering_2 = config.files_to_run
  367. expect(ordering_1).to eq(ordering_2)
  368. end
  369. end
  370. end
  371. %w[pattern= filename_pattern=].each do |setter|
  372. describe "##{setter}" do
  373. context "with single pattern" do
  374. before { config.send(setter, "**/*_foo.rb") }
  375. it "loads files following pattern" do
  376. file = File.expand_path(File.dirname(__FILE__) + "/resources/a_foo.rb")
  377. config.files_or_directories_to_run = file
  378. expect(config.files_to_run).to include(file)
  379. end
  380. it "loads files in directories following pattern" do
  381. dir = File.expand_path(File.dirname(__FILE__) + "/resources")
  382. config.files_or_directories_to_run = dir
  383. expect(config.files_to_run).to include("#{dir}/a_foo.rb")
  384. end
  385. it "does not load files in directories not following pattern" do
  386. dir = File.expand_path(File.dirname(__FILE__) + "/resources")
  387. config.files_or_directories_to_run = dir
  388. expect(config.files_to_run).not_to include("#{dir}/a_bar.rb")
  389. end
  390. end
  391. context "with multiple patterns" do
  392. it "supports comma separated values" do
  393. config.send(setter, "**/*_foo.rb,**/*_bar.rb")
  394. dir = File.expand_path(File.dirname(__FILE__) + "/resources")
  395. config.files_or_directories_to_run = dir
  396. expect(config.files_to_run).to include("#{dir}/a_foo.rb")
  397. expect(config.files_to_run).to include("#{dir}/a_bar.rb")
  398. end
  399. it "supports comma separated values with spaces" do
  400. config.send(setter, "**/*_foo.rb, **/*_bar.rb")
  401. dir = File.expand_path(File.dirname(__FILE__) + "/resources")
  402. config.files_or_directories_to_run = dir
  403. expect(config.files_to_run).to include("#{dir}/a_foo.rb")
  404. expect(config.files_to_run).to include("#{dir}/a_bar.rb")
  405. end
  406. it "supports curly braces glob syntax" do
  407. config.send(setter, "**/*_{foo,bar}.rb")
  408. dir = File.expand_path(File.dirname(__FILE__) + "/resources")
  409. config.files_or_directories_to_run = dir
  410. expect(config.files_to_run).to include("#{dir}/a_foo.rb")
  411. expect(config.files_to_run).to include("#{dir}/a_bar.rb")
  412. end
  413. end
  414. end
  415. end
  416. describe "path with line number" do
  417. it "assigns the line number as a location filter" do
  418. config.files_or_directories_to_run = "path/to/a_spec.rb:37"
  419. expect(config.filter).to eq({:locations => {File.expand_path("path/to/a_spec.rb") => [37]}})
  420. end
  421. end
  422. context "with full_description set" do
  423. it "overrides filters" do
  424. config.filter_run :focused => true
  425. config.full_description = "foo"
  426. expect(config.filter).not_to have_key(:focused)
  427. end
  428. it 'is possible to access the full description regular expression' do
  429. config.full_description = "foo"
  430. expect(config.full_description).to eq(/foo/)
  431. end
  432. end
  433. context "without full_description having been set" do
  434. it 'returns nil from #full_description' do
  435. expect(config.full_description).to eq nil
  436. end
  437. end
  438. context "with line number" do
  439. it "assigns the file and line number as a location filter" do
  440. config.files_or_directories_to_run = "path/to/a_spec.rb:37"
  441. expect(config.filter).to eq({:locations => {File.expand_path("path/to/a_spec.rb") => [37]}})
  442. end
  443. it "assigns multiple files with line numbers as location filters" do
  444. config.files_or_directories_to_run = "path/to/a_spec.rb:37", "other_spec.rb:44"
  445. expect(config.filter).to eq({:locations => {File.expand_path("path/to/a_spec.rb") => [37],
  446. File.expand_path("other_spec.rb") => [44]}})
  447. end
  448. it "assigns files with multiple line numbers as location filters" do
  449. config.files_or_directories_to_run = "path/to/a_spec.rb:37", "path/to/a_spec.rb:44"
  450. expect(config.filter).to eq({:locations => {File.expand_path("path/to/a_spec.rb") => [37, 44]}})
  451. end
  452. end
  453. context "with multiple line numbers" do
  454. it "assigns the file and line numbers as a location filter" do
  455. config.files_or_directories_to_run = "path/to/a_spec.rb:1:3:5:7"
  456. expect(config.filter).to eq({:locations => {File.expand_path("path/to/a_spec.rb") => [1,3,5,7]}})
  457. end
  458. end
  459. it "assigns the example name as the filter on description" do
  460. config.full_description = "foo"
  461. expect(config.filter).to eq({:full_description => /foo/})
  462. end
  463. it "assigns the example names as the filter on description if description is an array" do
  464. config.full_description = [ "foo", "bar" ]
  465. expect(config.filter).to eq({:full_description => Regexp.union(/foo/, /bar/)})
  466. end
  467. it 'is possible to access the full description regular expression' do
  468. config.full_description = "foo","bar"
  469. expect(config.full_description).to eq Regexp.union(/foo/,/bar/)
  470. end
  471. describe "#default_path" do
  472. it 'defaults to "spec"' do
  473. expect(config.default_path).to eq('spec')
  474. end
  475. end
  476. describe "#include" do
  477. module InstanceLevelMethods
  478. def you_call_this_a_blt?
  479. "egad man, where's the mayo?!?!?"
  480. end
  481. end
  482. it_behaves_like "metadata hash builder" do
  483. def metadata_hash(*args)
  484. config.include(InstanceLevelMethods, *args)
  485. config.include_or_extend_modules.last.last
  486. end
  487. end
  488. context "with no filter" do
  489. it "includes the given module into each example group" do
  490. RSpec.configure do |c|
  491. c.include(InstanceLevelMethods)
  492. end
  493. group = ExampleGroup.describe('does like, stuff and junk', :magic_key => :include) { }
  494. expect(group).not_to respond_to(:you_call_this_a_blt?)
  495. expect(group.new.you_call_this_a_blt?).to eq("egad man, where's the mayo?!?!?")
  496. end
  497. end
  498. context "with a filter" do
  499. it "includes the given module into each matching example group" do
  500. RSpec.configure do |c|
  501. c.include(InstanceLevelMethods, :magic_key => :include)
  502. end
  503. group = ExampleGroup.describe('does like, stuff and junk', :magic_key => :include) { }
  504. expect(group).not_to respond_to(:you_call_this_a_blt?)
  505. expect(group.new.you_call_this_a_blt?).to eq("egad man, where's the mayo?!?!?")
  506. end
  507. end
  508. end
  509. describe "#extend" do
  510. module ThatThingISentYou
  511. def that_thing
  512. end
  513. end
  514. it_behaves_like "metadata hash builder" do
  515. def metadata_hash(*args)
  516. config.extend(ThatThingISentYou, *args)
  517. config.include_or_extend_modules.last.last
  518. end
  519. end
  520. it "extends the given module into each matching example group" do
  521. RSpec.configure do |c|
  522. c.extend(ThatThingISentYou, :magic_key => :extend)
  523. end
  524. group = ExampleGroup.describe(ThatThingISentYou, :magic_key => :extend) { }
  525. expect(group).to respond_to(:that_thing)
  526. end
  527. end
  528. describe "#run_all_when_everything_filtered?" do
  529. it "defaults to false" do
  530. expect(config.run_all_when_everything_filtered?).to be_false
  531. end
  532. it "can be queried with question method" do
  533. config.run_all_when_everything_filtered = true
  534. expect(config.run_all_when_everything_filtered?).to be_true
  535. end
  536. end
  537. %w[color color_enabled].each do |color_option|
  538. describe "##{color_option}=" do
  539. context "given true" do
  540. before { config.send "#{color_option}=", true }
  541. context "with config.tty? and output.tty?" do
  542. it "does not set color_enabled" do
  543. output = StringIO.new
  544. config.output_stream = output
  545. config.tty = true
  546. config.output_stream.stub :tty? => true
  547. expect(config.send(color_option)).to be_true
  548. expect(config.send(color_option, output)).to be_true
  549. end
  550. end
  551. context "with config.tty? and !output.tty?" do
  552. it "sets color_enabled" do
  553. output = StringIO.new
  554. config.output_stream = output
  555. config.tty = true
  556. config.output_stream.stub :tty? => false
  557. expect(config.send(color_option)).to be_true
  558. expect(config.send(color_option, output)).to be_true
  559. end
  560. end
  561. context "with config.tty? and !output.tty?" do
  562. it "does not set color_enabled" do
  563. output = StringIO.new
  564. config.output_stream = output
  565. config.tty = false
  566. config.output_stream.stub :tty? => true
  567. expect(config.send(color_option)).to be_true
  568. expect(config.send(color_option, output)).to be_true
  569. end
  570. end
  571. context "with !config.tty? and !output.tty?" do
  572. it "does not set color_enabled" do
  573. output = StringIO.new
  574. config.output_stream = output
  575. config.tty = false
  576. config.output_stream.stub :tty? => false
  577. expect(config.send(color_option)).to be_false
  578. expect(config.send(color_option, output)).to be_false
  579. end
  580. end
  581. context "on windows" do
  582. before do
  583. @original_host = RbConfig::CONFIG['host_os']
  584. RbConfig::CONFIG['host_os'] = 'mingw'
  585. config.stub(:require)
  586. config.stub(:warn)
  587. end
  588. after do
  589. RbConfig::CONFIG['host_os'] = @original_host
  590. end
  591. context "with ANSICON available" do
  592. around(:each) { |e| with_env_vars('ANSICON' => 'ANSICON', &e) }
  593. it "enables colors" do
  594. config.output_stream = StringIO.new
  595. config.output_stream.stub :tty? => true
  596. config.send "#{color_option}=", true
  597. expect(config.send(color_option)).to be_true
  598. end
  599. it "leaves output stream intact" do
  600. config.output_stream = $stdout
  601. config.stub(:require) do |what|
  602. config.output_stream = 'foo' if what =~ /Win32/
  603. end
  604. config.send "#{color_option}=", true
  605. expect(config.output_stream).to eq($stdout)
  606. end
  607. end
  608. context "with ANSICON NOT available" do
  609. it "warns to install ANSICON" do
  610. config.stub(:require) { raise LoadError }
  611. config.should_receive(:warn).
  612. with(/You must use ANSICON/)
  613. config.send "#{color_option}=", true
  614. end
  615. it "sets color_enabled to false" do
  616. config.stub(:require) { raise LoadError }
  617. config.send "#{color_option}=", true
  618. config.color_enabled = true
  619. expect(config.send(color_option)).to be_false
  620. end
  621. end
  622. end
  623. end
  624. end
  625. it "prefers incoming cli_args" do
  626. config.output_stream = StringIO.new
  627. config.output_stream.stub :tty? => true
  628. config.force :color => true
  629. config.color = false
  630. expect(config.color).to be_true
  631. end
  632. end
  633. describe '#formatter=' do
  634. it "delegates to add_formatter (better API for user-facing configuration)" do
  635. config.should_receive(:add_formatter).with('these','options')
  636. config.add_formatter('these','options')
  637. end
  638. end
  639. describe "#add_formatter" do
  640. it "adds to the list of formatters" do
  641. config.add_formatter :documentation
  642. expect(config.formatters.first).to be_an_instance_of(Formatters::DocumentationFormatter)
  643. end
  644. it "finds a formatter by name (w/ Symbol)" do
  645. config.add_formatter :documentation
  646. expect(config.formatters.first).to be_an_instance_of(Formatters::DocumentationFormatter)
  647. end
  648. it "finds a formatter by name (w/ String)" do
  649. config.add_formatter 'documentation'
  650. expect(config.formatters.first).to be_an_instance_of(Formatters::DocumentationFormatter)
  651. end
  652. it "finds a formatter by class" do
  653. formatter_class = Class.new(Formatters::BaseTextFormatter)
  654. config.add_formatter formatter_class
  655. expect(config.formatters.first).to be_an_instance_of(formatter_class)
  656. end
  657. it "finds a formatter by class name" do
  658. stub_const("CustomFormatter", Class.new(Formatters::BaseFormatter))
  659. config.add_formatter "CustomFormatter"
  660. expect(config.formatters.first).to be_an_instance_of(CustomFormatter)
  661. end
  662. it "finds a formatter by class fully qualified name" do
  663. stub_const("RSpec::CustomFormatter", Class.new(Formatters::BaseFormatter))
  664. config.add_formatter "RSpec::CustomFormatter"
  665. expect(config.formatters.first).to be_an_instance_of(RSpec::CustomFormatter)
  666. end
  667. it "requires a formatter file based on its fully qualified name" do
  668. config.should_receive(:require).with('rspec/custom_formatter') do
  669. stub_const("RSpec::CustomFormatter", Class.new(Formatters::BaseFormatter))
  670. end
  671. config.add_formatter "RSpec::CustomFormatter"
  672. expect(config.formatters.first).to be_an_instance_of(RSpec::CustomFormatter)
  673. end
  674. it "raises NameError if class is unresolvable" do
  675. config.should_receive(:require).with('rspec/custom_formatter3')
  676. expect(lambda { config.add_formatter "RSpec::CustomFormatter3" }).to raise_error(NameError)
  677. end
  678. it "raises ArgumentError if formatter is unknown" do
  679. expect(lambda { config.add_formatter :progresss }).to raise_error(ArgumentError)
  680. end
  681. context "with a 2nd arg defining the output" do
  682. it "creates a file at that path and sets it as the output" do
  683. path = File.join(Dir.tmpdir, 'output.txt')
  684. config.add_formatter('doc', path)
  685. expect(config.formatters.first.output).to be_a(File)
  686. expect(config.formatters.first.output.path).to eq(path)
  687. end
  688. end
  689. end
  690. describe "#filter_run_including" do
  691. it_behaves_like "metadata hash builder" do
  692. def metadata_hash(*args)
  693. config.filter_run_including(*args)
  694. config.inclusion_filter
  695. end
  696. end
  697. it "sets the filter with a hash" do
  698. config.filter_run_including :foo => true
  699. expect(config.inclusion_filter[:foo]).to be(true)
  700. end
  701. it "sets the filter with a symbol" do
  702. RSpec.configuration.stub(:treat_symbols_as_metadata_keys_with_true_values? => true)
  703. config.filter_run_including :foo
  704. expect(config.inclusion_filter[:foo]).to be(true)
  705. end
  706. it "merges with existing filters" do
  707. config.filter_run_including :foo => true
  708. config.filter_run_including :bar => false
  709. expect(config.inclusion_filter[:foo]).to be(true)
  710. expect(config.inclusion_filter[:bar]).to be(false)
  711. end
  712. end
  713. describe "#filter_run_excluding" do
  714. it_behaves_like "metadata hash builder" do
  715. def metadata_hash(*args)
  716. config.filter_run_excluding(*args)
  717. config.exclusion_filter
  718. end
  719. end
  720. it "sets the filter" do
  721. config.filter_run_excluding :foo => true
  722. expect(config.exclusion_filter[:foo]).to be(true)
  723. end
  724. it "sets the filter using a symbol" do
  725. RSpec.configuration.stub(:treat_symbols_as_metadata_keys_with_true_values? => true)
  726. config.filter_run_excluding :foo
  727. expect(config.exclusion_filter[:foo]).to be(true)
  728. end
  729. it "merges with existing filters" do
  730. config.filter_run_excluding :foo => true
  731. config.filter_run_excluding :bar => false
  732. expect(config.exclusion_filter[:foo]).to be(true)
  733. expect(config.exclusion_filter[:bar]).to be(false)
  734. end
  735. end
  736. describe "#inclusion_filter" do
  737. it "returns {} even if set to nil" do
  738. config.inclusion_filter = nil
  739. expect(config.inclusion_filter).to eq({})
  740. end
  741. end
  742. describe "#inclusion_filter=" do
  743. it "treats symbols as hash keys with true values when told to" do
  744. RSpec.configuration.stub(:treat_symbols_as_metadata_keys_with_true_values? => true)
  745. config.inclusion_filter = :foo
  746. expect(config.inclusion_filter).to eq({:foo => true})
  747. end
  748. it "overrides any inclusion filters set on the command line or in configuration files" do
  749. config.force(:inclusion_filter => { :foo => :bar })
  750. config.inclusion_filter = {:want => :this}
  751. expect(config.inclusion_filter).to eq({:want => :this})
  752. end
  753. end
  754. describe "#exclusion_filter" do
  755. it "returns {} even if set to nil" do
  756. config.exclusion_filter = nil
  757. expect(config.exclusion_filter).to eq({})
  758. end
  759. describe "the default :if filter" do
  760. it "does not exclude a spec with { :if => true } metadata" do
  761. expect(config.exclusion_filter[:if].call(true)).to be_false
  762. end
  763. it "excludes a spec with { :if => false } metadata" do
  764. expect(config.exclusion_filter[:if].call(false)).to be_true
  765. end
  766. it "excludes a spec with { :if => nil } metadata" do
  767. expect(config.exclusion_filter[:if].call(nil)).to be_true
  768. end
  769. end
  770. describe "the default :unless filter" do
  771. it "excludes a spec with { :unless => true } metadata" do
  772. expect(config.exclusion_filter[:unless].call(true)).to be_true
  773. end
  774. it "does not exclude a spec with { :unless => false } metadata" do
  775. expect(config.exclusion_filter[:unless].call(false)).to be_false
  776. end
  777. it "does not exclude a spec with { :unless => nil } metadata" do
  778. expect(config.exclusion_filter[:unless].call(nil)).to be_false
  779. end
  780. end
  781. end
  782. describe "#exclusion_filter=" do
  783. it "treats symbols as hash keys with true values when told to" do
  784. RSpec.configuration.stub(:treat_symbols_as_metadata_keys_with_true_values? => true)
  785. config.exclusion_filter = :foo
  786. expect(config.exclusion_filter).to eq({:foo => true})
  787. end
  788. it "overrides any exclusion filters set on the command line or in configuration files" do
  789. config.force(:exclusion_filter => { :foo => :bar })
  790. config.exclusion_filter = {:want => :this}
  791. expect(config.exclusion_filter).to eq({:want => :this})
  792. end
  793. end
  794. describe "line_numbers=" do
  795. before { config.filter_manager.stub(:warn) }
  796. it "sets the line numbers" do
  797. config.line_numbers = ['37']
  798. expect(config.filter).to eq({:line_numbers => [37]})
  799. end
  800. it "overrides filters" do
  801. config.filter_run :focused => true
  802. config.line_numbers = ['37']
  803. expect(config.filter).to eq({:line_numbers => [37]})
  804. end
  805. it "prevents subsequent filters" do
  806. config.line_numbers = ['37']
  807. config.filter_run :focused => true
  808. expect(config.filter).to eq({:line_numbers => [37]})
  809. end
  810. end
  811. describe "line_numbers" do
  812. it "returns the line numbers from the filter" do
  813. config.line_numbers = ['42']
  814. expect(config.line_numbers).to eq [42]
  815. end
  816. it "defaults to empty" do
  817. expect(config.line_numbers).to eq []
  818. end
  819. end
  820. describe "#full_backtrace=" do
  821. context "given true" do
  822. it "clears the backtrace exclusion patterns" do
  823. config.full_backtrace = true
  824. expect(config.backtrace_exclusion_patterns).to eq([])
  825. end
  826. end
  827. context "given false" do
  828. it "restores backtrace clean patterns" do
  829. config.full_backtrace = false
  830. expect(config.backtrace_exclusion_patterns).to eq(RSpec::Core::BacktraceCleaner::DEFAULT_EXCLUSION_PATTERNS)
  831. end
  832. end
  833. it "doesn't impact other instances of config" do
  834. config_1 = Configuration.new
  835. config_2 = Configuration.new
  836. config_1.full_backtrace = true
  837. expect(config_2.backtrace_exclusion_patterns).not_to be_empty
  838. end
  839. end
  840. describe "#backtrace_clean_patterns=" do
  841. it "actually receives the new filter values" do
  842. RSpec.stub(:deprecate)
  843. config = Configuration.new
  844. config.backtrace_clean_patterns = [/.*/]
  845. expect(config.backtrace_cleaner.exclude? "this").to be_true
  846. end
  847. end
  848. describe 'full_backtrace' do
  849. it 'returns true when backtrace patterns is empty' do
  850. config.backtrace_exclusion_patterns = []
  851. expect(config.full_backtrace?).to eq true
  852. end
  853. it 'returns false when backtrace patterns isnt empty' do
  854. config.backtrace_exclusion_patterns = [:lib]
  855. expect(config.full_backtrace?).to eq false
  856. end
  857. end
  858. describe "#backtrace_clean_patterns" do
  859. before { allow(RSpec).to receive(:deprecate) }
  860. it "is deprecated" do
  861. RSpec.should_receive(:deprecate)
  862. config = Configuration.new
  863. config.backtrace_clean_patterns
  864. end
  865. it "can be appended to" do
  866. config = Configuration.new
  867. config.backtrace_clean_patterns << /.*/
  868. expect(config.backtrace_cleaner.exclude? "this").to be_true
  869. end
  870. end
  871. describe ".backtrace_cleaner#exclude? defaults" do
  872. it "returns true for rspec files" do
  873. expect(config.backtrace_cleaner.exclude?("lib/rspec/core.rb")).to be_true
  874. end
  875. it "returns true for spec_helper" do
  876. expect(config.backtrace_cleaner.exclude?("spec/spec_helper.rb")).to be_true
  877. end
  878. it "returns true for java files (for JRuby)" do
  879. expect(config.backtrace_cleaner.exclude?("org/jruby/RubyArray.java:2336")).to be_true
  880. end
  881. it "returns true for files within installed gems" do
  882. expect(config.backtrace_cleaner.exclude?('ruby-1.8.7-p334/gems/mygem-2.3.0/lib/mygem.rb')).to be_true
  883. end
  884. it "returns false for files in projects containing 'gems' in the name" do
  885. expect(config.backtrace_cleaner.exclude?('code/my-gems-plugin/lib/plugin.rb')).to be_false
  886. end
  887. it "returns false for something in the current working directory" do
  888. expect(config.backtrace_cleaner.exclude?("#{Dir.getwd}/arbitrary")).to be_false
  889. end
  890. end
  891. describe "#debug=true" do
  892. before do
  893. if defined?(Debugger)
  894. @orig_debugger = Debugger
  895. Object.send(:remove_const, :Debugger)
  896. else
  897. @orig_debugger = nil
  898. end
  899. config.stub(:require)
  900. Object.const_set("Debugger", debugger)
  901. end
  902. after do
  903. Object.send(:remove_const, :Debugger)
  904. Object.const_set("Debugger", @orig_debugger) if @orig_debugger
  905. end
  906. let(:debugger) { double('Debugger').as_null_object }
  907. it "requires 'ruby-debug'" do
  908. config.should_receive(:require).with('ruby-debug')
  909. config.debug = true
  910. end
  911. it "starts the debugger" do
  912. debugger.should_receive(:start)
  913. config.debug = true
  914. end
  915. end
  916. describe "#debug=false" do
  917. it "does not require 'ruby-debug'" do
  918. config.should_not_receive(:require).with('ruby-debug')
  919. config.debug = false
  920. end
  921. end
  922. describe "#debug?" do
  923. it 'returns true if the debugger has been loaded' do
  924. stub_const("Debugger", Object.new)
  925. expect(config.debug?).to be_true
  926. end
  927. it 'returns false if the debugger has not been loaded' do
  928. hide_const("Debugger")
  929. expect(config.debug?).to be_false
  930. end
  931. end
  932. describe "#output=" do
  933. it "sets the output" do
  934. output = double("output")
  935. config.output = output
  936. expect(config.output).to equal(output)
  937. end
  938. end
  939. describe "#libs=" do
  940. include_context "isolate load path mutation"
  941. it "adds directories to the LOAD_PATH" do
  942. $LOAD_PATH.should_receive(:unshift).with("a/dir")
  943. config.libs = ["a/dir"]
  944. end
  945. end
  946. describe "libs" do
  947. include_context "isolate load path mutation"
  948. it 'records paths added to the load path' do
  949. config.libs = ["a/dir"]
  950. expect(config.libs).to eq ["a/dir"]
  951. end
  952. end
  953. describe "#requires=" do
  954. before { RSpec.should_receive :deprecate }
  955. it "requires the configured files" do
  956. config.should_receive(:require).with('foo').ordered
  957. config.should_receive(:require).with('bar').ordered
  958. config.requires = ['foo', 'bar']
  959. end
  960. it "stores require paths" do
  961. config.should_receive(:require).with("a/path")
  962. config.requires = ["a/path"]
  963. expect(config.requires).to eq ['a/path']
  964. end
  965. end
  966. describe "#add_setting" do
  967. describe "with no modifiers" do
  968. context "with no additional options" do
  969. before do
  970. config.add_setting :custom_option
  971. end
  972. it "defaults to nil" do
  973. expect(config.custom_option).to be_nil
  974. end
  975. it "adds a predicate" do
  976. expect(config.custom_option?).to be_false
  977. end
  978. it "can be overridden" do
  979. config.custom_option = "a value"
  980. expect(config.custom_option).to eq("a value")
  981. end
  982. end
  983. context "with :default => 'a value'" do
  984. before do
  985. config.add_setting :custom_option, :default => 'a value'
  986. end
  987. it "defaults to 'a value'" do
  988. expect(config.custom_option).to eq("a value")
  989. end
  990. it "returns true for the predicate" do
  991. expect(config.custom_option?).to be_true
  992. end
  993. it "can be overridden with a truthy value" do
  994. config.custom_option = "a new value"
  995. expect(config.custom_option).to eq("a new value")
  996. end
  997. it "can be overridden with nil" do
  998. config.custom_option = nil
  999. expect(config.custom_option).to eq(nil)
  1000. end
  1001. it "can be overridden with false" do
  1002. config.custom_option = false
  1003. expect(config.custom_option).to eq(false)
  1004. end
  1005. end
  1006. end
  1007. context "with :alias => " do
  1008. it "is deprecated" do
  1009. RSpec::should_receive(:deprecate).with(/:alias option/, :replacement => ":alias_with")
  1010. config.add_setting :custom_option
  1011. config.add_setting :another_custom_option, :alias => :custom_option
  1012. end
  1013. end
  1014. context "with :alias_with => " do
  1015. before do
  1016. config.add_setting :custom_option, :alias_with => :another_custom_option
  1017. end
  1018. it "delegates the getter to the other option" do
  1019. config.another_custom_option = "this value"
  1020. expect(config.custom_option).to eq("this value")
  1021. end
  1022. it "delegates the setter to the other option" do
  1023. config.custom_option = "this value"
  1024. expect(config.another_custom_option).to eq("this value")
  1025. end
  1026. it "delegates the predicate to the other option" do
  1027. config.custom_option = true
  1028. expect(config.another_custom_option?).to be_true
  1029. end
  1030. end
  1031. end
  1032. describe "#configure_group" do
  1033. it "extends with 'extend'" do
  1034. mod = Module.new
  1035. group = ExampleGroup.describe("group", :foo => :bar)
  1036. config.extend(mod, :foo => :bar)
  1037. config.configure_group(group)
  1038. expect(group).to be_a(mod)
  1039. end
  1040. it "extends with 'module'" do
  1041. mod = Module.new
  1042. group = ExampleGroup.describe("group", :foo => :bar)
  1043. config.include(mod, :foo => :bar)
  1044. config.configure_group(group)
  1045. expect(group.included_modules).to include(mod)
  1046. end
  1047. it "requires only one matching filter" do
  1048. mod = Module.new
  1049. group = ExampleGroup.describe("group", :foo => :bar)
  1050. config.include(mod, :foo => :bar, :baz => :bam)
  1051. config.configure_group(group)
  1052. expect(group.included_modules).to include(mod)
  1053. end
  1054. it "includes each one before deciding whether to include the next" do
  1055. mod1 = Module.new do
  1056. def self.included(host)
  1057. host.metadata[:foo] = :bar
  1058. end
  1059. end
  1060. mod2 = Module.new
  1061. group = ExampleGroup.describe("group")
  1062. config.include(mod1)
  1063. config.include(mod2, :foo => :bar)
  1064. config.configure_group(group)
  1065. expect(group.included_modules).to include(mod1)
  1066. expect(group.included_modules).to include(mod2)
  1067. end
  1068. module IncludeOrExtendMeOnce
  1069. def self.included(host)
  1070. raise "included again" if host.instance_methods.include?(:foobar)
  1071. host.class_eval { def foobar; end }
  1072. end
  1073. def self.extended(host)
  1074. raise "extended again" if host.respond_to?(:foobar)
  1075. def host.foobar; end
  1076. end
  1077. end
  1078. it "doesn't include a module when already included in ancestor" do
  1079. config.include(IncludeOrExtendMeOnce, :foo => :bar)
  1080. group = ExampleGroup.describe("group", :foo => :bar)
  1081. child = group.describe("child")
  1082. config.configure_group(group)
  1083. config.configure_group(child)
  1084. end
  1085. it "doesn't extend when ancestor is already extended with same module" do
  1086. config.extend(IncludeOrExtendMeOnce, :foo => :bar)
  1087. group = ExampleGroup.describe("group", :foo => :bar)
  1088. child = group.describe("child")
  1089. config.configure_group(group)
  1090. config.configure_group(child)
  1091. end
  1092. end
  1093. describe "#alias_example_to" do
  1094. it_behaves_like "metadata hash builder" do
  1095. after do
  1096. RSpec::Core::ExampleGroup.module_eval do
  1097. class << self
  1098. undef :my_example_method if method_defined? :my_example_method
  1099. end
  1100. end
  1101. end
  1102. def metadata_hash(*args)
  1103. config.alias_example_to :my_example_method, *args
  1104. group = ExampleGroup.describe("group")
  1105. example = group.my_example_method("description")
  1106. example.metadata
  1107. end
  1108. end
  1109. end
  1110. describe "#reset" do
  1111. it "clears the reporter" do
  1112. expect(config.reporter).not_to be_nil
  1113. config.reset
  1114. expect(config.instance_variable_get("@reporter")).to be_nil
  1115. end
  1116. it "clears the formatters" do
  1117. config.add_formatter "doc"
  1118. config.reset
  1119. expect(config.formatters).to be_empty
  1120. end
  1121. end
  1122. describe "#force" do
  1123. it "forces order" do
  1124. config.force :order => "default"
  1125. config.order = "rand"
  1126. expect(config.order).to eq("default")
  1127. end
  1128. it "forces order and seed with :order => 'rand:37'" do
  1129. config.force :order => "rand:37"
  1130. config.order = "default"
  1131. expect(config.order).to eq("rand")
  1132. expect(config.seed).to eq(37)
  1133. end
  1134. it "forces order and seed with :seed => '37'" do
  1135. config.force :seed => "37"
  1136. config.order = "default"
  1137. expect(config.seed).to eq(37)
  1138. expect(config.order).to eq("rand")
  1139. end
  1140. it 'can set random ordering' do
  1141. config.force :seed => "rand:37"
  1142. RSpec.stub(:configuration => config)
  1143. list = [1, 2, 3, 4].extend(Extensions::Ordered::Examples)
  1144. Kernel.should_receive(:rand).and_return(3, 1, 4, 2)
  1145. expect(list.ordered).to eq([2, 4, 1, 3])
  1146. end
  1147. it "forces 'false' value" do
  1148. config.add_setting :custom_option
  1149. config.custom_option = true
  1150. expect(config.custom_option?).to be_true
  1151. config.force :custom_option => false
  1152. expect(config.custom_option?).to be_false
  1153. config.custom_option = true
  1154. expect(config.custom_option?).to be_false
  1155. end
  1156. end
  1157. describe '#seed' do
  1158. it 'returns the seed as an int' do
  1159. config.seed = '123'
  1160. expect(config.seed).to eq(123)
  1161. end
  1162. end
  1163. describe '#randomize?' do
  1164. context 'with order set to :random' do
  1165. before { config.order = :random }
  1166. it 'returns true' do
  1167. expect(config.randomize?).to be_true
  1168. end
  1169. end
  1170. context 'with order set to nil' do
  1171. before { config.order = nil }
  1172. it 'returns false' do
  1173. expect(config.randomize?).to be_false
  1174. end
  1175. end
  1176. end
  1177. describe '#order=' do
  1178. context 'given "random:123"' do
  1179. before { config.order = 'random:123' }
  1180. it 'sets order to "random"' do
  1181. expect(config.order).to eq('random')
  1182. end
  1183. it 'sets seed to 123' do
  1184. expect(config.seed).to eq(123)
  1185. end
  1186. it 'sets up random ordering' do
  1187. RSpec.stub(:configuration => config)
  1188. list = [1, 2, 3, 4].extend(Extensions::Ordered::Examples)
  1189. Kernel.should_receive(:rand).and_return(3, 1, 4, 2)
  1190. expect(list.ordered).to eq([2, 4, 1, 3])
  1191. end
  1192. end
  1193. context 'given "default"' do
  1194. before do
  1195. config.order = 'rand:123'
  1196. config.order = 'default'
  1197. end
  1198. it "sets the order to nil" do
  1199. expect(config.order).to be_nil
  1200. end
  1201. it "sets the seed to nil" do
  1202. expect(config.seed).to be_nil
  1203. end
  1204. it 'clears the random ordering' do
  1205. RSpec.stub(:configuration => config)
  1206. list = [1, 2, 3, 4].extend(Extensions::Ordered::Examples)
  1207. Kernel.should_not_receive(:rand)
  1208. expect(list.ordered).to eq([1, 2, 3, 4])
  1209. end
  1210. end
  1211. end
  1212. describe "#order_examples" do
  1213. before { RSpec.stub(:configuration => config) }
  1214. it 'sets a block that determines the ordering of a collection extended with Extensions::Ordered::Examples' do
  1215. examples = [1, 2, 3, 4]
  1216. examples.extend Extensions::Ordered::Examples
  1217. config.order_examples { |examples_to_order| examples_to_order.reverse }
  1218. expect(examples.ordered).to eq([4, 3, 2, 1])
  1219. end
  1220. it 'sets #order to "custom"' do
  1221. config.order_examples { |examples| examples.reverse }
  1222. expect(config.order).to eq("custom")
  1223. end
  1224. end
  1225. describe "#example_ordering_block" do
  1226. it 'defaults to a block that returns the passed argument' do
  1227. expect(config.example_ordering_block.call([1, 2, 3])).to eq([1, 2, 3])
  1228. end
  1229. end
  1230. describe "#order_groups" do
  1231. before { RSpec.stub(:configuration => config) }
  1232. it 'sets a block that determines the ordering of a collection extended with Extensions::Ordered::ExampleGroups' do
  1233. groups = [1, 2, 3, 4]
  1234. groups.extend Extensions::Ordered::ExampleGroups
  1235. config.order_groups { |groups_to_order| groups_to_order.reverse }
  1236. expect(groups.ordered).to eq([4, 3, 2, 1])
  1237. end
  1238. it 'sets #order to "custom"' do
  1239. config.order_groups { |groups| groups.reverse }
  1240. expect(config.order).to eq("custom")
  1241. end
  1242. end
  1243. describe "#group_ordering_block" do
  1244. it 'defaults to a block that returns the passed argument' do
  1245. expect(config.group_ordering_block.call([1, 2, 3])).to eq([1, 2, 3])
  1246. end
  1247. end
  1248. describe "#order_groups_and_examples" do
  1249. let(:examples) { [1, 2, 3, 4].extend Extensions::Ordered::Examples }
  1250. let(:groups) { [1, 2, 3, 4].extend Extensions::Ordered::ExampleGroups }
  1251. before do
  1252. RSpec.stub(:configuration => config)
  1253. config.order_groups_and_examples { |list| list.reverse }
  1254. end
  1255. it 'sets a block that determines the ordering of a collection extended with Extensions::Ordered::Examples' do
  1256. expect(examples.ordered).to eq([4, 3, 2, 1])
  1257. end
  1258. it 'sets a block that determines the ordering of a collection extended with Extensions::Ordered::ExampleGroups' do
  1259. expect(groups.ordered).to eq([4, 3, 2, 1])
  1260. end
  1261. end
  1262. describe '#warnings' do
  1263. around do |example|
  1264. @_original_setting = $VERBOSE
  1265. example.run
  1266. $VERBOSE = @_original_setting
  1267. end
  1268. it "sets verbose to true when true" do
  1269. config.warnings = true
  1270. expect($VERBOSE).to eq true
  1271. end
  1272. it "sets verbose to false when true" do
  1273. config.warnings = false
  1274. expect($VERBOSE).to eq false
  1275. end
  1276. it 'returns the verbosity setting' do
  1277. expect(config.warnings).to eq $VERBOSE
  1278. end
  1279. it 'is loaded from config by #force' do
  1280. config.force :warnings => true
  1281. expect($VERBOSE).to eq true
  1282. end
  1283. end
  1284. end
  1285. end