File: exist.rb

package info (click to toggle)
ruby-rspec-expectations 2.14.2-1~bpo70%2B1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy-backports
  • size: 920 kB
  • sloc: ruby: 8,202; makefile: 4
file content (26 lines) | stat: -rw-r--r-- 874 bytes parent folder | download | duplicates (2)
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