File: include.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 (61 lines) | stat: -rw-r--r-- 1,800 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
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
module RSpec
  module Matchers
    module BuiltIn
      class Include < BaseMatcher
        def initialize(*expected)
          @expected = expected
        end

        def matches?(actual)
          @actual = actual
          perform_match(:all?, :all?, @actual, @expected)
        end

        def does_not_match?(actual)
          @actual = actual
          perform_match(:none?, :any?, @actual, @expected)
        end

        def description
          "include#{expected_to_sentence}"
        end

        def diffable?
          # Matchers do not diff well, since diff uses their inspect
          # output, which includes their instance variables and such.
          @expected.none? { |e| RSpec::Matchers.is_a_matcher?(e) }
        end

        private

        def perform_match(predicate, hash_predicate, actuals, expecteds)
          expecteds.__send__(predicate) do |expected|
            if comparing_hash_values?(actuals, expected)
              expected.__send__(hash_predicate) { |k,v|
                actuals.has_key?(k) && actuals[k] == v
              }
            elsif comparing_hash_keys?(actuals, expected)
              actuals.has_key?(expected)
            elsif comparing_with_matcher?(actual, expected)
              actual.any? { |value| expected.matches?(value) }
            else
              actuals.include?(expected)
            end
          end
        end

        def comparing_hash_keys?(actual, expected)
          actual.is_a?(Hash) && !expected.is_a?(Hash)
        end

        def comparing_hash_values?(actual, expected)
          actual.is_a?(Hash) && expected.is_a?(Hash)
        end

        def comparing_with_matcher?(actual, expected)
          actual.is_a?(Array) && RSpec::Matchers.is_a_matcher?(expected)
        end
      end
    end
  end
end