Skip to content

Commit

Permalink
Add :alias_with option to configuration.add_setting and deprecate the
Browse files Browse the repository at this point in the history
:alias key.
  • Loading branch information
dchelimsky committed Oct 29, 2011
1 parent 8d53640 commit f87e9d7
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 19 deletions.
7 changes: 6 additions & 1 deletion Changelog.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
### DEV
### 2.8.0 (in development)

* Enhancements
* --order (Justin Ko)
* Run your examples in random order: `--order rand`
* Pass a seed: `--order random:123`
* SharedContext supports `let` (David Chelimsky)
* Override opposing tags from the command line (David Chelimsky)

* Bug fixes
* Make sure the `bar` in `--tag foo:bar` makes it to DRb (Aaron Gibralter)

* Deprecations
* :alias option on `configuration.add_setting`. Use `:alias_with` on the
original setting declaration instead.

### 2.7.1 / 2011-10-20

Expand Down
40 changes: 26 additions & 14 deletions lib/rspec/core/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,31 +14,40 @@ def self.define_predicate_for(name)

# @api private
#
# Delegated to by the `add_setting` instance method.
# Invoked by the `add_setting` instance method. Use that method on a
# `Configuration` instance rather than this class method.
def self.add_setting(name, opts={})
raise "Use the instance add_setting method if you want to set a default" if opts.has_key?(:default)
if opts[:alias]
RSpec.warn_deprecation <<-MESSAGE
The :alias option to add_setting is deprecated. Use :alias_with on the original setting instead.
Called from #{caller(0)[4]}
MESSAGE
alias_method name, opts[:alias]
alias_method "#{name}=", "#{opts[:alias]}="
define_predicate_for name
else
attr_accessor name
define_predicate_for name
end
if opts[:alias_with]
[opts[:alias_with]].flatten.each do |alias_name|
alias_method alias_name, name
alias_method "#{alias_name}=", "#{name}="
define_predicate_for alias_name
end
end
end

add_setting :error_stream
add_setting :output_stream
add_setting :output, :alias => :output_stream
add_setting :out, :alias => :output_stream
add_setting :output_stream, :alias_with => [:output, :out]
add_setting :drb
add_setting :drb_port
add_setting :profile_examples
add_setting :fail_fast
add_setting :failure_exit_code
add_setting :run_all_when_everything_filtered
add_setting :pattern
add_setting :filename_pattern, :alias => :pattern
add_setting :pattern, :alias_with => :filename_pattern
add_setting :files_to_run
add_setting :include_or_extend_modules
add_setting :backtrace_clean_patterns
Expand Down Expand Up @@ -95,8 +104,9 @@ def reset
# can add config settings that are domain specific. For example:
#
# RSpec.configure do |c|
# c.add_setting :use_transactional_fixtures, :default => true
# c.add_setting :use_transactional_examples, :alias => :use_transactional_fixtures
# c.add_setting :use_transactional_fixtures,
# :default => true,
# :alias_with => :use_transactional_examples
# end
#
# `add_setting` creates three methods on the configuration object, a
Expand All @@ -109,16 +119,18 @@ def reset
# ### Options
#
# `add_setting` takes an optional hash that supports the keys `:default`
# and `:alias`.
# and `:alias_with`.
#
# Use `:default` to set a default value for the generated getter and
# predicate methods:
#
# :default => "default value"
# add_setting(:foo, :default => "default value")
#
# Use `:alias` to alias the setter, getter, and predicate to another name:
# Use `:alias_with` to alias the setter, getter, and predicate to another
# name, or names:
#
# :alias => :other_setting
# add_setting(:foo, :alias_with => :bar)
# add_setting(:foo, :alias_with => [:bar, :baz])
#
def add_setting(name, opts={})
default = opts.delete(:default)
Expand Down Expand Up @@ -204,7 +216,7 @@ def expectation_framework=(framework)

# Sets the expectation framework module(s).
#
# `frameworks` can be :rspec, :stdlib, or both
# `frameworks` can be :rspec, :stdlib, or both
#
# Given :rspec, configures rspec/expectations.
# Given :stdlib, configures test/unit/assertions
Expand Down Expand Up @@ -610,7 +622,7 @@ def custom_formatter(formatter_ref)
end
end
end

def string_const?(str)
str.is_a?(String) && /\A[A-Z][a-zA-Z0-9_:]*\z/ =~ str
end
Expand Down
15 changes: 11 additions & 4 deletions spec/rspec/core/configuration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ module RSpec::Core
config.files_to_run.should eq([file])
end
end

context "with default default_path" do
it "loads files in the default path when run by rspec" do
config.stub(:command) { 'rspec' }
Expand Down Expand Up @@ -296,7 +296,7 @@ module RSpec::Core
config.full_description = "foo"
config.filter.should eq({:full_description => /foo/})
end

describe "#default_path" do
it 'defaults to "spec"' do
config.default_path.should eq('spec')
Expand Down Expand Up @@ -434,7 +434,7 @@ def metadata_hash(*args)
after(:all) do
ENV['ANSICON'] = @original_ansicon
end

it "enables colors" do
config.output_stream = StringIO.new
config.output_stream.stub(:tty?) { true }
Expand Down Expand Up @@ -844,10 +844,17 @@ def metadata_hash(*args)
end

context "with :alias => " do
before do
it "is deprecated" do
RSpec::should_receive(:warn).with /deprecated/
config.add_setting :custom_option
config.add_setting :another_custom_option, :alias => :custom_option
end
end

context "with :alias_with => " do
before do
config.add_setting :custom_option, :alias_with => :another_custom_option
end

it "delegates the getter to the other option" do
config.another_custom_option = "this value"
Expand Down

0 comments on commit f87e9d7

Please sign in to comment.