rspec-core 2.11.1 → 2.12.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (116) hide show
  1. data/Changelog.md +59 -0
  2. data/README.md +22 -0
  3. data/features/command_line/example_name_option.feature +5 -5
  4. data/features/command_line/exit_status.feature +6 -6
  5. data/features/command_line/format_option.feature +2 -2
  6. data/features/command_line/line_number_appended_to_path.feature +2 -2
  7. data/features/command_line/line_number_option.feature +2 -2
  8. data/features/command_line/pattern_option.feature +2 -2
  9. data/features/command_line/rake_task.feature +62 -8
  10. data/features/command_line/ruby.feature +1 -1
  11. data/features/command_line/tag.feature +1 -1
  12. data/features/configuration/alias_example_to.feature +2 -2
  13. data/features/configuration/custom_settings.feature +3 -3
  14. data/features/configuration/default_path.feature +3 -3
  15. data/features/configuration/fail_fast.feature +5 -5
  16. data/features/configuration/read_options_from_file.feature +4 -4
  17. data/features/example_groups/basic_structure.feature +2 -2
  18. data/features/example_groups/shared_context.feature +3 -3
  19. data/features/example_groups/shared_examples.feature +25 -7
  20. data/features/expectation_framework_integration/configure_expectation_framework.feature +6 -6
  21. data/features/filtering/exclusion_filters.feature +5 -5
  22. data/features/filtering/if_and_unless.feature +5 -5
  23. data/features/filtering/inclusion_filters.feature +5 -5
  24. data/features/filtering/run_all_when_everything_filtered.feature +3 -3
  25. data/features/formatters/custom_formatter.feature +2 -2
  26. data/features/formatters/json_formatter.feature +30 -0
  27. data/features/formatters/text_formatter.feature +5 -5
  28. data/features/helper_methods/arbitrary_methods.feature +2 -2
  29. data/features/helper_methods/let.feature +2 -2
  30. data/features/helper_methods/modules.feature +6 -6
  31. data/features/hooks/around_hooks.feature +11 -11
  32. data/features/hooks/before_and_after_hooks.feature +15 -11
  33. data/features/hooks/filtering.feature +6 -6
  34. data/features/metadata/current_example.feature +1 -1
  35. data/features/metadata/described_class.feature +1 -1
  36. data/features/metadata/user_defined.feature +4 -4
  37. data/features/mock_framework_integration/use_any_framework.feature +6 -6
  38. data/features/mock_framework_integration/use_flexmock.feature +5 -5
  39. data/features/mock_framework_integration/use_mocha.feature +5 -5
  40. data/features/mock_framework_integration/use_rr.feature +5 -5
  41. data/features/mock_framework_integration/use_rspec.feature +5 -5
  42. data/features/pending/pending_examples.feature +11 -11
  43. data/features/spec_files/arbitrary_file_suffix.feature +1 -1
  44. data/features/step_definitions/additional_cli_steps.rb +2 -0
  45. data/features/subject/attribute_of_subject.feature +5 -5
  46. data/features/subject/explicit_subject.feature +5 -5
  47. data/features/subject/implicit_receiver.feature +2 -2
  48. data/features/subject/implicit_subject.feature +3 -3
  49. data/lib/autotest/rspec2.rb +1 -1
  50. data/lib/rspec/core.rb +53 -32
  51. data/lib/rspec/core/configuration.rb +123 -15
  52. data/lib/rspec/core/configuration_options.rb +17 -2
  53. data/lib/rspec/core/example.rb +5 -4
  54. data/lib/rspec/core/example_group.rb +19 -9
  55. data/lib/rspec/core/extensions/ordered.rb +12 -6
  56. data/lib/rspec/core/formatters.rb +55 -0
  57. data/lib/rspec/core/formatters/base_formatter.rb +43 -38
  58. data/lib/rspec/core/formatters/base_text_formatter.rb +9 -5
  59. data/lib/rspec/core/formatters/documentation_formatter.rb +1 -1
  60. data/lib/rspec/core/formatters/helpers.rb +30 -5
  61. data/lib/rspec/core/formatters/html_formatter.rb +58 -368
  62. data/lib/rspec/core/formatters/html_printer.rb +407 -0
  63. data/lib/rspec/core/formatters/json_formatter.rb +73 -0
  64. data/lib/rspec/core/formatters/snippet_extractor.rb +3 -1
  65. data/lib/rspec/core/hooks.rb +4 -4
  66. data/lib/rspec/core/metadata.rb +14 -6
  67. data/lib/rspec/core/mocking/with_mocha.rb +25 -2
  68. data/lib/rspec/core/mocking/with_rspec.rb +6 -2
  69. data/lib/rspec/core/option_parser.rb +28 -7
  70. data/lib/rspec/core/project_initializer.rb +0 -1
  71. data/lib/rspec/core/rake_task.rb +49 -38
  72. data/lib/rspec/core/reporter.rb +2 -2
  73. data/lib/rspec/core/shared_example_group.rb +89 -41
  74. data/lib/rspec/core/subject.rb +6 -2
  75. data/lib/rspec/core/version.rb +1 -1
  76. data/lib/rspec/core/world.rb +2 -2
  77. data/spec/autotest/rspec_spec.rb +6 -1
  78. data/spec/command_line/order_spec.rb +67 -0
  79. data/spec/rspec/core/configuration_options_spec.rb +45 -38
  80. data/spec/rspec/core/configuration_spec.rb +219 -44
  81. data/spec/rspec/core/deprecations_spec.rb +9 -0
  82. data/spec/rspec/core/drb_command_line_spec.rb +1 -7
  83. data/spec/rspec/core/drb_options_spec.rb +1 -1
  84. data/spec/rspec/core/dsl_spec.rb +17 -9
  85. data/spec/rspec/core/example_group_spec.rb +51 -5
  86. data/spec/rspec/core/example_spec.rb +39 -7
  87. data/spec/rspec/core/filter_manager_spec.rb +20 -30
  88. data/spec/rspec/core/formatters/base_formatter_spec.rb +29 -1
  89. data/spec/rspec/core/formatters/base_text_formatter_spec.rb +6 -2
  90. data/spec/rspec/core/formatters/helpers_spec.rb +12 -0
  91. data/spec/rspec/core/formatters/html_formatted-1.8.7-rbx.html +462 -0
  92. data/spec/rspec/core/formatters/html_formatted-1.9.2-jruby.html +410 -0
  93. data/spec/rspec/core/formatters/html_formatted-1.9.3-rbx.html +462 -0
  94. data/spec/rspec/core/formatters/html_formatter_spec.rb +11 -3
  95. data/spec/rspec/core/formatters/json_formatter_spec.rb +110 -0
  96. data/spec/rspec/core/formatters/snippet_extractor_spec.rb +8 -0
  97. data/spec/rspec/core/formatters/text_mate_formatted-1.8.7-rbx.html +462 -0
  98. data/spec/rspec/core/formatters/text_mate_formatted-1.9.2-jruby.html +410 -0
  99. data/spec/rspec/core/formatters/text_mate_formatted-1.9.3-rbx.html +462 -0
  100. data/spec/rspec/core/formatters/text_mate_formatter_spec.rb +11 -3
  101. data/spec/rspec/core/hooks_filtering_spec.rb +6 -6
  102. data/spec/rspec/core/metadata_spec.rb +29 -0
  103. data/spec/rspec/core/option_parser_spec.rb +42 -0
  104. data/spec/rspec/core/project_initializer_spec.rb +2 -2
  105. data/spec/rspec/core/rake_task_spec.rb +60 -17
  106. data/spec/rspec/core/reporter_spec.rb +17 -0
  107. data/spec/rspec/core/runner_spec.rb +1 -0
  108. data/spec/rspec/core/shared_example_group_spec.rb +17 -5
  109. data/spec/rspec/core/subject_spec.rb +11 -0
  110. data/spec/spec_helper.rb +32 -2
  111. data/spec/support/config_options_helper.rb +1 -10
  112. data/spec/support/helper_methods.rb +13 -0
  113. data/spec/support/isolated_directory.rb +10 -0
  114. data/spec/support/isolated_home_directory.rb +16 -0
  115. metadata +145 -148
  116. data/lib/rspec/core/extensions.rb +0 -4
