Sha256: adc91e8889923c696ee2dcd1fafe0564e772df89e673c158640094a1dde4d276

Contents?: true

Size: 1.04 KB

Versions: 1

Compression:

Stored size: 1.04 KB

Contents

require 'rspec'
require 'rspec-deep-ignore-order-matcher/version'

RSpec::Matchers.define :be_deep_equal do |expected|
	match { |actual| m1 actual, expected }

	failure_message_for_should do |actual|
		"expected that #{actual} would be deep equal with #{expected}"
	end

	failure_message_for_should_not do |actual|
		"expected that #{actual} would not be deep equal with #{expected}"
	end

	description do
		"be deep equal with #{expected}"
	end

	def m1(actual, expected)
		return arrays_matches?(actual, expected) if expected.is_a?(Array) && actual.is_a?(Array)
		return hashes_matches?(actual, expected) if expected.is_a?(Hash) && actual.is_a?(Hash)
		expected == actual
	end

	def arrays_matches?(actual, expected)
		exp = expected.clone
		actual.each do |a|
			index = exp.find_index { |e| m1 a, e }
			return false if index.nil?
			exp.delete_at(index)
		end
		exp.length == 0
	end

	def hashes_matches?(actual, expected)
		return false unless actual.keys == expected.keys
		actual.each { |key, value| return false unless m1 value, expected[key] }
		true
	end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rspec-deep-ignore-order-matcher-0.0.1 lib/rspec-deep-ignore-order-matcher.rb