Telemetry: adding memory tests & solving problems on the go.
[chromium-blink-merge.git] / PRESUBMIT_test.py
blob183ec6e54c839f71ad98827228ce195faa71819e
1 #!/usr/bin/env python
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
4 # found in the LICENSE file.
6 import os
7 import re
8 import unittest
10 import PRESUBMIT
13 class MockInputApi(object):
14 def __init__(self):
15 self.re = re
16 self.os_path = os.path
19 class MockFile(object):
20 def __init__(self, local_path, new_contents):
21 self._local_path = local_path
22 self._new_contents = new_contents
23 self._changed_contents = [(i + 1, l) for i, l in enumerate(new_contents)]
25 def ChangedContents(self):
26 return self._changed_contents
28 def NewContents(self):
29 return self._new_contents
31 def LocalPath(self):
32 return self._local_path
35 class IncludeOrderTest(unittest.TestCase):
36 def testSystemHeaderOrder(self):
37 scope = [(1, '#include <csystem.h>'),
38 (2, '#include <cppsystem>'),
39 (3, '#include "acustom.h"')]
40 all_linenums = [linenum for (linenum, _) in scope]
41 mock_input_api = MockInputApi()
42 warnings = PRESUBMIT._CheckIncludeOrderForScope(scope, mock_input_api,
43 '', all_linenums)
44 self.assertEqual(0, len(warnings))
46 def testSystemHeaderOrderMismatch1(self):
47 scope = [(10, '#include <cppsystem>'),
48 (20, '#include <csystem.h>'),
49 (30, '#include "acustom.h"')]
50 all_linenums = [linenum for (linenum, _) in scope]
51 mock_input_api = MockInputApi()
52 warnings = PRESUBMIT._CheckIncludeOrderForScope(scope, mock_input_api,
53 '', all_linenums)
54 self.assertEqual(1, len(warnings))
55 self.assertTrue('20' in warnings[0])
57 def testSystemHeaderOrderMismatch2(self):
58 scope = [(10, '#include <cppsystem>'),
59 (20, '#include "acustom.h"'),
60 (30, '#include <csystem.h>')]
61 all_linenums = [linenum for (linenum, _) in scope]
62 mock_input_api = MockInputApi()
63 warnings = PRESUBMIT._CheckIncludeOrderForScope(scope, mock_input_api,
64 '', all_linenums)
65 self.assertEqual(1, len(warnings))
66 self.assertTrue('30' in warnings[0])
68 def testSystemHeaderOrderMismatch3(self):
69 scope = [(10, '#include "acustom.h"'),
70 (20, '#include <csystem.h>'),
71 (30, '#include <cppsystem>')]
72 all_linenums = [linenum for (linenum, _) in scope]
73 mock_input_api = MockInputApi()
74 warnings = PRESUBMIT._CheckIncludeOrderForScope(scope, mock_input_api,
75 '', all_linenums)
76 self.assertEqual(2, len(warnings))
77 self.assertTrue('20' in warnings[0])
78 self.assertTrue('30' in warnings[1])
80 def testAlphabeticalOrderMismatch(self):
81 scope = [(10, '#include <csystem.h>'),
82 (15, '#include <bsystem.h>'),
83 (20, '#include <cppsystem>'),
84 (25, '#include <bppsystem>'),
85 (30, '#include "bcustom.h"'),
86 (35, '#include "acustom.h"')]
87 all_linenums = [linenum for (linenum, _) in scope]
88 mock_input_api = MockInputApi()
89 warnings = PRESUBMIT._CheckIncludeOrderForScope(scope, mock_input_api,
90 '', all_linenums)
91 self.assertEqual(3, len(warnings))
92 self.assertTrue('15' in warnings[0])
93 self.assertTrue('25' in warnings[1])
94 self.assertTrue('35' in warnings[2])
96 def testSpecialFirstInclude1(self):
97 mock_input_api = MockInputApi()
98 contents = ['#include "some/path/foo.h"',
99 '#include "a/header.h"']
100 mock_file = MockFile('some/path/foo.cc', contents)
101 warnings = PRESUBMIT._CheckIncludeOrderInFile(
102 mock_input_api, mock_file, True, range(1, len(contents) + 1))
103 self.assertEqual(0, len(warnings))
105 def testSpecialFirstInclude2(self):
106 mock_input_api = MockInputApi()
107 contents = ['#include "some/other/path/foo.h"',
108 '#include "a/header.h"']
109 mock_file = MockFile('some/path/foo.cc', contents)
110 warnings = PRESUBMIT._CheckIncludeOrderInFile(
111 mock_input_api, mock_file, True, range(1, len(contents) + 1))
112 self.assertEqual(0, len(warnings))
114 def testSpecialFirstInclude3(self):
115 mock_input_api = MockInputApi()
116 contents = ['#include "some/path/foo.h"',
117 '#include "a/header.h"']
118 mock_file = MockFile('some/path/foo_platform.cc', contents)
119 warnings = PRESUBMIT._CheckIncludeOrderInFile(
120 mock_input_api, mock_file, True, range(1, len(contents) + 1))
121 self.assertEqual(0, len(warnings))
123 def testSpecialFirstInclude4(self):
124 mock_input_api = MockInputApi()
125 contents = ['#include "some/path/bar.h"',
126 '#include "a/header.h"']
127 mock_file = MockFile('some/path/foo_platform.cc', contents)
128 warnings = PRESUBMIT._CheckIncludeOrderInFile(
129 mock_input_api, mock_file, True, range(1, len(contents) + 1))
130 self.assertEqual(1, len(warnings))
131 self.assertTrue('2' in warnings[0])
133 def testOrderAlreadyWrong(self):
134 scope = [(1, '#include "b.h"'),
135 (2, '#include "a.h"'),
136 (3, '#include "c.h"')]
137 mock_input_api = MockInputApi()
138 warnings = PRESUBMIT._CheckIncludeOrderForScope(scope, mock_input_api,
139 '', [3])
140 self.assertEqual(0, len(warnings))
142 def testConflictAdded1(self):
143 scope = [(1, '#include "a.h"'),
144 (2, '#include "c.h"'),
145 (3, '#include "b.h"')]
146 mock_input_api = MockInputApi()
147 warnings = PRESUBMIT._CheckIncludeOrderForScope(scope, mock_input_api,
148 '', [2])
149 self.assertEqual(1, len(warnings))
150 self.assertTrue('3' in warnings[0])
152 def testConflictAdded2(self):
153 scope = [(1, '#include "c.h"'),
154 (2, '#include "b.h"'),
155 (3, '#include "d.h"')]
156 mock_input_api = MockInputApi()
157 warnings = PRESUBMIT._CheckIncludeOrderForScope(scope, mock_input_api,
158 '', [2])
159 self.assertEqual(1, len(warnings))
160 self.assertTrue('2' in warnings[0])
162 def testIfElifElseEndif(self):
163 mock_input_api = MockInputApi()
164 contents = ['#include "e.h"',
165 '#if foo',
166 '#include "d.h"',
167 '#elif bar',
168 '#include "c.h"',
169 '#else',
170 '#include "b.h"',
171 '#endif',
172 '#include "a.h"']
173 mock_file = MockFile('', contents)
174 warnings = PRESUBMIT._CheckIncludeOrderInFile(
175 mock_input_api, mock_file, True, range(1, len(contents) + 1))
176 self.assertEqual(0, len(warnings))
178 def testSysIncludes(self):
179 # #include <sys/...>'s can appear in any order.
180 mock_input_api = MockInputApi()
181 contents = ['#include <sys/b.h>',
182 '#include <sys/a.h>']
183 mock_file = MockFile('', contents)
184 warnings = PRESUBMIT._CheckIncludeOrderInFile(
185 mock_input_api, mock_file, True, range(1, len(contents) + 1))
186 self.assertEqual(0, len(warnings))
189 class VersionControlerConflictsTest(unittest.TestCase):
190 def testTypicalConflict(self):
191 lines = ['<<<<<<< HEAD',
192 ' base::ScopedTempDir temp_dir_;',
193 '=======',
194 ' ScopedTempDir temp_dir_;',
195 '>>>>>>> master']
196 errors = PRESUBMIT._CheckForVersionControlConflictsInFile(
197 MockInputApi(), MockFile('some/path/foo_platform.cc', lines))
198 self.assertEqual(3, len(errors))
199 self.assertTrue('1' in errors[0])
200 self.assertTrue('3' in errors[1])
201 self.assertTrue('5' in errors[2])
204 if __name__ == '__main__':
205 unittest.main()