@@ -193,8 +193,12 @@ module RSpec
193
193
  # @see ExampleMethods#subject
194
194
  # @see ExampleMethods#should
195
195
  def subject(name=nil, &block)
196
- define_method(name) { subject } if name
197
- block ? @explicit_subject_block = block : explicit_subject || implicit_subject
196
+ if name
197
+ let(name, &block)
198
+ subject { send name }
199
+ else
200
+ block ? @explicit_subject_block = block : explicit_subject || implicit_subject
201
+ end
198
202
  end
199
203
 
200
204
  attr_reader :explicit_subject_block
@@ -1,7 +1,7 @@
1
1
  module RSpec
2
2
  module Core
3
3
  module Version
4
- STRING = '2.11.1'
4
+ STRING = '2.12.0'
5
5
  end
6
6
  end
7
7
  end
@@ -9,14 +9,14 @@ module RSpec
9
9
 
10
10
  def initialize(configuration=RSpec.configuration)
11
11
  @configuration = configuration
12
- @example_groups = [].extend(Extensions::Ordered)
12
+ @example_groups = [].extend(Extensions::Ordered::ExampleGroups)
13
13
  @shared_example_groups = {}
14
14
  @filtered_examples = Hash.new { |hash,group|
15
15
  hash[group] = begin
16
16
  examples = group.examples.dup
17
17
  examples = filter_manager.prune(examples)
18
18
  examples.uniq
19
- examples.extend(Extensions::Ordered)
19
+ examples.extend(Extensions::Ordered::Examples)
20
20
  end
21
21
  }
