json_rspec_match_maker 0.1.1 → 0.1.2
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/Gemfile.lock +1 -1
- data/lib/json_rspec_match_maker/base.rb +14 -11
- data/lib/json_rspec_match_maker/target_value.rb +12 -20
- data/lib/json_rspec_match_maker/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4bae227cfdefaa6a79eb4f3000bd7288dbfe98d1959245811f5e5bdf831838e5
|
4
|
+
data.tar.gz: c4bb1ec682a23d791ee73caba8775242c3a6c90e64a10e77887556b244cc841b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 98cd96ca937e649b76f97957c7291344e1b64a968548dba869641f6499bbfb207f3e7b8e21b4c0dc179808e6c9bfc78c39dff7711c6004af4de24edd4b3ff8d7
|
7
|
+
data.tar.gz: 78f0bb747f35df684811b0b2c94814e1f542af1c9e2c0ae12c8c780bb8afc8881b09a4aedac45b4c611c74a0107db7c994a637016af08ef0bd8b14912b7b0b9c
|
data/Gemfile.lock
CHANGED
@@ -66,11 +66,16 @@ module JsonRspecMatchMaker
|
|
66
66
|
# @return [nil] returns nothing, adds to error list as side effect
|
67
67
|
def check_target_against_expected
|
68
68
|
raise MatchDefinitionNotFound, self.class.name unless @match_definition
|
69
|
-
@match_definition
|
69
|
+
check_definition(@match_definition)
|
70
|
+
end
|
71
|
+
|
72
|
+
def check_definition(definition, current_expected = expected, current_key = nil)
|
73
|
+
definition.each do |error_key, match_def|
|
74
|
+
key = [current_key, error_key].compact.join('.')
|
70
75
|
if match_def.respond_to? :call
|
71
|
-
check_values(
|
76
|
+
check_values(key, match_def, current_expected)
|
72
77
|
else
|
73
|
-
check_each(
|
78
|
+
check_each(key, match_def, current_expected)
|
74
79
|
end
|
75
80
|
end
|
76
81
|
end
|
@@ -84,13 +89,11 @@ module JsonRspecMatchMaker
|
|
84
89
|
# :each is a function that returns the list of items
|
85
90
|
# :attributes is a hash with the same structure as the top-level match_def hash
|
86
91
|
# @return [nil] returns nothing, adds to error list as side effect
|
87
|
-
def check_each(error_key, each_definition)
|
88
|
-
enumerable = each_definition[:each].call(
|
92
|
+
def check_each(error_key, each_definition, current_expected)
|
93
|
+
enumerable = each_definition[:each].call(current_expected)
|
89
94
|
enumerable.each_with_index do |each_instance, idx|
|
90
|
-
|
91
|
-
|
92
|
-
check_values(error_key, match_function, each_instance, each_opts)
|
93
|
-
end
|
95
|
+
full_key = [error_key, idx].join('.')
|
96
|
+
check_definition(each_definition[:attributes], each_instance, full_key)
|
94
97
|
end
|
95
98
|
end
|
96
99
|
|
@@ -108,9 +111,9 @@ module JsonRspecMatchMaker
|
|
108
111
|
# :error_key the subfield reported in the error
|
109
112
|
# the index if iterating through a list, otherwise nil
|
110
113
|
# @return [nil] returns nothing, adds to error list as side effect
|
111
|
-
def check_values(error_key, match_function, expected_instance = expected
|
114
|
+
def check_values(error_key, match_function, expected_instance = expected)
|
112
115
|
expected_value = ExpectedValue.new(match_function, expected_instance)
|
113
|
-
target_value = TargetValue.new(error_key,
|
116
|
+
target_value = TargetValue.new(error_key, target)
|
114
117
|
add_error(expected_value, target_value) unless expected_value == target_value
|
115
118
|
end
|
116
119
|
|
@@ -3,33 +3,25 @@ module JsonRspecMatchMaker
|
|
3
3
|
class TargetValue
|
4
4
|
attr_reader :error_key, :value
|
5
5
|
|
6
|
-
|
7
|
-
@error_key = full_error_key(key, each_opts)
|
8
|
-
@value = fetch_value(key, each_opts, target)
|
9
|
-
end
|
10
|
-
|
11
|
-
def full_error_key(key, each_opts)
|
12
|
-
return key if each_opts.nil?
|
6
|
+
NUMBER = /^\d+$/
|
13
7
|
|
14
|
-
|
8
|
+
def initialize(key, target)
|
9
|
+
@error_key = key
|
10
|
+
@value = value_for_key(key, target)
|
15
11
|
end
|
16
12
|
|
17
|
-
def
|
18
|
-
|
19
|
-
|
20
|
-
targets = value_for_key(key, target)
|
21
|
-
specific_target = targets[each_opts[:idx]]
|
22
|
-
value_for_key(each_opts[:error_key], specific_target)
|
13
|
+
def ==(other)
|
14
|
+
raise ArgumentError unless other.is_a? ExpectedValue
|
15
|
+
other.value == value
|
23
16
|
end
|
24
17
|
|
18
|
+
private
|
19
|
+
|
25
20
|
def value_for_key(key, json)
|
26
|
-
value = key.split('.').reduce(json)
|
21
|
+
value = key.split('.').reduce(json) do |j, k|
|
22
|
+
k.match?(NUMBER) ? (j[k.to_i] || {}) : j.fetch(k, {})
|
23
|
+
end
|
27
24
|
value == {} ? nil : value
|
28
25
|
end
|
29
|
-
|
30
|
-
def ==(other)
|
31
|
-
raise ArgumentError unless other.is_a? ExpectedValue
|
32
|
-
other.value == value
|
33
|
-
end
|
34
26
|
end
|
35
27
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: json_rspec_match_maker
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Patrick McGee
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-
|
11
|
+
date: 2018-05-10 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|