OLD | NEW |
---|---|
(Empty) | |
1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
M-A Ruel
2012/11/15 16:49:31
#!/usr/bin/env python
and do a chmod +x PRESUBMIT
marja
2012/11/15 17:48:46
Done.
| |
2 # Use of this source code is governed by a BSD-style license that can be | |
3 # found in the LICENSE file. | |
4 | |
5 import os | |
6 import re | |
7 import unittest | |
8 | |
9 import PRESUBMIT | |
10 | |
11 | |
12 class MockInputApi(object): | |
13 def __init__(self): | |
14 self.re = re | |
15 self.os_path = os.path | |
16 | |
17 | |
18 class MockFile(object): | |
19 def __init__(self, local_path, new_contents): | |
20 self._local_path = local_path | |
21 self._new_contents = new_contents | |
22 | |
23 def NewContents(self): | |
24 return self._new_contents | |
25 | |
26 def LocalPath(self): | |
27 return self._local_path | |
28 | |
29 | |
30 class IncludeOrderTest(unittest.TestCase): | |
31 def testSystemHeaderOrder(self): | |
32 scope = [(1, '#include <csystem.h>'), | |
33 (2, '#include <cppsystem>'), | |
34 (3, '#include "acustom.h"')] | |
35 all_linenums = [linenum for (linenum, _) in scope] | |
36 mock_input_api = MockInputApi() | |
37 warnings = PRESUBMIT._CheckIncludeOrderForScope(scope, mock_input_api, | |
38 '', all_linenums) | |
39 self.assertEqual(0, len(warnings)) | |
40 | |
41 def testSystemHeaderOrderMismatch1(self): | |
42 scope = [(10, '#include <cppsystem>'), | |
43 (20, '#include <csystem.h>'), | |
44 (30, '#include "acustom.h"')] | |
45 all_linenums = [linenum for (linenum, _) in scope] | |
46 mock_input_api = MockInputApi() | |
47 warnings = PRESUBMIT._CheckIncludeOrderForScope(scope, mock_input_api, | |
48 '', all_linenums) | |
49 self.assertEqual(1, len(warnings)) | |
50 self.assertTrue('20' in warnings[0]) | |
51 | |
52 def testSystemHeaderOrderMismatch2(self): | |
53 scope = [(10, '#include <cppsystem>'), | |
54 (20, '#include "acustom.h"'), | |
55 (30, '#include <csystem.h>')] | |
56 all_linenums = [linenum for (linenum, _) in scope] | |
57 mock_input_api = MockInputApi() | |
58 warnings = PRESUBMIT._CheckIncludeOrderForScope(scope, mock_input_api, | |
59 '', all_linenums) | |
60 self.assertEqual(1, len(warnings)) | |
61 self.assertTrue('30' in warnings[0]) | |
62 | |
63 def testSystemHeaderOrderMismatch3(self): | |
64 scope = [(10, '#include "acustom.h"'), | |
65 (20, '#include <csystem.h>'), | |
66 (30, '#include <cppsystem>')] | |
67 all_linenums = [linenum for (linenum, _) in scope] | |
68 mock_input_api = MockInputApi() | |
69 warnings = PRESUBMIT._CheckIncludeOrderForScope(scope, mock_input_api, | |
70 '', all_linenums) | |
71 self.assertEqual(2, len(warnings)) | |
72 self.assertTrue('20' in warnings[0]) | |
73 self.assertTrue('30' in warnings[1]) | |
74 | |
75 def testAlphabeticalOrderMismatch(self): | |
76 scope = [(10, '#include <csystem.h>'), | |
77 (15, '#include <bsystem.h>'), | |
78 (20, '#include <cppsystem>'), | |
79 (25, '#include <bppsystem>'), | |
80 (30, '#include "bcustom.h"'), | |
81 (35, '#include "acustom.h"')] | |
82 all_linenums = [linenum for (linenum, _) in scope] | |
83 mock_input_api = MockInputApi() | |
84 warnings = PRESUBMIT._CheckIncludeOrderForScope(scope, mock_input_api, | |
85 '', all_linenums) | |
86 self.assertEqual(3, len(warnings)) | |
87 self.assertTrue('15' in warnings[0]) | |
88 self.assertTrue('25' in warnings[1]) | |
89 self.assertTrue('35' in warnings[2]) | |
90 | |
91 def testSpecialFirstInclude1(self): | |
92 mock_input_api = MockInputApi() | |
93 contents = ['#include "some/path/foo.h"', | |
94 '#include "a/header.h"'] | |
95 mock_file = MockFile('some/path/foo.cc', contents) | |
96 warnings = PRESUBMIT._CheckIncludeOrderInFile( | |
97 mock_input_api, mock_file, True, range(1, len(contents) + 1)) | |
98 self.assertEqual(0, len(warnings)) | |
99 | |
100 def testSpecialFirstInclude2(self): | |
101 mock_input_api = MockInputApi() | |
102 contents = ['#include "some/other/path/foo.h"', | |
103 '#include "a/header.h"'] | |
104 mock_file = MockFile('some/path/foo.cc', contents) | |
105 warnings = PRESUBMIT._CheckIncludeOrderInFile( | |
106 mock_input_api, mock_file, True, range(1, len(contents) + 1)) | |
107 self.assertEqual(0, len(warnings)) | |
108 | |
109 def testSpecialFirstInclude3(self): | |
110 mock_input_api = MockInputApi() | |
111 contents = ['#include "some/path/foo.h"', | |
112 '#include "a/header.h"'] | |
113 mock_file = MockFile('some/path/foo_platform.cc', contents) | |
114 warnings = PRESUBMIT._CheckIncludeOrderInFile( | |
115 mock_input_api, mock_file, True, range(1, len(contents) + 1)) | |
116 self.assertEqual(0, len(warnings)) | |
117 | |
118 def testSpecialFirstInclude4(self): | |
119 mock_input_api = MockInputApi() | |
120 contents = ['#include "some/path/bar.h"', | |
121 '#include "a/header.h"'] | |
122 mock_file = MockFile('some/path/foo_platform.cc', contents) | |
123 warnings = PRESUBMIT._CheckIncludeOrderInFile( | |
124 mock_input_api, mock_file, True, range(1, len(contents) + 1)) | |
125 self.assertEqual(1, len(warnings)) | |
126 self.assertTrue('2' in warnings[0]) | |
127 | |
128 def testOrderAlreadyWrong(self): | |
129 scope = [(1, '#include "b.h"'), | |
130 (2, '#include "a.h"'), | |
131 (3, '#include "c.h"')] | |
132 mock_input_api = MockInputApi() | |
133 warnings = PRESUBMIT._CheckIncludeOrderForScope(scope, mock_input_api, | |
134 '', [3]) | |
135 self.assertEqual(0, len(warnings)) | |
136 | |
137 def testConflictAdded1(self): | |
138 scope = [(1, '#include "a.h"'), | |
139 (2, '#include "c.h"'), | |
140 (3, '#include "b.h"')] | |
141 mock_input_api = MockInputApi() | |
142 warnings = PRESUBMIT._CheckIncludeOrderForScope(scope, mock_input_api, | |
143 '', [2]) | |
144 self.assertEqual(1, len(warnings)) | |
145 self.assertTrue('3' in warnings[0]) | |
146 | |
147 def testConflictAdded2(self): | |
148 scope = [(1, '#include "c.h"'), | |
149 (2, '#include "b.h"'), | |
150 (3, '#include "d.h"')] | |
151 mock_input_api = MockInputApi() | |
152 warnings = PRESUBMIT._CheckIncludeOrderForScope(scope, mock_input_api, | |
153 '', [2]) | |
154 self.assertEqual(1, len(warnings)) | |
155 self.assertTrue('2' in warnings[0]) | |
156 | |
M-A Ruel
2012/11/15 16:49:31
two lines
marja
2012/11/15 17:48:46
Done.
| |
157 if __name__ == '__main__': | |
158 unittest.main() | |
OLD | NEW |