Navigation Menu

Skip to content

Commit

Permalink
Raise an error if should/should_not !=/!~ is used since these operato…
Browse files Browse the repository at this point in the history
…rs cannot be supported on ruby 1.8.

- Closes #33.
  • Loading branch information
myronmarston authored and dchelimsky committed Nov 6, 2010
1 parent e852032 commit 0e46795
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 0 deletions.
9 changes: 9 additions & 0 deletions lib/rspec/matchers/operator_matcher.rb
Expand Up @@ -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|
Expand Down
26 changes: 26 additions & 0 deletions spec/rspec/matchers/operator_matcher_spec.rb
Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions spec/spec_helper.rb
Expand Up @@ -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 0e46795

Please sign in to comment.