1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
tools / add_header_test.py [blame]
#!/usr/bin/env python3
# Copyright 2021 The Chromium Authors
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
import random
import unittest
import add_header
class DecoratedFilenameTest(unittest.TestCase):
def testCHeaderClassification(self):
self.assertTrue(add_header.IsCSystemHeader('<stdlib.h>'))
self.assertFalse(add_header.IsCSystemHeader('<type_traits>'))
self.assertFalse(add_header.IsCSystemHeader('"moo.h"'))
def testCXXHeaderClassification(self):
self.assertFalse(add_header.IsCXXSystemHeader('<stdlib.h>'))
self.assertTrue(add_header.IsCXXSystemHeader('<type_traits>'))
self.assertFalse(add_header.IsCXXSystemHeader('"moo.h"'))
def testUserHeaderClassification(self):
self.assertFalse(add_header.IsUserHeader('<stdlib.h>'))
self.assertFalse(add_header.IsUserHeader('<type_traits>'))
self.assertTrue(add_header.IsUserHeader('"moo.h"'))
def testClassifyHeader(self):
self.assertEqual(add_header.ClassifyHeader('<stdlib.h>'),
add_header._HEADER_TYPE_C_SYSTEM)
self.assertEqual(add_header.ClassifyHeader('<type_traits>'),
add_header._HEADER_TYPE_CXX_SYSTEM)
self.assertEqual(add_header.ClassifyHeader('"moo.h"'),
add_header._HEADER_TYPE_USER)
self.assertEqual(add_header.ClassifyHeader('invalid'),
add_header._HEADER_TYPE_INVALID)
class FindIncludesTest(unittest.TestCase):
def testEmpty(self):
begin, end = add_header.FindIncludes([])
self.assertEqual(begin, -1)
self.assertEqual(end, -1)
def testNoIncludes(self):
begin, end = add_header.FindIncludes(['a'])
self.assertEqual(begin, -1)
self.assertEqual(end, -1)
def testOneInclude(self):
begin, end = add_header.FindIncludes(['#include <algorithm>'])
self.assertEqual(begin, 0)
self.assertEqual(end, 1)
def testIncludeWithInlineComment(self):
begin, end = add_header.FindIncludes(
['#include "moo.h" // TODO: Add more sounds.'])
self.assertEqual(begin, 0)
self.assertEqual(end, 1)
def testNewlinesBetweenIncludes(self):
begin, end = add_header.FindIncludes(
['#include <utility>', '', '#include "moo.h"'])
self.assertEqual(begin, 0)
self.assertEqual(end, 3)
def testCommentsBetweenIncludes(self):
begin, end = add_header.FindIncludes([
'#include <utility>', '// TODO: Add goat support.', '#include "moo.h"'
])
self.assertEqual(begin, 0)
self.assertEqual(end, 3)
def testEmptyLinesNotIncluded(self):
begin, end = add_header.FindIncludes(
['', '#include <utility>', '', '#include "moo.h"', ''])
self.assertEqual(begin, 1)
self.assertEqual(end, 4)
def testCommentsNotIncluded(self):
begin, end = add_header.FindIncludes([
'// Cow module.', '#include <utility>', '// For cow speech synthesis.',
'#include "moo.h"', '// TODO: Add Linux audio support.'
])
self.assertEqual(begin, 1)
self.assertEqual(end, 4)
def testNonIncludesLinesBeforeIncludesIgnored(self):
begin, end = add_header.FindIncludes(
['#ifndef COW_H_', '#define COW_H_', '#include "moo.h"'])
self.assertEqual(begin, 2)
self.assertEqual(end, 3)
def testNonIncludesLinesAfterIncludesTerminates(self):
begin, end = add_header.FindIncludes([
'#include "moo.h"', '#ifndef COW_MESSAGES_H_', '#define COW_MESSAGE_H_'
])
self.assertEqual(begin, 0)
self.assertEqual(end, 1)
class IncludeTest(unittest.TestCase):
def testToSource(self):
self.assertEqual(
add_header.Include('<moo.h>', 'include', [], None).ToSource(),
['#include <moo.h>'])
def testIncludeWithPreambleToSource(self):
self.assertEqual(
add_header.Include('"moo.h"', 'include', ['// preamble'],
None).ToSource(),
['// preamble', '#include "moo.h"'])
def testIncludeWithInlineCommentToSource(self):
self.assertEqual(
add_header.Include('"moo.h"', 'include', [],
' inline comment').ToSource(),
['#include "moo.h" // inline comment'])
def testIncludeWithPreambleAndInlineCommentToSource(self):
# Make sure whitespace is vaguely normalized too.
self.assertEqual(
add_header.Include('"moo.h"', 'include', [
'// preamble with trailing space ',
], ' inline comment with trailing space ').ToSource(), [
'// preamble with trailing space',
'#include "moo.h" // inline comment with trailing space'
])
def testImportToSource(self):
self.assertEqual(
add_header.Include('"moo.h"', 'import', [], None).ToSource(),
['#import "moo.h"'])
class ParseIncludesTest(unittest.TestCase):
def testInvalid(self):
self.assertIsNone(add_header.ParseIncludes(['invalid']))
def testInclude(self):
includes = add_header.ParseIncludes(['#include "moo.h"'])
self.assertEqual(len(includes), 1)
self.assertEqual(includes[0].decorated_name, '"moo.h"')
self.assertEqual(includes[0].directive, 'include')
self.assertEqual(includes[0].preamble, [])
self.assertIsNone(includes[0].inline_comment)
self.assertEqual(includes[0].header_type, add_header._HEADER_TYPE_USER)
self.assertFalse(includes[0].is_primary_header)
def testIncludeSurroundedByWhitespace(self):
includes = add_header.ParseIncludes([' #include "moo.h" '])
self.assertEqual(len(includes), 1)
self.assertEqual(includes[0].decorated_name, '"moo.h"')
self.assertEqual(includes[0].directive, 'include')
self.assertEqual(includes[0].preamble, [])
self.assertIsNone(includes[0].inline_comment)
self.assertEqual(includes[0].header_type, add_header._HEADER_TYPE_USER)
self.assertFalse(includes[0].is_primary_header)
def testImport(self):
includes = add_header.ParseIncludes(['#import "moo.h"'])
self.assertEqual(len(includes), 1)
self.assertEqual(includes[0].decorated_name, '"moo.h"')
self.assertEqual(includes[0].directive, 'import')
self.assertEqual(includes[0].preamble, [])
self.assertIsNone(includes[0].inline_comment)
self.assertEqual(includes[0].header_type, add_header._HEADER_TYPE_USER)
self.assertFalse(includes[0].is_primary_header)
def testIncludeWithPreamble(self):
includes = add_header.ParseIncludes(
['// preamble comment ', '#include "moo.h"'])
self.assertEqual(len(includes), 1)
self.assertEqual(includes[0].decorated_name, '"moo.h"')
self.assertEqual(includes[0].directive, 'include')
self.assertEqual(includes[0].preamble, ['// preamble comment '])
self.assertIsNone(includes[0].inline_comment)
self.assertEqual(includes[0].header_type, add_header._HEADER_TYPE_USER)
self.assertFalse(includes[0].is_primary_header)
def testIncludeWithInvalidPreamble(self):
self.assertIsNone(
add_header.ParseIncludes(['// orphan comment', '', '#include "moo.h"']))
def testIncludeWIthInlineComment(self):
includes = add_header.ParseIncludes(['#include "moo.h"// For SFX '])
self.assertEqual(len(includes), 1)
self.assertEqual(includes[0].decorated_name, '"moo.h"')
self.assertEqual(includes[0].directive, 'include')
self.assertEqual(includes[0].preamble, [])
self.assertEqual(includes[0].inline_comment, ' For SFX ')
self.assertEqual(includes[0].header_type, add_header._HEADER_TYPE_USER)
self.assertFalse(includes[0].is_primary_header)
def testIncludeWithInlineCommentAndPreamble(self):
includes = add_header.ParseIncludes(
['// preamble comment ', '#include "moo.h" // For SFX '])
self.assertEqual(len(includes), 1)
self.assertEqual(includes[0].decorated_name, '"moo.h"')
self.assertEqual(includes[0].directive, 'include')
self.assertEqual(includes[0].preamble, ['// preamble comment '])
self.assertEqual(includes[0].inline_comment, ' For SFX ')
self.assertEqual(includes[0].header_type, add_header._HEADER_TYPE_USER)
self.assertFalse(includes[0].is_primary_header)
def testMultipleIncludes(self):
includes = add_header.ParseIncludes([
'#include <time.h>', '', '#include "moo.h" // For SFX ',
'// TODO: Implement death ray.', '#import "goat.h"'
])
self.assertEqual(len(includes), 3)
self.assertEqual(includes[0].decorated_name, '<time.h>')
self.assertEqual(includes[0].directive, 'include')
self.assertEqual(includes[0].preamble, [])
self.assertIsNone(includes[0].inline_comment)
self.assertEqual(includes[0].header_type, add_header._HEADER_TYPE_C_SYSTEM)
self.assertFalse(includes[0].is_primary_header)
self.assertEqual(includes[1].decorated_name, '"moo.h"')
self.assertEqual(includes[1].directive, 'include')
self.assertEqual(includes[1].preamble, [])
self.assertEqual(includes[1].inline_comment, ' For SFX ')
self.assertEqual(includes[1].header_type, add_header._HEADER_TYPE_USER)
self.assertFalse(includes[1].is_primary_header)
self.assertEqual(includes[2].decorated_name, '"goat.h"')
self.assertEqual(includes[2].directive, 'import')
self.assertEqual(includes[2].preamble, ['// TODO: Implement death ray.'])
self.assertIsNone(includes[2].inline_comment)
self.assertEqual(includes[2].header_type, add_header._HEADER_TYPE_USER)
self.assertFalse(includes[2].is_primary_header)
class MarkPrimaryIncludeTest(unittest.TestCase):
def _extract_primary_name(self, includes):
for include in includes:
if include.is_primary_header:
return include.decorated_name
def testNoOpOnHeader(self):
includes = [add_header.Include('"cow.h"', 'include', [], None)]
add_header.MarkPrimaryInclude(includes, 'cow.h')
self.assertIsNone(self._extract_primary_name(includes))
def testSystemHeaderNotMatched(self):
includes = [add_header.Include('<cow.h>', 'include', [], None)]
add_header.MarkPrimaryInclude(includes, 'cow.cc')
self.assertIsNone(self._extract_primary_name(includes))
def testExactMatch(self):
includes = [
add_header.Include('"cow.h"', 'include', [], None),
add_header.Include('"cow_posix.h"', 'include', [], None),
]
add_header.MarkPrimaryInclude(includes, 'cow.cc')
self.assertEqual(self._extract_primary_name(includes), '"cow.h"')
def testFuzzyMatch(self):
includes = [add_header.Include('"cow.h"', 'include', [], None)]
add_header.MarkPrimaryInclude(includes, 'cow_linux_unittest.cc')
self.assertEqual(self._extract_primary_name(includes), '"cow.h"')
def testFuzzymatchInReverse(self):
includes = [add_header.Include('"cow.h"', 'include', [], None)]
add_header.MarkPrimaryInclude(includes, 'cow_uitest_aura.cc')
self.assertEqual(self._extract_primary_name(includes), '"cow.h"')
def testFuzzyMatchDoesntMatchDifferentSuffixes(self):
includes = [add_header.Include('"cow_posix.h"', 'include', [], None)]
add_header.MarkPrimaryInclude(includes, 'cow_windows.cc')
self.assertIsNone(self._extract_primary_name(includes))
def testMarksMostSpecific(self):
includes = [
add_header.Include('"cow.h"', 'include', [], None),
add_header.Include('"cow_posix.h"', 'include', [], None),
]
add_header.MarkPrimaryInclude(includes, 'cow_posix.cc')
self.assertEqual(self._extract_primary_name(includes), '"cow_posix.h"')
def testFullPathMatch(self):
includes = [add_header.Include('"zfs/impl/cow.h"', 'include', [], None)]
add_header.MarkPrimaryInclude(includes, 'zfs/impl/cow.cc')
self.assertEqual(self._extract_primary_name(includes), '"zfs/impl/cow.h"')
def testTopmostDirectoryDoesNotMatch(self):
includes = [add_header.Include('"animal/impl/cow.h"', 'include', [], None)]
add_header.MarkPrimaryInclude(includes, 'zfs/impl/cow.cc')
self.assertIsNone(self._extract_primary_name(includes))
def testSubstantiallySimilarPaths(self):
includes = [
add_header.Include('"farm/public/animal/cow.h"', 'include', [], None)
]
add_header.MarkPrimaryInclude(includes, 'farm/animal/cow.cc')
self.assertEqual(self._extract_primary_name(includes),
'"farm/public/animal/cow.h"')
def testSubstantiallySimilarPathsAndExactMatch(self):
includes = [
add_header.Include('"ui/gfx/ipc/geometry/gfx_param_traits.h"',
'include', [], None),
add_header.Include('"ui/gfx/ipc/gfx_param_traits.h"', 'include', [],
None),
]
add_header.MarkPrimaryInclude(includes, 'ui/gfx/ipc/gfx_param_traits.cc')
self.assertEqual(self._extract_primary_name(includes),
'"ui/gfx/ipc/gfx_param_traits.h"')
def testNoMatchingSubdirectories(self):
includes = [add_header.Include('"base/zfs/cow.h"', 'include', [], None)]
add_header.MarkPrimaryInclude(includes, 'base/animal/cow.cc')
self.assertIsNone(self._extract_primary_name(includes))
class SerializeIncludesTest(unittest.TestCase):
def testSystemHeaders(self):
source = add_header.SerializeIncludes([
add_header.Include('<stdlib.h>', 'include', [], None),
add_header.Include('<map>', 'include', [], None),
])
self.assertEqual(source, ['#include <stdlib.h>', '', '#include <map>'])
def testUserHeaders(self):
source = add_header.SerializeIncludes([
add_header.Include('"goat.h"', 'include', [], None),
add_header.Include('"moo.h"', 'include', [], None),
])
self.assertEqual(source, ['#include "goat.h"', '#include "moo.h"'])
def testSystemAndUserHeaders(self):
source = add_header.SerializeIncludes([
add_header.Include('<stdlib.h>', 'include', [], None),
add_header.Include('<map>', 'include', [], None),
add_header.Include('"moo.h"', 'include', [], None),
])
self.assertEqual(
source,
['#include <stdlib.h>', '', '#include <map>', '', '#include "moo.h"'])
def testPrimaryAndSystemHeaders(self):
primary_header = add_header.Include('"cow.h"', 'include', [], None)
primary_header.is_primary_header = True
source = add_header.SerializeIncludes([
primary_header,
add_header.Include('<stdlib.h>', 'include', [], None),
add_header.Include('<map>', 'include', [], None),
])
self.assertEqual(
source,
['#include "cow.h"', '', '#include <stdlib.h>', '', '#include <map>'])
def testPrimaryAndUserHeaders(self):
primary_header = add_header.Include('"cow.h"', 'include', [], None)
primary_header.is_primary_header = True
source = add_header.SerializeIncludes([
primary_header,
add_header.Include('"moo.h"', 'include', [], None),
])
self.assertEqual(source, ['#include "cow.h"', '', '#include "moo.h"'])
def testPrimarySystemAndUserHeaders(self):
primary_header = add_header.Include('"cow.h"', 'include', [], None)
primary_header.is_primary_header = True
source = add_header.SerializeIncludes([
primary_header,
add_header.Include('<stdlib.h>', 'include', [], None),
add_header.Include('<map>', 'include', [], None),
add_header.Include('"moo.h"', 'include', [], None),
])
self.assertEqual(source, [
'#include "cow.h"', '', '#include <stdlib.h>', '', '#include <map>', '',
'#include "moo.h"'
])
def testSpecialHeaders(self):
includes = []
primary_header = add_header.Include('"cow.h"', 'include', [], None)
primary_header.is_primary_header = True
includes.append(primary_header)
includes.append(add_header.Include('<objbase.h>', 'include', [], None))
includes.append(add_header.Include('<atlbase.h>', 'include', [], None))
includes.append(add_header.Include('<ole2.h>', 'include', [], None))
includes.append(add_header.Include('<shobjidl.h>', 'include', [], None))
includes.append(add_header.Include('<tchar.h>', 'include', [], None))
includes.append(add_header.Include('<unknwn.h>', 'include', [], None))
includes.append(add_header.Include('<winsock2.h>', 'include', [], None))
includes.append(add_header.Include('<windows.h>', 'include', [], None))
includes.append(add_header.Include('<ws2tcpip.h>', 'include', [], None))
includes.append(add_header.Include('<stddef.h>', 'include', [], None))
includes.append(add_header.Include('<stdio.h>', 'include', [], None))
includes.append(add_header.Include('<string.h>', 'include', [], None))
includes.append(add_header.Include('"moo.h"', 'include', [], None))
random.shuffle(includes)
source = add_header.SerializeIncludes(includes)
self.assertEqual(source, [
'#include "cow.h"', '', '#include <objbase.h>', '#include <atlbase.h>',
'#include <ole2.h>', '#include <shobjidl.h>', '#include <tchar.h>',
'#include <unknwn.h>', '#include <windows.h>', '#include <winsock2.h>',
'#include <ws2tcpip.h>', '#include <stddef.h>', '#include <stdio.h>',
'#include <string.h>', '', '#include "moo.h"'
])
class AddHeaderToSourceTest(unittest.TestCase):
def testAddInclude(self):
source = add_header.AddHeaderToSource(
'cow.cc', '\n'.join([
'// Copyright info here.', '', '#include <utility>',
'// For cow speech synthesis.',
'#include "moo.h" // TODO: Add Linux audio support.',
'#include <time.h>', '#include "cow.h"', 'namespace bovine {', '',
'// TODO: Implement.', '} // namespace bovine'
]), '<memory>')
self.assertEqual(
source, '\n'.join([
'// Copyright info here.', '', '#include "cow.h"', '',
'#include <time.h>', '', '#include <memory>', '#include <utility>',
'', '// For cow speech synthesis.',
'#include "moo.h" // TODO: Add Linux audio support.',
'namespace bovine {', '', '// TODO: Implement.',
'} // namespace bovine', ''
]))
def testAlreadyIncluded(self):
# To make sure the original source is returned unmodified, the input source
# intentionally scrambles the #include order.
source = '\n'.join([
'// Copyright info here.', '', '#include "moo.h"', '#include <utility>',
'#include <memory>', '#include "cow.h"', 'namespace bovine {', '',
'// TODO: Implement.', '} // namespace bovine'
])
self.assertEqual(add_header.AddHeaderToSource('cow.cc', source, '<memory>'),
None)
def testConditionalIncludesLeftALone(self):
# TODO(dcheng): Conditional header handling could probably be more clever.
# But for the moment, this is probably Good Enough.
source = add_header.AddHeaderToSource(
'cow.cc', '\n'.join([
'// Copyright info here.', '', '#include "cow.h"',
'#include <utility>', '// For cow speech synthesis.',
'#include "moo.h" // TODO: Add Linux audio support.',
'#if defined(USE_AURA)', '#include <memory>',
'#endif // defined(USE_AURA)'
]), '<memory>')
self.assertEqual(
source, '\n'.join([
'// Copyright info here.', '', '#include "cow.h"', '',
'#include <memory>', '#include <utility>', '',
'// For cow speech synthesis.',
'#include "moo.h" // TODO: Add Linux audio support.',
'#if defined(USE_AURA)', '#include <memory>',
'#endif // defined(USE_AURA)', ''
]))
def testRemoveInclude(self):
source = add_header.AddHeaderToSource(
'cow.cc',
'\n'.join([
'// Copyright info here.', '', '#include <memory>',
'#include <utility>', '// For cow speech synthesis.',
'#include "moo.h" // TODO: Add Linux audio support.',
'#include <time.h>', '#include "cow.h"', 'namespace bovine {', '',
'// TODO: Implement.', '} // namespace bovine'
]),
'<utility>',
remove=True)
self.assertEqual(
source, '\n'.join([
'// Copyright info here.', '', '#include "cow.h"', '',
'#include <time.h>', '', '#include <memory>', '',
'// For cow speech synthesis.',
'#include "moo.h" // TODO: Add Linux audio support.',
'namespace bovine {', '', '// TODO: Implement.',
'} // namespace bovine', ''
]))
if __name__ == '__main__':
unittest.main()