check_please 0.5.2 → 0.5.3

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: c39ef90c83a488ddfa309002bfad243cbfe2415481aa0d82e3efd7119be7e420
4
- data.tar.gz: 54deb528cb61709484f86b11b4601e54770a6ce9a2a4694905eb7dcd3cceebd7
3
+ metadata.gz: d8c12644a3f83d1d263374a88b825a9f705f55d68582e90a50a3065ec2b0105e
4
+ data.tar.gz: 3f5ddc8b0d9e5c02bc4845e5a4ac21d07b358f75639146f32c71143bc1e6d605
5
5
  SHA512:
6
- metadata.gz: 2ab7404049194c7b70bf86b2ac4a3edc765e50b1ae63a2725e8877e67b1e49735a607f29f486414892a77d4fe604d8361db0b3bd6669b6c067e7ba90838c00bc
7
- data.tar.gz: 97fd7e608cac462e278601372d79a3e9fdee8013f3a13912078aad3d86cf875ce4724f68902282433df9a8fcee314c206414e473b9026c30b06e77c271b34393
6
+ metadata.gz: c95071934105bc8c8876287bf460a0466ecc996eab6356c64a994566824c95d2f24d6d446bc8bcc6d4d66b0a224023a4347839e75f5d8204860e9e2210e7e07d
7
+ data.tar.gz: ebdbe6bdeafd3f71bf9461f5eccd73f20772043d9e84ab01fa91c0a1dcff054a21c74d718aa40cf0c89bed3219d36d1f46e89ce37ea6d27483bb268518bb7d59
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- check_please (0.5.2)
4
+ check_please (0.5.3)
5
5
  table_print
6
6
 
7
7
  GEM
data/lib/check_please.rb CHANGED
@@ -143,6 +143,20 @@ module CheckPlease
143
143
  EOF
144
144
  end
145
145
 
146
+ Flags.define :match_by_value do |flag|
147
+ flag.repeatable
148
+ flag.coerce { |value| CheckPlease::Path.reify(value) }
149
+
150
+ flag.cli_long = "--match-by-value FOO"
151
+ flag.description = <<~EOF
152
+ When comparing two arrays that match a specified path, the candidate
153
+ array will be scanned for each element in the reference array.
154
+ May be repeated; values will be treated as an 'OR' list.
155
+ NOTE: explodes if either array at a given path contains other collections.
156
+ NOTE: paths of 'extra' diffs use the index in the candidate array.
157
+ EOF
158
+ end
159
+
146
160
  Flags.define :indifferent_keys do |flag|
147
161
  flag.default = false
148
162
  flag.coerce { |value| !!value }
@@ -42,8 +42,11 @@ module CheckPlease
42
42
  end
43
43
 
44
44
  def compare_arrays(ref_array, can_array, path)
45
- if ( key = path.key_for_compare(flags) )
45
+ case
46
+ when ( key = path.key_to_match_by(flags) )
46
47
  compare_arrays_by_key ref_array, can_array, path, key
48
+ when path.match_by_value?(flags)
49
+ compare_arrays_by_value ref_array, can_array, path
47
50
  else
48
51
  compare_arrays_by_index ref_array, can_array, path
49
52
  end
@@ -107,6 +110,45 @@ module CheckPlease
107
110
  elements_by_key
108
111
  end
109
112
 
113
+ # FIXME: this can generate duplicate paths.
114
+ # Time to introduce lft_path, rgt_path ?
115
+ def compare_arrays_by_value(ref_array, can_array, path)
116
+ assert_can_match_by_value! ref_array
117
+ assert_can_match_by_value! can_array
118
+
119
+ matches = can_array.map { false }
120
+
121
+ # Look for missing values
122
+ ref_array.each.with_index do |ref, i|
123
+ new_path = path + (i+1) # count in human pls
124
+
125
+ # Weird, but necessary to handle duplicates properly
126
+ j = can_array.index.with_index { |can, j|
127
+ matches[j] == false && can == ref
128
+ }
129
+
130
+ if j
131
+ matches[j] = true
132
+ else
133
+ record_diff ref, nil, new_path, :missing
134
+ end
135
+ end
136
+
137
+ # Look for extra values
138
+ can_array.zip(matches).each.with_index do |(can, match), i|
139
+ next if match
140
+ new_path = path + (i+1) # count in human pls
141
+ record_diff nil, can, new_path, :extra
142
+ end
143
+ end
144
+
145
+ def assert_can_match_by_value!(array)
146
+ if array.any? { |e| Array === e || Hash === e }
147
+ raise CheckPlease::BehaviorUndefined,
148
+ "match_by_value behavior is not defined for collections!"
149
+ end
150
+ end
151
+
110
152
  def compare_arrays_by_index(ref_array, can_array, path)
111
153
  max_len = [ ref_array, can_array ].map(&:length).max
112
154
  (0...max_len).each do |i|
@@ -6,6 +6,10 @@ module CheckPlease
6
6
  # instead....
7
7
  end
8
8
 
9
+ class BehaviorUndefined < ::StandardError
10
+ include CheckPlease::Error
11
+ end
12
+
9
13
  class DuplicateKeyError < ::IndexError
10
14
  include CheckPlease::Error
11
15
  end
@@ -81,13 +81,10 @@ module CheckPlease
81
81
  "<#{self.class.name} '#{to_s}'>"
82
82
  end
83
83
 
84
- # TODO: Naming Things
85
- def key_for_compare(flags)
86
- mbk_exprs = unpack_mbk_exprs(flags)
87
- matches = mbk_exprs.select { |mbk_expr|
88
- # NOTE: matching on parent because MBK '/foo/:id' should return 'id' for path '/foo'
89
- mbk_expr.parent.match?(self)
90
- }
84
+ def key_to_match_by(flags)
85
+ key_exprs = unpack_key_exprs(flags.match_by_key)
86
+ # NOTE: match on parent because if self.to_s == '/foo', MBK '/foo/:id' should return 'id'
87
+ matches = key_exprs.select { |e| e.parent.match?(self) }
91
88
 
92
89
  case matches.length
93
90
  when 0 ; nil
@@ -96,6 +93,10 @@ module CheckPlease
96
93
  end
97
94
  end
98
95
 
96
+ def match_by_value?(flags)
97
+ flags.match_by_value.any? { |e| e.match?(self) }
98
+ end
99
+
99
100
  def match?(path_or_string)
100
101
  # If the strings are literally equal, we're good..
101
102
  return true if self == path_or_string
@@ -160,8 +161,8 @@ module CheckPlease
160
161
  depth > flags.max_depth
161
162
  end
162
163
 
163
- def unpack_mbk_exprs(flags)
164
- flags.match_by_key
164
+ def unpack_key_exprs(path_list)
165
+ path_list
165
166
  .map { |path| path.send(:key_exprs) }
166
167
  .flatten
167
168
  .uniq { |e| e.to_s } # use the block form so we don't have to implement #hash and #eql? in horrible ways
@@ -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.2" # about to release? rerun `bundle check` to update Gemfile.lock also
4
+ VERSION = "0.5.3" # about to release? rerun `bundle lock` to update Gemfile.lock
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.2
4
+ version: 0.5.3
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-29 00:00:00.000000000 Z
11
+ date: 2021-02-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: table_print