File: exist.rb

package info (click to toggle)
ruby-rspec 3.13.0c0e0m0s1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 6,856 kB
  • sloc: ruby: 70,868; sh: 1,423; makefile: 99
file content (90 lines) | stat: -rw-r--r-- 2,612 bytes parent folder | download
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
87
88
89
90
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) && !deprecated(p, actual) }
          end

          def deprecated(predicate, actual)
            predicate == :exists? && (File == actual || FileTest == actual || Dir == actual)
          end
        end
      end
    end
  end
end