eq_wo_order 0.0.1 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rspec +2 -0
- data/eq_wo_order-0.0.1.gem +0 -0
- data/eq_wo_order.gemspec +1 -1
- data/lib/eq_wo_order.rb +41 -1
- data/spec/features/eq_wo_order_spec.rb +97 -0
- data/spec/spec_helper.rb +4 -0
- metadata +8 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: bc13907a659560faf9cb2c5b8f8272bd1ae9de28
|
4
|
+
data.tar.gz: 84b9da470b6badb48d437afbeb38eb6c3880cbc8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8407e36c00102f950c711a054c40d9c7448051b3867182b01c9aebfe4b68c94c23ea091114407891970b29629b3ab10ee6c82b53c1109a7904304ee867469b11
|
7
|
+
data.tar.gz: 7a8a05343889ac2fee08eb6cfd8cf19b7512ca3e9c2dea4dec93db69699b5171a93835e60ee96629c5c48bb02338514e77656ef7ee161ca658ae481b03664213
|
data/.rspec
ADDED
Binary file
|
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.0
|
3
|
+
s.version = '0.1.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
@@ -1,5 +1,45 @@
|
|
1
1
|
RSpec::Matchers.define :eq_wo_order do |expected|
|
2
2
|
match do |actual|
|
3
|
-
|
3
|
+
eq_wo_order_base(actual, expected)
|
4
|
+
end
|
5
|
+
|
6
|
+
def eq_wo_order_base(actual, expected)
|
7
|
+
if actual.class == Array
|
8
|
+
actual_sorted = sort_array_of_hashes_by_all_keys actual
|
9
|
+
expected_sorted = sort_array_of_hashes_by_all_keys expected
|
10
|
+
|
11
|
+
actual_sorted == expected_sorted
|
12
|
+
else
|
13
|
+
actual == expected
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def sort_array_of_hashes_by_all_keys(arr)
|
18
|
+
sorted_arr = []
|
19
|
+
|
20
|
+
hash_items = arr.collect { |x| x if x.class == Hash }.compact
|
21
|
+
array_items = arr.collect { |x| x if x.class == Array }.compact
|
22
|
+
primitive_items = arr.collect { |x| x unless x.class == Hash || x.class == Array }.compact
|
23
|
+
|
24
|
+
# primitives
|
25
|
+
sorted_arr.push primitive_items.sort.flatten
|
26
|
+
|
27
|
+
# arrays
|
28
|
+
sorted_array_items = array_items.collect { |item| sort_array_of_hashes_by_all_keys(item) }
|
29
|
+
sorted_arr.push sorted_array_items.flatten
|
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
|
36
|
+
|
37
|
+
sorted_arr
|
38
|
+
end
|
39
|
+
|
40
|
+
def intersect_keys(arr_of_hashes)
|
41
|
+
keys = arr_of_hashes.collect { |x| x.keys }
|
42
|
+
intersected_keys = keys.reduce(keys.first, :&)
|
43
|
+
intersected_keys == nil ? [] : intersected_keys
|
4
44
|
end
|
5
45
|
end
|
@@ -0,0 +1,97 @@
|
|
1
|
+
require_relative '../../lib/eq_wo_order'
|
2
|
+
|
3
|
+
describe '#eq_wo_order' do
|
4
|
+
describe 'basic types' do
|
5
|
+
describe 1 do
|
6
|
+
it { is_expected.to eq_wo_order(1) }
|
7
|
+
it { is_expected.not_to eq_wo_order(2) }
|
8
|
+
end
|
9
|
+
|
10
|
+
describe 'a' do
|
11
|
+
it { is_expected.to eq_wo_order('a') }
|
12
|
+
it { is_expected.not_to eq_wo_order('b') }
|
13
|
+
end
|
14
|
+
|
15
|
+
describe true do
|
16
|
+
it { is_expected.to eq_wo_order(true) }
|
17
|
+
it { is_expected.not_to eq_wo_order(false) }
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
describe 'arrays' do
|
22
|
+
describe ['a', 'b'] do
|
23
|
+
it { is_expected.to eq_wo_order(['a', 'b']) }
|
24
|
+
it { is_expected.to eq_wo_order(['b', 'a']) }
|
25
|
+
end
|
26
|
+
|
27
|
+
describe [1, 2] do
|
28
|
+
it { is_expected.to eq_wo_order([1, 2]) }
|
29
|
+
it { is_expected.to eq_wo_order([2, 1]) }
|
30
|
+
end
|
31
|
+
|
32
|
+
describe ['a', {b: 'c', d: 'e'}] do
|
33
|
+
it { is_expected.to eq_wo_order([{d: 'e', b: 'c'}, 'a']) }
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
describe 'hashes' do
|
38
|
+
describe({a: 'b', c: 'd'}) do
|
39
|
+
it { is_expected.to eq_wo_order({a: 'b', c: 'd'}) }
|
40
|
+
it { is_expected.to eq_wo_order({c: 'd', a: 'b'}) }
|
41
|
+
end
|
42
|
+
|
43
|
+
describe({a: 1, b: 2}) do
|
44
|
+
it { is_expected.to eq_wo_order({a: 1, b: 2}) }
|
45
|
+
it { is_expected.to eq_wo_order({b: 2, a: 1}) }
|
46
|
+
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
|
+
|
64
|
+
describe 'arrays of arrays' do
|
65
|
+
describe [[1, 2]] do
|
66
|
+
it { is_expected.to eq_wo_order([[2, 1]]) }
|
67
|
+
it { is_expected.not_to eq_wo_order([[2, 1, 3]]) }
|
68
|
+
end
|
69
|
+
|
70
|
+
describe [[1, 2, 3]] do
|
71
|
+
it { is_expected.to eq_wo_order([[2, 1, 3]]) }
|
72
|
+
it { is_expected.not_to eq_wo_order([[2, 1]]) }
|
73
|
+
end
|
74
|
+
|
75
|
+
describe [[[1, 2]]] do
|
76
|
+
it { is_expected.to eq_wo_order([[[2, 1]]]) }
|
77
|
+
end
|
78
|
+
end
|
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
|
85
|
+
|
86
|
+
describe 'hashes of arrays' do
|
87
|
+
describe({a: [1, 2]}) do
|
88
|
+
it { is_expected.to eq_wo_order({a: [2, 1]}) }
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
describe 'deeply nested hashes and arrays' do
|
93
|
+
describe [{a: [{b: 'c', d: 'e'}]}, {f: 5}, {g: 'h'}] do
|
94
|
+
it { is_expected.to eq_wo_order([{f: 5}, {a: [{d: 'e', b: 'c'}]}, {g: 'h'}]) }
|
95
|
+
end
|
96
|
+
end
|
97
|
+
end
|
data/spec/spec_helper.rb
ADDED
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.0
|
4
|
+
version: 0.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jean de Klerk
|
@@ -60,14 +60,18 @@ extensions: []
|
|
60
60
|
extra_rdoc_files: []
|
61
61
|
files:
|
62
62
|
- .gitignore
|
63
|
+
- .rspec
|
63
64
|
- CHANGELOG
|
64
65
|
- Gemfile
|
65
66
|
- LICENSE.txt
|
66
67
|
- README.md
|
67
68
|
- Rakefile
|
69
|
+
- eq_wo_order-0.0.1.gem
|
68
70
|
- eq_wo_order.gemspec
|
69
71
|
- lib/eq_wo_order.rb
|
70
72
|
- lib/version/version.rb
|
73
|
+
- spec/features/eq_wo_order_spec.rb
|
74
|
+
- spec/spec_helper.rb
|
71
75
|
homepage: http://rubygems.org/gems/eq_wo_order
|
72
76
|
licenses:
|
73
77
|
- MIT
|
@@ -92,4 +96,6 @@ rubygems_version: 2.4.8
|
|
92
96
|
signing_key:
|
93
97
|
specification_version: 4
|
94
98
|
summary: RSpec equality matcher that ignores nested order
|
95
|
-
test_files:
|
99
|
+
test_files:
|
100
|
+
- spec/features/eq_wo_order_spec.rb
|
101
|
+
- spec/spec_helper.rb
|