|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require "test_helper" |
| 4 | +require "erblint-github/linters/custom_helpers" |
| 5 | + |
| 6 | +class CustomHelpersTest < LinterTestCase |
| 7 | + include ERBLint::Linters::CustomHelpers |
| 8 | + |
| 9 | + class FakeLinter < ERBLint::Linter |
| 10 | + attr_accessor :offenses |
| 11 | + |
| 12 | + MESSAGE = "Please fix your code." |
| 13 | + end |
| 14 | + |
| 15 | + def linter_class |
| 16 | + CustomHelpersTest::FakeLinter |
| 17 | + end |
| 18 | + |
| 19 | + def extended_linter |
| 20 | + @linter.extend(ERBLint::Linters::CustomHelpers) |
| 21 | + end |
| 22 | + |
| 23 | + def test_rule_disabled_clears_offenses_if_rule_is_disabled |
| 24 | + @file = <<~HTML |
| 25 | + <%# erblint:disable CustomHelpersTest::FakeLinter %> |
| 26 | + HTML |
| 27 | + @linter.offenses = ["fake offense"] |
| 28 | + assert_equal @linter.offenses.length, 1 |
| 29 | + |
| 30 | + extended_linter.rule_disabled?(processed_source) |
| 31 | + assert_empty @linter.offenses |
| 32 | + end |
| 33 | + |
| 34 | + def test_rule_disabled_adds_offense_if_disable_comment_is_present_but_no_offense |
| 35 | + @file = <<~HTML |
| 36 | + <%# erblint:disable CustomHelpersTest::FakeLinter %> |
| 37 | + HTML |
| 38 | + assert_empty @linter.offenses |
| 39 | + |
| 40 | + extended_linter.rule_disabled?(processed_source) |
| 41 | + |
| 42 | + assert_equal @linter.offenses.length, 1 |
| 43 | + end |
| 44 | + |
| 45 | + def test_generate_offense_with_message_defined_in_linter_class |
| 46 | + @file = <<~HTML |
| 47 | + <%# erblint:disable CustomHelpersTest::FakeLinter %> |
| 48 | + <div id="fake-div-id">Hello.</div> |
| 49 | + HTML |
| 50 | + assert_empty @linter.offenses |
| 51 | + |
| 52 | + tag = extended_linter.tags(processed_source).first |
| 53 | + extended_linter.generate_offense(CustomHelpersTest::FakeLinter, processed_source, tag) |
| 54 | + |
| 55 | + assert_equal @linter.offenses.length, 1 |
| 56 | + assert_match CustomHelpersTest::FakeLinter::MESSAGE, @linter.offenses.first.message |
| 57 | + end |
| 58 | + |
| 59 | + def test_generate_offense_with_message_passed_in_as_parameter |
| 60 | + @file = <<~HTML |
| 61 | + <div id="fake-div-id">Hello.</div> |
| 62 | + HTML |
| 63 | + assert_empty @linter.offenses |
| 64 | + |
| 65 | + tag = extended_linter.tags(processed_source).first |
| 66 | + extended_linter.generate_offense(CustomHelpersTest::FakeLinter, processed_source, tag, "bad!") |
| 67 | + |
| 68 | + assert_equal @linter.offenses.length, 1 |
| 69 | + assert_match(/bad!/, @linter.offenses.first.message) |
| 70 | + end |
| 71 | + |
| 72 | + def test_possible_attribute_values_returns_attribute_value_matches |
| 73 | + @file = <<~HTML |
| 74 | + <img alt="mona lisa"> |
| 75 | + <button type="button"></button> |
| 76 | + <a></a> |
| 77 | + HTML |
| 78 | + |
| 79 | + image_tag = extended_linter.tags(processed_source).first |
| 80 | + image_tag_alt_values = extended_linter.possible_attribute_values(image_tag, "alt") |
| 81 | + assert_equal image_tag_alt_values, ["mona lisa"] |
| 82 | + |
| 83 | + button_tag = extended_linter.tags(processed_source)[1] |
| 84 | + button_tag_type_values = extended_linter.possible_attribute_values(button_tag, "type") |
| 85 | + assert_equal button_tag_type_values, ["button"] |
| 86 | + end |
| 87 | + |
| 88 | + def test_possible_attribute_values_returns_empty_array_if_no_attribute_value_matches |
| 89 | + @file = <<~HTML |
| 90 | + <a></a> |
| 91 | + HTML |
| 92 | + |
| 93 | + anchor_tag = extended_linter.tags(processed_source).first |
| 94 | + possible_attribute_values = extended_linter.possible_attribute_values(anchor_tag, "href") |
| 95 | + assert_equal possible_attribute_values, [] |
| 96 | + end |
| 97 | +end |
0 commit comments