Skip to content

Commit fa7ddfe

Browse files
committed
introduce rule tests
1 parent 17dd622 commit fa7ddfe

File tree

6 files changed

+92
-31
lines changed

6 files changed

+92
-31
lines changed

docs/rules/accessibility/no-title-attribute-counter.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ Use a `<title>` element instead of the `title` attribute, or an `aria-label`.
2323
<a title="A home for all developers" href="github.com">GitHub</a>
2424
```
2525

26-
```
26+
```erb
2727
<a href="/" title="github.com">GitHub</a>
2828
```
2929

@@ -34,6 +34,6 @@ Use a `<title>` element instead of the `title` attribute, or an `aria-label`.
3434
<p id="description" class="tooltip js-tooltip">A home for all developers</p>
3535
```
3636

37-
```
37+
```erb
3838
<a href="github.com">GitHub</a>
3939
```

lib/erblint-github/linters/custom_helpers.rb

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,19 +31,19 @@ def counter_correct?(processed_source)
3131
offenses_count = @offenses.length
3232

3333
processed_source.parser.ast.descendants(:erb).each do |node|
34-
indicator_node, _, code_node, _ = *node
34+
indicator_node, _, code_node, = *node
3535
indicator = indicator_node&.loc&.source
3636
comment = code_node&.loc&.source&.strip
3737

38-
if indicator == "#" && comment.start_with?("erblint:count") && comment.match(rule_name)
38+
if indicator == "#" && comment.start_with?("erblint:counter") && comment.match(rule_name)
3939
comment_node = node
4040
expected_count = comment.match(/\s(\d+)\s?$/)[1].to_i
4141
end
4242
end
4343

44-
if offenses_count == 0
44+
if offenses_count.zero?
4545
# have to adjust to get `\n` so we delete the whole line
46-
add_offense(processed_source.to_source_range(comment_node.loc.adjust(end_pos: 1)), "Unused erblint:count comment for #{rule_name}", "") if comment_node
46+
add_offense(processed_source.to_source_range(comment_node.loc.adjust(end_pos: 1)), "Unused erblint:counter comment for #{rule_name}", "") if comment_node
4747
return
4848
end
4949

@@ -53,9 +53,7 @@ def counter_correct?(processed_source)
5353
add_offense(processed_source.to_source_range(first_offense.source_range), "#{rule_name}: If you must, add <%# erblint:counter #{rule_name} #{offenses_count} %> to bypass this check.", "<%# erblint:counter #{rule_name} #{offenses_count} %>")
5454
else
5555
clear_offenses
56-
if expected_count != offenses_count
57-
add_offense(processed_source.to_source_range(comment_node.loc), "Incorrect erblint:counter number for #{rule_name}. Expected: #{expected_count}, actual: #{offenses_count}.", "<%# erblint:counter #{rule_name} #{offenses_count} %>")
58-
end
56+
add_offense(processed_source.to_source_range(comment_node.loc), "Incorrect erblint:counter number for #{rule_name}. Expected: #{expected_count}, actual: #{offenses_count}.", "<%# erblint:counter #{rule_name} #{offenses_count} %>") if expected_count != offenses_count
5957
end
6058
end
6159

lib/erblint-github/linters/github/accessibility/no_title_attribute_counter.rb

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,37 +10,30 @@ class NoTitleAttributeCounter < Linter
1010
include ERBLint::Linters::CustomHelpers
1111
include LinterRegistry
1212

13-
MESSAGE = "The title attribute is inaccesible to several groups of users. Please avoid setting unless for an `<iframe>`."
13+
MESSAGE = "The title attribute should never be used unless for an `<iframe>` as it is inaccessible for several groups of users."
1414

1515
def run(processed_source)
1616
tags(processed_source).each do |tag|
17-
next if tag.name != "iframe"
17+
next if tag.name == "iframe"
1818
next if tag.closing?
1919

2020
title = possible_attribute_values(tag, "title")
21-
2221
generate_offense(self.class, processed_source, tag) if title.present?
2322
end
2423

2524
counter_correct?(processed_source)
2625
end
2726

28-
private
29-
30-
def correct_counter(corrector, processed_source, offense)
31-
if processed_source.file_content.include?("erblint:counter DeprecatedInPrimerCounter")
32-
corrector.replace(offense.source_range, offense.context)
33-
else
34-
corrector.insert_before(processed_source.source_buffer.source_range, "#{offense.context}\n")
35-
end
36-
end
37-
3827
def autocorrect(processed_source, offense)
3928
return unless offense.context
40-
29+
4130
lambda do |corrector|
42-
if offense.context.include?("erblint:counter DeprecatedInPrimerCounter")
43-
correct_counter(corrector, processed_source, offense)
31+
if processed_source.file_content.include?("erblint:counter #{simple_class_name}")
32+
# update the counter if exists
33+
corrector.replace(offense.source_range, offense.context)
34+
else
35+
# add comment with counter if none
36+
corrector.insert_before(processed_source.source_buffer.source_range, "#{offense.context}\n")
4437
end
4538
end
4639
end

