eq_wo_order 0.2.3 → 0.2.4

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: eaee03f0ce69e34896909a941a98035527836c5e
4
- data.tar.gz: f85e7071fcdb43c02c4093c9afc18eea722e07c4
3
+ metadata.gz: 8c745e416ae194cf358f919b8d45a49cc4195fc2
4
+ data.tar.gz: 9fbb9d7568a585c7cd49db4707779a44e3b6a699
5
5
  SHA512:
6
- metadata.gz: f18f4fc97534351dabfba68a6e95dc0b7c778ab1b77d2a4be082dbc9fda66714f586241270e4274f62394b1b12736f49f62bc4812baed310dae0813ac0430b8c
7
- data.tar.gz: 65fb8e9a014a587f7bfa971fb35cbee18cf0acf5e90ce60f8c92930f66f3fc5d05e1906641e28ddbd49ff07bac0cb708ee6c7e150c9567b8dc67c17e26e42feb
6
+ metadata.gz: 837cc0dcfe1dc23bd417f38d891f7b9c7459cd09e56fc070ada8087b6683759fa72311938bc755079a7c1649fd84743f38b898b07ccc4eae99b7097ac4dc4bdb
7
+ data.tar.gz: 83ab48db977a78032493bf706b2f33b114cc1d522aab45b837396a84906bc5430cc5007414b2273109b54f89e4b3943bc5952fd5b9de69b2d9fd07873526507b
data/CHANGELOG CHANGED
@@ -4,3 +4,4 @@ Version 0.2.0 Support for arrays of hashes of arrays etc - more complicated nest
4
4
  Version 0.2.1 Small description update
5
5
  Version 0.2.2 Include CHANGELOG
6
6
  Version 0.2.3 Update description with github link
7
+ Version 0.2.4 Ruby-ism refactors for terseness
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = 'eq_wo_order'
3
- s.version = '0.2.3'
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'
@@ -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
- if actual.class == Array
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)
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
- # 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
+ 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
- # 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)
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
- 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)
29
- else
30
- return actual == expected
31
- end
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 { |search_item|
39
- found = false
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.sort { |x, y| x.to_s <=> y.to_s }
55
+ arr.sort_by(&:to_s)
52
56
  end
53
57
  end
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.2.3
4
+ version: 0.2.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jean de Klerk