class Unparser::Validation

Validation of unparser results

def self.const_unit(_); end

mutant:disable
def self.const_unit(_); end

def self.from_ast(ast:)

Returns:
  • (Validator) -

Parameters:
  • ast (Unparser::AST) --
def self.from_ast(ast:)
  generated_source = Unparser.unparse_ast_either(ast)
  generated_node = generated_source
    .lmap(&method(:const_unit))
    .bind(&method(:parse_ast_either))
    .fmap(&:node)
  new(
    identification:   '(string)',
    original_source:  generated_source,
    original_ast:     Either::Right.new(ast),
    generated_source: generated_source,
    generated_node:   generated_node
  )
end

def self.from_path(path)

Returns:
  • (Validator) -

Parameters:
  • path (Pathname) --
def self.from_path(path)
  from_string(path.read.freeze).with(identification: path.to_s)
end

def self.from_string(original_source)

Returns:
  • (Validator) -

Parameters:
  • original_source (String) --
def self.from_string(original_source)
  original_ast = parse_ast_either(original_source)
  generated_source = original_ast
    .lmap(&method(:const_unit))
    .bind(&method(:unparse_ast_either))
  generated_node = generated_source
    .lmap(&method(:const_unit))
    .bind(&method(:parse_ast_either))
    .fmap(&:node)
  new(
    generated_node:   generated_node,
    generated_source: generated_source,
    identification:   '(string)',
    original_ast:     original_ast,
    original_source:  Either::Right.new(original_source)
  )
end

def self.parse_ast_either(source)

mutant:disable
def self.parse_ast_either(source)
  Unparser.parse_ast_either(source)
end

def self.unparse_ast_either(ast)

mutant:disable
def self.unparse_ast_either(ast)
  Unparser.unparse_ast_either(ast)
end

def make_report(label, attribute_name)

mutant:disable
def make_report(label, attribute_name)
  ["#{label}:"].concat(public_send(attribute_name).either(method(:report_exception), ->(value) { [value] }))
end

def node_diff_report

mutant:disable
def node_diff_report
  diff = nil
  original_node.fmap do |original|
    generated_node.fmap do |generated|
      diff = Diff.new(
        original.to_s.lines.map(&:chomp),
        generated.to_s.lines.map(&:chomp)
      ).colorized_diff
    end
  end
  diff ? ['Node-Diff:', diff] : []
end

def original_node

mutant:disable
def original_node
  original_ast.fmap(&:node)
end

def report

Other tags:
    Api: - private

Returns:
  • (String) -
def report
  message = [identification]
  message.concat(make_report('Original-Source',  :original_source))
  message.concat(make_report('Generated-Source', :generated_source))
  message.concat(make_report('Original-Node',    :original_node))
  message.concat(make_report('Generated-Node',   :generated_node))
  message.concat(node_diff_report)
  message.join("\n")
end

def report_exception(phase_exception)

mutant:disable
def report_exception(phase_exception)
  if phase_exception
    [phase_exception.inspect].concat(phase_exception.backtrace.take(20))
  else
    %w[undefined]
  end
end

def success?

Other tags:
    Api: - private

Returns:
  • (Boolean) -
def success?
  [
    original_source,
    original_ast,
    generated_source,
    generated_node
  ].all?(&:right?) && generated_node.from_right.==(original_node.from_right)
end