22
22
  end
@@ -3,7 +3,7 @@ require "spec_helper"
3
3
  describe Autotest::Rspec2 do
4
4
  let(:rspec_autotest) { Autotest::Rspec2.new }
5
5
  let(:spec_cmd) { File.expand_path("../../../exe/rspec", __FILE__) }
6
- let(:ruby_cmd) { "ruby" }
6
+ let(:ruby_cmd) { "/path/to/ruby" }
7
7
 
8
8
  before do
9
9
  File.stub(:exist?) { false }
@@ -50,6 +50,11 @@ describe Autotest::Rspec2 do
50
50
  end
51
51
  end
52
52
 
53
+ it "quotes the path of the ruby executable" do
54
+ cmd = rspec_autotest.make_test_cmd(@files_to_test)
55
+ cmd.should match(%r('/path/to/ruby'))
56
+ end
57
+
53
58
  it "gives '--tty' to #{Autotest::Rspec2::RSPEC_EXECUTABLE}, not '--autotest'" do
54
59
  cmd = rspec_autotest.make_test_cmd(@files_to_test)
55
60
  cmd.should match(' --tty ')
@@ -5,6 +5,32 @@ describe 'command line', :ui do
5
5
  let(:stdout) { StringIO.new }
6
6
 
7
7
  before :all do
