Module: Oselvar::Var::Core::Conformance

Defined in:
lib/oselvar/var/core/conformance.rb

Overview

Projections from the internal pipeline values to the camelCase wire dicts compared against golden/*.json. Port of conformance.ts. (var-doc stage; registry/plan/trace projections are added in later stages.)

Class Method Summary collapse

Class Method Details

.block_hash(block) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/oselvar/var/core/conformance.rb', line 43

def block_hash(block)
  case block.kind
  when 'paragraph', 'blockquote'
    {
      'kind' => block.kind,
      'text' => block.text,
      'span' => span_hash(block.span),
      'segmentMap' => block.segment_map.map { |so| segment_hash(so) }
    }
  when 'heading'
    {
      'kind' => block.kind,
      'level' => block.level,
      'text' => block.text,
      'span' => span_hash(block.span)
    }
  when 'list_item'
    {
      'kind' => block.kind,
      'text' => block.text,
      'span' => span_hash(block.span),
      'segmentMap' => block.segment_map.map { |so| segment_hash(so) },
      'ordered' => block.ordered,
      'markerSpan' => span_hash(block.marker_span)
    }
  when 'table'
    {
      'kind' => block.kind,
      'span' => span_hash(block.span),
      'header' => row_hash(block.header),
      'rows' => block.rows.map { |r| row_hash(r) }
    }
  when 'fence'
    {
      'kind' => block.kind,
      'span' => span_hash(block.span),
      'info' => block.info,
      'body' => block.body,
      'bodySpan' => span_hash(block.body_span)
    }
  when 'thematic_break'
    {
      'kind' => block.kind,
      'span' => span_hash(block.span)
    }
  else
    raise "Unknown block kind: #{block.kind}"
  end
end

.doc_string_hash(doc_string) ⇒ Object



130
131
132
133
134
135
136
# File 'lib/oselvar/var/core/conformance.rb', line 130

def doc_string_hash(doc_string)
  {
    'content' => doc_string.content,
    'contentType' => doc_string.content_type,
    'span' => span_hash(doc_string.span)
  }
end

.example_hash(example) ⇒ Object



93
94
95
96
97
98
99
# File 'lib/oselvar/var/core/conformance.rb', line 93

def example_hash(example)
  {
    'scopeStack' => example.scope_stack,
    'span' => span_hash(example.span),
    'body' => example.body.map { |b| block_hash(b) }
  }
end

.file_stem(path) ⇒ Object

Return the file stem: "path/to/foo.steps.rb" -> "foo.steps".



181
182
183
# File 'lib/oselvar/var/core/conformance.rb', line 181

def file_stem(path)
  File.basename(path, '.*')
end

.parameter_type_names(compiled) ⇒ Object

Parameter-type names in source order from a compiled CucumberExpression. The Ruby gem populates @parameter_types in source order during construction (it has no public reader), mirroring the TS AST walk.



113
114
115
# File 'lib/oselvar/var/core/conformance.rb', line 113

def parameter_type_names(compiled)
  compiled.instance_variable_get(:@parameter_types).map(&:name)
end

.planned_example_hash(example, source) ⇒ Object



149
150
151
152
153
154
155
156
157
158
159
# File 'lib/oselvar/var/core/conformance.rb', line 149

def planned_example_hash(example, source)
  result = {
    'name' => example.name,
    'scopeStack' => example.scope_stack,
    'span' => span_hash(example.span),
    'expectedOutcome' => example.expected_outcome || 'pass'
  }
  result['expectedErrorMessage'] = example.expected_error_message if example.expected_error_message
  result['steps'] = example.steps.map { |s| planned_step_hash(s, source) }
  result
end

.planned_step_hash(step, source) ⇒ Object



161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/oselvar/var/core/conformance.rb', line 161

def planned_step_hash(step, source)
  step_names = parameter_type_names(step.step_def.compiled)
  result = {
    'text' => step.text,
    'matchSpan' => span_hash(step.match_span),
    'paramSpans' => step.param_spans.map { |s| span_hash(s) },
    'matchedExpression' => step.step_def.expression,
    'args' => step.param_spans.each_with_index.map do |s, i|
      {
        'value' => Offsets.utf16_slice(source, s.start_offset, s.end_offset),
        'parameterType' => i < step_names.length ? step_names[i] : nil
      }
    end
  }
  result['dataTable'] = block_hash(step.data_table) if step.data_table
  result['docString'] = doc_string_hash(step.doc_string) if step.doc_string
  result
end

.row_hash(row) ⇒ Object



35
36
37
38
39
40
41
# File 'lib/oselvar/var/core/conformance.rb', line 35

def row_hash(row)
  {
    'cells' => row.cells,
    'cellSpans' => row.cell_spans.map { |cs| span_hash(cs) },
    'span' => span_hash(row.span)
  }
end

.run_conformance(var_doc, registry, create_context, parameter_types = []) ⇒ Object

Run all examples and return the four-artifact bundle. Port of runConformance.



217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
# File 'lib/oselvar/var/core/conformance.rb', line 217

def run_conformance(var_doc, registry, create_context, parameter_types = [])
  execution = Plan.plan(var_doc, registry)
  observed = Hash.new { |h, k| h[k] = [] }
  observer = ->(o) { observed[o.example_index] << o }
  queue = Execute.collect_examples(execution, create_context: create_context, observer: observer)

  trace_examples = queue.each_with_index.map do |queued, k|
    outcome = 'pass'
    begin
      queued.run.call
    rescue StandardError
      outcome = 'fail'
    end

    planned = execution.examples[k]
    obs_list = observed[k]
    steps = planned.steps.each_with_index.map do |step, i|
      ordinal = i + 1
      matches = obs_list.select { |x| x.ordinal == ordinal }
      observation = matches.find { |m| m.outcome == 'fail' } || matches.last
      step_outcome = observation ? observation.outcome : 'skipped'
      step_dict = {
        'exampleName' => queued.name,
        'ordinal' => ordinal,
        'stepText' => step.text,
        'matchedExpression' => step.step_def.expression,
        'contextKey' => { 'exampleName' => queued.name,
                          'stepFile' => file_stem(step.step_def.expression_source_file) },
        'outcome' => step_outcome
      }
      step_dict['failure'] = to_failure_artifact(observation&.error, step.match_span) if step_outcome == 'fail'
      step_dict
    end

    { 'name' => queued.name, 'outcome' => outcome, 'steps' => steps }
  end

  {
    var_doc: to_var_doc_artifact(var_doc),
    registry: to_registry_artifact(registry, parameter_types),
    plan: to_plan_artifact(execution),
    trace: { 'examples' => trace_examples }
  }
end

.segment_hash(segment_offset) ⇒ Object



28
29
30
31
32
33
# File 'lib/oselvar/var/core/conformance.rb', line 28

def segment_hash(segment_offset)
  {
    'textOffset' => segment_offset.text_offset,
    'sourceOffset' => segment_offset.source_offset
  }
end

.span_hash(span) ⇒ Object



17
18
19
20
21
22
23
24
25
26
# File 'lib/oselvar/var/core/conformance.rb', line 17

def span_hash(span)
  {
    'startOffset' => span.start_offset,
    'endOffset' => span.end_offset,
    'startLine' => span.start_line,
    'startCol' => span.start_col,
    'endLine' => span.end_line,
    'endCol' => span.end_col
  }
end

.to_failure_artifact(error, match_span) ⇒ Object

Project an execution error to a FailureArtifact dict. line and anchor are deterministic source positions (never scraped from a backtrace).



187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'lib/oselvar/var/core/conformance.rb', line 187

def to_failure_artifact(error, match_span)
  line = match_span.start_line
  anchor = span_hash(FailureAnchor.failure_anchor(error, match_span))
  case error
  when CellMismatchError
    {
      'kind' => 'cell-mismatch', 'line' => line, 'anchor' => anchor,
      'cells' => error.cells.reject(&:ok).map do |c|
        { 'column' => c.column, 'expected' => c.expected, 'actual' => c.actual, 'span' => span_hash(c.span) }
      end
    }
  when DocStringMismatchError
    {
      'kind' => 'doc-string-mismatch', 'line' => line, 'anchor' => anchor,
      'diff' => {
        'expected' => error.diff.expected,
        'actual' => error.diff.actual,
        'span' => span_hash(error.diff.span)
      }
    }
  when ReturnShapeError
    { 'kind' => 'return-shape', 'line' => line, 'anchor' => anchor }
  when UnexpectedPassError
    { 'kind' => 'unexpected-pass', 'line' => line, 'anchor' => anchor }
  else
    { 'kind' => 'thrown', 'line' => line, 'anchor' => anchor }
  end
end

.to_plan_artifact(plan) ⇒ Object

Project an ExecutionPlan to the wire dict for the plan artifact.



139
140
141
142
143
144
145
146
147
# File 'lib/oselvar/var/core/conformance.rb', line 139

def to_plan_artifact(plan)
  source = plan.var_doc.source
  {
    'examples' => plan.examples.map { |ex| planned_example_hash(ex, source) },
    'diagnostics' => plan.diagnostics.map do |d|
      { 'code' => d.code, 'severity' => d.severity, 'span' => span_hash(d.span) }
    end
  }
end

.to_registry_artifact(registry, parameter_types = []) ⇒ Object

Project a Registry to the wire dict for the registry artifact. parameter_types is the custom-type list ("name","regexp").



119
120
121
122
123
124
125
126
127
128
# File 'lib/oselvar/var/core/conformance.rb', line 119

def to_registry_artifact(registry, parameter_types = [])
  {
    'steps' => registry.steps.map do |s|
      { 'expression' => s.expression, 'parameterTypeNames' => parameter_type_names(s.compiled) }
    end,
    'parameterTypes' => parameter_types.map do |p|
      { 'name' => p['name'], 'regexp' => p['regexp'] }
    end
  }
end

.to_var_doc_artifact(doc) ⇒ Object

Project a VarDoc to the wire dict for the var-doc artifact.



102
103
104
105
106
107
108
# File 'lib/oselvar/var/core/conformance.rb', line 102

def to_var_doc_artifact(doc)
  {
    'path' => doc.path,
    'examples' => doc.examples.map { |ex| example_hash(ex) },
    'orphanAttachments' => doc.orphan_attachments.map { |b| block_hash(b) }
  }
end