Module: Oselvar::Var::Core::CellDiffs

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

Overview

Pure comparison of row/table step returns against the authored cells. Port of cell-diff.ts.

Class Method Summary collapse

Class Method Details

.compare_row(returned, checks) ⇒ Object

Compare a row step's returned Hash against the row's cells. Only columns present on returned are checked; a non-Hash return checks nothing.



51
52
53
54
55
56
57
58
59
60
61
# File 'lib/oselvar/var/core/cell_diff.rb', line 51

def compare_row(returned, checks)
  return [] unless returned.is_a?(Hash)

  checks.filter_map do |check|
    next unless returned.key?(check.column)

    actual = render_cell_value(returned[check.column])
    CellDiff.new(column: check.column, span: check.span, expected: check.value,
                 actual: actual, ok: actual == check.value)
  end
end

.compare_table(returned, input_table) ⇒ Object

Compare a whole-table step's returned table against the input table. returned: nil (no checks), Array of Arrays (positional), or Array of Hashes (keyed by header). Cells compare as exact strings.

Raises:



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
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/oselvar/var/core/cell_diff.rb', line 66

def compare_table(returned, input_table)
  return [] if returned.nil?
  raise ReturnShapeError, "expected a table (array of rows), got #{returned.class}" unless returned.is_a?(Array)

  columns = input_table.header.cells
  data_rows = input_table.rows
  if returned.length != data_rows.length
    raise ReturnShapeError, "expected #{data_rows.length} row(s), got #{returned.length}"
  end

  all_arrays = returned.all?(Array)
  all_records = returned.all?(Hash)
  raise ReturnShapeError, 'table rows must be all arrays or all objects' if !all_arrays && !all_records

  diffs = []
  data_rows.each_with_index do |row, i|
    ret = returned[i]
    if all_arrays && ret.length != columns.length
      raise ReturnShapeError, "row #{i}: expected #{columns.length} column(s), got #{ret.length}"
    end

    columns.each_with_index do |column, j|
      if all_arrays
        actual_value = ret[j]
      else
        raise ReturnShapeError, "row #{i}: missing column \"#{column}\"" unless ret.key?(column)

        actual_value = ret[column]
      end
      expected = j < row.cells.length ? row.cells[j] : ''
      actual = render_cell_value(actual_value)
      span = j < row.cell_spans.length ? row.cell_spans[j] : row.span
      diffs << CellDiff.new(column: column, span: span, expected: expected, actual: actual,
                            ok: actual == expected)
    end
  end
  diffs
end

.render_cell_value(value) ⇒ Object

Display rules 2-4 of the mismatch-rendering chain (rule 1, the parameter type's format, is applied in param_diff). A string renders as-is, other primitives via to_s, anything else via inspect. The inspect fallback is port-native and deliberately outside conformance.



41
42
43
44
45
46
47
# File 'lib/oselvar/var/core/cell_diff.rb', line 41

def render_cell_value(value)
  return value if value.is_a?(String)
  return value.to_s if value.nil? || value == true || value == false ||
                       value.is_a?(Integer) || value.is_a?(Float)

  value.inspect
end