# File lib/rspec/matchers/built_in/include.rb, line 5 def initialize(*expected) @expected = expected end
# File lib/rspec/matchers/built_in/include.rb, line 19 def description "include#{expected_to_sentence}" end
# File lib/rspec/matchers/built_in/include.rb, line 23 def diffable? # Matchers do not diff well, since diff uses their inspect # output, which includes their instance variables and such. @expected.none? { |e| is_a_matcher?(e) } end
# File lib/rspec/matchers/built_in/include.rb, line 14 def does_not_match?(actual) @actual = actual perform_match(:none?, :any?, @actual, @expected) end
# File lib/rspec/matchers/built_in/include.rb, line 9 def matches?(actual) @actual = actual perform_match(:all?, :all?, @actual, @expected) end
# File lib/rspec/matchers/built_in/include.rb, line 47 def comparing_hash_keys?(actual, expected) actual.is_a?(Hash) && !expected.is_a?(Hash) end
# File lib/rspec/matchers/built_in/include.rb, line 51 def comparing_hash_values?(actual, expected) actual.is_a?(Hash) && expected.is_a?(Hash) end
# File lib/rspec/matchers/built_in/include.rb, line 55 def comparing_with_matcher?(actual, expected) actual.is_a?(Array) && is_a_matcher?(expected) end
# File lib/rspec/matchers/built_in/include.rb, line 59 def is_a_matcher?(object) return false if object.respond_to?(:i_respond_to_everything_so_im_not_really_a_matcher) [:failure_message_for_should, :failure_message].any? do |msg| object.respond_to?(msg) end && object.respond_to?(:matches?) end
# File lib/rspec/matchers/built_in/include.rb, line 31 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