diff --git a/lib/rspec/matchers/operator_matcher.rb b/lib/rspec/matchers/operator_matcher.rb index d51d96990..98d3dc91c 100644 --- a/lib/rspec/matchers/operator_matcher.rb +++ b/lib/rspec/matchers/operator_matcher.rb @@ -29,6 +29,15 @@ def self.use_custom_matcher_or_delegate(operator) eval_match(@actual, operator, expected) end end + + negative_operator = operator.sub(/^=/, '!') + if negative_operator != operator && respond_to?(negative_operator) + define_method(negative_operator) do |expected| + opposite_should = ::RSpec::Matchers.last_should == :should ? :should_not : :should + raise "RSpec does not support `#{::RSpec::Matchers.last_should} #{negative_operator} expected`. " + + "Use `#{opposite_should} #{operator} expected` instead." + end + end end ['==', '===', '=~', '>', '>=', '<', '<='].each do |operator| diff --git a/spec/rspec/matchers/operator_matcher_spec.rb b/spec/rspec/matchers/operator_matcher_spec.rb index 14114fe9a..2f6448b76 100644 --- a/spec/rspec/matchers/operator_matcher_spec.rb +++ b/spec/rspec/matchers/operator_matcher_spec.rb @@ -21,6 +21,32 @@ end +describe "unsupported operators", :ruby => '1.9' do + it "raises an appropriate error for should != expected" do + expect { + "apple".should != "pear" + }.to raise_error(/does not support `should != expected`. Use `should_not == expected`/) + end + + it "raises an appropriate error for should_not != expected" do + expect { + "apple".should_not != "pear" + }.to raise_error(/does not support `should_not != expected`. Use `should == expected`/) + end + + it "raises an appropriate error for should !~ expected" do + expect { + "apple".should !~ /regex/ + }.to raise_error(/does not support `should !~ expected`. Use `should_not =~ expected`/) + end + + it "raises an appropriate error for should_not !~ expected" do + expect { + "apple".should_not !~ /regex/ + }.to raise_error(/does not support `should_not !~ expected`. Use `should =~ expected`/) + end +end + describe "should_not ==" do it "delegates message to target" do diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index f511dc414..bc121520e 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -49,5 +49,17 @@ def fail_with(message) config.include RSpec::Mocks::Methods config.color_enabled = true config.filter_run :focused => true + + config.filter_run_excluding :ruby => lambda {|version| + case version.to_s + when "!jruby" + RUBY_ENGINE != "jruby" + when /^> (.*)/ + !(RUBY_VERSION.to_s > $1) + else + !(RUBY_VERSION.to_s =~ /^#{version.to_s}/) + end + } + config.run_all_when_everything_filtered = true end