eq_wo_order 0.1.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.gitignore +1 -0
- data/.travis.yml +3 -0
- data/CHANGELOG +3 -0
- data/Gemfile +0 -1
- data/README.md +16 -5
- data/Rakefile +4 -0
- data/eq_wo_order.gemspec +1 -1
- data/lib/eq_wo_order.rb +36 -28
- data/spec/features/eq_wo_order_spec.rb +80 -39
- metadata +2 -2
- data/eq_wo_order-0.0.1.gem +0 -0
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0a13884e3cd9352d899c4aeedae8c519b38eaa52
|
4
|
+
data.tar.gz: 8226fa5acae1da4c68b7c2badc1e38226b85740b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a40168c509368079b33bb9e7712a89137f390444d7fbd8789ca86fdbde0b8baa9d94b366a978079d42b7bf09467d8f1b9a939ee1bed0c689bd45d3bc6b06e736
|
7
|
+
data.tar.gz: 9ebeac453bc021f895d365e76b67fa8236a53521b7a1e901116d67d68d65525e8ec324f7c4d75a54e43b4966f6577293e59da1e1aeed2ef0c2bffbb8ef0cdbbc
|
data/.gitignore
CHANGED
data/.travis.yml
ADDED
data/CHANGELOG
CHANGED
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -1,6 +1,8 @@
|
|
1
1
|
# EqWoOrder
|
2
2
|
|
3
|
-
|
3
|
+
[](https://travis-ci.org/jadekler/eq_wo_order)
|
4
|
+
|
5
|
+
RSpec equality matcher that recursively sorts lists, hashes, and lists of hashes before comparing
|
4
6
|
|
5
7
|
## Installation
|
6
8
|
|
@@ -11,16 +13,25 @@ gem 'eq_wo_order'
|
|
11
13
|
```
|
12
14
|
|
13
15
|
And then execute:
|
14
|
-
|
15
|
-
|
16
|
+
|
17
|
+
```
|
18
|
+
$ bundle
|
19
|
+
```
|
16
20
|
|
17
21
|
Or install it yourself as:
|
18
22
|
|
19
|
-
|
23
|
+
```
|
24
|
+
$ gem install eq_wo_order
|
25
|
+
```
|
20
26
|
|
21
27
|
## Usage
|
22
28
|
|
23
|
-
|
29
|
+
```
|
30
|
+
first = [[1, 2, 3]]
|
31
|
+
second = [[3, 1, 2]]
|
32
|
+
|
33
|
+
expect(first).to eq_wo_order second
|
34
|
+
```
|
24
35
|
|
25
36
|
## Contributing
|
26
37
|
|
data/Rakefile
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.
|
3
|
+
s.version = '0.2.0'
|
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 recursively sorts lists, hashes, and lists of hashes before comparing'
|
data/lib/eq_wo_order.rb
CHANGED
@@ -5,41 +5,49 @@ RSpec::Matchers.define :eq_wo_order do |expected|
|
|
5
5
|
|
6
6
|
def eq_wo_order_base(actual, expected)
|
7
7
|
if actual.class == Array
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
8
|
+
# arrays
|
9
|
+
actual_array_items = actual.find_all { |x| x.class == Array }
|
10
|
+
expected_array_items = expected.find_all { |x| x.class == Array }
|
11
|
+
array_items_match = all_items_in_source?(actual_array_items, expected_array_items) &&
|
12
|
+
all_items_in_source?(expected_array_items, actual_array_items)
|
13
|
+
|
14
|
+
# hashes
|
15
|
+
actual_hash_items = actual.find_all { |x| x.class == Hash }
|
16
|
+
expected_hash_items = expected.find_all { |x| x.class == Hash }
|
17
|
+
hash_items_match = all_items_in_source?(actual_hash_items, expected_hash_items) &&
|
18
|
+
all_items_in_source?(expected_hash_items, actual_hash_items)
|
19
|
+
|
20
|
+
# primitives
|
21
|
+
actual_primitive_items = actual.find_all { |x| x.class != Hash && x.class != Array }
|
22
|
+
expected_primitive_items = expected.find_all { |x| x.class != Hash && x.class != Array }
|
23
|
+
primitive_items_match = sort_as_s(actual_primitive_items) == sort_as_s(expected_primitive_items)
|
24
|
+
|
25
|
+
return primitive_items_match && array_items_match && hash_items_match
|
26
|
+
elsif actual.class == Hash
|
27
|
+
return all_items_in_source?(actual, expected) &&
|
28
|
+
all_items_in_source?(expected, actual)
|
12
29
|
else
|
13
|
-
actual == expected
|
30
|
+
return actual == expected
|
14
31
|
end
|
15
32
|
end
|
16
33
|
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
# primitives
|
25
|
-
sorted_arr.push primitive_items.sort.flatten
|
34
|
+
# given one array of arrays/hashes
|
35
|
+
# and another array of arrays/hashes
|
36
|
+
# are all the items of the first found in the second?
|
37
|
+
def all_items_in_source?(search, source)
|
38
|
+
search.map { |search_item|
|
39
|
+
found = false
|
26
40
|
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
# hashes
|
32
|
-
intersect_keys(hash_items).each do |key|
|
33
|
-
hash_items = hash_items.sort_by { |item| item[key] }
|
34
|
-
end
|
35
|
-
sorted_arr.push hash_items.flatten
|
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
|
44
|
+
end
|
36
45
|
|
37
|
-
|
46
|
+
found
|
47
|
+
}.all?
|
38
48
|
end
|
39
49
|
|
40
|
-
def
|
41
|
-
|
42
|
-
intersected_keys = keys.reduce(keys.first, :&)
|
43
|
-
intersected_keys == nil ? [] : intersected_keys
|
50
|
+
def sort_as_s(arr)
|
51
|
+
arr.sort { |x, y| x.to_s <=> y.to_s }
|
44
52
|
end
|
45
53
|
end
|
@@ -29,8 +29,67 @@ describe '#eq_wo_order' do
|
|
29
29
|
it { is_expected.to eq_wo_order([2, 1]) }
|
30
30
|
end
|
31
31
|
|
32
|
-
describe [
|
33
|
-
it { is_expected.to eq_wo_order([
|
32
|
+
describe [] do
|
33
|
+
it { is_expected.to eq_wo_order([]) }
|
34
|
+
end
|
35
|
+
|
36
|
+
describe 'of disparate types' do
|
37
|
+
describe ['a', {b: 'c', d: 'e'}] do
|
38
|
+
it { is_expected.to eq_wo_order([{d: 'e', b: 'c'}, 'a']) }
|
39
|
+
it { is_expected.not_to eq_wo_order([5, {d: 'e', b: 'c'}, 'a']) }
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
describe 'of hashes' do
|
44
|
+
describe [{a: 1}, {a: 2}] do
|
45
|
+
it { is_expected.to eq_wo_order([{a: 2}, {a: 1}]) }
|
46
|
+
it { is_expected.not_to eq_wo_order([{a: 1}]) }
|
47
|
+
it { is_expected.not_to eq_wo_order([{a: 1}, {a: 2}, {a: 3}]) }
|
48
|
+
end
|
49
|
+
|
50
|
+
describe [{a: 1, b: 1}, {a: 1, b: 2}] do
|
51
|
+
it { is_expected.to eq_wo_order([{a: 1, b: 2}, {a: 1, b: 1}]) }
|
52
|
+
it { is_expected.not_to eq_wo_order([{a: 1, b: 1}, {a: 1, b: 2, c: 3}]) }
|
53
|
+
end
|
54
|
+
|
55
|
+
describe [{a: 1, b: 1}, {a: 1, b: 2, c: 3}] do
|
56
|
+
it { is_expected.not_to eq_wo_order([{a: 1, b: 2}, {a: 1, b: 1}]) }
|
57
|
+
end
|
58
|
+
|
59
|
+
describe 'of arrays' do
|
60
|
+
describe [{a: [1, 2]}, {a: [3]}] do
|
61
|
+
it { is_expected.to eq_wo_order([{a: [2, 1]}, {a: [3]}]) }
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
describe 'of arrays' do
|
67
|
+
describe [[1, 2]] do
|
68
|
+
it { is_expected.to eq_wo_order([[2, 1]]) }
|
69
|
+
it { is_expected.not_to eq_wo_order([[2, 1, 3]]) }
|
70
|
+
end
|
71
|
+
|
72
|
+
describe [[1, 2], [3, 4]] do
|
73
|
+
it { is_expected.to eq_wo_order([[1, 2], [3, 4]]) }
|
74
|
+
it { is_expected.to eq_wo_order([[1, 2], [4, 3]]) }
|
75
|
+
it { is_expected.to eq_wo_order([[2, 1], [4, 3]]) }
|
76
|
+
it { is_expected.to eq_wo_order([[2, 1], [3, 4]]) }
|
77
|
+
it { is_expected.to eq_wo_order([[3, 4], [1, 2]]) }
|
78
|
+
it { is_expected.to eq_wo_order([[3, 4], [2, 1]]) }
|
79
|
+
it { is_expected.to eq_wo_order([[4, 3], [2, 1]]) }
|
80
|
+
it { is_expected.to eq_wo_order([[4, 3], [1, 2]]) }
|
81
|
+
it { is_expected.not_to eq_wo_order([[1, 2]]) }
|
82
|
+
it { is_expected.not_to eq_wo_order([[1, 2], [3, 4], [5, 6]]) }
|
83
|
+
end
|
84
|
+
|
85
|
+
describe [[1, 2, 3]] do
|
86
|
+
it { is_expected.to eq_wo_order([[2, 1, 3]]) }
|
87
|
+
it { is_expected.not_to eq_wo_order([[2, 1]]) }
|
88
|
+
end
|
89
|
+
|
90
|
+
describe [[[1, 2]]] do
|
91
|
+
it { is_expected.to eq_wo_order([[[2, 1]]]) }
|
92
|
+
end
|
34
93
|
end
|
35
94
|
end
|
36
95
|
|
@@ -38,54 +97,36 @@ describe '#eq_wo_order' do
|
|
38
97
|
describe({a: 'b', c: 'd'}) do
|
39
98
|
it { is_expected.to eq_wo_order({a: 'b', c: 'd'}) }
|
40
99
|
it { is_expected.to eq_wo_order({c: 'd', a: 'b'}) }
|
100
|
+
it { is_expected.not_to eq_wo_order({a: 'b', c: 'd', e: 'f'}) }
|
41
101
|
end
|
42
102
|
|
43
103
|
describe({a: 1, b: 2}) do
|
44
104
|
it { is_expected.to eq_wo_order({a: 1, b: 2}) }
|
45
105
|
it { is_expected.to eq_wo_order({b: 2, a: 1}) }
|
46
106
|
end
|
47
|
-
end
|
48
|
-
|
49
|
-
describe 'arrays of hashes' do
|
50
|
-
describe [{a: 1}, {a: 2}] do
|
51
|
-
it { is_expected.to eq_wo_order([{a: 2}, {a: 1}]) }
|
52
|
-
end
|
53
|
-
|
54
|
-
describe [{a: 1, b: 1}, {a: 1, b: 2}] do
|
55
|
-
it { is_expected.to eq_wo_order([{a: 1, b: 2}, {a: 1, b: 1}]) }
|
56
|
-
it { is_expected.not_to eq_wo_order([{a: 1, b: 1}, {a: 1, b: 2, c: 3}]) }
|
57
|
-
end
|
58
|
-
|
59
|
-
describe [{a: 1, b: 1}, {a: 1, b: 2, c: 3}] do
|
60
|
-
it { is_expected.not_to eq_wo_order([{a: 1, b: 2}, {a: 1, b: 1}]) }
|
61
|
-
end
|
62
|
-
end
|
63
107
|
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
end
|
108
|
+
describe 'of arrays' do
|
109
|
+
describe({a: [1, 2]}) do
|
110
|
+
it { is_expected.to eq_wo_order({a: [2, 1]}) }
|
111
|
+
end
|
69
112
|
|
70
|
-
|
71
|
-
|
72
|
-
|
113
|
+
describe 'of hashes' do
|
114
|
+
describe({a: [{a: 5}, {b: 7}]}) do
|
115
|
+
it { is_expected.to eq_wo_order({a: [{b: 7}, {a: 5}]}) }
|
116
|
+
end
|
117
|
+
end
|
73
118
|
end
|
74
119
|
|
75
|
-
describe
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
describe 'hashes of hashes' do
|
81
|
-
describe({a: {b: {c: 'd', e: 'f'}}}) do
|
82
|
-
it { is_expected.to eq_wo_order({a: {b: {e: 'f', c: 'd'}}}) }
|
83
|
-
end
|
84
|
-
end
|
120
|
+
describe 'of hashes' do
|
121
|
+
describe({a: {b: {c: 'd', e: 'f'}}}) do
|
122
|
+
it { is_expected.to eq_wo_order({a: {b: {e: 'f', c: 'd'}}}) }
|
123
|
+
end
|
85
124
|
|
86
|
-
|
87
|
-
|
88
|
-
|
125
|
+
describe 'of arrays' do
|
126
|
+
describe({a: {b: [1, 2]}}) do
|
127
|
+
it { is_expected.to eq_wo_order({a: {b: [2, 1]}}) }
|
128
|
+
end
|
129
|
+
end
|
89
130
|
end
|
90
131
|
end
|
91
132
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: eq_wo_order
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jean de Klerk
|
@@ -61,12 +61,12 @@ extra_rdoc_files: []
|
|
61
61
|
files:
|
62
62
|
- .gitignore
|
63
63
|
- .rspec
|
64
|
+
- .travis.yml
|
64
65
|
- CHANGELOG
|
65
66
|
- Gemfile
|
66
67
|
- LICENSE.txt
|
67
68
|
- README.md
|
68
69
|
- Rakefile
|
69
|
-
- eq_wo_order-0.0.1.gem
|
70
70
|
- eq_wo_order.gemspec
|
71
71
|
- lib/eq_wo_order.rb
|
72
72
|
- lib/version/version.rb
|
data/eq_wo_order-0.0.1.gem
DELETED
Binary file
|