Skip to content

Commit

Permalink
exist matcher from dsl to class
Browse files Browse the repository at this point in the history
  • Loading branch information
dchelimsky committed Oct 21, 2011
1 parent 17054d5 commit 4fc2207
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 18 deletions.
3 changes: 3 additions & 0 deletions lib/rspec/matchers.rb
Expand Up @@ -240,6 +240,9 @@ def be_nil
BeNil.new
end

def exist(*args)
Exist.new(*args)
end
end
end

Expand Down
35 changes: 17 additions & 18 deletions lib/rspec/matchers/exist.rb
@@ -1,24 +1,23 @@
module RSpec
module Matchers
# :call-seq:
# should exist
# should_not exist
#
# Passes if actual.exist? or actual.exists?
def exist(*args)
Matcher.new :exist do
match do |actual|
predicates = [:exist?, :exists?].select { |p| actual.respond_to?(p) }
existance_values = predicates.map { |p| actual.send(p, *args) }
uniq_truthy_values = existance_values.map { |v| !!v }.uniq
class Exist
include BaseMatcher

case uniq_truthy_values.size
when 0; raise NoMethodError.new("#{actual.inspect} does not respond to either #exist? or #exists?")
when 1; existance_values.first
else raise "#exist? and #exists? returned different values:\n\n" +
" exist?: #{existance_values.first}\n" +
"exists?: #{existance_values.last}"
end
def initialize(*args)
@args = args
end

def matches?(actual)
predicates = [:exist?, :exists?].select { |p| actual.respond_to?(p) }
existance_values = predicates.map { |p| actual.send(p, *@args) }
uniq_truthy_values = existance_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; existance_values.first
else raise "#exist? and #exists? returned different values:\n\n" +
" exist?: #{existance_values.first}\n" +
"exists?: #{existance_values.last}"
end
end
end
Expand Down

0 comments on commit 4fc2207

Please sign in to comment.