Skip to content

Commit

Permalink
- add #is_a? support
Browse files Browse the repository at this point in the history
- add docs for #respond_to?
  • Loading branch information
jordansissel committed Sep 6, 2013
1 parent 4cd3177 commit 28586fc
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
10 changes: 10 additions & 0 deletions lib/insist/predicates.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,21 @@ module Insist::Predicates
include Insist::Assert
PREDICATE_METHOD_RE = /\?$/

# Fails if the value does not respond to a method.
#
# insist { "hurray" }.respond_to?(:size)
def respond_to?(method)
assert(value.respond_to?(method),
"#{value.class} does not respond to the '#{method}' method")
end # def respond_to?

# Fails if the value.is_a?(klass) returns false.
#
# insist { "hurray" }.is_a?(Number)
def is_a?(klass)
assert(value.is_a?(klass), "#{value.class} is not a #{klass}")
end

# Pass through any 'foo?' style method calls to the 'value'
# and fail if the the return is false.
def method_missing(m, *args)
Expand Down
12 changes: 12 additions & 0 deletions spec/insist/predicates_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,18 @@
# The predicates feature will delegateany predicate method calls (ones ending
# in "?") to the block value and fail if the return is false.
describe Insist::Predicates do
describe "#is_a?" do
it "should succeed if value#is_a? returns true" do
insist { "some string" }.is_a?(Object)
insist { "some string" }.is_a?(String)
end
it "should fail if value#is_a? returns false" do
reject { "some string" }.is_a?(Numeric)
reject { "some string" }.is_a?(Fixnum)
reject { "some string" }.is_a?(File)
end
end

describe "#respond_to?" do
subject do
insist { [1, 2, 3] }
Expand Down

0 comments on commit 28586fc

Please sign in to comment.