Skip to content

Commit

Permalink
add boolean custom option
Browse files Browse the repository at this point in the history
  • Loading branch information
dchelimsky committed May 15, 2010
1 parent f720e8b commit 16e7f6b
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 4 deletions.
6 changes: 3 additions & 3 deletions features/configuration/custom_options.feature
@@ -1,4 +1,3 @@
@wip
Feature: custom options

In order to seamlessly provide my users more options
Expand Down Expand Up @@ -45,6 +44,7 @@ Feature: custom options
When I run "rspec boolean_option_spec.rb"
Then I should see "2 examples, 0 failures"

@wip
Scenario: boolean option overridden in client app
Given a file named "boolean_option_spec.rb" with:
"""
Expand All @@ -53,12 +53,12 @@ Feature: custom options
end
Rspec.configure do |c|
c.custom_option = :reset
c.custom_option = true
end
describe "custom option" do
it "returns the value set in the client app" do
Rspec.configuration.custom_option.should == :reset
Rspec.configuration.custom_option.should be_true
end
it "is exposed as a predicate" do
Expand Down
11 changes: 11 additions & 0 deletions lib/rspec/core/configuration.rb
Expand Up @@ -239,6 +239,17 @@ def require_files_to_run
files_to_run.map {|f| require f }
end

def add_option(mname, options)
case options[:type]
when :boolean
(class << self; self; end).class_eval do
attr_reader mname
define_method("#{mname}?") { !!(send mname) }
end
instance_variable_set "@#{mname}", options[:default]
end
end

end
end
end
29 changes: 28 additions & 1 deletion spec/rspec/core/configuration_spec.rb
Expand Up @@ -261,6 +261,33 @@ def that_thing
end
end

end
describe "#add_option" do
describe ":type => :boolean" do
context "with no additional options" do
before { config.add_option :custom_option, :type => :boolean }

it "defaults to false" do
config.custom_option.should be_false
end

it "adds a predicate" do
config.custom_option?.should be_false
end
end

context "with :default => true" do
before { config.add_option :custom_option, :type => :boolean, :default => true }

it "defaults to false" do
config.custom_option.should be_true
end

it "adds a predicate" do
config.custom_option?.should be_true
end
end
end
end

end
end

0 comments on commit 16e7f6b

Please sign in to comment.