/usr/lib/ruby/vendor_ruby/rspec/matchers/built_in/exist.rb is in ruby-rspec-expectations 3.4.0c3e0m1s1-1ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 | 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
# @api private
# @return [Boolean]
def does_not_match?(actual)
@actual = actual
@test = ExistenceTest.new @actual, @expected
@test.valid_test? && !@test.actual_exists?
end
# @api private
# @return [String]
def failure_message
"expected #{actual_formatted} to exist#{@test.validity_message}"
end
# @api private
# @return [String]
def failure_message_when_negated
"expected #{actual_formatted} not to exist#{@test.validity_message}"
end
# @api private
# Simple class for memoizing actual/expected for this matcher
# and examining the match
class ExistenceTest < Struct.new(:actual, :expected)
# @api private
# @return [Boolean]
def valid_test?
uniq_truthy_values.size == 1
end
# @api private
# @return [Boolean]
def actual_exists?
existence_values.first
end
# @api private
# @return [String]
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
|