rspec-deep-ignore-order-matcher 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
data/README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# RSpec Deep Matcher
|
2
2
|
|
3
|
-
This gem adds a custom matcher to RSpec to recursively compare nested Ruby data-structures consisting of
|
3
|
+
This gem adds a custom matcher to RSpec to recursively compare nested Ruby data-structures consisting of Hash and Array elements.
|
4
4
|
An order of elements in an array is ignored.
|
5
5
|
|
6
6
|
## Install
|
@@ -11,9 +11,15 @@ or add to your `Gemfile`
|
|
11
11
|
```
|
12
12
|
gem 'rspec-deep-ignore-order-matcher'
|
13
13
|
```
|
14
|
+
and run
|
15
|
+
```
|
16
|
+
bundle install
|
17
|
+
```
|
14
18
|
|
15
19
|
## Example
|
16
20
|
```
|
21
|
+
require 'rspec-deep-ignore-order-matcher'
|
22
|
+
|
17
23
|
describe 'Products' do
|
18
24
|
it "should ignore order of product's tags" do
|
19
25
|
expected = [{ :product => { :title => 'Product 1', :tags => ['large', 'blue', 'heavy'] } }]
|
@@ -21,4 +27,4 @@ describe 'Products' do
|
|
21
27
|
actual.should be_deep_equal expected
|
22
28
|
end
|
23
29
|
end
|
24
|
-
```
|
30
|
+
```
|
@@ -2,7 +2,7 @@ require 'rspec'
|
|
2
2
|
require 'rspec-deep-ignore-order-matcher/version'
|
3
3
|
|
4
4
|
RSpec::Matchers.define :be_deep_equal do |expected|
|
5
|
-
match { |actual|
|
5
|
+
match { |actual| m? actual, expected }
|
6
6
|
|
7
7
|
failure_message_for_should do |actual|
|
8
8
|
"expected that #{actual} would be deep equal with #{expected}"
|
@@ -16,7 +16,7 @@ RSpec::Matchers.define :be_deep_equal do |expected|
|
|
16
16
|
"be deep equal with #{expected}"
|
17
17
|
end
|
18
18
|
|
19
|
-
def
|
19
|
+
def m?(actual, expected)
|
20
20
|
return arrays_matches?(actual, expected) if expected.is_a?(Array) && actual.is_a?(Array)
|
21
21
|
return hashes_matches?(actual, expected) if expected.is_a?(Hash) && actual.is_a?(Hash)
|
22
22
|
expected == actual
|
@@ -25,7 +25,7 @@ RSpec::Matchers.define :be_deep_equal do |expected|
|
|
25
25
|
def arrays_matches?(actual, expected)
|
26
26
|
exp = expected.clone
|
27
27
|
actual.each do |a|
|
28
|
-
index = exp.find_index { |e|
|
28
|
+
index = exp.find_index { |e| m? a, e }
|
29
29
|
return false if index.nil?
|
30
30
|
exp.delete_at(index)
|
31
31
|
end
|
@@ -33,8 +33,8 @@ RSpec::Matchers.define :be_deep_equal do |expected|
|
|
33
33
|
end
|
34
34
|
|
35
35
|
def hashes_matches?(actual, expected)
|
36
|
-
return false unless actual.keys == expected.keys
|
37
|
-
actual.each { |key, value| return false unless
|
36
|
+
return false unless actual.keys.sort == expected.keys.sort
|
37
|
+
actual.each { |key, value| return false unless m? value, expected[key] }
|
38
38
|
true
|
39
39
|
end
|
40
40
|
end
|
data/spec/matcher_spec.rb
CHANGED