OLD | NEW |
(Empty) | |
| 1 #!/usr/bin/env python |
| 2 # Copyright 2015 The Chromium Authors. All rights reserved. |
| 3 # Use of this source code is governed by a BSD-style license that can be |
| 4 # found in the LICENSE file. |
| 5 |
| 6 import html_checker |
| 7 from os import path as os_path |
| 8 import re |
| 9 from sys import path as sys_path |
| 10 import test_util |
| 11 import unittest |
| 12 |
| 13 _HERE = os_path.dirname(os_path.abspath(__file__)) |
| 14 sys_path.append(os_path.join(_HERE, '..', '..', '..', 'tools')) |
| 15 |
| 16 import find_depot_tools # pylint: disable=W0611 |
| 17 from testing_support.super_mox import SuperMoxTestBase |
| 18 |
| 19 |
| 20 class HtmlCheckerTest(SuperMoxTestBase): |
| 21 def setUp(self): |
| 22 SuperMoxTestBase.setUp(self) |
| 23 |
| 24 input_api = self.mox.CreateMockAnything() |
| 25 input_api.re = re |
| 26 output_api = self.mox.CreateMockAnything() |
| 27 self.checker = html_checker.HtmlChecker(input_api, output_api) |
| 28 |
| 29 def ShouldFailCheck(self, line, checker): |
| 30 """Checks that the |checker| flags |line| as a style error.""" |
| 31 error = checker(1, line) |
| 32 self.assertNotEqual('', error, 'Should be flagged as style error: ' + line) |
| 33 highlight = test_util.GetHighlight(line, error).strip() |
| 34 |
| 35 def ShouldPassCheck(self, line, checker): |
| 36 """Checks that the |checker| doesn't flag |line| as a style error.""" |
| 37 error = checker(1, line) |
| 38 self.assertEqual('', error, 'Should not be flagged as style error: ' + line) |
| 39 |
| 40 def testClassesUseDashFormCheckFails(self): |
| 41 lines = [ |
| 42 ' <a class="Foo-bar" href="classBar"> ', |
| 43 '<b class="foo-Bar"> ', |
| 44 '<i class="foo_bar" >', |
| 45 ' <hr class="fooBar"> ', |
| 46 ] |
| 47 for line in lines: |
| 48 self.ShouldFailCheck(line, self.checker.ClassesUseDashFormCheck) |
| 49 |
| 50 def testClassesUseDashFormCheckPasses(self): |
| 51 lines = [ |
| 52 ' class="abc" ', |
| 53 'class="foo-bar"', |
| 54 '<div class="foo-bar" id="classBar"', |
| 55 ] |
| 56 for line in lines: |
| 57 self.ShouldPassCheck(line, self.checker.ClassesUseDashFormCheck) |
| 58 |
| 59 def testDoNotCloseSingleTagsCheckFails(self): |
| 60 lines = [ |
| 61 "<input/>", |
| 62 ' <input id="a" /> ', |
| 63 "<div/>", |
| 64 "<br/>", |
| 65 "<br />", |
| 66 ] |
| 67 for line in lines: |
| 68 self.ShouldFailCheck(line, self.checker.DoNotCloseSingleTagsCheck) |
| 69 |
| 70 def testDoNotCloseSingleTagsCheckPasses(self): |
| 71 lines = [ |
| 72 "<input>", |
| 73 "<link>", |
| 74 "<div></div>", |
| 75 '<input text="/">', |
| 76 ] |
| 77 for line in lines: |
| 78 self.ShouldPassCheck(line, self.checker.DoNotCloseSingleTagsCheck) |
| 79 |
| 80 def testDoNotUseBrElementCheckFails(self): |
| 81 lines = [ |
| 82 " <br>", |
| 83 "<br > ", |
| 84 "<br\>", |
| 85 '<br name="a">', |
| 86 ] |
| 87 for line in lines: |
| 88 self.ShouldFailCheck( |
| 89 line, self.checker.DoNotUseBrElementCheck) |
| 90 |
| 91 def testDoNotUseBrElementCheckPasses(self): |
| 92 lines = [ |
| 93 "br", |
| 94 "br>", |
| 95 "give me a break" |
| 96 ] |
| 97 for line in lines: |
| 98 self.ShouldPassCheck( |
| 99 line, self.checker.DoNotUseBrElementCheck) |
| 100 |
| 101 def testDoNotUseInputTypeButtonCheckFails(self): |
| 102 lines = [ |
| 103 '<input type="button">', |
| 104 ' <input id="a" type="button" >', |
| 105 '<input type="button" id="a"> ', |
| 106 ] |
| 107 for line in lines: |
| 108 self.ShouldFailCheck(line, self.checker.DoNotUseInputTypeButtonCheck) |
| 109 |
| 110 def testDoNotUseInputTypeButtonCheckPasses(self): |
| 111 lines = [ |
| 112 "<input>", |
| 113 '<input type="text">', |
| 114 '<input type="result">', |
| 115 '<input type="submit">', |
| 116 "<button>", |
| 117 '<button type="button">', |
| 118 '<button type="reset">', |
| 119 '<button type="submit">', |
| 120 |
| 121 ] |
| 122 for line in lines: |
| 123 self.ShouldPassCheck(line, self.checker.DoNotUseInputTypeButtonCheck) |
| 124 |
| 125 def testI18nContentJavaScriptCaseCheckFails(self): |
| 126 lines = [ |
| 127 ' i18n-content="foo-bar" ', |
| 128 'i18n-content="foo_bar"', |
| 129 'i18n-content="FooBar"', |
| 130 'i18n-content="_foo"', |
| 131 'i18n-content="foo_"', |
| 132 'i18n-content="-foo"', |
| 133 'i18n-content="foo-"', |
| 134 'i18n-content="Foo"', |
| 135 ] |
| 136 for line in lines: |
| 137 self.ShouldFailCheck(line, self.checker.I18nContentJavaScriptCaseCheck) |
| 138 |
| 139 def testI18nContentJavaScriptCaseCheckPasses(self): |
| 140 lines = [ |
| 141 ' i18n-content="abc" ', |
| 142 'i18n-content="fooBar"', |
| 143 '<div i18n-content="exampleTitle"', |
| 144 ] |
| 145 for line in lines: |
| 146 self.ShouldPassCheck(line, self.checker.I18nContentJavaScriptCaseCheck) |
| 147 |
| 148 def testLabelCheckFails(self): |
| 149 lines = [ |
| 150 ' for="abc"', |
| 151 "for= ", |
| 152 " \tfor= ", |
| 153 " for=" |
| 154 ] |
| 155 for line in lines: |
| 156 self.ShouldFailCheck(line, self.checker.LabelCheck) |
| 157 |
| 158 def testLabelCheckPass(self): |
| 159 lines = [ |
| 160 ' my-for="abc" ', |
| 161 ' myfor="abc" ', |
| 162 " <for", |
| 163 ] |
| 164 for line in lines: |
| 165 self.ShouldPassCheck(line, self.checker.LabelCheck) |
| 166 |
| 167 |
| 168 if __name__ == '__main__': |
| 169 unittest.main() |
OLD | NEW |