Permalink
Browse files

add +kind_of+ and +respond_to+ expectations, similar to rspec.

Signed-off-by: Jeremy McAnally <jeremymcanally@gmail.com>
  • Loading branch information...
1 parent 5aa2914 commit d7257342fb5c92af62cd8035013d90cc0ecd2cc8 @mattly mattly committed with Jan 18, 2009
Showing with 92 additions and 1 deletion.
  1. +51 −1 lib/matchy/built_in/truth_expectations.rb
  2. +41 −0 test/test_truth_expectations.rb
@@ -16,6 +16,21 @@ def negative_failure_message
end
end
+ class BeKindOfExpectation < Base
+ def matches?(receiver)
+ @receiver = receiver
+ @receiver.kind_of?(@expected)
+ end
+
+ def failure_message
+ "Expected #{@receiver.inspect} to be kind of #{@expected.inspect}."
+ end
+
+ def negative_failure_message
+ "Expected #{@receiver.inspect} to not be kind of #{@expected.inspect}."
+ end
+ end
+
class BeCloseExpectation < Base
def initialize(expected, delta, test_case)
@expected = expected
@@ -102,6 +117,21 @@ def negative_failure_message
end
end
+ class RespondToExpectation < Base
+ def matches?(receiver)
+ @receiver = receiver
+ @receiver.respond_to?(@expected)
+ end
+
+ def failure_message
+ "Expected #{@receiver.inspect} to respond to #{@expected.inspect}."
+ end
+
+ def negative_failure_message
+ "Expected #{@receiver.inspect} to not respond to #{@expected.inspect}."
+ end
+ end
+
module TestCaseExtensions
# Simply checks if the receiver matches the expected object.
# TODO: Fill this out to implement much of the RSpec functionality (and then some)
@@ -115,6 +145,16 @@ def be(obj)
Matchy::Expectations::BeExpectation.new(obj, self)
end
+ # Checks if the given object is kind_of? the expected class
+ #
+ # ==== Examples
+ #
+ # "hello".should be_kind_of(String)
+ # 3.should be_kind_of(Fixnum)
+ def be_kind_of(klass)
+ Matchy::Expectations::BeKindOfExpectation.new(klass, self)
+ end
+
# Checks if the given object is within a given object and delta.
#
# ==== Examples
@@ -175,7 +215,17 @@ def equal(obj)
#
def satisfy(obj)
Matchy::Expectations::SatisfyExpectation.new(obj, self)
- end
+ end
+
+ # Checks if the given object responds to the given method
+ #
+ # ==== Examples
+ #
+ # "foo".should respond_to(:length)
+ # {}.should respond_to(:has_key?)
+ def respond_to(meth)
+ Matchy::Expectations::RespondToExpectation.new(meth, self)
+ end
end
end
end
@@ -214,4 +214,45 @@ def test_eql_negative_fail_message
obj.negative_failure_message.should be "Expected 4 to not satisfy given block."
end
+
+ def test_kind_of
+ 3.should be_kind_of(Fixnum)
+ end
+
+ def test_kind_of_fail
+ lambda {
+ 3.should be_kind_of(Hash)
+ }.should raise_error(Test::Unit::AssertionFailedError)
+ end
+
+ def test_negative_kind_of
+ 3.should_not be_kind_of(Hash)
+ end
+
+ def test_negative_kind_of_fail
+ lambda {
+ 3.should_not be_kind_of(Fixnum)
+ }.should raise_error(Test::Unit::AssertionFailedError)
+ end
+
+ def test_respond_to
+ "foo".should respond_to?(:length)
+ end
+
+ def test_kind_of_fail
+ lambda {
+ "foo".should respond_to?(:nonexistant_method)
+ }.should raise_error(Test::Unit::AssertionFailedError)
+ end
+
+ def test_negative_kind_of
+ "foo".should_not respond_to?(:nonexistant_method)
+ end
+
+ def test_negative_kind_of_fail
+ lambda {
+ "foo".should_not respond_to?(:length)
+ }.should raise_error(Test::Unit::AssertionFailedError)
+ end
+
end

0 comments on commit d725734

Please sign in to comment.