class Unparser::Validation
Validation
of unparser results
Public Class Methods
from_node(original_node)
click to toggle source
Create validator from node
@param [Parser::AST::Node] original_node
@return [Validator]
# File lib/unparser/validation.rb, line 79 def self.from_node(original_node) generated_source = Unparser.unparse_either(original_node) generated_node = generated_source .lmap(&method(:const_unit)) .bind(&Unparser.public_method(:parse_either)) new( identification: '(string)', original_source: generated_source, original_node: Either::Right.new(original_node), generated_source: generated_source, generated_node: generated_node ) end
from_path(path)
click to toggle source
Create validator from file
@param [Pathname] path
@return [Validator]
# File lib/unparser/validation.rb, line 100 def self.from_path(path) from_string(path.read).with(identification: path.to_s) end
from_string(original_source)
click to toggle source
Create validator from string
@param [String] original_source
@return [Validator]
# File lib/unparser/validation.rb, line 53 def self.from_string(original_source) original_node = Unparser .parse_either(original_source) generated_source = original_node .lmap(&method(:const_unit)) .bind(&Unparser.method(:unparse_either)) generated_node = generated_source .lmap(&method(:const_unit)) .bind(&Unparser.method(:parse_either)) new( identification: '(string)', original_source: Either::Right.new(original_source), original_node: original_node, generated_source: generated_source, generated_node: generated_node ) end
Private Class Methods
const_unit(_value)
click to toggle source
# File lib/unparser/validation.rb, line 133 def self.const_unit(_value); end
Public Instance Methods
report()
click to toggle source
Return error report
@return [String]
@api private
# File lib/unparser/validation.rb, line 35 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
success?()
click to toggle source
Test if source could be unparsed successfully
@return [Boolean]
@api private
# File lib/unparser/validation.rb, line 20 def success? [ original_source, original_node, generated_source, generated_node ].all?(&:right?) && generated_node.from_right.==(original_node.from_right) end
Private Instance Methods
make_report(label, attribute_name)
click to toggle source
# File lib/unparser/validation.rb, line 106 def make_report(label, attribute_name) ["#{label}:"].concat(public_send(attribute_name).either(method(:report_exception), ->(value) { [value] })) end
node_diff_report()
click to toggle source
# File lib/unparser/validation.rb, line 118 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
report_exception(exception)
click to toggle source
# File lib/unparser/validation.rb, line 110 def report_exception(exception) if exception [exception.inspect].concat(exception.backtrace.take(20)) else ['undefined'] end end