check_please 0.5.1 → 0.5.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d25dd7c99923b45110f0bfb0903006414f50a60f89e0e3f4385487a3cf11d68b
4
- data.tar.gz: c1e4457594ba8f1234692a431acf859f9f708e26e14512c2c7a348359ef0ab04
3
+ metadata.gz: c39ef90c83a488ddfa309002bfad243cbfe2415481aa0d82e3efd7119be7e420
4
+ data.tar.gz: 54deb528cb61709484f86b11b4601e54770a6ce9a2a4694905eb7dcd3cceebd7
5
5
  SHA512:
6
- metadata.gz: 38b21a94a84943dabc03fda78f8054b36340363a932e53754a437f8a813cfa72fbd04d232d924a16e3897dab019951b35bc47e03f6f46aa0deaea050d64553b4
7
- data.tar.gz: 45bedfd9641c128f1e0f21c308f323fa497ef0bf0c91e255fe3e0afde528a90e2e84c15103de32ed1020fd0a71862b143d812bce498570394d9be37fcd9aea17
6
+ metadata.gz: 2ab7404049194c7b70bf86b2ac4a3edc765e50b1ae63a2725e8877e67b1e49735a607f29f486414892a77d4fe604d8361db0b3bd6669b6c067e7ba90838c00bc
7
+ data.tar.gz: 97fd7e608cac462e278601372d79a3e9fdee8013f3a13912078aad3d86cf875ce4724f68902282433df9a8fcee314c206414e473b9026c30b06e77c271b34393
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- check_please (0.5.1)
4
+ check_please (0.5.2)
5
5
  table_print
6
6
 
7
7
  GEM
@@ -143,4 +143,24 @@ module CheckPlease
143
143
  EOF
144
144
  end
145
145
 
146
+ Flags.define :indifferent_keys do |flag|
147
+ flag.default = false
148
+ flag.coerce { |value| !!value }
149
+
150
+ flag.cli_long = "--indifferent-keys"
151
+ flag.description = <<~EOF
152
+ When comparing hashes, convert symbol keys to strings
153
+ EOF
154
+ end
155
+
156
+ Flags.define :indifferent_values do |flag|
157
+ flag.default = false
158
+ flag.coerce { |value| !!value }
159
+
160
+ flag.cli_long = "--indifferent-values"
161
+ flag.description = <<~EOF
162
+ When comparing values (that aren't arrays or hashes), convert symbols to strings
163
+ EOF
164
+ end
165
+
146
166
  end
@@ -22,7 +22,7 @@ module CheckPlease
22
22
  def compare(ref, can, path)
23
23
  return if path.excluded?(flags)
24
24
 
25
- case types(ref, can)
25
+ case types_for_compare(ref, can)
26
26
  when [ :array, :array ] ; compare_arrays ref, can, path
27
27
  when [ :hash, :hash ] ; compare_hashes ref, can, path
28
28
  when [ :other, :other ] ; compare_others ref, can, path
@@ -31,7 +31,7 @@ module CheckPlease
31
31
  end
32
32
  end
33
33
 
34
- def types(*list)
34
+ def types_for_compare(*list)
35
35
  list.map { |e|
36
36
  case e
37
37
  when Array ; :array
@@ -52,6 +52,7 @@ module CheckPlease
52
52
  def compare_arrays_by_key(ref_array, can_array, path, key_name)
53
53
  refs_by_key = index_array!(ref_array, path, key_name, "reference")
54
54
  cans_by_key = index_array!(can_array, path, key_name, "candidate")
55
+
55
56
  key_values = (refs_by_key.keys | cans_by_key.keys)
56
57
 
57
58
  key_values.compact! # NOTE: will break if nil is ever used as a key (but WHO WOULD DO THAT?!)
@@ -79,6 +80,10 @@ module CheckPlease
79
80
  "The element at position #{i} in the #{ref_or_can} array is not a hash."
80
81
  end
81
82
 
83
+ if flags.indifferent_keys
84
+ h = stringify_symbol_keys(h)
85
+ end
86
+
82
87
  # try to get the value of the attribute identified by key_name
83
88
  key_value = h.fetch(key_name) {
84
89
  raise CheckPlease::NoSuchKeyError, \
@@ -121,11 +126,27 @@ module CheckPlease
121
126
  end
122
127
 
123
128
  def compare_hashes(ref_hash, can_hash, path)
129
+ if flags.indifferent_keys
130
+ ref_hash = stringify_symbol_keys(ref_hash)
131
+ can_hash = stringify_symbol_keys(can_hash)
132
+ end
124
133
  record_missing_keys ref_hash, can_hash, path
125
134
  compare_common_keys ref_hash, can_hash, path
126
135
  record_extra_keys ref_hash, can_hash, path
127
136
  end
128
137
 
138
+ def stringify_symbol_keys(h)
139
+ Hash[
140
+ h.map { |k,v|
141
+ [ stringify_symbol(k), v ]
142
+ }
143
+ ]
144
+ end
145
+
146
+ def stringify_symbol(x)
147
+ Symbol === x ? x.to_s : x
148
+ end
149
+
129
150
  def record_missing_keys(ref_hash, can_hash, path)
130
151
  keys = ref_hash.keys - can_hash.keys
131
152
  keys.each do |k|
@@ -148,6 +169,10 @@ module CheckPlease
148
169
  end
149
170
 
150
171
  def compare_others(ref, can, path)
172
+ if flags.indifferent_values
173
+ ref = stringify_symbol(ref)
174
+ can = stringify_symbol(can)
175
+ end
151
176
  return if ref == can
152
177
  record_diff ref, can, path, :mismatch
153
178
  end
@@ -1,5 +1,5 @@
1
1
  module CheckPlease
2
2
  # NOTE: 'check_please_rspec_matcher' depends on this,
3
3
  # so try to keep them roughly in sync
4
- VERSION = "0.5.1" # about to release? rerun `bundle check` to update Gemfile.lock also
4
+ VERSION = "0.5.2" # about to release? rerun `bundle check` to update Gemfile.lock also
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: check_please
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.1
4
+ version: 0.5.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sam Livingston-Gray
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-01-26 00:00:00.000000000 Z
11
+ date: 2021-01-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: table_print