Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Comparing changes

Choose two branches to see what's changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
base fork: rspec/rspec-core
base: v2.11.0
...
head fork: rspec/rspec-core
compare: custom_ordering_strategies
  • 4 commits
  • 7 files changed
  • 0 commit comments
  • 1 contributor
View
87 lib/rspec/core/configuration.rb
@@ -799,6 +799,79 @@ def randomize?
order.to_s.match(/rand/)
end
+ # @private
+ DEFAULT_ORDERING = lambda { |list| list }
+
+ # @private
+ RANDOM_ORDERING = lambda do |list|
+ Kernel.srand RSpec.configuration.seed
+ list.sort_by { Kernel.rand(list.size) }
+ end
+
+ # Sets a strategy by which to order examples.
+ #
+ # @example
+ # RSpec.configure do |config|
+ # config.order_examples do |examples|
+ # examples.reverse
+ # end
+ # end
+ #
+ # @see #order_groups
+ # @see #order_groups_and_examples
+ # @see #order=
+ # @see #seed=
+ def order_examples(&block)
+ @example_ordering_block = block
+ @order = "custom" unless built_in_orderer?(block)
+ end
+
+ # @private
+ def example_ordering_block
+ @example_ordering_block ||= DEFAULT_ORDERING
+ end
+
+ # Sets a strategy by which to order groups.
+ #
+ # @example
+ # RSpec.configure do |config|
+ # config.order_groups do |groups|
+ # groups.reverse
+ # end
+ # end
+ #
+ # @see #order_examples
+ # @see #order_groups_and_examples
+ # @see #order=
+ # @see #seed=
+ def order_groups(&block)
+ @group_ordering_block = block
+ @order = "custom" unless built_in_orderer?(block)
+ end
+
+ # @private
+ def group_ordering_block
+ @group_ordering_block ||= DEFAULT_ORDERING
+ end
+
+ # Sets a strategy by which to order groups and examples.
+ #
+ # @example
+ # RSpec.configure do |config|
+ # config.order_groups_and_examples do |groups_or_examples|
+ # groups_or_examples.reverse
+ # end
+ # end
+ #
+ # @see #order_groups
+ # @see #order_examples
+ # @see #order=
+ # @see #seed=
+ def order_groups_and_examples(&block)
+ order_groups(&block)
+ order_examples(&block)
+ end
+
private
def get_files_to_run(paths)
@@ -923,6 +996,7 @@ def file_at(path)
end
def order_and_seed_from_seed(value)
+ order_groups_and_examples(&RANDOM_ORDERING)
@order, @seed = 'rand', value.to_i
end
@@ -935,10 +1009,21 @@ def order_and_seed_from_order(type)
order, seed = type.to_s.split(':')
@order = order
@seed = seed = seed.to_i if seed
- @order, @seed = nil, nil if order == 'default'
+
+ if randomize?
+ order_groups_and_examples(&RANDOM_ORDERING)
+ elsif order == 'default'
+ @order, @seed = nil, nil
+ order_groups_and_examples(&DEFAULT_ORDERING)
+ end
+
return order, seed
end
+ def built_in_orderer?(block)
+ [DEFAULT_ORDERING, RANDOM_ORDERING].include?(block)
+ end
+
end
end
end
View
2  lib/rspec/core/example_group.rb
@@ -241,7 +241,7 @@ def self.subclass(parent, args, &example_group_block)
# @private
def self.children
- @children ||= [].extend(Extensions::Ordered)
+ @children ||= [].extend(Extensions::Ordered::ExampleGroups)
end
# @private
View
18 lib/rspec/core/extensions/ordered.rb
@@ -7,12 +7,18 @@ module Extensions
# strategies like randomization.
module Ordered
# @private
- def ordered
- if RSpec.configuration.randomize?
- Kernel.srand RSpec.configuration.seed
- sort_by { Kernel.rand size }
- else
- self
+ module ExampleGroups
+ # @private
+ def ordered
+ RSpec.configuration.group_ordering_block.call(self)
+ end
+ end
+
+ # @private
+ module Examples
+ # @private
+ def ordered
+ RSpec.configuration.example_ordering_block.call(self)
end
end
end
View
4 lib/rspec/core/world.rb
@@ -9,14 +9,14 @@ class World
def initialize(configuration=RSpec.configuration)
@configuration = configuration
- @example_groups = [].extend(Extensions::Ordered)
+ @example_groups = [].extend(Extensions::Ordered::ExampleGroups)
@shared_example_groups = {}
@filtered_examples = Hash.new { |hash,group|
hash[group] = begin
examples = group.examples.dup
examples = filter_manager.prune(examples)
examples.uniq
- examples.extend(Extensions::Ordered)
+ examples.extend(Extensions::Ordered::Examples)
end
}
end
View
33 spec/command_line/order_spec.rb
@@ -110,6 +110,39 @@
end
end
+ context 'when a custom order is configured' do
+ before do
+ write_file 'spec/custom_order_spec.rb', """
+ RSpec.configure do |config|
+ config.order_groups_and_examples do |list|
+ list.sort_by { |item| item.description }
+ end
+ end
+
+ describe 'group B' do
+ specify('group B example D') {}
+ specify('group B example B') {}
+ specify('group B example A') {}
+ specify('group B example C') {}
+ end
+
+ describe 'group A' do
+ specify('group A example 1') {}
+ end
+ """
+ end
+
+ it 'orders the groups and examples by the provided strategy' do
+ run_command 'tmp/aruba/spec/custom_order_spec.rb -f doc'
+
+ top_level_groups { |groups| groups.flatten.should eq(['group A', 'group B']) }
+ examples('group B') do |examples|
+ letters = examples.flatten.map { |e| e[/(.)\z/, 1] }
+ letters.should eq(['A', 'B', 'C', 'D'])
+ end
+ end
+ end
+
def examples(group)
yield split_in_half(stdout.string.scan(/^\s+#{group} example.*$/))
end
View
84 spec/rspec/core/configuration_spec.rb
@@ -1128,6 +1128,14 @@ def metadata_hash(*args)
config.order.should eq("rand")
end
+ it 'can set random ordering' do
+ config.force :seed => "rand:37"
+ RSpec.stub(:configuration => config)
+ list = [1, 2, 3, 4].extend(Extensions::Ordered::Examples)
+ Kernel.should_receive(:rand).and_return(3, 1, 4, 2)
+ list.ordered.should eq([2, 4, 1, 3])
+ end
+
it "forces 'false' value" do
config.add_setting :custom_option
config.custom_option = true
@@ -1175,6 +1183,13 @@ def metadata_hash(*args)
it 'sets seed to 123' do
config.seed.should eq(123)
end
+
+ it 'sets up random ordering' do
+ RSpec.stub(:configuration => config)
+ list = [1, 2, 3, 4].extend(Extensions::Ordered::Examples)
+ Kernel.should_receive(:rand).and_return(3, 1, 4, 2)
+ list.ordered.should eq([2, 4, 1, 3])
+ end
end
context 'given "default"' do
@@ -1190,6 +1205,75 @@ def metadata_hash(*args)
it "sets the seed to nil" do
config.seed.should be_nil
end
+
+ it 'clears the random ordering' do
+ RSpec.stub(:configuration => config)
+ list = [1, 2, 3, 4].extend(Extensions::Ordered::Examples)
+ Kernel.should_not_receive(:rand)
+ list.ordered.should eq([1, 2, 3, 4])
+ end
+ end
+ end
+
+ describe "#order_examples" do
+ before { RSpec.stub(:configuration => config) }
+
+ it 'sets a block that determines the ordering of a collection extended with Extensions::Ordered::Examples' do
+ examples = [1, 2, 3, 4]
+ examples.extend Extensions::Ordered::Examples
+ config.order_examples { |examples| examples.reverse }
+ examples.ordered.should eq([4, 3, 2, 1])
+ end
+
+ it 'sets #order to "custom"' do
+ config.order_examples { |examples| examples.reverse }
+ config.order.should eq("custom")
+ end
+ end
+
+ describe "#example_ordering_block" do
+ it 'defaults to a block that returns the passed argument' do
+ config.example_ordering_block.call([1, 2, 3]).should eq([1, 2, 3])
+ end
+ end
+
+ describe "#order_groups" do
+ before { RSpec.stub(:configuration => config) }
+
+ it 'sets a block that determines the ordering of a collection extended with Extensions::Ordered::ExampleGroups' do
+ groups = [1, 2, 3, 4]
+ groups.extend Extensions::Ordered::ExampleGroups
+ config.order_groups { |groups| groups.reverse }
+ groups.ordered.should eq([4, 3, 2, 1])
+ end
+
+ it 'sets #order to "custom"' do
+ config.order_groups { |groups| groups.reverse }
+ config.order.should eq("custom")
+ end
+ end
+
+ describe "#group_ordering_block" do
+ it 'defaults to a block that returns the passed argument' do
+ config.group_ordering_block.call([1, 2, 3]).should eq([1, 2, 3])
+ end
+ end
+
+ describe "#order_groups_and_examples" do
+ let(:examples) { [1, 2, 3, 4].extend Extensions::Ordered::Examples }
+ let(:groups) { [1, 2, 3, 4].extend Extensions::Ordered::ExampleGroups }
+
+ before do
+ RSpec.stub(:configuration => config)
+ config.order_groups_and_examples { |list| list.reverse }
+ end
+
+ it 'sets a block that determines the ordering of a collection extended with Extensions::Ordered::Examples' do
+ examples.ordered.should eq([4, 3, 2, 1])
+ end
+
+ it 'sets a block that determines the ordering of a collection extended with Extensions::Ordered::ExampleGroups' do
+ groups.ordered.should eq([4, 3, 2, 1])
end
end
end
View
6 spec/rspec/core/example_group_spec.rb
@@ -716,7 +716,7 @@ def metadata_hash(*args)
example('ex 1') { 1.should eq(1) }
example('ex 2') { 1.should eq(1) }
end
- group.stub(:filtered_examples) { group.examples.extend(Extensions::Ordered) }
+ group.stub(:filtered_examples) { group.examples.extend(Extensions::Ordered::Examples) }
group.run(reporter).should be_true
end
@@ -725,7 +725,7 @@ def metadata_hash(*args)
example('ex 1') { 1.should eq(1) }
example('ex 2') { 1.should eq(2) }
end
- group.stub(:filtered_examples) { group.examples.extend(Extensions::Ordered) }
+ group.stub(:filtered_examples) { group.examples.extend(Extensions::Ordered::Examples) }
group.run(reporter).should be_false
end
@@ -734,7 +734,7 @@ def metadata_hash(*args)
example('ex 1') { 1.should eq(2) }
example('ex 2') { 1.should eq(1) }
end
- group.stub(:filtered_examples) { group.examples.extend(Extensions::Ordered) }
+ group.stub(:filtered_examples) { group.examples.extend(Extensions::Ordered::Examples) }
group.filtered_examples.each do |example|
example.should_receive(:run)
end

No commit comments for this range

Something went wrong with that request. Please try again.