Permalink
Browse files

Raise an error if should/should_not !=/!~ is used since these operato…

…rs cannot be supported on ruby 1.8.

- Closes #33.
  • Loading branch information...
1 parent c9a3033 commit 2d1a14588a34a738d1b66af7ee3d1c4f18d3e15b @myronmarston myronmarston committed with dchelimsky Oct 19, 2010
Showing with 47 additions and 0 deletions.
  1. +9 −0 lib/rspec/matchers/operator_matcher.rb
  2. +26 −0 spec/rspec/matchers/operator_matcher_spec.rb
  3. +12 −0 spec/spec_helper.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|
@@ -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
View
@@ -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

0 comments on commit 2d1a145

Please sign in to comment.