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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: bc13907a659560faf9cb2c5b8f8272bd1ae9de28
4
- data.tar.gz: 84b9da470b6badb48d437afbeb38eb6c3880cbc8
3
+ metadata.gz: 0a13884e3cd9352d899c4aeedae8c519b38eaa52
4
+ data.tar.gz: 8226fa5acae1da4c68b7c2badc1e38226b85740b
5
5
  SHA512:
6
- metadata.gz: 8407e36c00102f950c711a054c40d9c7448051b3867182b01c9aebfe4b68c94c23ea091114407891970b29629b3ab10ee6c82b53c1109a7904304ee867469b11
7
- data.tar.gz: 7a8a05343889ac2fee08eb6cfd8cf19b7512ca3e9c2dea4dec93db69699b5171a93835e60ee96629c5c48bb02338514e77656ef7ee161ca658ae481b03664213
6
+ metadata.gz: a40168c509368079b33bb9e7712a89137f390444d7fbd8789ca86fdbde0b8baa9d94b366a978079d42b7bf09467d8f1b9a939ee1bed0c689bd45d3bc6b06e736
7
+ data.tar.gz: 9ebeac453bc021f895d365e76b67fa8236a53521b7a1e901116d67d68d65525e8ec324f7c4d75a54e43b4966f6577293e59da1e1aeed2ef0c2bffbb8ef0cdbbc
data/.gitignore CHANGED
@@ -14,3 +14,4 @@
14
14
  mkmf.log
15
15
  .idea
16
16
  *.iml
17
+ *.gem
data/.travis.yml ADDED
@@ -0,0 +1,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.4
data/CHANGELOG CHANGED
@@ -0,0 +1,3 @@
1
+ Version 0.0.1 eq_wo_order is released
2
+ Version 0.1.0 Support for arrays of arrays and more complicated structures.
3
+ Version 0.2.0 Support for arrays of hashes of arrays etc - more complicated nesting. Initial support for arrays of disparate types.
data/Gemfile CHANGED
@@ -1,4 +1,3 @@
1
1
  source 'https://rubygems.org'
2
2
 
3
- # Specify your gem's dependencies in eq_wo_order.gemspec
4
3
  gemspec
data/README.md CHANGED
@@ -1,6 +1,8 @@
1
1
  # EqWoOrder
2
2
 
3
- TODO: Write a gem description
3
+ [![Build Status](https://travis-ci.org/jadekler/eq_wo_order.svg?branch=master)](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
- $ bundle
16
+
17
+ ```
18
+ $ bundle
19
+ ```
16
20
 
17
21
  Or install it yourself as:
18
22
 
19
- $ gem install eq_wo_order
23
+ ```
24
+ $ gem install eq_wo_order
25
+ ```
20
26
 
21
27
  ## Usage
22
28
 
23
- TODO: Write usage instructions here
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
@@ -1 +1,5 @@
1
1
  require 'bundler/gem_tasks'
2
+
3
+ task :default do
4
+ puts system('bundle exec rspec')
5
+ end
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.1.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
- 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
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
- 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
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
- # 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
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
- sorted_arr
46
+ found
47
+ }.all?
38
48
  end
39
49
 
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
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 ['a', {b: 'c', d: 'e'}] do
33
- it { is_expected.to eq_wo_order([{d: 'e', b: 'c'}, 'a']) }
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
- 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
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
- 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]]) }
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 [[[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
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
- describe 'hashes of arrays' do
87
- describe({a: [1, 2]}) do
88
- it { is_expected.to eq_wo_order({a: [2, 1]}) }
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.1.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
Binary file