Class: SvgConform::CompatibilityAnalyzer

Inherits:
Object
  • Object
show all
Defined in:
lib/svg_conform/compatibility_analyzer.rb

Overview

Analyzes compatibility between svg_conform and svgcheck results

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCompatibilityAnalyzer

Returns a new instance of CompatibilityAnalyzer.



10
11
12
# File 'lib/svg_conform/compatibility_analyzer.rb', line 10

def initialize
  @results = {}
end

Instance Attribute Details

#resultsObject (readonly)

Returns the value of attribute results.



8
9
10
# File 'lib/svg_conform/compatibility_analyzer.rb', line 8

def results
  @results
end

Instance Method Details

#analyze_all_filesObject

Analyze all test files and compare results



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/svg_conform/compatibility_analyzer.rb', line 15

def analyze_all_files
  input_dir = File.join(__dir__, "..", "..", "spec", "fixtures",
                        "svgcheck", "inputs")
  check_dir = File.join(__dir__, "..", "..", "spec", "fixtures",
                        "svgcheck", "check")
  repair_dir = File.join(__dir__, "..", "..", "spec", "fixtures",
                         "svgcheck", "repair")

  Dir.glob(File.join(input_dir, "*.svg")).each do |input_file|
    basename = File.basename(input_file, ".svg")
    next if basename.start_with?("rfc") # Skip non-SVG files

    puts "Analyzing #{basename}..."
    result = analyze_file(input_file, check_dir, repair_dir)
    @results[basename] = result
  end

  @results
end

#analyze_file(input_file, check_dir, repair_dir) ⇒ Object

Analyze a single file



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/svg_conform/compatibility_analyzer.rb', line 36

def analyze_file(input_file, check_dir, repair_dir)
  basename = File.basename(input_file, ".svg")

  # Load svgcheck results
  svgcheck_out = load_svgcheck_file(check_dir, "#{basename}.svg.out")
  svgcheck_err = load_svgcheck_file(check_dir, "#{basename}.svg.err")
  svgcheck_code = load_svgcheck_file(check_dir, "#{basename}.svg.code")
  svgcheck_repair = load_svgcheck_file(repair_dir, "#{basename}.svg.file")

  # Run our validation
  our_validation = run_our_validation(input_file)
  our_remediation = nil # Skip remediation for now

  # Parse svgcheck results
  svgcheck_errors = parse_svgcheck_errors(svgcheck_out)
  svgcheck_valid = svgcheck_out.include?("INFO: File conforms to SVG requirements")

  # Compare results
  {
    file: basename,
    input_file: input_file,
    svgcheck: {
      valid: svgcheck_valid,
      errors: svgcheck_errors,
      error_count: svgcheck_errors.length,
      exit_code: svgcheck_code&.strip&.to_i,
      raw_output: svgcheck_out,
      raw_error: svgcheck_err,
      repaired_content: svgcheck_repair,
    },
    our_results: {
      valid: our_validation[:valid],
      errors: our_validation[:errors],
      error_count: our_validation[:errors].length,
      remediated_content: our_remediation,
    },
    compatibility: analyze_compatibility(svgcheck_errors, svgcheck_valid,
                                         our_validation),
  }
end

#generate_reportObject

Generate a detailed report



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/svg_conform/compatibility_analyzer.rb', line 78

def generate_report
  return "No analysis results available" if @results.empty?

  report = []
  report << "SVG Conform vs SVGCheck Compatibility Analysis"
  report << "=" * 50
  report << ""

  # Summary statistics
  total_files = @results.length
  compatible_files = @results.count do |_, r|
    r[:compatibility][:overall_compatible]
  end

  report << "Summary:"
  report << "  Total files analyzed: #{total_files}"
  report << "  Compatible files: #{compatible_files}"
  report << "  Compatibility rate: #{(compatible_files.to_f / total_files * 100).round(1)}%"
  report << ""

  # Detailed analysis for each file
  @results.each do |basename, result|
    report << "File: #{basename}"
    report << "-" * 30

    # Validation comparison
    svgcheck_valid = result[:svgcheck][:valid]
    our_valid = result[:our_results][:valid]

    report << "  Validation:"
    report << "    SVGCheck: #{svgcheck_valid ? 'VALID' : 'INVALID'} (#{result[:svgcheck][:error_count]} errors)"
    report << "    Our tool:  #{our_valid ? 'VALID' : 'INVALID'} (#{result[:our_results][:error_count]} errors)"
    report << "    Match: #{svgcheck_valid == our_valid ? 'YES' : 'NO'}"

    # Error analysis
    if result[:svgcheck][:error_count].positive? || result[:our_results][:error_count].positive?
      report << "  Errors:"

      if result[:svgcheck][:error_count].positive?
        report << "    SVGCheck errors:"
        result[:svgcheck][:errors].each do |error|
          report << "      - Line #{error[:line]}: #{error[:message]}"
        end
      end

      if result[:our_results][:error_count].positive?
        report << "    Our errors:"
        result[:our_results][:errors].each do |error|
          report << "      - #{error}"
        end
      end
    end

    # Compatibility issues
    compatibility = result[:compatibility]
    unless compatibility[:issues].empty?
      report << "  Compatibility Issues:"
      compatibility[:issues].each do |issue|
        report << "    - #{issue}"
      end
    end

    report << ""
  end

  # Pattern analysis
  report << "Common Patterns:"
  report << "-" * 20

  # Find common error types
  all_svgcheck_errors = @results.values.flat_map do |r|
    r[:svgcheck][:errors]
  end
  error_patterns = all_svgcheck_errors.group_by do |e|
    extract_error_pattern(e[:message])
  end

  error_patterns.each do |pattern, errors|
    report << "  #{pattern}: #{errors.length} occurrences"
  end

  report.join("\n")
end