Class: Canon::Comparison::ComparisonResult
- Inherits:
-
Object
- Object
- Canon::Comparison::ComparisonResult
- Defined in:
- lib/canon/comparison/comparison_result.rb
Overview
Encapsulates the result of a comparison operation Provides methods to query equivalence based on normative diffs
Instance Attribute Summary collapse
-
#algorithm ⇒ Object
readonly
Returns the value of attribute algorithm.
-
#differences ⇒ Object
readonly
Returns the value of attribute differences.
-
#format ⇒ Object
readonly
Returns the value of attribute format.
-
#html_version ⇒ Object
readonly
Returns the value of attribute html_version.
-
#match_options ⇒ Object
readonly
Returns the value of attribute match_options.
-
#original_strings ⇒ Object
readonly
Returns the value of attribute original_strings.
-
#parse_errors_expected ⇒ Object
readonly
Returns the value of attribute parse_errors_expected.
-
#parse_errors_received ⇒ Object
readonly
Returns the value of attribute parse_errors_received.
-
#preprocessed_strings ⇒ Object
readonly
Returns the value of attribute preprocessed_strings.
Instance Method Summary collapse
-
#diff(use_color: true, context_lines: 3, diff_grouping_lines: nil, show_diffs: :all, diff_mode: :separate, legacy_terminal: false) ⇒ String
Generate formatted diff output.
-
#equivalent? ⇒ Boolean
Check if documents are semantically equivalent (no normative diffs).
-
#has_informative_diffs? ⇒ Boolean
Check if there are any informative (textual-only) differences.
-
#has_normative_diffs? ⇒ Boolean
Check if there are any normative (semantic) differences Includes both DiffNode objects marked as normative AND legacy Hash differences (which represent structural differences like element name mismatches).
-
#informative_differences ⇒ Array<DiffNode>
Get all informative differences.
-
#initialize(differences:, preprocessed_strings:, format:, html_version: nil, match_options: nil, algorithm: :dom, original_strings: nil, parse_errors_expected: nil, parse_errors_received: nil) ⇒ ComparisonResult
constructor
A new instance of ComparisonResult.
-
#normative_differences ⇒ Array<DiffNode>
Get all normative differences.
-
#operations ⇒ Array<Operation>
Get tree diff operations (only available when diff_algorithm: :semantic).
-
#parse_errors? ⇒ Boolean
Whether either side reported parse errors.
-
#summary ⇒ String
Generate a human-readable summary of the first difference.
Constructor Details
#initialize(differences:, preprocessed_strings:, format:, html_version: nil, match_options: nil, algorithm: :dom, original_strings: nil, parse_errors_expected: nil, parse_errors_received: nil) ⇒ ComparisonResult
Returns a new instance of ComparisonResult.
21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/canon/comparison/comparison_result.rb', line 21 def initialize(differences:, preprocessed_strings:, format:, html_version: nil, match_options: nil, algorithm: :dom, original_strings: nil, parse_errors_expected: nil, parse_errors_received: nil) @differences = differences @preprocessed_strings = preprocessed_strings @original_strings = original_strings || preprocessed_strings @format = format @html_version = html_version @match_options = @algorithm = algorithm @parse_errors_expected = Array(parse_errors_expected) @parse_errors_received = Array(parse_errors_received) end |
Instance Attribute Details
#algorithm ⇒ Object (readonly)
Returns the value of attribute algorithm.
8 9 10 |
# File 'lib/canon/comparison/comparison_result.rb', line 8 def algorithm @algorithm end |
#differences ⇒ Object (readonly)
Returns the value of attribute differences.
8 9 10 |
# File 'lib/canon/comparison/comparison_result.rb', line 8 def differences @differences end |
#format ⇒ Object (readonly)
Returns the value of attribute format.
8 9 10 |
# File 'lib/canon/comparison/comparison_result.rb', line 8 def format @format end |
#html_version ⇒ Object (readonly)
Returns the value of attribute html_version.
8 9 10 |
# File 'lib/canon/comparison/comparison_result.rb', line 8 def html_version @html_version end |
#match_options ⇒ Object (readonly)
Returns the value of attribute match_options.
8 9 10 |
# File 'lib/canon/comparison/comparison_result.rb', line 8 def @match_options end |
#original_strings ⇒ Object (readonly)
Returns the value of attribute original_strings.
8 9 10 |
# File 'lib/canon/comparison/comparison_result.rb', line 8 def original_strings @original_strings end |
#parse_errors_expected ⇒ Object (readonly)
Returns the value of attribute parse_errors_expected.
8 9 10 |
# File 'lib/canon/comparison/comparison_result.rb', line 8 def parse_errors_expected @parse_errors_expected end |
#parse_errors_received ⇒ Object (readonly)
Returns the value of attribute parse_errors_received.
8 9 10 |
# File 'lib/canon/comparison/comparison_result.rb', line 8 def parse_errors_received @parse_errors_received end |
#preprocessed_strings ⇒ Object (readonly)
Returns the value of attribute preprocessed_strings.
8 9 10 |
# File 'lib/canon/comparison/comparison_result.rb', line 8 def preprocessed_strings @preprocessed_strings end |
Instance Method Details
#diff(use_color: true, context_lines: 3, diff_grouping_lines: nil, show_diffs: :all, diff_mode: :separate, legacy_terminal: false) ⇒ String
Generate formatted diff output
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/canon/comparison/comparison_result.rb', line 134 def diff(use_color: true, context_lines: 3, diff_grouping_lines: nil, show_diffs: :all, diff_mode: :separate, legacy_terminal: false) require_relative "../diff_formatter" formatter = Canon::DiffFormatter.new( use_color: use_color, mode: :by_line, context_lines: context_lines, diff_grouping_lines: diff_grouping_lines, show_diffs: show_diffs, diff_mode: diff_mode, legacy_terminal: legacy_terminal, ) # Pass self (ComparisonResult) so formatter can check equivalent? status formatter.format( self, @format, doc1: @preprocessed_strings[0], doc2: @preprocessed_strings[1], html_version: @html_version, ) end |
#equivalent? ⇒ Boolean
Check if documents are semantically equivalent (no normative diffs)
46 47 48 |
# File 'lib/canon/comparison/comparison_result.rb', line 46 def equivalent? !has_normative_diffs? end |
#has_informative_diffs? ⇒ Boolean
Check if there are any informative (textual-only) differences
70 71 72 73 74 |
# File 'lib/canon/comparison/comparison_result.rb', line 70 def has_informative_diffs? @differences.any? do |diff| diff.is_a?(Canon::Diff::DiffNode) && diff.informative? end end |
#has_normative_diffs? ⇒ Boolean
Check if there are any normative (semantic) differences Includes both DiffNode objects marked as normative AND legacy Hash differences (which represent structural differences like element name mismatches)
55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/canon/comparison/comparison_result.rb', line 55 def has_normative_diffs? @differences.any? do |diff| # DiffNode objects - check if marked normative if diff.is_a?(Canon::Diff::DiffNode) diff.normative? # Legacy Hash format - always considered normative (structural differences) else diff.is_a?(Hash) end end end |
#informative_differences ⇒ Array<DiffNode>
Get all informative differences
88 89 90 91 92 |
# File 'lib/canon/comparison/comparison_result.rb', line 88 def informative_differences @differences.select do |diff| diff.is_a?(Canon::Diff::DiffNode) && diff.informative? end end |
#normative_differences ⇒ Array<DiffNode>
Get all normative differences
79 80 81 82 83 |
# File 'lib/canon/comparison/comparison_result.rb', line 79 def normative_differences @differences.select do |diff| diff.is_a?(Canon::Diff::DiffNode) && diff.normative? end end |
#operations ⇒ Array<Operation>
Get tree diff operations (only available when diff_algorithm: :semantic)
97 98 99 |
# File 'lib/canon/comparison/comparison_result.rb', line 97 def operations @match_options&.[](:tree_diff_operations) || [] end |
#parse_errors? ⇒ Boolean
Whether either side reported parse errors. Used by the diff formatter to decide whether to render the parse-error banner.
39 40 41 |
# File 'lib/canon/comparison/comparison_result.rb', line 39 def parse_errors? @parse_errors_expected.any? || @parse_errors_received.any? end |
#summary ⇒ String
Generate a human-readable summary of the first difference.
When documents are equivalent, returns “Equivalent”. When they differ, returns a single-line string with the first normative (or first informative) difference location and reason.
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/canon/comparison/comparison_result.rb', line 108 def summary return "Equivalent" if equivalent? diff = normative_differences.first || informative_differences.first || @differences.first # rubocop:disable Layout/MultilineOperationIndentation return "Not equivalent" unless diff if diff.is_a?(Canon::Diff::DiffNode) summarize_diff_node(diff) elsif diff.is_a?(Hash) summarize_legacy_hash(diff) else "Not equivalent" end end |