OLD | NEW |
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. |
3 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
5 | 5 |
6 """Unit tests for Web Development Style Guide checker.""" | 6 """Unit tests for Web Development Style Guide checker.""" |
7 | 7 |
8 import os | 8 import os |
9 import re | 9 import re |
10 import sys | 10 import sys |
11 import unittest | 11 import unittest |
12 | 12 |
13 test_dir = os.path.dirname(os.path.abspath(__file__)) | 13 test_dir = os.path.dirname(os.path.abspath(__file__)) |
14 sys.path.extend([ | 14 sys.path.extend([ |
15 os.path.normpath(os.path.join(test_dir, '..', '..', 'tools')), | 15 os.path.normpath(os.path.join(test_dir, '..', '..', 'tools')), |
16 os.path.join(test_dir), | 16 os.path.join(test_dir), |
17 ]) | 17 ]) |
18 | 18 |
19 import find_depot_tools # pylint: disable=W0611 | 19 import find_depot_tools # pylint: disable=W0611 |
20 from testing_support.super_mox import SuperMoxTestBase | 20 from testing_support.super_mox import SuperMoxTestBase |
21 from web_dev_style import css_checker, js_checker # pylint: disable=F0401 | 21 from web_dev_style import resource_checker, css_checker, js_checker # pylint: di
sable=F0401 |
| 22 |
| 23 |
| 24 def GetHighlight(line, error): |
| 25 """Returns the substring of |line| that is highlighted in |error|.""" |
| 26 error_lines = error.split('\n') |
| 27 highlight = error_lines[error_lines.index(line) + 1] |
| 28 return ''.join(ch1 for (ch1, ch2) in zip(line, highlight) if ch2 == '^') |
| 29 |
| 30 |
| 31 class ResourceStyleGuideTest(SuperMoxTestBase): |
| 32 def setUp(self): |
| 33 SuperMoxTestBase.setUp(self) |
| 34 |
| 35 input_api = self.mox.CreateMockAnything() |
| 36 input_api.re = re |
| 37 output_api = self.mox.CreateMockAnything() |
| 38 self.checker = resource_checker.ResourceChecker(input_api, output_api) |
| 39 |
| 40 def ShouldFailIncludeCheck(self, line): |
| 41 """Checks that the '</include>' checker flags |line| as a style error.""" |
| 42 error = self.checker.IncludeCheck(1, line) |
| 43 self.assertNotEqual('', error, |
| 44 'Should be flagged as style error: ' + line) |
| 45 self.assertEqual(GetHighlight(line, error), '</include>') |
| 46 |
| 47 def ShouldPassIncludeCheck(self, line): |
| 48 """Checks that the '</include>' checker doesn't flag |line| as an error.""" |
| 49 self.assertEqual('', self.checker.IncludeCheck(1, line), |
| 50 'Should not be flagged as style error: ' + line) |
| 51 |
| 52 def testIncludeFails(self): |
| 53 lines = [ |
| 54 "</include> ", |
| 55 " </include>", |
| 56 " </include> ", |
| 57 ] |
| 58 for line in lines: |
| 59 self.ShouldFailIncludeCheck(line) |
| 60 |
| 61 def testIncludePasses(self): |
| 62 lines = [ |
| 63 '<include src="assert.js">', |
| 64 "<include src='../../assert.js'>", |
| 65 "<i>include src='blah'</i>", |
| 66 "</i>nclude", |
| 67 "</i>include", |
| 68 ] |
| 69 for line in lines: |
| 70 self.ShouldPassIncludeCheck(line) |
22 | 71 |
23 | 72 |
24 class JsStyleGuideTest(SuperMoxTestBase): | 73 class JsStyleGuideTest(SuperMoxTestBase): |
25 def setUp(self): | 74 def setUp(self): |
26 SuperMoxTestBase.setUp(self) | 75 SuperMoxTestBase.setUp(self) |
27 | 76 |
28 input_api = self.mox.CreateMockAnything() | 77 input_api = self.mox.CreateMockAnything() |
29 input_api.re = re | 78 input_api.re = re |
30 output_api = self.mox.CreateMockAnything() | 79 output_api = self.mox.CreateMockAnything() |
31 self.checker = js_checker.JSChecker(input_api, output_api) | 80 self.checker = js_checker.JSChecker(input_api, output_api) |
32 | 81 |
33 def GetHighlight(self, line, error): | |
34 """Returns the substring of |line| that is highlighted in |error|.""" | |
35 error_lines = error.split('\n') | |
36 highlight = error_lines[error_lines.index(line) + 1] | |
37 return ''.join(ch1 for (ch1, ch2) in zip(line, highlight) if ch2 == '^') | |
38 | |
39 def ShouldFailConstCheck(self, line): | 82 def ShouldFailConstCheck(self, line): |
40 """Checks that the 'const' checker flags |line| as a style error.""" | 83 """Checks that the 'const' checker flags |line| as a style error.""" |
41 error = self.checker.ConstCheck(1, line) | 84 error = self.checker.ConstCheck(1, line) |
42 self.assertNotEqual('', error, | 85 self.assertNotEqual('', error, |
43 'Should be flagged as style error: ' + line) | 86 'Should be flagged as style error: ' + line) |
44 self.assertEqual(self.GetHighlight(line, error), 'const') | 87 self.assertEqual(GetHighlight(line, error), 'const') |
45 | 88 |
46 def ShouldPassConstCheck(self, line): | 89 def ShouldPassConstCheck(self, line): |
47 """Checks that the 'const' checker doesn't flag |line| as a style error.""" | 90 """Checks that the 'const' checker doesn't flag |line| as a style error.""" |
48 self.assertEqual('', self.checker.ConstCheck(1, line), | 91 self.assertEqual('', self.checker.ConstCheck(1, line), |
49 'Should not be flagged as style error: ' + line) | 92 'Should not be flagged as style error: ' + line) |
50 | 93 |
51 def testConstFails(self): | 94 def testConstFails(self): |
52 lines = [ | 95 lines = [ |
53 "const foo = 'bar';", | 96 "const foo = 'bar';", |
54 " const bar = 'foo';", | 97 " const bar = 'foo';", |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
93 #"var str = 'a const in the middle';", | 136 #"var str = 'a const in the middle';", |
94 ] | 137 ] |
95 for line in lines: | 138 for line in lines: |
96 self.ShouldPassConstCheck(line) | 139 self.ShouldPassConstCheck(line) |
97 | 140 |
98 def ShouldFailChromeSendCheck(self, line): | 141 def ShouldFailChromeSendCheck(self, line): |
99 """Checks that the 'chrome.send' checker flags |line| as a style error.""" | 142 """Checks that the 'chrome.send' checker flags |line| as a style error.""" |
100 error = self.checker.ChromeSendCheck(1, line) | 143 error = self.checker.ChromeSendCheck(1, line) |
101 self.assertNotEqual('', error, | 144 self.assertNotEqual('', error, |
102 'Should be flagged as style error: ' + line) | 145 'Should be flagged as style error: ' + line) |
103 self.assertEqual(self.GetHighlight(line, error), ', []') | 146 self.assertEqual(GetHighlight(line, error), ', []') |
104 | 147 |
105 def ShouldPassChromeSendCheck(self, line): | 148 def ShouldPassChromeSendCheck(self, line): |
106 """Checks that the 'chrome.send' checker doesn't flag |line| as a style | 149 """Checks that the 'chrome.send' checker doesn't flag |line| as a style |
107 error. | 150 error. |
108 """ | 151 """ |
109 self.assertEqual('', self.checker.ChromeSendCheck(1, line), | 152 self.assertEqual('', self.checker.ChromeSendCheck(1, line), |
110 'Should not be flagged as style error: ' + line) | 153 'Should not be flagged as style error: ' + line) |
111 | 154 |
112 def testChromeSendFails(self): | 155 def testChromeSendFails(self): |
113 lines = [ | 156 lines = [ |
(...skipping 11 matching lines...) Expand all Loading... |
125 " chrome.send('message', constructArgs([]));", | 168 " chrome.send('message', constructArgs([]));", |
126 ] | 169 ] |
127 for line in lines: | 170 for line in lines: |
128 self.ShouldPassChromeSendCheck(line) | 171 self.ShouldPassChromeSendCheck(line) |
129 | 172 |
130 def ShouldFailEndJsDocCommentCheck(self, line): | 173 def ShouldFailEndJsDocCommentCheck(self, line): |
131 """Checks that the **/ checker flags |line| as a style error.""" | 174 """Checks that the **/ checker flags |line| as a style error.""" |
132 error = self.checker.EndJsDocCommentCheck(1, line) | 175 error = self.checker.EndJsDocCommentCheck(1, line) |
133 self.assertNotEqual('', error, | 176 self.assertNotEqual('', error, |
134 'Should be flagged as style error: ' + line) | 177 'Should be flagged as style error: ' + line) |
135 self.assertEqual(self.GetHighlight(line, error), '**/') | 178 self.assertEqual(GetHighlight(line, error), '**/') |
136 | 179 |
137 def ShouldPassEndJsDocCommentCheck(self, line): | 180 def ShouldPassEndJsDocCommentCheck(self, line): |
138 """Checks that the **/ checker doesn't flag |line| as a style error.""" | 181 """Checks that the **/ checker doesn't flag |line| as a style error.""" |
139 self.assertEqual('', self.checker.EndJsDocCommentCheck(1, line), | 182 self.assertEqual('', self.checker.EndJsDocCommentCheck(1, line), |
140 'Should not be flagged as style error: ' + line) | 183 'Should not be flagged as style error: ' + line) |
141 | 184 |
142 def testEndJsDocCommentFails(self): | 185 def testEndJsDocCommentFails(self): |
143 lines = [ | 186 lines = [ |
144 "/** @override **/", | 187 "/** @override **/", |
145 "/** @type {number} @const **/", | 188 "/** @type {number} @const **/", |
(...skipping 14 matching lines...) Expand all Loading... |
160 for line in lines: | 203 for line in lines: |
161 self.ShouldPassEndJsDocCommentCheck(line) | 204 self.ShouldPassEndJsDocCommentCheck(line) |
162 | 205 |
163 def ShouldFailGetElementByIdCheck(self, line): | 206 def ShouldFailGetElementByIdCheck(self, line): |
164 """Checks that the 'getElementById' checker flags |line| as a style | 207 """Checks that the 'getElementById' checker flags |line| as a style |
165 error. | 208 error. |
166 """ | 209 """ |
167 error = self.checker.GetElementByIdCheck(1, line) | 210 error = self.checker.GetElementByIdCheck(1, line) |
168 self.assertNotEqual('', error, | 211 self.assertNotEqual('', error, |
169 'Should be flagged as style error: ' + line) | 212 'Should be flagged as style error: ' + line) |
170 self.assertEqual(self.GetHighlight(line, error), 'document.getElementById') | 213 self.assertEqual(GetHighlight(line, error), 'document.getElementById') |
171 | 214 |
172 def ShouldPassGetElementByIdCheck(self, line): | 215 def ShouldPassGetElementByIdCheck(self, line): |
173 """Checks that the 'getElementById' checker doesn't flag |line| as a style | 216 """Checks that the 'getElementById' checker doesn't flag |line| as a style |
174 error. | 217 error. |
175 """ | 218 """ |
176 self.assertEqual('', self.checker.GetElementByIdCheck(1, line), | 219 self.assertEqual('', self.checker.GetElementByIdCheck(1, line), |
177 'Should not be flagged as style error: ' + line) | 220 'Should not be flagged as style error: ' + line) |
178 | 221 |
179 def testGetElementByIdFails(self): | 222 def testGetElementByIdFails(self): |
180 lines = [ | 223 lines = [ |
(...skipping 19 matching lines...) Expand all Loading... |
200 "if (doc.getElementById('foo').hidden) {", | 243 "if (doc.getElementById('foo').hidden) {", |
201 ] | 244 ] |
202 for line in lines: | 245 for line in lines: |
203 self.ShouldPassGetElementByIdCheck(line) | 246 self.ShouldPassGetElementByIdCheck(line) |
204 | 247 |
205 def ShouldFailInheritDocCheck(self, line): | 248 def ShouldFailInheritDocCheck(self, line): |
206 """Checks that the '@inheritDoc' checker flags |line| as a style error.""" | 249 """Checks that the '@inheritDoc' checker flags |line| as a style error.""" |
207 error = self.checker.InheritDocCheck(1, line) | 250 error = self.checker.InheritDocCheck(1, line) |
208 self.assertNotEqual('', error, | 251 self.assertNotEqual('', error, |
209 msg='Should be flagged as style error: ' + line) | 252 msg='Should be flagged as style error: ' + line) |
210 self.assertEqual(self.GetHighlight(line, error), '@inheritDoc') | 253 self.assertEqual(GetHighlight(line, error), '@inheritDoc') |
211 | 254 |
212 def ShouldPassInheritDocCheck(self, line): | 255 def ShouldPassInheritDocCheck(self, line): |
213 """Checks that the '@inheritDoc' checker doesn't flag |line| as a style | 256 """Checks that the '@inheritDoc' checker doesn't flag |line| as a style |
214 error. | 257 error. |
215 """ | 258 """ |
216 self.assertEqual('', self.checker.InheritDocCheck(1, line), | 259 self.assertEqual('', self.checker.InheritDocCheck(1, line), |
217 msg='Should not be flagged as style error: ' + line) | 260 msg='Should not be flagged as style error: ' + line) |
218 | 261 |
219 def testInheritDocFails(self): | 262 def testInheritDocFails(self): |
220 lines = [ | 263 lines = [ |
(...skipping 13 matching lines...) Expand all Loading... |
234 for line in lines: | 277 for line in lines: |
235 self.ShouldPassInheritDocCheck(line) | 278 self.ShouldPassInheritDocCheck(line) |
236 | 279 |
237 def ShouldFailWrapperTypeCheck(self, line): | 280 def ShouldFailWrapperTypeCheck(self, line): |
238 """Checks that the use of wrapper types (i.e. new Number(), @type {Number}) | 281 """Checks that the use of wrapper types (i.e. new Number(), @type {Number}) |
239 is a style error. | 282 is a style error. |
240 """ | 283 """ |
241 error = self.checker.WrapperTypeCheck(1, line) | 284 error = self.checker.WrapperTypeCheck(1, line) |
242 self.assertNotEqual('', error, | 285 self.assertNotEqual('', error, |
243 msg='Should be flagged as style error: ' + line) | 286 msg='Should be flagged as style error: ' + line) |
244 highlight = self.GetHighlight(line, error) | 287 highlight = GetHighlight(line, error) |
245 self.assertTrue(highlight in ('Boolean', 'Number', 'String')) | 288 self.assertTrue(highlight in ('Boolean', 'Number', 'String')) |
246 | 289 |
247 def ShouldPassWrapperTypeCheck(self, line): | 290 def ShouldPassWrapperTypeCheck(self, line): |
248 """Checks that the wrapper type checker doesn't flag |line| as a style | 291 """Checks that the wrapper type checker doesn't flag |line| as a style |
249 error. | 292 error. |
250 """ | 293 """ |
251 self.assertEqual('', self.checker.WrapperTypeCheck(1, line), | 294 self.assertEqual('', self.checker.WrapperTypeCheck(1, line), |
252 msg='Should not be flagged as style error: ' + line) | 295 msg='Should not be flagged as style error: ' + line) |
253 | 296 |
254 def testWrapperTypePasses(self): | 297 def testWrapperTypePasses(self): |
(...skipping 21 matching lines...) Expand all Loading... |
276 " * @param {number|String}", | 319 " * @param {number|String}", |
277 ] | 320 ] |
278 for line in lines: | 321 for line in lines: |
279 self.ShouldFailWrapperTypeCheck(line) | 322 self.ShouldFailWrapperTypeCheck(line) |
280 | 323 |
281 def ShouldFailVarNameCheck(self, line): | 324 def ShouldFailVarNameCheck(self, line): |
282 """Checks that var unix_hacker, $dollar are style errors.""" | 325 """Checks that var unix_hacker, $dollar are style errors.""" |
283 error = self.checker.VarNameCheck(1, line) | 326 error = self.checker.VarNameCheck(1, line) |
284 self.assertNotEqual('', error, | 327 self.assertNotEqual('', error, |
285 msg='Should be flagged as style error: ' + line) | 328 msg='Should be flagged as style error: ' + line) |
286 highlight = self.GetHighlight(line, error) | 329 highlight = GetHighlight(line, error) |
287 self.assertFalse('var ' in highlight); | 330 self.assertFalse('var ' in highlight); |
288 | 331 |
289 def ShouldPassVarNameCheck(self, line): | 332 def ShouldPassVarNameCheck(self, line): |
290 """Checks that variableNamesLikeThis aren't style errors.""" | 333 """Checks that variableNamesLikeThis aren't style errors.""" |
291 self.assertEqual('', self.checker.VarNameCheck(1, line), | 334 self.assertEqual('', self.checker.VarNameCheck(1, line), |
292 msg='Should not be flagged as style error: ' + line) | 335 msg='Should not be flagged as style error: ' + line) |
293 | 336 |
294 def testVarNameFails(self): | 337 def testVarNameFails(self): |
295 lines = [ | 338 lines = [ |
296 "var private_;", | 339 "var private_;", |
(...skipping 360 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
657 opacity: .0; | 700 opacity: .0; |
658 opacity: 0.0; | 701 opacity: 0.0; |
659 opacity: 0.; | 702 opacity: 0.; |
660 border-width: 0mm; | 703 border-width: 0mm; |
661 height: 0cm; | 704 height: 0cm; |
662 width: 0in; | 705 width: 0in; |
663 """) | 706 """) |
664 | 707 |
665 if __name__ == '__main__': | 708 if __name__ == '__main__': |
666 unittest.main() | 709 unittest.main() |
OLD | NEW |