| 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, '..', '..', '..', 'build')) | |
| 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 testSingleQuoteCheckFails(self): | |
| 60 lines = [ | |
| 61 """ <a href='classBar'> """, | |
| 62 """<a foo$="bar" href$='classBar'>""", | |
| 63 """<a foo="bar" less="more" href='classBar' kittens="cats">""", | |
| 64 """<a cats href='classBar' dogs>""", | |
| 65 """<a cats\n href='classBat\nclassBaz'\n dogs>""", | |
| 66 ] | |
| 67 for line in lines: | |
| 68 self.ShouldFailCheck(line, self.checker.DoNotUseSingleQuotesCheck) | |
| 69 | |
| 70 def testSingleQuoteCheckPasses(self): | |
| 71 lines = [ | |
| 72 """<b id="super-valid">SO VALID!</b>""", | |
| 73 """<a text$="i ain't got invalid quotes">i don't</a>""", | |
| 74 """<span>[[i18n('blah')]]</span> """, | |
| 75 """<a cats href="classBar" dogs>""", | |
| 76 """<a cats\n href="classBar"\n dogs>""", | |
| 77 ] | |
| 78 for line in lines: | |
| 79 self.ShouldPassCheck(line, self.checker.DoNotUseSingleQuotesCheck) | |
| 80 | |
| 81 def testDoNotCloseSingleTagsCheckFails(self): | |
| 82 lines = [ | |
| 83 "<input/>", | |
| 84 ' <input id="a" /> ', | |
| 85 "<div/>", | |
| 86 "<br/>", | |
| 87 "<br />", | |
| 88 ] | |
| 89 for line in lines: | |
| 90 self.ShouldFailCheck(line, self.checker.DoNotCloseSingleTagsCheck) | |
| 91 | |
| 92 def testDoNotCloseSingleTagsCheckPasses(self): | |
| 93 lines = [ | |
| 94 "<input>", | |
| 95 "<link>", | |
| 96 "<div></div>", | |
| 97 '<input text="/">', | |
| 98 ] | |
| 99 for line in lines: | |
| 100 self.ShouldPassCheck(line, self.checker.DoNotCloseSingleTagsCheck) | |
| 101 | |
| 102 def testDoNotUseBrElementCheckFails(self): | |
| 103 lines = [ | |
| 104 " <br>", | |
| 105 "<br > ", | |
| 106 "<br\>", | |
| 107 '<br name="a">', | |
| 108 ] | |
| 109 for line in lines: | |
| 110 self.ShouldFailCheck( | |
| 111 line, self.checker.DoNotUseBrElementCheck) | |
| 112 | |
| 113 def testDoNotUseBrElementCheckPasses(self): | |
| 114 lines = [ | |
| 115 "br", | |
| 116 "br>", | |
| 117 "give me a break" | |
| 118 ] | |
| 119 for line in lines: | |
| 120 self.ShouldPassCheck( | |
| 121 line, self.checker.DoNotUseBrElementCheck) | |
| 122 | |
| 123 def testDoNotUseInputTypeButtonCheckFails(self): | |
| 124 lines = [ | |
| 125 '<input type="button">', | |
| 126 ' <input id="a" type="button" >', | |
| 127 '<input type="button" id="a"> ', | |
| 128 ] | |
| 129 for line in lines: | |
| 130 self.ShouldFailCheck(line, self.checker.DoNotUseInputTypeButtonCheck) | |
| 131 | |
| 132 def testDoNotUseInputTypeButtonCheckPasses(self): | |
| 133 lines = [ | |
| 134 "<input>", | |
| 135 '<input type="text">', | |
| 136 '<input type="result">', | |
| 137 '<input type="submit">', | |
| 138 "<button>", | |
| 139 '<button type="button">', | |
| 140 '<button type="reset">', | |
| 141 '<button type="submit">', | |
| 142 | |
| 143 ] | |
| 144 for line in lines: | |
| 145 self.ShouldPassCheck(line, self.checker.DoNotUseInputTypeButtonCheck) | |
| 146 | |
| 147 def testI18nContentJavaScriptCaseCheckFails(self): | |
| 148 lines = [ | |
| 149 ' i18n-content="foo-bar" ', | |
| 150 'i18n-content="foo_bar"', | |
| 151 'i18n-content="FooBar"', | |
| 152 'i18n-content="_foo"', | |
| 153 'i18n-content="foo_"', | |
| 154 'i18n-content="-foo"', | |
| 155 'i18n-content="foo-"', | |
| 156 'i18n-content="Foo"', | |
| 157 ] | |
| 158 for line in lines: | |
| 159 self.ShouldFailCheck(line, self.checker.I18nContentJavaScriptCaseCheck) | |
| 160 | |
| 161 def testI18nContentJavaScriptCaseCheckPasses(self): | |
| 162 lines = [ | |
| 163 ' i18n-content="abc" ', | |
| 164 'i18n-content="fooBar"', | |
| 165 'i18n-content="validName" attr="invalidName_"', | |
| 166 '<div i18n-content="exampleTitle"', | |
| 167 ] | |
| 168 for line in lines: | |
| 169 self.ShouldPassCheck(line, self.checker.I18nContentJavaScriptCaseCheck) | |
| 170 | |
| 171 def testLabelCheckFails(self): | |
| 172 lines = [ | |
| 173 ' <label for="abc"', | |
| 174 " <label for= ", | |
| 175 " <label\tfor= ", | |
| 176 ' <label\n blah="1" blee="3"\n for="goop"', | |
| 177 ] | |
| 178 for line in lines: | |
| 179 self.ShouldFailCheck(line, self.checker.LabelCheck) | |
| 180 | |
| 181 def testLabelCheckPasses(self): | |
| 182 lines = [ | |
| 183 ' my-for="abc" ', | |
| 184 ' myfor="abc" ', | |
| 185 " <for", | |
| 186 ' <paper-tooltip for="id-name"', | |
| 187 ] | |
| 188 for line in lines: | |
| 189 self.ShouldPassCheck(line, self.checker.LabelCheck) | |
| 190 | |
| 191 def testQuotePolymerBindingsFails(self): | |
| 192 lines = [ | |
| 193 "<a href=[[blah]]>", | |
| 194 "<div class$=[[class_]]>", | |
| 195 "<settings-checkbox prefs={{prefs}}", | |
| 196 "<paper-button actionable$=[[isActionable_(a,b)]]>", | |
| 197 ] | |
| 198 for line in lines: | |
| 199 self.ShouldFailCheck(line, self.checker.QuotePolymerBindings) | |
| 200 | |
| 201 def testQuotePolymerBindingsPasses(self): | |
| 202 lines = [ | |
| 203 '<a href="[[blah]]">', | |
| 204 '<span id="blah">[[text]]</span>', | |
| 205 '<setting-checkbox prefs="{{prefs}}">', | |
| 206 '<paper-input tab-index="[[tabIndex_]]">', | |
| 207 '<div style="font: [[getFont_(item)]]">', | |
| 208 ] | |
| 209 for line in lines: | |
| 210 self.ShouldPassCheck(line, self.checker.QuotePolymerBindings) | |
| 211 | |
| 212 | |
| 213 if __name__ == '__main__': | |
| 214 unittest.main() | |
| OLD | NEW |