Navigation Menu

Skip to content

Commit

Permalink
--order --seed and #force. Removed duplication.
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexKVal committed Dec 18, 2011
1 parent 5e54d76 commit 5a2dcc4
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 33 deletions.
54 changes: 24 additions & 30 deletions lib/rspec/core/configuration.rb
Expand Up @@ -109,7 +109,7 @@ def self.add_setting(name, opts={})

# Determines the order in which examples are run (default: OS standard
# load order for files, declaration order for groups and examples).
add_setting :order
define_reader :order

# Default: `$stdout`.
# Also known as `output` and `out`
Expand All @@ -135,7 +135,7 @@ def self.add_setting(name, opts={})
#
# We recommend, actually, that you use the command line approach so you
# don't accidentally leave the seed encoded.
add_setting :seed
define_reader :seed

# When a block passed to pending fails (as expected), display the failure
# without reporting it as a failure (default: false).
Expand Down Expand Up @@ -197,19 +197,10 @@ def initialize
#
# Used to set higher priority option values from the command line.
def force(hash)
# TODO - remove the duplication between this and seed=
if hash.has_key?(:seed)
hash[:seed] = hash[:seed].to_i
hash[:order] = "rand"
self.seed = hash[:seed]
end

# TODO - remove the duplication between this and order=
if hash.has_key?(:order)
self.order = hash[:order]
order, seed = hash[:order].split(":")
hash[:order] = order
hash[:seed] = seed.to_i if seed
hash[:order], hash[:seed] = order_and_seed_from_seed(hash[:seed])
elsif hash.has_key?(:order)
set_order_and_seed(hash)
end
@preferred_options.merge!(hash)
end
Expand Down Expand Up @@ -645,32 +636,18 @@ def load_spec_files
raise_if_rspec_1_is_loaded
end

remove_method :seed=

# @api
#
# Sets the seed value and sets `order='rand'`
def seed=(seed)
# TODO - remove the duplication between this and force
@order = 'rand'
@seed = seed.to_i
order_and_seed_from_seed(seed)
end

remove_method :order=

# @api
#
# Sets the order and, if order is `'rand:<seed>'`, also sets the seed.
def order=(type)
# TODO - remove the duplication between this and force
order, seed = type.to_s.split(':')
if order == 'default'
@order = nil
@seed = nil
else
@order = order
@seed = seed.to_i if seed
end
order_and_seed_from_order(type)
end

def randomize?
Expand Down Expand Up @@ -799,6 +776,23 @@ def file_at(path)
File.new(path, 'w')
end

def order_and_seed_from_seed(value)
@order, @seed = 'rand', value.to_i
end

def set_order_and_seed(hash)
hash[:order], seed = order_and_seed_from_order(hash[:order])
hash[:seed] = seed if seed
end

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'
return order, seed
end

end
end
end
7 changes: 4 additions & 3 deletions lib/rspec/core/option_parser.rb
Expand Up @@ -42,14 +42,15 @@ def parser(options)
options[:custom_options_file] = path
end

parser.on('--order TYPE', 'Run examples by the specified order type',
' [rand] randomized',
parser.on('--order TYPE[:SEED]', 'Run examples by the specified order type',
' [default] files are ordered based on the underlying file system\'s order',
' [rand] randomize the order of files, groups and examples',
' [random] alias for rand',
' [random:SEED] e.g. --order random:123') do |o|
options[:order] = o
end

parser.on('--seed SEED', "Equivalent of --order rand:SEED") do |seed|
parser.on('--seed SEED', Integer, 'Equivalent of --order rand:SEED') do |seed|
options[:order] = "rand:#{seed}"
end

Expand Down
10 changes: 10 additions & 0 deletions spec/rspec/core/configuration_spec.rb
Expand Up @@ -1041,6 +1041,16 @@ def metadata_hash(*args)
config.seed.should eq(37)
config.order.should eq("rand")
end

it "forces 'false' value" do
config.add_setting :custom_option
config.custom_option = true
config.custom_option?.should be_true
config.force :custom_option => false
config.custom_option?.should be_false
config.custom_option = true
config.custom_option?.should be_false
end
end

describe '#seed' do
Expand Down

0 comments on commit 5a2dcc4

Please sign in to comment.