Skip to content

Commit

Permalink
Add #add_should_and_should_not to configuration API.
Browse files Browse the repository at this point in the history
This allows users to manually add these methods to proxy objects that do not have them.

Closes #114.
  • Loading branch information
myronmarston committed Jun 15, 2012
1 parent 4325717 commit a954586
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
13 changes: 13 additions & 0 deletions lib/rspec/matchers/configuration.rb
Expand Up @@ -38,6 +38,19 @@ def syntax
syntaxes << :expect if Expectations::Syntax.expect_enabled? syntaxes << :expect if Expectations::Syntax.expect_enabled?
syntaxes syntaxes
end end

# Adds `should` and `should_not` to the given classes
# or modules. This can be used to ensure `should` works
# properly on things like proxy objects (particular
# `Delegator`-subclassed objects on 1.8).
#
# @param [Array<Module>] modules the list of classes or modules
# to add `should` and `should_not` to.
def add_should_and_should_not_to(*modules)
modules.each do |mod|
Expectations::Syntax.enable_should(mod)
end
end
end end


# The configuration object # The configuration object
Expand Down
19 changes: 19 additions & 0 deletions spec/rspec/matchers/configuration_spec.rb
@@ -1,4 +1,5 @@
require 'spec_helper' require 'spec_helper'
require 'delegate'


module RSpec module RSpec
module Matchers module Matchers
Expand All @@ -7,6 +8,24 @@ module Matchers
RSpec::Matchers.configuration.should be_a(RSpec::Matchers::Configuration) RSpec::Matchers.configuration.should be_a(RSpec::Matchers::Configuration)
RSpec::Matchers.configuration.should be(RSpec::Matchers.configuration) RSpec::Matchers.configuration.should be(RSpec::Matchers.configuration)
end end

context 'on an interpreter that does not provide BasicObject', :unless => defined?(::BasicObject) do
before { RSpec::Expectations::Syntax.disable_should(Delegator) }

let(:klass) do
Class.new(SimpleDelegator) do
def delegated?; true; end
end
end

let(:instance) { klass.new(Object.new) }

it 'provides a means to manually add it Delegator' do
instance.should_not respond_to(:delegated?) # because #should is being delegated...
RSpec::Matchers.configuration.add_should_and_should_not_to Delegator
instance.should respond_to(:delegated?) # now it should work!
end
end
end end


shared_examples_for "configuring the expectation syntax" do shared_examples_for "configuring the expectation syntax" do
Expand Down

0 comments on commit a954586

Please sign in to comment.