test/custom_helpers_test.rb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class FakeLinter < ERBLint::Linter
1111

1212
MESSAGE = "Please fix your code."
1313
end
14-
14+
1515
def linter_class
1616
CustomHelpersTest::FakeLinter
1717
end
@@ -43,26 +43,26 @@ def test_rule_disabled_adds_offense_if_disable_comment_is_present_but_no_offense
4343

4444
def test_counter_correct_does_not_add_offense_if_counter_matches_offense_count
4545
@file = <<~HTML
46-
<%# erblint:count CustomHelpersTest::FakeLinter 1 %>
46+
<%# erblint:counter CustomHelpersTest::FakeLinter 1 %>
4747
HTML
4848
@linter.offenses = ["fake offense"]
49-
49+
5050
extended_linter.counter_correct?(processed_source)
5151
assert_empty @linter.offenses
5252
end
5353

5454
def test_counter_correct_add_offense_if_counter_comment_is_unused
5555
@file = <<~HTML
56-
<%# erblint:count CustomHelpersTest::FakeLinter 1 %>
56+
<%# erblint:counter CustomHelpersTest::FakeLinter 1 %>
5757
HTML
5858

5959
extended_linter.counter_correct?(processed_source)
60-
assert_equal "Unused erblint:count comment for CustomHelpersTest::FakeLinter", @linter.offenses.first.message
60+
assert_equal "Unused erblint:counter comment for CustomHelpersTest::FakeLinter", @linter.offenses.first.message
6161
end
6262

6363
def test_counter_correct_add_offense_if_counter_comment_count_is_incorrect
6464
@file = <<~HTML
65-
<%# erblint:count CustomHelpersTest::FakeLinter 2 %>
65+
<%# erblint:counter CustomHelpersTest::FakeLinter 2 %>
6666
HTML
6767
@linter.offenses = ["fake offense"]
6868

test/linter_test_case.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,15 @@ def processed_source
2424
def tags
2525
processed_source.parser.nodes_with_type(:tag).map { |tag_node| BetterHtml::Tree::Tag.from_node(tag_node) }
2626
end
27+
28+
def corrected_content
29+
@corrected_content ||= begin
30+
source = processed_source
31+
32+
@linter.run(source)
33+
corrector = ERBLint::Corrector.new(source, offenses)
34+
35+
corrector.corrected_content
36+
end
37+
end
2738
end
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# frozen_string_literal: true
2+
3+
require "test_helper"
4+
5+
class NoTitleAttributeCounterTest < LinterTestCase
6+
def linter_class
7+
ERBLint::Linters::GitHub::Accessibility::NoTitleAttributeCounter
8+
end
9+
10+
def test_warns_if_element_sets_title_and_has_no_counter_comment
11+
@file = "<img title='octopus'></img>"
12+
@linter.run(processed_source)
13+
14+
assert_equal(2, @linter.offenses.count)
15+
error_messages = @linter.offenses.map(&:message).sort
16+
assert_match(/If you must, add <%# erblint:counter GitHub::Accessibility::NoTitleAttributeCounter 1 %> to bypass this check./, error_messages.first)
17+
assert_match(/The title attribute should never be used unless for an `<iframe>` as it is inaccessible for several groups of users./, error_messages.last)
18+
end
19+
20+
def test_does_not_warns_if_element_sets_title_and_has_correct_counter_comment
21+
@file = <<~ERB
22+
<%# erblint:counter GitHub::Accessibility::NoTitleAttributeCounter 1 %>
23+
<a href="/" title="bad">some website</a>
24+
ERB
25+
@linter.run(processed_source)
26+
27+
assert_equal 0, @linter.offenses.count
28+
end
29+
30+
def test_does_not_warn_if_iframe_sets_title
31+
@file = "<iframe title='Introduction video'></iframe>"
32+
@linter.run(processed_source)
33+
34+
assert_empty @linter.offenses
35+
end
36+
37+
def test_does_not_autocorrect_when_ignores_are_correct
38+
@file = <<~ERB
39+
<%# erblint:counter GitHub::Accessibility::NoTitleAttributeCounter 1 %>
40+
<a href="/" title="bad">some website</a>
41+
ERB
42+
43+
assert_equal @file, corrected_content
44+
end
45+
46+
def test_does_autocorrect_when_ignores_are_not_correct
47+
@file = <<~ERB
48+
<%# erblint:counter GitHub::Accessibility::NoTitleAttributeCounter 3 %>
49+
<a href="/" title="bad">some website</a>
50+
ERB
51+
refute_equal @file, corrected_content
52+
53+
expected_content = <<~ERB
54+
<%# erblint:counter GitHub::Accessibility::NoTitleAttributeCounter 1 %>
55+
<a href="/" title="bad">some website</a>
56+
ERB
57+
assert_equal expected_content, corrected_content
58+
end
59+
end

0 commit comments

Comments
 (0)