Skip to content

Commit

Permalink
Change to --order from --randomize
Browse files Browse the repository at this point in the history
  • Loading branch information
justinko committed Oct 24, 2011
1 parent 9d7de9c commit 08876c2
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 14 deletions.
16 changes: 14 additions & 2 deletions lib/rspec/core/configuration.rb
Expand Up @@ -43,8 +43,7 @@ def self.add_setting(name, opts={})
add_setting :expecting_with_rspec
add_setting :default_path, :default => 'spec'
add_setting :show_failures_in_pending_blocks, :default => false
add_setting :randomize, :default => false
add_setting :seed, :default => srand % 0xFFFF
add_setting :order

CONDITIONAL_FILTERS = {
:if => lambda { |value, metadata| metadata.has_key?(:if) && !value },
Expand All @@ -60,12 +59,15 @@ def self.add_setting(name, opts={})
/lib\/rspec\/(core|expectations|matchers|mocks)/
]

attr_accessor :seed

def initialize
@color_enabled = false
self.include_or_extend_modules = []
self.files_to_run = []
self.backtrace_clean_patterns = DEFAULT_BACKTRACE_PATTERNS.dup
self.exclusion_filter = CONDITIONAL_FILTERS.dup
self.seed = srand % 0xFFFF
end

def reset
Expand Down Expand Up @@ -466,6 +468,16 @@ def seed_to_report
randomize? ? seed : nil
end

def randomize?
order ? !!order.match(/rand/) : false
end

def orderby=(type)
order, seed = type.to_s.split(':')
self.order = order
self.seed = seed.to_i if seed
end

private

def assert_no_example_groups_defined(config_option)
Expand Down
12 changes: 5 additions & 7 deletions lib/rspec/core/option_parser.rb
Expand Up @@ -80,14 +80,12 @@ def parser(options)
parser.on('-O', '--options PATH', 'Specify the path to an options file') do |path|
options[:custom_options_file] = path
end

parser.on('-s', '--seed SEED', Integer, 'Sets the seed for randomization') do |seed|
options[:seed] = seed.to_i
options[:randomize] = true
end

parser.on('--rand', '--randomize', 'Run examples in a random order') do
options[:randomize] = true
parser.on('--order TYPE', 'Run examples by the specified order type',
'[rand] randomized',
'[random] alias for rand',
'append ":SEED" to specify a seed. Example: --order random:123') do |o|
options[:orderby] = o
end

parser.on('-p', '--profile', 'Enable profiling of examples with output of the top 10 slowest examples') do |o|
Expand Down
Expand Up @@ -2,7 +2,7 @@

describe 'command line', :ui do
before :all do
write_file 'spec/randomize_spec.rb', """
write_file 'spec/order_spec.rb', """
describe 'group 1' do
specify('group 1 example 1') {}
specify('group 1 example 2') {}
Expand Down Expand Up @@ -52,9 +52,9 @@
"""
end

describe '--randomize' do
describe '--order rand' do
it 'runs the examples and groups in a different order each time' do
2.times { run_command 'rspec spec/randomize_spec.rb --randomize -f doc' }
2.times { run_command 'rspec spec/order_spec.rb --order rand -f doc' }

top_level_groups {|first_run, second_run| first_run.should_not eq(second_run)}
nested_groups {|first_run, second_run| first_run.should_not eq(second_run)}
Expand All @@ -67,9 +67,9 @@
end
end

describe '--seed' do
describe '--order rand:SEED' do
it 'runs the examples and groups in the same order each time' do
2.times { run_command 'rspec spec/randomize_spec.rb --seed 123 -f doc' }
2.times { run_command 'rspec spec/order_spec.rb --order rand:123 -f doc' }

top_level_groups {|first_run, second_run| first_run.should eq(second_run)}
nested_groups {|first_run, second_run| first_run.should eq(second_run)}
Expand Down

0 comments on commit 08876c2

Please sign in to comment.