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