Skip to content

Commit

Permalink
Added be_something matchers with method_missing.
Browse files Browse the repository at this point in the history
Signed-off-by: Jeremy McAnally <jeremymcanally@gmail.com>
  • Loading branch information
mhennemeyer authored and jm committed Feb 11, 2009
1 parent 79bd961 commit 1bc3282
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 3 deletions.
28 changes: 27 additions & 1 deletion lib/matchy/built_in/truth_expectations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,33 @@ def respond_to(*meth)
matcher.negative_msg = "Expected #{@receiver.inspect} to not respond to #{@expected.inspect}."
@receiver.respond_to?(@expected)
end
end
end

alias_method :old_missing, :method_missing
# ==be_*something*
#
# ===This method_missing acts as a matcher builder.
# If a call to be_xyz() reaches this method_missing (say: obj.should be_xyz),
# a matcher with the name xyz will be built, whose defining property
# is that it returns the value of obj.xyz? for matches?.
# ==== Examples
#
# nil.should be_nil
# obj.something? #=> true
# obj.should be_something
def method_missing(name, *args, &block)
if (name.to_s =~ /^be_(.+)/)
build_matcher(name, args) do |receiver, matcher, args|
@receiver = receiver

matcher.positive_msg = "Expected #{@receiver.inspect} to return true for #{$1}?."
matcher.negative_msg = "Expected #{@receiver.inspect} to return false for #{$1}?."
@receiver.send(($1 + "?").to_sym)
end
else
old_missing(name, *args, &block)
end
end
end
end
end
4 changes: 2 additions & 2 deletions test/test_operator_expectations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def test_negative_less_than_fails
}.should raise_error(Test::Unit::AssertionFailedError)
end

# GREATER THAN (<)
# GREATER THAN (>)
def test_greater_than
3.should > 2
end
Expand Down Expand Up @@ -89,7 +89,7 @@ def test_negative_less_than_equal_fails
}.should raise_error(Test::Unit::AssertionFailedError)
end

# GREATER THAN EQUALS (<=)
# GREATER THAN EQUALS (>=)
def test_greater_than_equal
3.should >= 2
end
Expand Down
55 changes: 55 additions & 0 deletions test/test_truth_expectations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ def exist?
end

class TestTruthExpectations < Test::Unit::TestCase

def setup
@obj = Object.new
end

def test_equal
3.should equal(3)
end
Expand Down Expand Up @@ -262,5 +267,55 @@ def test_negative_respond_to_fail
"foo".should_not respond_to(:length)
}.should raise_error(Test::Unit::AssertionFailedError)
end

def test_positive_be_something_method_missing_pass
def @obj.something?
true
end
@obj.should be_something
end

def test_positive_be_something_method_missing_fails
def @obj.something?
false
end
lambda {@obj.should be_something}.should raise_error(Test::Unit::AssertionFailedError)
end

def test_negative_be_something_method_missing_pass
def @obj.something?
false
end
@obj.should_not be_something
end

def test_negative_be_something_method_missing_fails
def @obj.something?
true
end
lambda {@obj.should_not be_something}.should raise_error(Test::Unit::AssertionFailedError)
end

def test_be_something_method_missing_fail_message
obj = "foo"
def obj.something?
true
end
matcher_obj = be_something
obj.should matcher_obj

matcher_obj.failure_message.should be("Expected \"foo\" to return true for something?.")
end

def test_be_something_method_missing_negative_fail_message
obj = "foo"
def obj.something?
false
end
matcher_obj = be_something
obj.should_not matcher_obj

matcher_obj.negative_failure_message.should be("Expected \"foo\" to return false for something?.")
end

end

0 comments on commit 1bc3282

Please sign in to comment.