8
+ write_file 'spec/simple_spec.rb', """
9
+ describe 'group 1' do
10
+ specify('group 1 example 1') {}
11
+ specify('group 1 example 2') {}
12
+ specify('group 1 example 3') {}
13
+ describe 'group 1-1' do
14
+ specify('group 1-1 example 1') {}
15
+ specify('group 1-1 example 2') {}
16
+ specify('group 1-1 example 3') {}
17
+ end
18
+ end
19
+ """
20
+
21
+ write_file 'spec/simple_spec2.rb', """
22
+ describe 'group 2' do
23
+ specify('group 2 example 1') {}
24
+ specify('group 2 example 2') {}
25
+ specify('group 2 example 3') {}
26
+ describe 'group 2-1' do
27
+ specify('group 2-1 example 1') {}
28
+ specify('group 2-1 example 2') {}
29
+ specify('group 2-1 example 3') {}
30
+ end
31
+ end
32
+ """
33
+
8
34
  write_file 'spec/order_spec.rb', """
9
35
  describe 'group 1' do
10
36
  specify('group 1 example 1') {}
@@ -94,6 +120,14 @@ describe 'command line', :ui do
94
120
  examples('group 1') {|first_run, second_run| first_run.should eq(second_run)}
95
121
  examples('group 1-1') {|first_run, second_run| first_run.should eq(second_run)}
96
122
  end
123
+
124
+ it "runs examples in the same order, regardless of the order in which files are given" do
125
+ run_command 'tmp/aruba/spec/simple_spec.rb tmp/aruba/spec/simple_spec2.rb --seed 1337 -f doc'
126
+ run_command 'tmp/aruba/spec/simple_spec2.rb tmp/aruba/spec/simple_spec.rb --seed 1337 -f doc'
127
+
128
+ top_level_groups {|first_run, second_run| first_run.should eq(second_run)}
129
+ nested_groups {|first_run, second_run| first_run.should eq(second_run)}
130
+ end
97
131
  end
98
132
 
99
133
  describe '--order default on CLI with --order rand in .rspec' do
@@ -110,6 +144,39 @@ describe 'command line', :ui do
110
144
  end
111
145
  end
112
146
 
147
+ context 'when a custom order is configured' do
148
+ before do
149
+ write_file 'spec/custom_order_spec.rb', """
150
+ RSpec.configure do |config|
151
+ config.order_groups_and_examples do |list|
152
+ list.sort_by { |item| item.description }
153
+ end
154
+ end
155
+
156
+ describe 'group B' do
157
+ specify('group B example D') {}
158
+ specify('group B example B') {}
159
+ specify('group B example A') {}
160
+ specify('group B example C') {}
161
+ end
162
+
163
+ describe 'group A' do
164
+ specify('group A example 1') {}
165
+ end
166
+ """
167
+ end
168
+
169
+ it 'orders the groups and examples by the provided strategy' do
170
+ run_command 'tmp/aruba/spec/custom_order_spec.rb -f doc'
171
+
172
+ top_level_groups { |groups| groups.flatten.should eq(['group A', 'group B']) }
173
+ examples('group B') do |examples|
174
+ letters = examples.flatten.map { |e| e[/(.)\z/, 1] }
175
+ letters.should eq(['A', 'B', 'C', 'D'])
176
+ end
177
+ end
178
+ end
179
+
113
180
  def examples(group)
114
181
  yield split_in_half(stdout.string.scan(/^\s+#{group} example.*$/))
115
182
  end
@@ -1,17 +1,15 @@
1
1
  require 'spec_helper'
2
2
  require 'ostruct'
3
+ require 'rspec/core/drb_options'
3
4
 
4
- describe RSpec::Core::ConfigurationOptions, :fakefs do
5
+ describe RSpec::Core::ConfigurationOptions, :isolated_directory => true, :isolated_home => true do
5
6
  include ConfigOptionsHelper
6
7
 
7
8
  it "warns when HOME env var is not set", :unless => (RUBY_PLATFORM == 'java') do
8
- begin
9
- orig_home = ENV.delete("HOME")
9
+ without_env_vars 'HOME' do
10
10
  coo = RSpec::Core::ConfigurationOptions.new([])
11
11
  coo.should_receive(:warn)
12
12
  coo.parse_options
13
- ensure
14
- ENV["HOME"] = orig_home
15
13
  end
16
14
  end
17
15
 
@@ -97,19 +95,21 @@ describe RSpec::Core::ConfigurationOptions, :fakefs do
97
95
  end
98
96
 
99
97
  it "merges --require specified by multiple configuration sources" do
100
- ENV['SPEC_OPTS'] = "--require file_from_env"
101
- opts = config_options_object(*%w[--require file_from_opts])
102
- config = RSpec::Core::Configuration.new
103
- config.should_receive(:requires=).with(["file_from_opts", "file_from_env"])
104
- opts.configure(config)
98
+ with_env_vars 'SPEC_OPTS' => "--require file_from_env" do
99
+ opts = config_options_object(*%w[--require file_from_opts])
100
+ config = RSpec::Core::Configuration.new
101
+ config.should_receive(:requires=).with(["file_from_opts", "file_from_env"])
102
+ opts.configure(config)
103
+ end
105
104
  end
106
105
 
107
106
  it "merges --I specified by multiple configuration sources" do
108
- ENV['SPEC_OPTS'] = "-I dir_from_env"
109
- opts = config_options_object(*%w[-I dir_from_opts])
110
- config = RSpec::Core::Configuration.new
111
- config.should_receive(:libs=).with(["dir_from_opts", "dir_from_env"])
112
- opts.configure(config)
107
+ with_env_vars 'SPEC_OPTS' => "-I dir_from_env" do
108
+ opts = config_options_object(*%w[-I dir_from_opts])
109
+ config = RSpec::Core::Configuration.new
110
+ config.should_receive(:libs=).with(["dir_from_opts", "dir_from_env"])
111
+ opts.configure(config)
112
+ end
113
113
  end
114
114
  end
115
115
 
@@ -324,49 +324,56 @@ describe RSpec::Core::ConfigurationOptions, :fakefs do
324
324
  end
325
325
  end
326
326
 
327
- describe "sources: ~/.rspec, ./.rspec, custom, CLI, and SPEC_OPTS" do
328
- before(:each) do
329
- FileUtils.mkpath(File.expand_path("~"))
330
- end
331
-
327
+ describe "sources: ~/.rspec, ./.rspec, ./.rspec-local, custom, CLI, and SPEC_OPTS" do
332
328
  it "merges global, local, SPEC_OPTS, and CLI" do
333
329
  File.open("./.rspec", "w") {|f| f << "--line 37"}
334
- File.open("~/.rspec", "w") {|f| f << "--color"}
335
- ENV["SPEC_OPTS"] = "--debug --example 'foo bar'"
336
- options = parse_options("--drb")
337
- options[:color].should be_true
338
- options[:line_numbers].should eq(["37"])
339
- options[:debug].should be_true
340
- options[:full_description].should eq([/foo\ bar/])
341
- options[:drb].should be_true
330
+ File.open("./.rspec-local", "w") {|f| f << "--format global"}
331
+ File.open(File.expand_path("~/.rspec"), "w") {|f| f << "--color"}
332
+ with_env_vars 'SPEC_OPTS' => "--debug --example 'foo bar'" do
333
+ options = parse_options("--drb")
334
+ options[:color].should be_true
335
+ options[:line_numbers].should eq(["37"])
336
+ options[:debug].should be_true
337
+ options[:full_description].should eq([/foo\ bar/])
338
+ options[:drb].should be_true
339
+ options[:formatters].should eq([['global']])
340
+ end
342
341
  end
343
342
 
344
343
  it "prefers SPEC_OPTS over CLI" do
345
- ENV["SPEC_OPTS"] = "--format spec_opts"
346
- parse_options("--format", "cli")[:formatters].should eq([['spec_opts']])
344
+ with_env_vars 'SPEC_OPTS' => "--format spec_opts" do
345
+ parse_options("--format", "cli")[:formatters].should eq([['spec_opts']])
346
+ end
347
347
  end
348
348
 
349
349
  it "prefers CLI over file options" do
350
- File.open("./.rspec", "w") {|f| f << "--format local"}
351
- File.open("~/.rspec", "w") {|f| f << "--format global"}
350
+ File.open("./.rspec", "w") {|f| f << "--format project"}
351
+ File.open(File.expand_path("~/.rspec"), "w") {|f| f << "--format global"}
352
352
  parse_options("--format", "cli")[:formatters].should eq([['cli']])
353
353
  end
354
354
 
355
- it "prefers local file options over global" do
356
- File.open("./.rspec", "w") {|f| f << "--format local"}
357
- File.open("~/.rspec", "w") {|f| f << "--format global"}
355
+ it "prefers project file options over global file options" do
356
+ File.open("./.rspec", "w") {|f| f << "--format project"}
357
+ File.open(File.expand_path("~/.rspec"), "w") {|f| f << "--format global"}
358
+ parse_options[:formatters].should eq([['project']])
359
+ end
360
+
361
+ it "prefers local file options over project file options" do
362
+ File.open("./.rspec-local", "w") {|f| f << "--format local"}
363
+ File.open("./.rspec", "w") {|f| f << "--format global"}
358
364
  parse_options[:formatters].should eq([['local']])
359
365
  end
360
366
 
361
367
  context "with custom options file" do
362
- it "ignores local and global options files" do
363
- File.open("./.rspec", "w") {|f| f << "--format local"}
364
- File.open("~/.rspec", "w") {|f| f << "--format global"}
368
+ it "ignores project and global options files" do
369
+ File.open("./.rspec", "w") {|f| f << "--format project"}
370
+ File.open(File.expand_path("~/.rspec"), "w") {|f| f << "--format global"}
365
371
  File.open("./custom.opts", "w") {|f| f << "--color"}
366
372
  options = parse_options("-O", "./custom.opts")
367
373
  options[:format].should be_nil
368
374
  options[:color].should be_true
369
375
  end
376
+
370
377
  it "parses -e 'full spec description'" do
371
378
  File.open("./custom.opts", "w") {|f| f << "-e 'The quick brown fox jumps over the lazy dog'"}
372
379
  options = parse_options("-O", "./custom.opts")
@@ -1,15 +1,21 @@
1
1
  require 'spec_helper'
2
2
  require 'tmpdir'
3
3
 
4
- # so the stdlib module is available...
5
- module Test; module Unit; module Assertions; end; end; end
6
-
7
4
  module RSpec::Core
8
5
 
9
6
  describe Configuration do
10
7
 
11
8
  let(:config) { Configuration.new }
12
9
 
10
+ describe "RSpec.configuration with a block" do
11
+ before { RSpec.stub(:warn_deprecation) }
12
+
13
+ it "is deprecated" do
14
+ RSpec.should_receive(:warn_deprecation)
15
+ RSpec.configuration {}
16
+ end
17
+ end
18
+
13
19
  describe "#load_spec_files" do
14
20
 
15
21
  it "loads files using load" do
@@ -25,12 +31,8 @@ module RSpec::Core
25
31
  end
26
32
 
27
33
  context "with rspec-1 loaded" do
28
- before do
29
- Object.const_set(:Spec, Module.new)
30
- ::Spec::const_set(:VERSION, Module.new)
31
- ::Spec::VERSION::const_set(:MAJOR, 1)
32
- end
33
- after { Object.__send__(:remove_const, :Spec) }
34
+ before { stub_const("Spec::VERSION::MAJOR", 1) }
35
+
34
36
  it "raises with a helpful message" do
35
37
  expect {
36
38
  config.load_spec_files
@@ -101,6 +103,16 @@ module RSpec::Core
101
103
  end
102
104
  end
103
105
 
106
+ it "allows rspec-mocks to be configured with a provided block" do
107
+ mod = Module.new
108
+
109
+ RSpec::Mocks.configuration.should_receive(:add_stub_and_should_receive_to).with(mod)
110
+
111
+ config.mock_with :rspec do |c|
112
+ c.add_stub_and_should_receive_to mod
113
+ end
114
+ end
115
+
104
116
  context "with a module" do
105
117
  it "sets the mock_framework_adapter to that module" do
106
118
  mod = Module.new
@@ -153,7 +165,10 @@ module RSpec::Core
153
165
  end
154
166
 
155
167
  describe "#expect_with" do
156
- before { config.stub(:require) }
168
+ before do
169
+ stub_const("Test::Unit::Assertions", Module.new)
170
+ config.stub(:require)
171
+ end
157
172
 
158
173
  it_behaves_like "a configurable framework adapter", :expect_with
159
174
 
@@ -213,7 +228,10 @@ module RSpec::Core
213
228
  end
214
229
 
215
230
  describe "#expecting_with_rspec?" do
216
- before { config.stub(:require) }
231
+ before do
232
+ stub_const("Test::Unit::Assertions", Module.new)
233
+ config.stub(:require)
234
+ end
217
235
 
218
236
  it "returns false by default" do
219
237
  config.should_not be_expecting_with_rspec
@@ -314,12 +332,68 @@ module RSpec::Core
314
332
  config.files_to_run.should_not be_empty
315
333
  end
316
334
 
335
+ it "loads files in the default path when run with DRB (e.g., spork)" do
336
+ config.stub(:command) { 'spork' }
337
+ RSpec::Core::Runner.stub(:running_in_drb?) { true }
338
+ config.files_or_directories_to_run = []
339
+ config.files_to_run.should_not be_empty
340
+ end
341
+
317
342
  it "does not load files in the default path when run by ruby" do
318
343
  config.stub(:command) { 'ruby' }
319
344
  config.files_or_directories_to_run = []
320
345
  config.files_to_run.should be_empty
321
346
  end
322
347
  end
348
+
349
+ def specify_consistent_ordering_of_files_to_run
350
+ File.stub(:directory?).with('a') { true }
351
+
352
+ orderings = [
353
+ %w[ a/1.rb a/2.rb a/3.rb ],
354
+ %w[ a/2.rb a/1.rb a/3.rb ],
355
+ %w[ a/3.rb a/2.rb a/1.rb ]
356
+ ].map do |files|
357
+ Dir.should_receive(:[]).with(/^a/) { files }
358
+ yield
359
+ config.files_to_run
360
+ end
361
+
362
+ orderings.uniq.size.should eq(1)
363
+ end
364
+
365
+ context 'when the given directories match the pattern' do
366
+ it 'orders the files in a consistent ordering, regardless of the underlying OS ordering' do
367
+ specify_consistent_ordering_of_files_to_run do
368
+ config.pattern = 'a/*.rb'
369
+ config.files_or_directories_to_run = 'a'
370
+ end
371
+ end
372
+ end
373
+
374
+ context 'when the pattern is given relative to the given directories' do
375
+ it 'orders the files in a consistent ordering, regardless of the underlying OS ordering' do
376
+ specify_consistent_ordering_of_files_to_run do
377
+ config.pattern = '*.rb'
378
+ config.files_or_directories_to_run = 'a'
379
+ end
380
+ end
381
+ end
382
+
383
+ context 'when given multiple file paths' do
384
+ it 'orders the files in a consistent ordering, regardless of the given order' do
385
+ File.stub(:directory?) { false } # fake it into thinking these a full file paths
386
+
387
+ files = ['a/b/c_spec.rb', 'c/b/a_spec.rb']
388
+ config.files_or_directories_to_run = *files
389
+ ordering_1 = config.files_to_run
390
+
391
+ config.files_or_directories_to_run = *(files.reverse)
392
+ ordering_2 = config.files_to_run
393
+
394
+ expect(ordering_1).to eq(ordering_2)
395
+ end
396
+ end
323
397
  end
324
398
 
325
399
  %w[pattern= filename_pattern=].each do |setter|
@@ -503,33 +577,57 @@ module RSpec::Core
503
577
  %w[color color_enabled].each do |color_option|
504
578
  describe "##{color_option}=" do
505
579
  context "given true" do
506
- context "with non-tty output and no autotest" do
580
+ before { config.send "#{color_option}=", true }
581
+
582
+ context "with config.tty? and output.tty?" do
507
583
  it "does not set color_enabled" do
508
- config.output_stream = StringIO.new
509
- config.output_stream.stub(:tty?) { false }
510
- config.tty = false
511
- config.send "#{color_option}=", true
512
- config.send(color_option).should be_false
584
+ output = StringIO.new
585
+ config.output_stream = output
586
+
587
+ config.tty = true
588
+ config.output_stream.stub :tty? => true
589
+
590
+ config.send(color_option).should be_true
591
+ config.send(color_option, output).should be_true
513
592
  end
514
593
  end
515
594
 
516
- context "with tty output" do
595
+ context "with config.tty? and !output.tty?" do
596
+ it "sets color_enabled" do
597
+ output = StringIO.new
598
+ config.output_stream = output
599
+
600
+ config.tty = true
601
+ config.output_stream.stub :tty? => false
602
+
603
+ config.send(color_option).should be_true
604
+ config.send(color_option, output).should be_true
605
+ end
606
+ end
607
+
608
+ context "with config.tty? and !output.tty?" do
517
609
  it "does not set color_enabled" do
518
- config.output_stream = StringIO.new
519
- config.output_stream.stub(:tty?) { true }
610
+ output = StringIO.new
611
+ config.output_stream = output
612
+
520
613
  config.tty = false
521
- config.send "#{color_option}=", true
614
+ config.output_stream.stub :tty? => true
615
+
522
616
  config.send(color_option).should be_true
617
+ config.send(color_option, output).should be_true
523
618
  end
524
619
  end
525
620
 
526
- context "with tty set" do
621
+ context "with !config.tty? and !output.tty?" do
527
622
  it "does not set color_enabled" do
528
- config.output_stream = StringIO.new
529
- config.output_stream.stub(:tty?) { false }
530
- config.tty = true
531
- config.send "#{color_option}=", true
532
- config.send(color_option).should be_true
623
+ output = StringIO.new
624
+ config.output_stream = output
625
+
626
+ config.tty = false
627
+ config.output_stream.stub :tty? => false
628
+
629
+ config.send(color_option).should be_false
630
+ config.send(color_option, output).should be_false
533
631
  end
534
632
  end
535
633
 
@@ -546,18 +644,11 @@ module RSpec::Core
546
644
  end
547
645
 
548
646
  context "with ANSICON available" do
549
- before(:all) do
550
- @original_ansicon = ENV['ANSICON']
551
- ENV['ANSICON'] = 'ANSICON'
552
- end
553
-
554
- after(:all) do
555
- ENV['ANSICON'] = @original_ansicon
556
- end
647
+ around(:each) { |e| with_env_vars('ANSICON' => 'ANSICON', &e) }
557
648
 
558
649
  it "enables colors" do
559
650
  config.output_stream = StringIO.new
560
- config.output_stream.stub(:tty?) { true }
651
+ config.output_stream.stub :tty? => true
561
652
  config.send "#{color_option}=", true
562
653
  config.send(color_option).should be_true
563
654
  end
@@ -631,23 +722,23 @@ module RSpec::Core
631
722
  end
632
723
 
633
724
  it "finds a formatter by class name" do
634
- Object.const_set("ACustomFormatter", Class.new(Formatters::BaseFormatter))
635
- config.add_formatter "ACustomFormatter"
636
- config.formatters.first.should be_an_instance_of(ACustomFormatter)
725
+ stub_const("CustomFormatter", Class.new(Formatters::BaseFormatter))
726
+ config.add_formatter "CustomFormatter"
727
+ config.formatters.first.should be_an_instance_of(CustomFormatter)
637
728
  end
638
729
 
639
730
  it "finds a formatter by class fully qualified name" do
640
- RSpec.const_set("CustomFormatter", Class.new(Formatters::BaseFormatter))
731
+ stub_const("RSpec::CustomFormatter", Class.new(Formatters::BaseFormatter))
641
732
  config.add_formatter "RSpec::CustomFormatter"
642
733
  config.formatters.first.should be_an_instance_of(RSpec::CustomFormatter)
643
734
  end
644
735
 
645
736
  it "requires a formatter file based on its fully qualified name" do
646
- config.should_receive(:require).with('rspec/custom_formatter2') do
647
- RSpec.const_set("CustomFormatter2", Class.new(Formatters::BaseFormatter))
737
+ config.should_receive(:require).with('rspec/custom_formatter') do
738
+ stub_const("RSpec::CustomFormatter", Class.new(Formatters::BaseFormatter))
648
739
  end
649
- config.add_formatter "RSpec::CustomFormatter2"
650
- config.formatters.first.should be_an_instance_of(RSpec::CustomFormatter2)
740
+ config.add_formatter "RSpec::CustomFormatter"
741
+ config.formatters.first.should be_an_instance_of(RSpec::CustomFormatter)
651
742
  end
652
743
 
653
744
  it "raises NameError if class is unresolvable" do
@@ -1128,6 +1219,14 @@ module RSpec::Core
1128
1219
  config.order.should eq("rand")
1129
1220
  end
1130
1221
 
1222
+ it 'can set random ordering' do
1223
+ config.force :seed => "rand:37"
1224
+ RSpec.stub(:configuration => config)
1225
+ list = [1, 2, 3, 4].extend(Extensions::Ordered::Examples)
1226
+ Kernel.should_receive(:rand).and_return(3, 1, 4, 2)
1227
+ list.ordered.should eq([2, 4, 1, 3])
1228
+ end
1229
+
1131
1230
  it "forces 'false' value" do
1132
1231
  config.add_setting :custom_option
1133
1232
  config.custom_option = true
@@ -1175,6 +1274,13 @@ module RSpec::Core
1175
1274
  it 'sets seed to 123' do
1176
1275
  config.seed.should eq(123)
1177
1276
  end
1277
+
1278
+ it 'sets up random ordering' do
1279
+ RSpec.stub(:configuration => config)
1280
+ list = [1, 2, 3, 4].extend(Extensions::Ordered::Examples)
1281
+ Kernel.should_receive(:rand).and_return(3, 1, 4, 2)
1282
+ list.ordered.should eq([2, 4, 1, 3])
1283
+ end
1178
1284
  end
1179
1285
 
1180
1286
  context 'given "default"' do
@@ -1190,6 +1296,75 @@ module RSpec::Core
1190
1296
  it "sets the seed to nil" do
1191
1297
  config.seed.should be_nil
1192
1298
  end
1299
+
1300
+ it 'clears the random ordering' do
1301
+ RSpec.stub(:configuration => config)
1302
+ list = [1, 2, 3, 4].extend(Extensions::Ordered::Examples)
1303
+ Kernel.should_not_receive(:rand)
1304
+ list.ordered.should eq([1, 2, 3, 4])
1305
+ end
1306
+ end
1307
+ end
1308
+
1309
+ describe "#order_examples" do
1310
+ before { RSpec.stub(:configuration => config) }
1311
+
1312
+ it 'sets a block that determines the ordering of a collection extended with Extensions::Ordered::Examples' do
1313
+ examples = [1, 2, 3, 4]
1314
+ examples.extend Extensions::Ordered::Examples
1315
+ config.order_examples { |examples| examples.reverse }
1316
+ examples.ordered.should eq([4, 3, 2, 1])
1317
+ end
1318
+
1319
+ it 'sets #order to "custom"' do
1320
+ config.order_examples { |examples| examples.reverse }
1321
+ config.order.should eq("custom")
1322
+ end
1323
+ end
1324
+
1325
+ describe "#example_ordering_block" do
1326
+ it 'defaults to a block that returns the passed argument' do
1327
+ config.example_ordering_block.call([1, 2, 3]).should eq([1, 2, 3])
1328
+ end
1329
+ end
1330
+
1331
+ describe "#order_groups" do
1332
+ before { RSpec.stub(:configuration => config) }
1333
+
1334
+ it 'sets a block that determines the ordering of a collection extended with Extensions::Ordered::ExampleGroups' do
1335
+ groups = [1, 2, 3, 4]
1336
+ groups.extend Extensions::Ordered::ExampleGroups
1337
+ config.order_groups { |groups| groups.reverse }
1338
+ groups.ordered.should eq([4, 3, 2, 1])
1339
+ end
1340
+
1341
+ it 'sets #order to "custom"' do
1342
+ config.order_groups { |groups| groups.reverse }
1343
+ config.order.should eq("custom")
1344
+ end
1345
+ end
1346
+
1347
+ describe "#group_ordering_block" do
1348
+ it 'defaults to a block that returns the passed argument' do
1349
+ config.group_ordering_block.call([1, 2, 3]).should eq([1, 2, 3])
1350
+ end
1351
+ end
1352
+
1353
+ describe "#order_groups_and_examples" do
1354
+ let(:examples) { [1, 2, 3, 4].extend Extensions::Ordered::Examples }
1355
+ let(:groups) { [1, 2, 3, 4].extend Extensions::Ordered::ExampleGroups }
1356
+
1357
+ before do
1358
+ RSpec.stub(:configuration => config)
1359
+ config.order_groups_and_examples { |list| list.reverse }
1360
+ end
1361
+
1362
+ it 'sets a block that determines the ordering of a collection extended with Extensions::Ordered::Examples' do
1363
+ examples.ordered.should eq([4, 3, 2, 1])
1364
+ end
1365
+
1366
+ it 'sets a block that determines the ordering of a collection extended with Extensions::Ordered::ExampleGroups' do
1367
+ groups.ordered.should eq([4, 3, 2, 1])
1193
1368
  end
1194
1369
  end
1195
1370
  end