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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 6250d92c058590c2cd7c686df5a699043f44f6c967e8d39ef52bdc11bbc7f90c
4
- data.tar.gz: 923b242752395042a5d6d6a1f58c042445f88a710d936b1931fa631420f34a52
3
+ metadata.gz: 4bae227cfdefaa6a79eb4f3000bd7288dbfe98d1959245811f5e5bdf831838e5
4
+ data.tar.gz: c4bb1ec682a23d791ee73caba8775242c3a6c90e64a10e77887556b244cc841b
5
5
  SHA512:
6
- metadata.gz: e8c43634f75ca2f13c4076089e159777c603d26befc90e40268087c14d54f6287185b35d5fda45dcc42fe9b1ba997e39c462f37b4713f5e5e357e01fad6fdeda
7
- data.tar.gz: 7b23c90a5d8a6cae7f6188ee6f6cac003f8a99007d90e46f1e9a831af98b3ab4d7fa8249626321d8ebc3a82af752d78abd75a897bf7e97f2765f7c061b8047da
6
+ metadata.gz: 98cd96ca937e649b76f97957c7291344e1b64a968548dba869641f6499bbfb207f3e7b8e21b4c0dc179808e6c9bfc78c39dff7711c6004af4de24edd4b3ff8d7
7
+ data.tar.gz: 78f0bb747f35df684811b0b2c94814e1f542af1c9e2c0ae12c8c780bb8afc8881b09a4aedac45b4c611c74a0107db7c994a637016af08ef0bd8b14912b7b0b9c
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- json_rspec_match_maker (0.1.0)
4
+ json_rspec_match_maker (0.1.2)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
@@ -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.each do |error_key, match_def|
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(error_key, match_def)
76
+ check_values(key, match_def, current_expected)
72
77
  else
73
- check_each(error_key, match_def)
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(expected)
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
- each_definition[:attributes].each do |attr_error_key, match_function|
91
- each_opts = { idx: idx, error_key: attr_error_key }
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, each_opts = nil)
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, each_opts, target)
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
- def initialize(key, each_opts, target)
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
- "#{key}[#{each_opts[:idx]}].#{each_opts[:error_key]}"
8
+ def initialize(key, target)
9
+ @error_key = key
10
+ @value = value_for_key(key, target)
15
11
  end
16
12
 
17
- def fetch_value(key, each_opts, target)
18
- return value_for_key(key, target) if each_opts.nil?
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) { |j, k| j.fetch(k, {}) }
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
@@ -1,3 +1,3 @@
1
1
  module JsonRspecMatchMaker
2
- VERSION = '0.1.1'.freeze
2
+ VERSION = '0.1.2'.freeze
3
3
  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.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-04-17 00:00:00.000000000 Z
11
+ date: 2018-05-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler