varar-core 0.8.1 → 0.9.0

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: 76ca7dbd630ae0e9925114d31967993e9a0f366a83844da58778afffaa1bf555
4
- data.tar.gz: e37e4f44a2469a038ba6c733b6098249501c6c8bcdbc9e6db84a4d0219872bed
3
+ metadata.gz: 6b3afe2eac41dbbd5e500dd3e0e845c77a8bdf3c8d6fd80dd61b63c95abbbf03
4
+ data.tar.gz: 9e683dc8f095c659aeea657a9dc93b35b6ab561bf5b08feab08f242d41423ead
5
5
  SHA512:
6
- metadata.gz: 5e9e258f4c033d489f529ddccb3d460afbae2c064ac58de82bed5f25b166ecc295d3c3537ad257d1a88b06784f8f21e3df26d808c5d36f521c66c3a147af9881
7
- data.tar.gz: 9dc4d46d4052654752e5736b1703236e517e95412c193bd7851d1c37d6c2a88e4eea6f77c9aee7617272f769a796eb608d85111d243125b1209227ff95320454
6
+ metadata.gz: 1635a2d5f71a1714286f4bd167fbca6792a8097673e652d0979ae939fc44fe8a279ed0ef1cea3ed54f0cf7325fe8a367ecc5cc989e5b9fd15053d27c78d96ba3
7
+ data.tar.gz: 387588fbc0526a7c99fe8281f2993c1f6097e1edeca095aaf5816e36906a79722d8753d10b7cde65b4cba176f8a1067af2f3d0bd949d78c2d6e2c99de8aa0ede
@@ -55,6 +55,7 @@ module Varar
55
55
  def compare_row(returned, checks)
56
56
  return [] unless returned.is_a?(Hash)
57
57
 
58
+ check_record_columns(returned, checks.map(&:column), '')
58
59
  checks.filter_map do |check|
59
60
  next unless returned.key?(check.column)
60
61
 
@@ -64,9 +65,25 @@ module Varar
64
65
  end
65
66
  end
66
67
 
67
- # Compare a whole-table step's returned table against the input table.
68
- # +returned+: nil (no checks), Array of Arrays (positional), or Array of
69
- # Hashes (keyed by header). Cells compare as exact strings.
68
+ # A returned record checks only the columns it has (#114), so its keys must
69
+ # be columns of the table — a misspelt key would otherwise check nothing —
70
+ # and it must have at least one, or it checks nothing at all.
71
+ def check_record_columns(record, columns, where)
72
+ unknown = record.keys.find { |key| !columns.include?(key) }
73
+ raise ReturnShapeError, "#{where}unknown column \"#{unknown}\"" unless unknown.nil?
74
+ return if columns.any? { |column| record.key?(column) }
75
+
76
+ raise ReturnShapeError, "#{where}returned no column of the table"
77
+ end
78
+
79
+ # Compare a whole-table step's returned table against the input table (the
80
+ # header row is labels, never compared). +returned+: nil (no checks), Array
81
+ # of Arrays (positional), which reproduces every column of every row, or
82
+ # Array of Hashes (keyed by header), which checks only the columns each
83
+ # record has — so a table can carry columns for people alone, such as a
84
+ # "why" column, that no step echoes back (#114). Cells compare as exact
85
+ # strings. A record key that is no column, or a record with no column at
86
+ # all, is a ReturnShapeError.
70
87
  def compare_table(returned, input_table)
71
88
  return [] if returned.nil?
72
89
  raise ReturnShapeError, "expected a table (array of rows), got #{returned.class}" unless returned.is_a?(Array)
@@ -84,18 +101,15 @@ module Varar
84
101
  diffs = []
85
102
  data_rows.each_with_index do |row, i|
86
103
  ret = returned[i]
104
+ check_record_columns(ret, columns, "row #{i}: ") if all_records
87
105
  if all_arrays && ret.length != columns.length
88
106
  raise ReturnShapeError, "row #{i}: expected #{columns.length} column(s), got #{ret.length}"
89
107
  end
90
108
 
91
109
  columns.each_with_index do |column, j|
92
- if all_arrays
93
- actual_value = ret[j]
94
- else
95
- raise ReturnShapeError, "row #{i}: missing column \"#{column}\"" unless ret.key?(column)
110
+ next if all_records && !ret.key?(column)
96
111
 
97
- actual_value = ret[column]
98
- end
112
+ actual_value = all_arrays ? ret[j] : ret[column]
99
113
  expected = j < row.cells.length ? row.cells[j] : ''
100
114
  actual = render_cell_value(actual_value)
101
115
  span = j < row.cell_spans.length ? row.cell_spans[j] : row.span
@@ -151,6 +151,7 @@ module Varar
151
151
  def planned_example_hash(example, source)
152
152
  result = {
153
153
  'name' => example.name,
154
+ 'key' => example.key,
154
155
  'scopeStack' => example.scope_stack,
155
156
  'span' => span_hash(example.span),
156
157
  'expectedOutcome' => example.expected_outcome || 'pass'
@@ -1,27 +1,32 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'json'
4
- require 'varar/core/hash'
5
4
  require 'varar/core/diagnostics'
6
5
  require 'varar/core/plan'
7
- require 'varar/core/canonical_json'
8
6
 
9
7
  module Varar
10
8
  module Core
11
- # One example-producing paragraph, as recorded in the baseline.
12
- BaselineExample = Data.define(:name, :line)
13
- # The committed baseline for one oath file.
14
- OathBaseline = Data.define(:source_hash, :examples)
15
- # The whole varar.lock.json: every oath keyed by its POSIX path.
9
+ # The committed baseline for one oath file (ADR 0017): the normalised text
10
+ # of each example-producing paragraph, in document order — the same text as
11
+ # an example's key (Plan.derive_example_key). A text may repeat; the k-th
12
+ # occurrence breaks ties toward the k-th equally similar paragraph. Empty
13
+ # for an oath that was baselined with nothing to record.
14
+ OathBaseline = Data.define(:paragraphs)
15
+ # The whole varar.lock: every oath keyed by its POSIX path.
16
16
  LockFile = Data.define(:version, :oaths)
17
17
  # A paragraph the baseline says was an example and now matches no step.
18
18
  Drift = Data.define(:name, :line, :span)
19
+ # A paragraph that became an example since the baseline was recorded (#120).
20
+ NewExample = Data.define(:text, :line)
19
21
 
20
- # Oath drift detection: a paragraph the committed varar.lock.json baseline
22
+ # Oath drift detection: a paragraph the committed varar.lock baseline
21
23
  # recorded as an example that now matches no step. Pure, byte-identical to
22
- # the TS port so varar.lock.json is shared across languages. Port of drift.ts.
24
+ # the TS port so varar.lock is shared across languages. Port of drift.ts.
23
25
  #
24
26
  # BaselineStore is a duck-typed port: #read -> String|nil, #write(contents).
27
+ # A filesystem store reads varar.lock, falling back to a version 2
28
+ # varar.lock.json while that is all there is (parse_lock_file reads both),
29
+ # and #write writes varar.lock and deletes the legacy file.
25
30
  module Drifts
26
31
  # A paragraph may be moved anywhere and reworded up to ~half its words
27
32
  # and still be recognized; edit it past this and it reads as remove+add,
@@ -29,6 +34,16 @@ module Varar
29
34
  SIMILARITY_THRESHOLD = 0.5
30
35
  TOKEN_RE = /[[:alnum:]]+/
31
36
 
37
+ LOCK_VERSION = 3
38
+ # The lock file's name, and the version 2 name it replaces. A store reads
39
+ # the legacy file only while the new one is absent, and deletes it on its
40
+ # first write (ADR 0017).
41
+ LOCK_FILE_NAME = 'varar.lock'
42
+ LEGACY_LOCK_FILE_NAME = 'varar.lock.json'
43
+ # The first line of every varar.lock. Exact: a file without it is not a lock.
44
+ LOCK_HEADER = '# varar.lock v3 — generated by Varar; commit it. ' \
45
+ 'See https://varar.dev/reference/examples/#drift-detection'
46
+
32
47
  module_function
33
48
 
34
49
  # Do the two spans overlap at all (offset ranges intersect)? A candidate
@@ -61,52 +76,105 @@ module Varar
61
76
  union.zero? ? 0.0 : intersection.to_f / union
62
77
  end
63
78
 
64
- # The current example-producing paragraphs, in document order.
79
+ # The current example-producing paragraphs, in document order — what a
80
+ # clean run records as the new baseline for an oath.
65
81
  def live_examples(doc, plan)
66
- doc.examples.filter_map do |candidate|
67
- next unless live?(candidate.span, plan)
68
-
69
- BaselineExample.new(name: Plan.derive_example_name(candidate.body), line: candidate.span.start_line)
70
- end
82
+ doc.examples.select { |candidate| live?(candidate.span, plan) }
83
+ .map { |candidate| Plan.derive_example_key(candidate.body) }
71
84
  end
72
85
 
73
- def derive_oath_baseline(source, doc, plan)
74
- OathBaseline.new(source_hash: Hash32.hash_source(source), examples: live_examples(doc, plan))
86
+ # The baseline record for an oath: its live example-producing paragraphs.
87
+ def derive_oath_baseline(doc, plan)
88
+ OathBaseline.new(paragraphs: live_examples(doc, plan))
75
89
  end
76
90
 
77
91
  # Paragraphs the baseline recorded as examples that now match zero steps.
78
- # Each re-identified by the most word-similar current paragraph at/above
79
- # the threshold (exact name scores 1; ties break toward the nearest line).
92
+ # Each is re-identified by the most word-similar current paragraph at or
93
+ # above the threshold (an exact text scores 1), so moving a paragraph never
94
+ # looks like drift. Among equally similar paragraphs, the k-th occurrence
95
+ # of a text in the baseline takes the k-th of them in document order (the
96
+ # last, if fewer), so repeated paragraphs pair up in order.
97
+ #
98
+ # A paragraph in a section a reference block consumes (ADR 0016) is
99
+ # neither: it is not live here, but it still runs where it is referenced,
100
+ # and a broken step there fails as `reference-empty`. So a baseline
101
+ # paragraph that now lands in a consumed section — setup hoisted into a
102
+ # section the example links to — is not drift (#117), the same as when the
103
+ # whole file is referenced.
104
+ # matched & live → not drift · matched & consumed → not drift
105
+ # matched & dead → DRIFT · no match → remove+add
80
106
  def detect_drift(baseline, doc, plan)
81
107
  return [] if baseline.nil?
82
108
 
83
109
  candidates = doc.examples
84
- tokens = candidates.map { |c| tokenize(Plan.derive_example_name(c.body)) }
110
+ tokens = candidates.map { |c| tokenize(Plan.derive_example_key(c.body)) }
85
111
  live = candidates.map { |c| live?(c.span, plan) }
112
+ consumed = candidates.map do |c|
113
+ plan.consumed.any? { |s| s.start_offset == c.span.start_offset && s.end_offset == c.span.end_offset }
114
+ end
115
+ occurrences = Hash.new(0)
86
116
 
87
- baseline.examples.filter_map do |b|
88
- b_tokens = tokenize(b.name)
89
- best_idx = -1
90
- best_score = 0.0
91
- candidates.each_with_index do |candidate, i|
92
- score = similarity(b_tokens, tokens[i])
93
- next if score < SIMILARITY_THRESHOLD
117
+ baseline.paragraphs.filter_map do |text|
118
+ occurrences[text] += 1
119
+ best = best_candidates(tokenize(text), tokens)
120
+ # No recognizable paragraph — a rewrite/removal, not drift.
121
+ next if best.empty?
94
122
 
95
- line = candidate.span.start_line
96
- best_line = best_idx >= 0 ? candidates[best_idx].span.start_line : 0
97
- next unless best_idx.negative? || score > best_score ||
98
- (score == best_score && (line - b.line).abs < (best_line - b.line).abs)
123
+ best_idx = best[[occurrences[text], best.length].min - 1]
124
+ next if live[best_idx] # still an example — not drift
125
+ next if consumed[best_idx] # runs where it is referenced — not drift (#117)
99
126
 
100
- best_idx = i
127
+ Drift.new(name: text, line: candidates[best_idx].span.start_line, span: candidates[best_idx].span)
128
+ end
129
+ end
130
+
131
+ # Indexes of every candidate sharing the best similarity at or above the
132
+ # threshold, in document order.
133
+ def best_candidates(b_tokens, tokens)
134
+ best_score = 0.0
135
+ best = []
136
+ tokens.each_with_index do |candidate_tokens, i|
137
+ score = similarity(b_tokens, candidate_tokens)
138
+ next if score < SIMILARITY_THRESHOLD
139
+
140
+ if best.empty? || score > best_score
101
141
  best_score = score
142
+ best = [i]
143
+ elsif score == best_score
144
+ best << i
102
145
  end
103
- next if best_idx.negative?
104
- next if live[best_idx]
146
+ end
147
+ best
148
+ end
149
+
150
+ # The inverse of drift (#120): paragraphs that are examples now but that
151
+ # the baseline does not recognise — no baseline paragraph is at least
152
+ # SIMILARITY_THRESHOLD similar. Usually a new example someone wrote; but it
153
+ # is also how a step rename or a new step definition turns a sentence of
154
+ # explanation into an example nobody meant. Never gated, only reported
155
+ # when the baseline is rewritten, so it is seen when it is introduced.
156
+ # Empty when there is no baseline yet: on a first run everything is new,
157
+ # and saying so is noise.
158
+ def new_examples(baseline, doc, plan)
159
+ return [] if baseline.nil?
105
160
 
106
- Drift.new(name: b.name, line: candidates[best_idx].span.start_line, span: candidates[best_idx].span)
161
+ known = baseline.paragraphs.map { |text| tokenize(text) }
162
+ doc.examples.select { |candidate| live?(candidate.span, plan) }.filter_map do |candidate|
163
+ text = Plan.derive_example_key(candidate.body)
164
+ tokens = tokenize(text)
165
+ next if known.any? { |k| similarity(k, tokens) >= SIMILARITY_THRESHOLD }
166
+
167
+ NewExample.new(text: text, line: candidate.span.start_line)
107
168
  end
108
169
  end
109
170
 
171
+ # The one-line notice every adapter prints for a new example when it
172
+ # rewrites the baseline. Shared so the wording is the same in every port.
173
+ def new_example_notice(oath_path, example)
174
+ "varar: #{oath_path}:#{example.line} became an example: \"#{example.text}\" " \
175
+ '— if that sentence is explanation, reword it so it matches no step'
176
+ end
177
+
110
178
  def drift_diagnostics(drifts)
111
179
  drifts.map { |d| Diagnostics.drift_detected(d.name, d.span) }
112
180
  end
@@ -114,16 +182,20 @@ module Varar
114
182
  # One oath's baseline reconciliation against a BaselineStore. In update
115
183
  # mode, accept all drift (re-record, report nothing); otherwise detect
116
184
  # drift and rewrite the baseline only on a clean run, so an unacknowledged
117
- # drift keeps its old entry (and stays red).
118
- def reconcile_drift(store, oath_path, source, doc, plan, update: false)
185
+ # drift keeps its old entry (and stays red). When the baseline is
186
+ # rewritten, +notify+ (if given) receives one new_example_notice line per
187
+ # paragraph that became an example (#120) — the adapter prints it; it
188
+ # never fails or gates anything.
189
+ def reconcile_drift(store, oath_path, doc, plan, update: false, notify: nil)
119
190
  text = store.read
120
191
  lock = text ? parse_lock_file(text) : nil
121
192
  baseline = lock ? lock.oaths[oath_path] : nil
122
193
  drifts = update ? [] : detect_drift(baseline, doc, plan)
123
194
  if update || drifts.empty?
124
195
  oaths = lock ? lock.oaths.dup : {}
125
- oaths[oath_path] = derive_oath_baseline(source, doc, plan)
126
- store.write(stringify_lock_file(LockFile.new(version: 2, oaths: oaths)))
196
+ oaths[oath_path] = derive_oath_baseline(doc, plan)
197
+ store.write(stringify_lock_file(LockFile.new(version: LOCK_VERSION, oaths: oaths)))
198
+ new_examples(baseline, doc, plan).each { |ex| notify&.call(new_example_notice(oath_path, ex)) }
127
199
  end
128
200
  drifts
129
201
  end
@@ -134,7 +206,7 @@ module Varar
134
206
  # exists" means.
135
207
  def prune_lock_file(lock, keep_paths)
136
208
  keep = keep_paths.to_a
137
- LockFile.new(version: 2, oaths: lock.oaths.slice(*keep))
209
+ LockFile.new(version: LOCK_VERSION, oaths: lock.oaths.slice(*keep))
138
210
  end
139
211
 
140
212
  # The whole-lock counterpart of reconcile_drift, run ONCE per run rather
@@ -161,65 +233,67 @@ module Varar
161
233
  stale
162
234
  end
163
235
 
236
+ # Parse a lock: varar.lock (version 3), or a version 2 varar.lock.json
237
+ # read during migration, whose example names become paragraph texts.
238
+ # Returns nil on malformed input (treated as "no baseline yet").
164
239
  def parse_lock_file(text)
165
- parsed = JSON.parse(text)
166
- return nil unless parsed.is_a?(::Hash) && parsed['version'] == 2
240
+ return parse_legacy_lock_file(text) if text.lstrip.start_with?('{')
167
241
 
168
- oaths_raw = parsed['oaths']
169
- return nil unless oaths_raw.is_a?(::Hash)
242
+ lines = text.split("\n", -1).map { |line| line.delete_suffix("\r") }
243
+ return nil unless lines.first == LOCK_HEADER
170
244
 
171
245
  oaths = {}
172
- oaths_raw.each do |path, value|
173
- baseline = parse_oath_baseline(value)
174
- return nil if baseline.nil?
246
+ lines.drop(1).each do |line|
247
+ next if line.empty? || line.start_with?('#')
248
+
249
+ path, paragraph = line.split("\t", 2)
250
+ return nil if path.empty?
175
251
 
176
- oaths[path] = baseline
252
+ paragraphs = (oaths[path] ||= [])
253
+ paragraphs << paragraph unless paragraph.nil?
177
254
  end
178
- LockFile.new(version: 2, oaths: oaths)
179
- rescue JSON::ParserError, TypeError
180
- nil
255
+ LockFile.new(version: LOCK_VERSION, oaths: oaths.transform_values { |p| OathBaseline.new(paragraphs: p) })
181
256
  end
182
257
 
183
- def parse_oath_baseline(value)
184
- return nil unless value.is_a?(::Hash)
185
-
186
- source_hash = value['sourceHash']
187
- examples_raw = value['examples']
188
- return nil unless source_hash.is_a?(String) && examples_raw.is_a?(Array)
189
-
190
- examples = []
191
- examples_raw.each do |item|
192
- parsed = parse_baseline_example(item)
193
- return nil if parsed.nil?
194
-
195
- examples << parsed
258
+ # Serialize a varar.lock deterministically: the header, then oath paths
259
+ # sorted, each oath's paragraphs in document order, one per line, and a
260
+ # bare path for an oath with none. Byte-stable across runs so a clean
261
+ # re-run produces no git diff, and line-oriented so git merges it line by
262
+ # line.
263
+ def stringify_lock_file(lock)
264
+ lines = [LOCK_HEADER]
265
+ lock.oaths.keys.sort.each do |path|
266
+ paragraphs = lock.oaths[path].paragraphs
267
+ lines << path if paragraphs.empty?
268
+ paragraphs.each { |text| lines << "#{path}\t#{text}" }
196
269
  end
197
- OathBaseline.new(source_hash: source_hash, examples: examples)
270
+ "#{lines.join("\n")}\n"
198
271
  end
199
272
 
200
- def parse_baseline_example(value)
201
- return nil unless value.is_a?(::Hash)
202
-
203
- name = value['name']
204
- line = value['line']
205
- return nil unless name.is_a?(String) && line.is_a?(Integer)
273
+ # A version 2 varar.lock.json: { version: 2, oaths: { path: { sourceHash,
274
+ # examples: [{ name, line }] } } }. Only the names survive: sourceHash was
275
+ # never read, and line only broke ties (ADR 0017). Read during migration
276
+ # only.
277
+ def parse_legacy_lock_file(text)
278
+ parsed = JSON.parse(text)
279
+ return nil unless parsed.is_a?(::Hash) && parsed['version'] == 2
206
280
 
207
- BaselineExample.new(name: name, line: line)
208
- end
281
+ oaths_raw = parsed['oaths']
282
+ return nil unless oaths_raw.is_a?(::Hash)
209
283
 
210
- # Serialize varar.lock.json deterministically: oath paths sorted, examples
211
- # in document order, insertion-order keys otherwise (version, oaths;
212
- # sourceHash, examples; name, line) — NOT canonical JSON's key sort.
213
- def stringify_lock_file(lock)
214
284
  oaths = {}
215
- lock.oaths.keys.sort.each do |path|
216
- baseline = lock.oaths[path]
217
- oaths[path] = {
218
- 'sourceHash' => baseline.source_hash,
219
- 'examples' => baseline.examples.map { |e| { 'name' => e.name, 'line' => e.line } }
220
- }
285
+ oaths_raw.each do |path, value|
286
+ examples = value.is_a?(::Hash) ? value['examples'] : nil
287
+ return nil unless examples.is_a?(Array)
288
+
289
+ names = examples.map { |e| e.is_a?(::Hash) ? e['name'] : nil }
290
+ return nil unless names.all?(String)
291
+
292
+ oaths[path] = OathBaseline.new(paragraphs: names)
221
293
  end
222
- CanonicalJson.ordered_stringify({ 'version' => 2, 'oaths' => oaths })
294
+ LockFile.new(version: LOCK_VERSION, oaths: oaths)
295
+ rescue JSON::ParserError
296
+ nil
223
297
  end
224
298
  end
225
299
  end
@@ -48,11 +48,11 @@ module Varar
48
48
  seen_lines = {}
49
49
  ex.steps.each { |s| seen_lines[s.match_span.start_line] = true }
50
50
  info = { lines: seen_lines.keys }
51
- sink.call(ex.name, build_run(plan, ex, example_index, create_ctx, observer, oath_path), info)
51
+ sink.call(ex.name, build_run(ex, example_index, create_ctx, observer, oath_path), info)
52
52
  end
53
53
  end
54
54
 
55
- def build_run(plan, ex, example_index, create_ctx, observer, oath_path)
55
+ def build_run(ex, example_index, create_ctx, observer, oath_path)
56
56
  lambda do
57
57
  state_by_file = {}
58
58
  last_return = nil
@@ -92,7 +92,7 @@ module Varar
92
92
  state_by_file[file] = state
93
93
  end
94
94
  when 'sensor'
95
- compare_sensor_return(plan, ex, step, returned, extra) if ex.row_checks.nil?
95
+ compare_sensor_return(ex, step, returned, extra) if ex.row_checks.nil?
96
96
  else
97
97
  raise ReturnShapeError, "unknown step kind: #{step.step_def.kind}"
98
98
  end
@@ -145,7 +145,7 @@ module Varar
145
145
  # return is REQUIRED — returning nothing used to skip the comparison
146
146
  # silently, so a typo turned an assertion into a no-op. Raises the
147
147
  # appropriate diff error.
148
- def compare_sensor_return(plan, _ex, step, returned, extra)
148
+ def compare_sensor_return(_ex, step, returned, extra)
149
149
  slot_count = step.args.length + extra.length
150
150
  if slot_count.zero?
151
151
  return if returned.nil?
@@ -176,10 +176,10 @@ module Varar
176
176
  end
177
177
 
178
178
  inline_returned = slots[0...step.args.length]
179
- source_texts = step.param_spans.map do |s|
180
- Offsets.utf16_slice(plan.doc.source, s.start_offset, s.end_offset)
181
- end
182
- param_diffs = ParamDiff.compare_params(inline_returned, step.args, step.param_spans, source_texts,
179
+ # The text each parameter matched, sliced at plan time from the
180
+ # document the step was written in — which, for a step spliced in by
181
+ # a reference block, is not plan.doc.
182
+ param_diffs = ParamDiff.compare_params(inline_returned, step.args, step.param_spans, step.param_texts,
183
183
  step.formats).reject(&:ok)
184
184
  raise CellMismatchError, param_diffs unless param_diffs.empty?
185
185
 
@@ -3,7 +3,7 @@
3
3
  module Varar
4
4
  module Core
5
5
  # FNV-1a (32-bit) change-detector over UTF-16 code units. Not a security
6
- # hash: tiny and byte-identical to the TS/Python/JVM ports so varar.lock.json
6
+ # hash: tiny and byte-identical to the TS/Python/JVM ports so run-result
7
7
  # fingerprints match everywhere. The "fnv1a:" prefix namespaces the algorithm.
8
8
  # Port of hash.ts.
9
9
  module Hash32
@@ -27,20 +27,17 @@ module Varar
27
27
  module Matcher
28
28
  module_function
29
29
 
30
- # A compiled, un-anchored pattern from the step's CucumberExpression.
31
- def unanchored_pattern(step)
32
- regexp = step.compiled.instance_variable_get(:@tree_regexp).regexp
33
- source = regexp.source
34
- source = source[1..] if source.start_with?('^')
35
- source = source[0...-1] if source.end_with?('$')
36
- Regexp.new(source, regexp.options)
37
- end
38
-
39
- # Every expression match found anywhere in +sentence+.
40
- def find_hits(sentence, registry)
30
+ # Every expression match found anywhere in +sentence+, in registration
31
+ # order. +index+ (a StepIndex) only narrows which steps are tried and
32
+ # never changes the answer: without one, every step is tried. A caller
33
+ # matching many sentences against one registry builds it once with
34
+ # StepIndex.create.
35
+ def find_hits(sentence, registry, index = nil)
41
36
  hits = []
42
- registry.steps.each do |step|
43
- pattern = unanchored_pattern(step)
37
+ positions = index ? index.candidates(registry.steps, sentence) : (0...registry.steps.length)
38
+ positions.each do |position|
39
+ step = registry.steps[position]
40
+ pattern = step.scan.pattern
44
41
  pos = 0
45
42
  while pos <= sentence.length
46
43
  m = pattern.match(sentence, pos)
@@ -11,7 +11,8 @@ module Varar
11
11
  module_function
12
12
 
13
13
  def parse(path, source, plugins = [])
14
- Structurer.structure(path, source, Scanner.scan(source, plugins))
14
+ idx = Offsets.index(source)
15
+ Structurer.structure(path, source, Scanner.scan(source, plugins, idx), idx)
15
16
  end
16
17
  end
17
18
  end