Created
April 1, 2014 07:31
-
-
Save danielfone/9909503 to your computer and use it in GitHub Desktop.
Memoized exist matcher
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module RSpec | |
module Matchers | |
module BuiltIn | |
# @api private | |
# Provides the implementation for `exist`. | |
# Not intended to be instantiated directly. | |
class Exist < BaseMatcher | |
def initialize(*expected) | |
@expected = expected | |
end | |
# @api private | |
# @return [Boolean] | |
def matches?(actual) | |
@actual = actual | |
@test = ExistenceTest.new @actual, @expected | |
@test.valid_test? && @test.actual_exists? | |
end | |
def does_not_match?(actual) | |
@actual = actual | |
@test = ExistenceTest.new @actual, @expected | |
@test.valid_test? && [email protected]_exists? | |
end | |
# @api private | |
# @return [String] | |
def failure_message | |
"expected #{@actual.inspect} to exist#{@test.validity_message}" | |
end | |
# @api private | |
# @return [String] | |
def failure_message_when_negated | |
"expected #{@actual.inspect} not to exist#{@test.validity_message}" | |
end | |
private | |
class ExistenceTest < Struct.new(:actual, :expected) | |
def valid_test? | |
uniq_truthy_values.size == 1 | |
end | |
def actual_exists? | |
existence_values.first | |
end | |
def validity_message | |
case uniq_truthy_values.size | |
when 0 | |
" but it does not respond to either `exist?` or `exists?`" | |
when 2 | |
" but `exist?` and `exists?` returned different values:\n\n"\ | |
" exist?: #{existence_values.first}\n"\ | |
"exists?: #{existence_values.last}" | |
end | |
end | |
private | |
def uniq_truthy_values | |
@uniq_truthy_values ||= existence_values.map { |v| !!v }.uniq | |
end | |
def existence_values | |
@existence_values ||= predicates.map { |p| actual.__send__(p, *expected) } | |
end | |
def predicates | |
@predicates ||= [:exist?, :exists?].select { |p| actual.respond_to?(p) } | |
end | |
end | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment