eq_wo_order 0.2.3 → 0.2.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG +1 -0
- data/eq_wo_order.gemspec +1 -1
- data/lib/eq_wo_order.rb +36 -32
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8c745e416ae194cf358f919b8d45a49cc4195fc2
|
4
|
+
data.tar.gz: 9fbb9d7568a585c7cd49db4707779a44e3b6a699
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 837cc0dcfe1dc23bd417f38d891f7b9c7459cd09e56fc070ada8087b6683759fa72311938bc755079a7c1649fd84743f38b898b07ccc4eae99b7097ac4dc4bdb
|
7
|
+
data.tar.gz: 83ab48db977a78032493bf706b2f33b114cc1d522aab45b837396a84906bc5430cc5007414b2273109b54f89e4b3943bc5952fd5b9de69b2d9fd07873526507b
|
data/CHANGELOG
CHANGED
data/eq_wo_order.gemspec
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
Gem::Specification.new do |s|
|
2
2
|
s.name = 'eq_wo_order'
|
3
|
-
s.version = '0.2.
|
3
|
+
s.version = '0.2.4'
|
4
4
|
s.date = '2016-02-29'
|
5
5
|
s.summary = 'RSpec equality matcher that ignores nested order'
|
6
6
|
s.description = 'RSpec equality matcher that deeply compares array without order - arrays of primitives, hashes, and arrays. Examples at github.com/jadekler/eq_wo_order'
|
data/lib/eq_wo_order.rb
CHANGED
@@ -4,50 +4,54 @@ RSpec::Matchers.define :eq_wo_order do |expected|
|
|
4
4
|
end
|
5
5
|
|
6
6
|
def eq_wo_order_base(actual, expected)
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
7
|
+
case actual
|
8
|
+
when Array
|
9
|
+
primitive_items_match?(actual, expected) &&
|
10
|
+
array_items_match?(actual, expected) &&
|
11
|
+
hash_items_match?(actual, expected)
|
12
|
+
when Hash
|
13
|
+
all_items_in_source?(actual, expected) && all_items_in_source?(expected, actual)
|
14
|
+
else
|
15
|
+
actual == expected
|
16
|
+
end
|
17
|
+
end
|
13
18
|
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
+
def primitive_items_match?(actual, expected)
|
20
|
+
actual_primitive_items = primitives(actual)
|
21
|
+
expected_primitive_items = primitives(expected)
|
22
|
+
sort_as_s(actual_primitive_items) == sort_as_s(expected_primitive_items)
|
23
|
+
end
|
19
24
|
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
25
|
+
def hash_items_match?(actual, expected)
|
26
|
+
actual_hash_items = actual.grep(Hash)
|
27
|
+
expected_hash_items = expected.grep(Hash)
|
28
|
+
all_items_in_source?(actual_hash_items, expected_hash_items) &&
|
29
|
+
all_items_in_source?(expected_hash_items, actual_hash_items)
|
30
|
+
end
|
24
31
|
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
+
def array_items_match?(actual, expected)
|
33
|
+
actual_array_items = actual.grep(Array)
|
34
|
+
expected_array_items = expected.grep(Array)
|
35
|
+
all_items_in_source?(actual_array_items, expected_array_items) &&
|
36
|
+
all_items_in_source?(expected_array_items, actual_array_items)
|
37
|
+
end
|
38
|
+
|
39
|
+
def primitives(list)
|
40
|
+
list.find_all { |x| x.class != Hash && x.class != Array }
|
32
41
|
end
|
33
42
|
|
34
43
|
# given one array of arrays/hashes
|
35
44
|
# and another array of arrays/hashes
|
36
45
|
# are all the items of the first found in the second?
|
37
46
|
def all_items_in_source?(search, source)
|
38
|
-
search.map
|
39
|
-
|
40
|
-
|
41
|
-
source.each do |source_item|
|
42
|
-
eq_wo_order_base = eq_wo_order_base search_item, source_item
|
43
|
-
found = true if eq_wo_order_base
|
47
|
+
search.map do |search_item|
|
48
|
+
source.any? do |source_item|
|
49
|
+
eq_wo_order_base search_item, source_item
|
44
50
|
end
|
45
|
-
|
46
|
-
found
|
47
|
-
}.all?
|
51
|
+
end.all?
|
48
52
|
end
|
49
53
|
|
50
54
|
def sort_as_s(arr)
|
51
|
-
arr.
|
55
|
+
arr.sort_by(&:to_s)
|
52
56
|
end
|
53
57
|
end
|