Class: Stormbreaker::AxeViolationManager

Inherits:
Object
  • Object
show all
Defined in:
lib/stormbreaker/axe_violation_manager.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeAxeViolationManager

Returns a new instance of AxeViolationManager.



7
8
9
10
# File 'lib/stormbreaker/axe_violation_manager.rb', line 7

def initialize
  self.total_violations = []
  self.test_violations = []
end

Instance Attribute Details

#test_violationsObject

Returns the value of attribute test_violations.



5
6
7
# File 'lib/stormbreaker/axe_violation_manager.rb', line 5

def test_violations
  @test_violations
end

#total_violationsObject

Returns the value of attribute total_violations.



5
6
7
# File 'lib/stormbreaker/axe_violation_manager.rb', line 5

def total_violations
  @total_violations
end

Instance Method Details

#add_failure(called_by: Set.new, violation: nil, node: nil) ⇒ Object



12
13
14
15
16
# File 'lib/stormbreaker/axe_violation_manager.rb', line 12

def add_failure(called_by: Set.new, violation: nil, node: nil)
  axe_violation = get_violation(called_by:, violation:, node:)
  add_failure_to_test(axe_violation)
  add_failure_to_total(axe_violation)
end

#add_failure_to_test(axe_violation) ⇒ Object



33
34
35
# File 'lib/stormbreaker/axe_violation_manager.rb', line 33

def add_failure_to_test(axe_violation)
  @test_violations << axe_violation
end

#add_failure_to_total(axe_violation) ⇒ Object



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

def add_failure_to_total(axe_violation)
  match = @total_violations.select do |v|
    (v.violation == axe_violation.violation && v.element == axe_violation.element)
  end
  raise "Multiple total_violations Found for #{v.description}" if match.count > 1

  if match.any?
    match.first.called_by = match.first.called_by.merge(axe_violation.called_by)
  else
    @total_violations << axe_violation
  end
end

#clear_failures_from_testObject



50
51
52
# File 'lib/stormbreaker/axe_violation_manager.rb', line 50

def clear_failures_from_test
  @test_violations = []
end

#clear_failures_from_totalObject



54
55
56
# File 'lib/stormbreaker/axe_violation_manager.rb', line 54

def clear_failures_from_total
  @total_violations = []
end

#get_violation(called_by: Set.new, violation: nil, node: nil) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/stormbreaker/axe_violation_manager.rb', line 18

def get_violation(called_by: Set.new, violation: nil, node: nil)
  raise "No violation found" unless violation && node

  severity = violation.impact
  element = node.html
  rule = violation.description
  complete_summary = node.failure_messages.flatten.join("\n")

  AxeViolation.new(called_by:,
                   severity:,
                   element:,
                   violation: rule,
                   complete_summary:)
end

#list_test_resultsObject



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/stormbreaker/axe_violation_manager.rb', line 74

def list_test_results
  summary = ""
  prev_violation = ""
  sort_test_violations.each do |violation|
    summary += <<~TEXT
      #{(prev_violation == violation.violation) ? "" : "\n#{violation.violation} - Severity: #{violation.severity}\n"}
        #{violation.complete_summary.gsub("\n", "\n  ")}

        Violations Found In:
          #{violation.called_by.to_a.join("\n")}
    TEXT
    prev_violation = violation.violation
  end
  summary
end

#sort_test_violationsObject



66
67
68
69
70
71
72
# File 'lib/stormbreaker/axe_violation_manager.rb', line 66

def sort_test_violations
  sorted = []
  Stormbreaker.configuration.enabled_severity_categories.each do |severity|
    sorted += @test_violations.select { |v| v.severity == severity }.sort_by(&:violation)
  end
  sorted
end

#summarize_total_results_by_severityObject



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
# File 'lib/stormbreaker/axe_violation_manager.rb', line 90

def summarize_total_results_by_severity
  summary = ""
  error_categories = Stormbreaker.configuration.enabled_severity_categories
  unique_violations = 0

  error_categories.each do |severity|
    violation_map = {}
    @total_violations.select { |v| v.severity == severity }.each do |error|
      if violation_map[error.violation]
        violation_map[error.violation] += error.called_by.count
      else
        unique_violations += 1
        violation_map[error.violation] = error.called_by.count
      end
    end

    next if violation_map.empty?

    summary += "\n\n  Level: #{severity}"
    violation_map.each do |error, count|
      summary += "\n   - #{error} : #{count} instance(s)"
    end
  end
  "\nFound #{unique_violations} unique violation(s)" + summary
end

#test_passed?Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/stormbreaker/axe_violation_manager.rb', line 58

def test_passed?
  @test_violations.empty?
end

#total_passed?Boolean

Returns:

  • (Boolean)


62
63
64
# File 'lib/stormbreaker/axe_violation_manager.rb', line 62

def total_passed?
  @total_violations.empty?
end