Class: Spoom::Sorbet::Translate::ValidationResult

Inherits:
Object
  • Object
show all
Defined in:
lib/spoom/sorbet/translate/validator.rb

Overview

The outcome of comparing an original source with its rewritten form.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(missing_from_rewritten_output:, excess_in_rewritten_output:, on_wrong_line:) ⇒ ValidationResult

: ( | missing_from_rewritten_output: Array, | excess_in_rewritten_output: Array, | on_wrong_line: Array | ) -> void



87
88
89
90
91
# File 'lib/spoom/sorbet/translate/validator.rb', line 87

def initialize(missing_from_rewritten_output:, excess_in_rewritten_output:, on_wrong_line:)
  @missing_from_rewritten_output = missing_from_rewritten_output
  @excess_in_rewritten_output = excess_in_rewritten_output
  @on_wrong_line = on_wrong_line
end

Instance Attribute Details

#excess_in_rewritten_outputObject (readonly)

Landmarks present in the rewrite with no match in the original. : Array



76
77
78
# File 'lib/spoom/sorbet/translate/validator.rb', line 76

def excess_in_rewritten_output
  @excess_in_rewritten_output
end

#missing_from_rewritten_outputObject (readonly)

Landmarks present in the original but missing from the rewrite. : Array



72
73
74
# File 'lib/spoom/sorbet/translate/validator.rb', line 72

def missing_from_rewritten_output
  @missing_from_rewritten_output
end

#on_wrong_lineObject (readonly)

Landmarks present in both sources, but that moved to a different line. : Array



80
81
82
# File 'lib/spoom/sorbet/translate/validator.rb', line 80

def on_wrong_line
  @on_wrong_line
end

Instance Method Details

#errorsObject

Human-readable, one-per-line descriptions of every difference. Empty when the result is valid. : -> Array



104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/spoom/sorbet/translate/validator.rb', line 104

def errors
  errors = @missing_from_rewritten_output.map do |entry|
    "missing `#{entry[:landmark_id]}` (expected at line #{entry[:line]})"
  end
  errors += @excess_in_rewritten_output.map do |entry|
    "excess `#{entry[:landmark_id]}` (found at line #{entry[:line]})"
  end
  errors += @on_wrong_line.map do |entry|
    "`#{entry[:landmark_id]}` on the wrong line " \
      "(expected at #{format_lines(entry[:expected])}, found at #{format_lines(entry[:actual])})"
  end
  errors
end

#pretty_print(printer) ⇒ Object

: (untyped) -> void



119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/spoom/sorbet/translate/validator.rb', line 119

def pretty_print(printer)
  if valid?
    printer.text("#<#{self.class.name} valid>")
    return
  end

  printer.text("#<#{self.class.name} invalid")
  errors.each do |error|
    printer.breakable
    printer.text("  #{error}")
  end
  printer.breakable
  printer.text(">")
end

#valid?Boolean

True when every landmark survived the rewrite on its original line. : -> bool

Returns:

  • (Boolean)


95
96
97
98
99
# File 'lib/spoom/sorbet/translate/validator.rb', line 95

def valid?
  @missing_from_rewritten_output.empty? &&
    @excess_in_rewritten_output.empty? &&
    @on_wrong_line.empty?
end