OLD | NEW |
| (Empty) |
1 #!/usr/bin/env python | |
2 # | |
3 # Copyright 2012 The Closure Linter Authors. All Rights Reserved. | |
4 # Licensed under the Apache License, Version 2.0 (the "License"); | |
5 # you may not use this file except in compliance with the License. | |
6 # You may obtain a copy of the License at | |
7 # | |
8 # http://www.apache.org/licenses/LICENSE-2.0 | |
9 # | |
10 # Unless required by applicable law or agreed to in writing, software | |
11 # distributed under the License is distributed on an "AS-IS" BASIS, | |
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
13 # See the License for the specific language governing permissions and | |
14 # limitations under the License. | |
15 | |
16 """Unit tests for the error_fixer module.""" | |
17 | |
18 # Allow non-Google copyright | |
19 # pylint: disable=g-bad-file-header | |
20 | |
21 | |
22 | |
23 import unittest as googletest | |
24 from closure_linter import error_fixer | |
25 from closure_linter import testutil | |
26 from closure_linter import tokenutil | |
27 | |
28 | |
29 class ErrorFixerTest(googletest.TestCase): | |
30 """Unit tests for error_fixer.""" | |
31 | |
32 def setUp(self): | |
33 self.error_fixer = error_fixer.ErrorFixer() | |
34 | |
35 def testDeleteToken(self): | |
36 start_token = testutil.TokenizeSourceAndRunEcmaPass(_TEST_SCRIPT) | |
37 second_token = start_token.next | |
38 self.error_fixer.HandleFile('test_file', start_token) | |
39 | |
40 self.error_fixer._DeleteToken(start_token) | |
41 | |
42 self.assertEqual(second_token, self.error_fixer._file_token) | |
43 | |
44 def testDeleteTokens(self): | |
45 start_token = testutil.TokenizeSourceAndRunEcmaPass(_TEST_SCRIPT) | |
46 fourth_token = start_token.next.next.next | |
47 self.error_fixer.HandleFile('test_file', start_token) | |
48 | |
49 self.error_fixer._DeleteTokens(start_token, 3) | |
50 | |
51 self.assertEqual(fourth_token, self.error_fixer._file_token) | |
52 | |
53 def DoTestFixJsDocPipeNull(self, expected, original): | |
54 _, comments = testutil.ParseFunctionsAndComments( | |
55 '/** @param {%s} */' % original) | |
56 jstype = comments[0].GetDocFlags()[0].jstype | |
57 self.error_fixer.HandleFile('unittest', None) | |
58 self.error_fixer._FixJsDocPipeNull(jstype) | |
59 self.assertEquals(expected, repr(jstype)) | |
60 result = tokenutil.TokensToString(jstype.FirstToken()).strip('} */') | |
61 self.assertEquals(expected, result) | |
62 | |
63 def testFixJsDocPipeNull(self): | |
64 self.DoTestFixJsDocPipeNull('?Object', 'Object|null') | |
65 self.DoTestFixJsDocPipeNull('function(?Object)', 'function(Object|null)') | |
66 self.DoTestFixJsDocPipeNull('function(?Object=)', | |
67 'function(Object|null=)') | |
68 self.DoTestFixJsDocPipeNull( | |
69 'function(?(Object)=,null=,?(Object)=):string', | |
70 'function((Object|null)=,null=,(Object|null)=):string') | |
71 | |
72 _TEST_SCRIPT = """\ | |
73 var x = 3; | |
74 """ | |
75 | |
76 if __name__ == '__main__': | |
77 googletest.main() | |
OLD | NEW |