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