1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
|
module RSpec
module Matchers
module BuiltIn
class Exist < BaseMatcher
def initialize(*expected)
@expected = expected
end
def matches?(actual)
@actual = actual
predicates = [:exist?, :exists?].select { |p| @actual.respond_to?(p) }
existence_values = predicates.map { |p| @actual.__send__(p, *@expected) }
uniq_truthy_values = existence_values.map { |v| !!v }.uniq
case uniq_truthy_values.size
when 0; raise NoMethodError.new("#{@actual.inspect} does not respond to either #exist? or #exists?")
when 1; existence_values.first
else raise "#exist? and #exists? returned different values:\n\n" +
" exist?: #{existence_values.first}\n" +
"exists?: #{existence_values.last}"
end
end
end
end
end
end
|