Class: Unparser::Validation

Inherits:
Object
  • Object
show all
Includes:
Adamantium
Defined in:
lib/unparser/validation.rb

Overview

Validation of unparser results

Direct Known Subclasses

Literal

Defined Under Namespace

Classes: Literal

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.from_node(original_node) ⇒ Validator

Create validator from node

Parameters:

  • original_node (Parser::AST::Node)

Returns:

  • (Validator)


81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/unparser/validation.rb', line 81

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) ⇒ Validator

Create validator from file

Parameters:

  • path (Pathname)

Returns:

  • (Validator)


102
103
104
# File 'lib/unparser/validation.rb', line 102

def self.from_path(path)
  from_string(path.read).with(identification: path.to_s)
end

.from_string(original_source) ⇒ Validator

Create validator from string

Parameters:

  • original_source (String)

Returns:

  • (Validator)


55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/unparser/validation.rb', line 55

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

Instance Method Details

#reportString

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Return error report

Returns:

  • (String)


37
38
39
40
41
42
43
44
45
46
47
# File 'lib/unparser/validation.rb', line 37

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?Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Test if source could be unparsed successfully

rubocop:disable Style/OperatorMethodCall

Returns:

  • (Boolean)


21
22
23
24
25
26
27
28
# File 'lib/unparser/validation.rb', line 21

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