Skip to content

Commit

Permalink
Allows ordering strategy objects to be registered.
Browse files Browse the repository at this point in the history
  • Loading branch information
myronmarston committed Sep 27, 2013
1 parent e7eaa57 commit 69c12db
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 8 deletions.
14 changes: 7 additions & 7 deletions lib/rspec/core/ordering.rb
Original file line number Diff line number Diff line change
Expand Up @@ -151,17 +151,17 @@ def order_groups_and_examples(ordering=nil, &block)
order_examples(ordering, &block)
end

def register_group_ordering(name, &block)
@group_ordering_registry.register(name, Custom.new(block))
def register_group_ordering(name, strategy = Custom.new(Proc.new { |list| yield list }))
@group_ordering_registry.register(name, strategy)
end

def register_example_ordering(name, &block)
@example_ordering_registry.register(name, Custom.new(block))
def register_example_ordering(name, strategy = Custom.new(Proc.new { |list| yield list }))
@example_ordering_registry.register(name, strategy)
end

def register_group_and_example_ordering(name, &block)
register_group_ordering(name, &block)
register_example_ordering(name, &block)
def register_group_and_example_ordering(name, strategy = Custom.new(Proc.new { |list| yield list }))
register_group_ordering(name, strategy)
register_example_ordering(name, strategy)
end
end
end
Expand Down
21 changes: 20 additions & 1 deletion spec/rspec/core/configuration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1434,21 +1434,40 @@ def use_seed_on(registry)
end

describe "#register_group_and_example_ordering" do
before { config.register_group_and_example_ordering(:reverse, &:reverse) }
def register_reverse_ordering
config.register_group_and_example_ordering(:reverse, &:reverse)
end

it 'stores the ordering for group use' do
register_reverse_ordering

list = [1, 2, 3]
strategy = config.group_ordering_registry.fetch(:reverse)
expect(strategy).to be_a(Ordering::Custom)
expect(strategy.order(list)).to eq([3, 2, 1])
end

it 'stores the ordering for example use' do
register_reverse_ordering

list = [1, 2, 3]
strategy = config.example_ordering_registry.fetch(:reverse)
expect(strategy).to be_a(Ordering::Custom)
expect(strategy.order(list)).to eq([3, 2, 1])
end

it 'can register an ordering object' do
strategy = Object.new
def strategy.order(list)
list.reverse
end

config.register_group_and_example_ordering(:reverse, strategy)
list = [1, 2, 3]
fetched = config.example_ordering_registry.fetch(:reverse)
expect(fetched).to be(strategy)
expect(fetched.order(list)).to eq([3, 2, 1])
end
end

describe "#order_examples" do
Expand Down

0 comments on commit 69c12db

Please sign in to comment.