Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

clean up operator matcher

  • Loading branch information...
commit fdc5a255db6011bd2516eb2eb53c3cd322e584ae 1 parent 8b2faa6
David Chelimsky dchelimsky authored
Showing with 20 additions and 43 deletions.
  1. +20 −43 lib/spec/matchers/operator_matcher.rb
63 lib/spec/matchers/operator_matcher.rb
View
@@ -1,40 +1,20 @@
module Spec
module Matchers
class BaseOperatorMatcher
- def initialize(given)
- @given = given
+ def initialize(actual)
+ @actual = actual
end
-
- def ==(expected)
- __delegate_method_missing_to_given("==", expected)
- end
-
- def ===(expected)
- __delegate_method_missing_to_given("===", expected)
- end
-
- def =~(expected)
- __delegate_method_missing_to_given("=~", expected)
- end
-
- def >(expected)
- __delegate_method_missing_to_given(">", expected)
- end
-
- def >=(expected)
- __delegate_method_missing_to_given(">=", expected)
- end
-
- def <(expected)
- __delegate_method_missing_to_given("<", expected)
- end
-
- def <=(expected)
- __delegate_method_missing_to_given("<=", expected)
+
+ ['==','===','<','<=','>=','>','=~'].each do |operator|
+ define_method operator do |expected|
+ ::Spec::Matchers.last_matcher = self
+ @operator, @expected = operator, expected
+ __delegate_operator(@actual, operator, expected)
+ end
end
def fail_with_message(message)
- Spec::Expectations.fail_with(message, @expected, @given)
+ Spec::Expectations.fail_with(message, @expected, @actual)
end
def description
@@ -45,25 +25,22 @@ def description
class PositiveOperatorMatcher < BaseOperatorMatcher #:nodoc:
- def __delegate_method_missing_to_given(operator, expected)
- @expected = expected
- @operator = operator
- ::Spec::Matchers.last_matcher = self
- return true if @given.__send__(operator, expected)
- return fail_with_message("expected: #{expected.inspect},\n got: #{@given.inspect} (using #{operator})") if ['==','===', '=~'].include?(operator)
- return fail_with_message("expected: #{operator} #{expected.inspect},\n got: #{operator.gsub(/./, ' ')} #{@given.inspect}")
+ def __delegate_operator(actual, operator, expected)
+ return true if actual.__send__(operator, expected)
+ if ['==','===', '=~'].include?(operator)
+ fail_with_message("expected: #{expected.inspect},\n got: #{actual.inspect} (using #{operator})")
+ else
+ fail_with_message("expected: #{operator} #{expected.inspect},\n got: #{operator.gsub(/./, ' ')} #{actual.inspect}")
+ end
end
end
class NegativeOperatorMatcher < BaseOperatorMatcher #:nodoc:
- def __delegate_method_missing_to_given(operator, expected)
- @expected = expected
- @operator = operator
- ::Spec::Matchers.last_matcher = self
- return true unless @given.__send__(operator, expected)
- return fail_with_message("expected not: #{operator} #{expected.inspect},\n got: #{operator.gsub(/./, ' ')} #{@given.inspect}")
+ def __delegate_operator(actual, operator, expected)
+ return true unless actual.__send__(operator, expected)
+ return fail_with_message("expected not: #{operator} #{expected.inspect},\n got: #{operator.gsub(/./, ' ')} #{actual.inspect}")
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.