Roll src/third_party/WebKit f298044:aa8346d (svn 202628:202629)
[chromium-blink-merge.git] / chrome / browser / web_dev_style / html_checker_test.py
blob4eac6cc18feb1219bb66eb77898e8b6d8199ed80
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.
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
13 _HERE = os_path.dirname(os_path.abspath(__file__))
14 sys_path.append(os_path.join(_HERE, '..', '..', '..', 'tools'))
16 import find_depot_tools # pylint: disable=W0611
17 from testing_support.super_mox import SuperMoxTestBase
20 class HtmlCheckerTest(SuperMoxTestBase):
21 def setUp(self):
22 SuperMoxTestBase.setUp(self)
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)
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()
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)
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"> ',
47 for line in lines:
48 self.ShouldFailCheck(line, self.checker.ClassesUseDashFormCheck)
50 def testClassesUseDashFormCheckPasses(self):
51 lines = [
52 ' class="abc" ',
53 'class="foo-bar"',
54 '<div class="foo-bar" id="classBar"',
56 for line in lines:
57 self.ShouldPassCheck(line, self.checker.ClassesUseDashFormCheck)
59 def testDoNotCloseSingleTagsCheckFails(self):
60 lines = [
61 "<input/>",
62 ' <input id="a" /> ',
63 "<div/>",
64 "<br/>",
65 "<br />",
67 for line in lines:
68 self.ShouldFailCheck(line, self.checker.DoNotCloseSingleTagsCheck)
70 def testDoNotCloseSingleTagsCheckPasses(self):
71 lines = [
72 "<input>",
73 "<link>",
74 "<div></div>",
75 '<input text="/">',
77 for line in lines:
78 self.ShouldPassCheck(line, self.checker.DoNotCloseSingleTagsCheck)
80 def testDoNotUseBrElementCheckFails(self):
81 lines = [
82 " <br>",
83 "<br > ",
84 "<br\>",
85 '<br name="a">',
87 for line in lines:
88 self.ShouldFailCheck(
89 line, self.checker.DoNotUseBrElementCheck)
91 def testDoNotUseBrElementCheckPasses(self):
92 lines = [
93 "br",
94 "br>",
95 "give me a break"
97 for line in lines:
98 self.ShouldPassCheck(
99 line, self.checker.DoNotUseBrElementCheck)
101 def testDoNotUseInputTypeButtonCheckFails(self):
102 lines = [
103 '<input type="button">',
104 ' <input id="a" type="button" >',
105 '<input type="button" id="a"> ',
107 for line in lines:
108 self.ShouldFailCheck(line, self.checker.DoNotUseInputTypeButtonCheck)
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">',
122 for line in lines:
123 self.ShouldPassCheck(line, self.checker.DoNotUseInputTypeButtonCheck)
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"',
136 for line in lines:
137 self.ShouldFailCheck(line, self.checker.I18nContentJavaScriptCaseCheck)
139 def testI18nContentJavaScriptCaseCheckPasses(self):
140 lines = [
141 ' i18n-content="abc" ',
142 'i18n-content="fooBar"',
143 'i18n-content="validName" attr="invalidName_"',
144 '<div i18n-content="exampleTitle"',
146 for line in lines:
147 self.ShouldPassCheck(line, self.checker.I18nContentJavaScriptCaseCheck)
149 def testLabelCheckFails(self):
150 lines = [
151 ' for="abc"',
152 "for= ",
153 " \tfor= ",
154 " for="
156 for line in lines:
157 self.ShouldFailCheck(line, self.checker.LabelCheck)
159 def testLabelCheckPass(self):
160 lines = [
161 ' my-for="abc" ',
162 ' myfor="abc" ',
163 " <for",
165 for line in lines:
166 self.ShouldPassCheck(line, self.checker.LabelCheck)
169 if __name__ == '__main__':
170 unittest.main()