OLD | NEW |
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 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 import js_checker | 6 import js_checker |
7 from os import path as os_path | 7 from os import path as os_path |
8 import re | 8 import re |
9 from sys import path as sys_path | 9 from sys import path as sys_path |
10 import test_util | 10 import test_util |
11 import unittest | 11 import unittest |
12 | 12 |
13 _HERE = os_path.dirname(os_path.abspath(__file__)) | 13 _HERE = os_path.dirname(os_path.abspath(__file__)) |
14 sys_path.append(os_path.join(_HERE, '..', '..', '..', 'build')) | 14 sys_path.append(os_path.join(_HERE, '..', '..', '..', 'build')) |
15 | 15 |
16 import find_depot_tools # pylint: disable=W0611 | 16 import find_depot_tools # pylint: disable=W0611 |
17 from testing_support.super_mox import SuperMoxTestBase | 17 from testing_support.super_mox import SuperMoxTestBase |
18 | 18 |
19 | 19 |
20 class JsCheckerTest(SuperMoxTestBase): | 20 class JsCheckerTest(SuperMoxTestBase): |
21 def setUp(self): | 21 def setUp(self): |
22 SuperMoxTestBase.setUp(self) | 22 SuperMoxTestBase.setUp(self) |
23 | 23 |
24 input_api = self.mox.CreateMockAnything() | 24 input_api = self.mox.CreateMockAnything() |
25 input_api.re = re | 25 input_api.re = re |
26 output_api = self.mox.CreateMockAnything() | 26 output_api = self.mox.CreateMockAnything() |
27 self.checker = js_checker.JSChecker(input_api, output_api) | 27 self.checker = js_checker.JSChecker(input_api, output_api) |
28 | 28 |
| 29 def ShouldFailCommentCheck(self, line): |
| 30 """Checks that uncommented '<if>' and '<include>' are a style error.""" |
| 31 error = self.checker.CommentIfAndIncludeCheck(1, line) |
| 32 self.assertNotEqual('', error, 'Should be flagged as style error: ' + line) |
| 33 highlight = test_util.GetHighlight(line, error).strip() |
| 34 self.assertTrue(highlight.startswith(('<if', '<include'))) |
| 35 |
| 36 def ShouldPassCommentCheck(self, line): |
| 37 """Checks that commented '<if>' and '<include>' are allowed.""" |
| 38 self.assertEqual('', self.checker.CommentIfAndIncludeCheck(1, line), |
| 39 'Should not be flagged as style error: ' + line) |
| 40 |
| 41 def testCommentFails(self): |
| 42 lines = [ |
| 43 '<include src="blah.js">', |
| 44 # Currently, only "// " is accepted (not just "//" or "//\s+") as Python |
| 45 # can't do variable-length lookbehind. |
| 46 '//<include src="blah.js">', |
| 47 '// <include src="blah.js">', |
| 48 ' <include src="blee.js">', |
| 49 ' <if expr="chromeos">', |
| 50 '<if expr="lang == \'de\'">', |
| 51 '//<if expr="bitness == 64">', |
| 52 ] |
| 53 for line in lines: |
| 54 self.ShouldFailCommentCheck(line) |
| 55 |
| 56 def testCommentPasses(self): |
| 57 lines = [ |
| 58 '// <include src="assert.js">', |
| 59 ' // <include src="util.js"/>', |
| 60 '// <if expr="chromeos">', |
| 61 ' // <if expr="not chromeos">', |
| 62 " '<iframe src=blah.html>';", |
| 63 ] |
| 64 for line in lines: |
| 65 self.ShouldPassCommentCheck(line) |
| 66 |
29 def ShouldFailConstCheck(self, line): | 67 def ShouldFailConstCheck(self, line): |
30 """Checks that the 'const' checker flags |line| as a style error.""" | 68 """Checks that the 'const' checker flags |line| as a style error.""" |
31 error = self.checker.ConstCheck(1, line) | 69 error = self.checker.ConstCheck(1, line) |
32 self.assertNotEqual('', error, | 70 self.assertNotEqual('', error, |
33 'Should be flagged as style error: ' + line) | 71 'Should be flagged as style error: ' + line) |
34 self.assertEqual(test_util.GetHighlight(line, error), 'const') | 72 self.assertEqual(test_util.GetHighlight(line, error), 'const') |
35 | 73 |
36 def ShouldPassConstCheck(self, line): | 74 def ShouldPassConstCheck(self, line): |
37 """Checks that the 'const' checker doesn't flag |line| as a style error.""" | 75 """Checks that the 'const' checker doesn't flag |line| as a style error.""" |
38 self.assertEqual('', self.checker.ConstCheck(1, line), | 76 self.assertEqual('', self.checker.ConstCheck(1, line), |
(...skipping 313 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
352 " var SCARE_SMALL_CHILDREN = [", # TODO(dbeam): add @const in | 390 " var SCARE_SMALL_CHILDREN = [", # TODO(dbeam): add @const in |
353 # front of all these vars like | 391 # front of all these vars like |
354 "/** @const */ CONST_VAR = 1;", # this line has (<--). | 392 "/** @const */ CONST_VAR = 1;", # this line has (<--). |
355 ] | 393 ] |
356 for line in lines: | 394 for line in lines: |
357 self.ShouldPassVarNameCheck(line) | 395 self.ShouldPassVarNameCheck(line) |
358 | 396 |
359 | 397 |
360 if __name__ == '__main__': | 398 if __name__ == '__main__': |
361 unittest.main() | 399 unittest.main() |
OLD | NEW |