hash_dealer 1.5.1 → 1.5.2
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION +1 -1
- data/coverage/index.html +726 -528
- data/hash_dealer.gemspec +1 -1
- data/lib/comparator.rb +19 -6
- data/lib/hash.rb +24 -3
- data/lib/matcher.rb +0 -1
- data/spec/lib/comparator_spec.rb +26 -0
- data/spec/lib/matcher_spec.rb +22 -0
- metadata +3 -1
data/hash_dealer.gemspec
CHANGED
data/lib/comparator.rb
CHANGED
@@ -18,12 +18,18 @@ class Comparator
|
|
18
18
|
return val if val.is_a?(TimeDateMatcher)
|
19
19
|
val = ActiveSupport::JSON.decode(val) if val.is_a?(String)
|
20
20
|
val = self.stringify_keys(val) if val.is_a?(Hash)
|
21
|
-
|
21
|
+
|
22
|
+
if val.is_a?(Array)
|
23
|
+
val = val.collect{|v|
|
24
|
+
v.is_a?(Hash) || v.is_a?(Array) ? self.normalize_value(v) : v
|
25
|
+
}
|
26
|
+
end
|
27
|
+
|
22
28
|
val
|
23
29
|
end
|
24
30
|
|
25
31
|
def self.diff(obj1, obj2)
|
26
|
-
return {} if obj1
|
32
|
+
return {} if obj1.eql?(obj2)
|
27
33
|
if obj1.is_a?(Array) && obj2.is_a?(Array)
|
28
34
|
return self.array_diff(obj1, obj2)
|
29
35
|
elsif obj1.is_a?(Hash) && obj2.is_a?(Hash)
|
@@ -64,11 +70,18 @@ class Comparator
|
|
64
70
|
end
|
65
71
|
|
66
72
|
def self.stringify_keys(hash_or_array)
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
73
|
+
if hash_or_array.is_a?(Array)
|
74
|
+
return hash_or_array.collect{|v|
|
75
|
+
v.is_a?(Hash) || v.is_a?(Array) ? self.stringify_keys(v) : v
|
76
|
+
}
|
77
|
+
end
|
78
|
+
|
79
|
+
ret = hash_or_array.stringify_keys
|
80
|
+
ret.each_pair do |k,v|
|
81
|
+
if v.is_a?(Hash) || v.is_a?(Array)
|
82
|
+
ret[k] = self.stringify_keys(v)
|
71
83
|
end
|
72
84
|
end
|
85
|
+
ret
|
73
86
|
end
|
74
87
|
end
|
data/lib/hash.rb
CHANGED
@@ -1,15 +1,32 @@
|
|
1
1
|
class HashDealer
|
2
2
|
class Hash < ::Hash
|
3
3
|
|
4
|
+
attr_accessor :optional_attributes
|
5
|
+
|
4
6
|
def initialize(optional_attributes = [])
|
5
7
|
@optional_attributes = optional_attributes
|
6
8
|
end
|
7
9
|
|
8
10
|
def eql?(other)
|
9
|
-
|
10
|
-
|
11
|
+
self_copy, other_copy = self.get_copies_for_comparison(other)
|
12
|
+
self_copy.eql?(other_copy)
|
13
|
+
end
|
14
|
+
|
15
|
+
def matcher(opts = {})
|
16
|
+
ret = super
|
17
|
+
ret.optional_attributes = self.optional_attributes
|
18
|
+
ret
|
19
|
+
end
|
11
20
|
|
12
|
-
|
21
|
+
def ==(other)
|
22
|
+
self_copy, other_copy = self.get_copies_for_comparison(other)
|
23
|
+
self_copy == other_copy
|
24
|
+
end
|
25
|
+
|
26
|
+
def stringify_keys
|
27
|
+
ret = super
|
28
|
+
ret.optional_attributes = self.optional_attributes.collect(&:to_s)
|
29
|
+
ret
|
13
30
|
end
|
14
31
|
|
15
32
|
def to_hash
|
@@ -22,6 +39,10 @@ class HashDealer
|
|
22
39
|
|
23
40
|
protected
|
24
41
|
|
42
|
+
def get_copies_for_comparison(other)
|
43
|
+
[self.remove_optional_keys(self), self.remove_optional_keys(other)]
|
44
|
+
end
|
45
|
+
|
25
46
|
def remove_optional_keys(hash)
|
26
47
|
hash = hash.clone.to_hash
|
27
48
|
@optional_attributes.each do |optional_attribute|
|
data/lib/matcher.rb
CHANGED
@@ -0,0 +1,26 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Comparator do
|
4
|
+
|
5
|
+
context ".diff" do
|
6
|
+
|
7
|
+
it "should respect optional arguments when doing a diff on matchers" do
|
8
|
+
|
9
|
+
HashDealer.define(:comparator_with_options) do
|
10
|
+
required_val("val")
|
11
|
+
optional_val("opt", optional: true)
|
12
|
+
end
|
13
|
+
|
14
|
+
matcher = HashDealer.roll(:comparator_with_options).matcher
|
15
|
+
|
16
|
+
Comparator.diff(matcher, {:required_val => "x", :optional_val => "y"}).should eql({})
|
17
|
+
|
18
|
+
Comparator.diff(matcher, {:required_val => "x"}).should eql({})
|
19
|
+
Comparator.diff(matcher, {:optional_val => "x"}).should_not eql({})
|
20
|
+
|
21
|
+
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
25
|
+
|
26
|
+
end
|
data/spec/lib/matcher_spec.rb
CHANGED
@@ -29,6 +29,28 @@ describe "match_response Matcher" do
|
|
29
29
|
{:a => "a"}.matcher.should match_response({"a" => "abcde"})
|
30
30
|
{:a => {:b => "c"}}.should match_response({"a" => {"b" => "c"}})
|
31
31
|
end
|
32
|
+
|
33
|
+
it "should handle optional parameters" do
|
34
|
+
|
35
|
+
HashDealer.define(:matcher_optional_values) do
|
36
|
+
required_val("123")
|
37
|
+
optional_val("456", optional: true)
|
38
|
+
end
|
39
|
+
hash_dealer = HashDealer.roll(:matcher_optional_values)
|
40
|
+
|
41
|
+
|
42
|
+
{"required_val" => "123"}.should match_response(
|
43
|
+
hash_dealer
|
44
|
+
)
|
45
|
+
|
46
|
+
{"required_val" => "123", "optional_val" => "456"}.should match_response(
|
47
|
+
hash_dealer
|
48
|
+
)
|
49
|
+
{"optional_val" => "456"}.should_not match_response(
|
50
|
+
hash_dealer
|
51
|
+
)
|
52
|
+
|
53
|
+
end
|
32
54
|
|
33
55
|
context ".diff" do
|
34
56
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: hash_dealer
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.5.
|
4
|
+
version: 1.5.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -232,6 +232,7 @@ files:
|
|
232
232
|
- Gemfile
|
233
233
|
- Gemfile.lock
|
234
234
|
- Guardfile
|
235
|
+
- hash_dealer-1.5.2.gem
|
235
236
|
- hash_dealer.gemspec
|
236
237
|
- lib/comparator.rb
|
237
238
|
- lib/core_extensions.rb
|
@@ -244,6 +245,7 @@ files:
|
|
244
245
|
- pkg/hash_dealer-1.4.10.gem
|
245
246
|
- Rakefile
|
246
247
|
- README.rdoc
|
248
|
+
- spec/lib/comparator_spec.rb
|
247
249
|
- spec/lib/hash_dealer_spec.rb
|
248
250
|
- spec/lib/matcher_spec.rb
|
249
251
|
- spec/lib/path_string_spec.rb
|