Skip to content
This repository has been archived by the owner on Oct 27, 2021. It is now read-only.

Commit

Permalink
convert be_an_instance_of matcher to Matcher
Browse files Browse the repository at this point in the history
  • Loading branch information
dchelimsky committed Apr 11, 2009
1 parent 1e7a070 commit bf09efe
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 30 deletions.
33 changes: 7 additions & 26 deletions lib/spec/matchers/be_instance_of.rb
@@ -1,28 +1,5 @@
module Spec
module Matchers
class BeInstanceOf
def initialize(expected)
@expected = expected
end

def matches?(actual)
@actual = actual
@actual.instance_of?(@expected)
end

def description
"be an instance of #{@expected}"
end

def failure_message_for_should
"expected instance of #{@expected}, got #{@actual.inspect}"
end

def failure_message_for_should_not
"expected #{@actual.inspect} not to be an instance of #{@expected}"
end
end

# :call-seq:
# should be_instance_of(expected)
# should be_an_instance_of(expected)
Expand All @@ -36,10 +13,14 @@ def failure_message_for_should_not
# 5.should be_instance_of(Fixnum)
# 5.should_not be_instance_of(Numeric)
# 5.should_not be_instance_of(Float)
def be_instance_of(expected)
BeInstanceOf.new(expected)
def be_an_instance_of(expected)
Matcher.new :be_an_instance_of, expected do |expected|
match do |actual|
actual.instance_of?(expected)
end
end
end

alias_method :be_an_instance_of, :be_instance_of
alias_method :be_instance_of, :be_an_instance_of
end
end
10 changes: 6 additions & 4 deletions spec/spec/matchers/be_instance_of_spec.rb
Expand Up @@ -2,22 +2,24 @@

module Spec
module Matchers
[:be_instance_of, :be_an_instance_of].each do |method|
[:be_an_instance_of, :be_instance_of].each do |method|
describe "actual.should #{method}(expected)" do
it "passes if actual is instance of expected class" do
5.should send(method, Fixnum)
end

it "fails if actual is instance of subclass of expected class" do
lambda { 5.should send(method, Numeric) }.should fail_with(%Q{expected instance of Numeric, got 5})
lambda { 5.should send(method, Numeric) }.should fail_with(%Q{expected 5 to be an instance of Numeric})
end

it "fails with failure message for should unless actual is instance of expected class" do
lambda { "foo".should send(method, Array) }.should fail_with(%Q{expected instance of Array, got "foo"})
lambda { "foo".should send(method, Array) }.should fail_with(%Q{expected "foo" to be an instance of Array})
end

it "provides a description" do
Spec::Matchers::BeInstanceOf.new(Class).description.should == "be an instance of Class"
matcher = be_an_instance_of(Fixnum)
matcher.matches?(Numeric)
matcher.description.should == "be an instance of Fixnum"
end
end

Expand Down

0 comments on commit bf09efe

Please sign in to comment.