class Unparser::Validation
Validation of unparser results
def self.const_unit(_); end
def self.const_unit(_); end
def self.from_ast(ast:)
-
(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)
-
(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)
-
(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)
def self.parse_ast_either(source) Unparser.parse_ast_either(source) end
def self.unparse_ast_either(ast)
def self.unparse_ast_either(ast) Unparser.unparse_ast_either(ast) end
def make_report(label, attribute_name)
def make_report(label, attribute_name) ["#{label}:"].concat(public_send(attribute_name).either(method(:report_exception), ->(value) { [value] })) end
def node_diff_report
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
def original_node original_ast.fmap(&:node) end
def report
- 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)
def report_exception(phase_exception) if phase_exception [phase_exception.inspect].concat(phase_exception.backtrace.take(20)) else %w[undefined] end end
def success?
- 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