Benjamin Kramer | 6b23626 | 2016-04-20 12:43:43 +0000 | [diff] [blame] | 1 | //===-- IncludeFixerTest.cpp - Include fixer unit tests -------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
Benjamin Kramer | a3d8233 | 2016-05-13 09:27:54 +0000 | [diff] [blame] | 10 | #include "InMemorySymbolIndex.h" |
Benjamin Kramer | 6b23626 | 2016-04-20 12:43:43 +0000 | [diff] [blame] | 11 | #include "IncludeFixer.h" |
Benjamin Kramer | a3d8233 | 2016-05-13 09:27:54 +0000 | [diff] [blame] | 12 | #include "SymbolIndexManager.h" |
Benjamin Kramer | 3a45fab | 2016-04-28 11:21:29 +0000 | [diff] [blame] | 13 | #include "unittests/Tooling/RewriterTestContext.h" |
Benjamin Kramer | 6b23626 | 2016-04-20 12:43:43 +0000 | [diff] [blame] | 14 | #include "clang/Tooling/Tooling.h" |
| 15 | #include "gtest/gtest.h" |
Benjamin Kramer | 6b23626 | 2016-04-20 12:43:43 +0000 | [diff] [blame] | 16 | |
| 17 | namespace clang { |
| 18 | namespace include_fixer { |
| 19 | namespace { |
| 20 | |
Haojian Wu | 631e5f2 | 2016-05-13 15:17:17 +0000 | [diff] [blame] | 21 | using find_all_symbols::SymbolInfo; |
| 22 | |
Benjamin Kramer | 6b23626 | 2016-04-20 12:43:43 +0000 | [diff] [blame] | 23 | static bool runOnCode(tooling::ToolAction *ToolAction, StringRef Code, |
Benjamin Kramer | 3a45fab | 2016-04-28 11:21:29 +0000 | [diff] [blame] | 24 | StringRef FileName, |
| 25 | const std::vector<std::string> &ExtraArgs) { |
Benjamin Kramer | 6b23626 | 2016-04-20 12:43:43 +0000 | [diff] [blame] | 26 | llvm::IntrusiveRefCntPtr<vfs::InMemoryFileSystem> InMemoryFileSystem( |
| 27 | new vfs::InMemoryFileSystem); |
| 28 | llvm::IntrusiveRefCntPtr<FileManager> Files( |
| 29 | new FileManager(FileSystemOptions(), InMemoryFileSystem)); |
Benjamin Kramer | 2ecd090 | 2016-05-18 09:28:45 +0000 | [diff] [blame] | 30 | // FIXME: Investigate why -fms-compatibility breaks tests. |
| 31 | std::vector<std::string> Args = {"include_fixer", "-fsyntax-only", |
| 32 | "-fno-ms-compatibility", FileName}; |
Benjamin Kramer | 3a45fab | 2016-04-28 11:21:29 +0000 | [diff] [blame] | 33 | Args.insert(Args.end(), ExtraArgs.begin(), ExtraArgs.end()); |
Benjamin Kramer | 6b23626 | 2016-04-20 12:43:43 +0000 | [diff] [blame] | 34 | tooling::ToolInvocation Invocation( |
Benjamin Kramer | 3a45fab | 2016-04-28 11:21:29 +0000 | [diff] [blame] | 35 | Args, ToolAction, Files.get(), |
| 36 | std::make_shared<PCHContainerOperations>()); |
Benjamin Kramer | 6b23626 | 2016-04-20 12:43:43 +0000 | [diff] [blame] | 37 | |
| 38 | InMemoryFileSystem->addFile(FileName, 0, |
| 39 | llvm::MemoryBuffer::getMemBuffer(Code)); |
| 40 | |
| 41 | InMemoryFileSystem->addFile("foo.h", 0, |
| 42 | llvm::MemoryBuffer::getMemBuffer("\n")); |
Benjamin Kramer | 3a45fab | 2016-04-28 11:21:29 +0000 | [diff] [blame] | 43 | InMemoryFileSystem->addFile("dir/bar.h", 0, |
| 44 | llvm::MemoryBuffer::getMemBuffer("\n")); |
| 45 | InMemoryFileSystem->addFile("dir/otherdir/qux.h", 0, |
Benjamin Kramer | 6b23626 | 2016-04-20 12:43:43 +0000 | [diff] [blame] | 46 | llvm::MemoryBuffer::getMemBuffer("\n")); |
Haojian Wu | 9c6cb03 | 2016-06-03 11:26:02 +0000 | [diff] [blame] | 47 | InMemoryFileSystem->addFile("header.h", 0, |
| 48 | llvm::MemoryBuffer::getMemBuffer("bar b;")); |
Benjamin Kramer | 6b23626 | 2016-04-20 12:43:43 +0000 | [diff] [blame] | 49 | return Invocation.run(); |
| 50 | } |
| 51 | |
Benjamin Kramer | 3a45fab | 2016-04-28 11:21:29 +0000 | [diff] [blame] | 52 | static std::string runIncludeFixer( |
| 53 | StringRef Code, |
| 54 | const std::vector<std::string> &ExtraArgs = std::vector<std::string>()) { |
Haojian Wu | 631e5f2 | 2016-05-13 15:17:17 +0000 | [diff] [blame] | 55 | std::vector<SymbolInfo> Symbols = { |
| 56 | SymbolInfo("string", SymbolInfo::SymbolKind::Class, "<string>", 1, |
| 57 | {{SymbolInfo::ContextType::Namespace, "std"}}), |
| 58 | SymbolInfo("sting", SymbolInfo::SymbolKind::Class, "\"sting\"", 1, |
| 59 | {{SymbolInfo::ContextType::Namespace, "std"}}), |
Haojian Wu | 631e5f2 | 2016-05-13 15:17:17 +0000 | [diff] [blame] | 60 | SymbolInfo("foo", SymbolInfo::SymbolKind::Class, "\"dir/otherdir/qux.h\"", |
| 61 | 1, {{SymbolInfo::ContextType::Namespace, "b"}, |
| 62 | {SymbolInfo::ContextType::Namespace, "a"}}), |
Eric Liu | a4be83a | 2016-06-13 19:05:07 +0000 | [diff] [blame] | 63 | SymbolInfo("bar", SymbolInfo::SymbolKind::Class, "\"bar.h\"", 1, |
| 64 | {{SymbolInfo::ContextType::Namespace, "b"}, |
| 65 | {SymbolInfo::ContextType::Namespace, "a"}}), |
Haojian Wu | 0c05e2e | 2016-07-14 09:39:12 +0000 | [diff] [blame^] | 66 | SymbolInfo("bar", SymbolInfo::SymbolKind::Class, "\"bar2.h\"", 1, |
| 67 | {{SymbolInfo::ContextType::Namespace, "c"}, |
| 68 | {SymbolInfo::ContextType::Namespace, "a"}}), |
Eric Liu | a4be83a | 2016-06-13 19:05:07 +0000 | [diff] [blame] | 69 | SymbolInfo("Green", SymbolInfo::SymbolKind::Class, "\"color.h\"", 1, |
| 70 | {{SymbolInfo::ContextType::EnumDecl, "Color"}, |
| 71 | {SymbolInfo::ContextType::Namespace, "b"}, |
| 72 | {SymbolInfo::ContextType::Namespace, "a"}}), |
| 73 | SymbolInfo("Vector", SymbolInfo::SymbolKind::Class, "\"Vector.h\"", 1, |
| 74 | {{SymbolInfo::ContextType::Namespace, "__a"}, |
| 75 | {SymbolInfo::ContextType::Namespace, "a"}}, |
| 76 | /*num_occurrences=*/2), |
| 77 | SymbolInfo("Vector", SymbolInfo::SymbolKind::Class, "\"Vector.h\"", 2, |
| 78 | {{SymbolInfo::ContextType::Namespace, "a"}}, |
| 79 | /*num_occurrences=*/1), |
Benjamin Kramer | 3a45fab | 2016-04-28 11:21:29 +0000 | [diff] [blame] | 80 | }; |
Benjamin Kramer | a3d8233 | 2016-05-13 09:27:54 +0000 | [diff] [blame] | 81 | auto SymbolIndexMgr = llvm::make_unique<include_fixer::SymbolIndexManager>(); |
| 82 | SymbolIndexMgr->addSymbolIndex( |
Haojian Wu | 631e5f2 | 2016-05-13 15:17:17 +0000 | [diff] [blame] | 83 | llvm::make_unique<include_fixer::InMemorySymbolIndex>(Symbols)); |
Eric Liu | 692aca6 | 2016-05-04 08:22:35 +0000 | [diff] [blame] | 84 | |
Haojian Wu | 11e9bd2 | 2016-05-31 09:31:51 +0000 | [diff] [blame] | 85 | IncludeFixerContext FixerContext; |
| 86 | IncludeFixerActionFactory Factory(*SymbolIndexMgr, FixerContext, "llvm"); |
| 87 | |
Haojian Wu | 627ca96 | 2016-07-08 09:10:29 +0000 | [diff] [blame] | 88 | std::string FakeFileName = "input.cc"; |
| 89 | runOnCode(&Factory, Code, FakeFileName, ExtraArgs); |
Haojian Wu | 68c34a0 | 2016-07-13 16:43:54 +0000 | [diff] [blame] | 90 | if (FixerContext.getHeaderInfos().empty()) |
Eric Liu | f83187d | 2016-05-31 13:52:59 +0000 | [diff] [blame] | 91 | return Code; |
Eric Liu | a452db4 | 2016-07-11 13:53:21 +0000 | [diff] [blame] | 92 | auto Replaces = clang::include_fixer::createInsertHeaderReplacements( |
Haojian Wu | 68c34a0 | 2016-07-13 16:43:54 +0000 | [diff] [blame] | 93 | Code, FakeFileName, FixerContext.getHeaderInfos().front().Header); |
Eric Liu | a452db4 | 2016-07-11 13:53:21 +0000 | [diff] [blame] | 94 | EXPECT_TRUE(static_cast<bool>(Replaces)) |
| 95 | << llvm::toString(Replaces.takeError()) << "\n"; |
| 96 | if (!Replaces) |
| 97 | return ""; |
Benjamin Kramer | 6b23626 | 2016-04-20 12:43:43 +0000 | [diff] [blame] | 98 | clang::RewriterTestContext Context; |
Haojian Wu | 627ca96 | 2016-07-08 09:10:29 +0000 | [diff] [blame] | 99 | clang::FileID ID = Context.createInMemoryFile(FakeFileName, Code); |
Haojian Wu | 68c34a0 | 2016-07-13 16:43:54 +0000 | [diff] [blame] | 100 | Replaces->insert({FakeFileName, FixerContext.getSymbolRange().getOffset(), |
| 101 | FixerContext.getSymbolRange().getLength(), |
| 102 | FixerContext.getHeaderInfos().front().QualifiedName}); |
Eric Liu | a452db4 | 2016-07-11 13:53:21 +0000 | [diff] [blame] | 103 | clang::tooling::applyAllReplacements(*Replaces, Context.Rewrite); |
Benjamin Kramer | 6b23626 | 2016-04-20 12:43:43 +0000 | [diff] [blame] | 104 | return Context.getRewrittenText(ID); |
| 105 | } |
| 106 | |
| 107 | TEST(IncludeFixer, Typo) { |
Eric Liu | 516f18e | 2016-05-31 14:48:45 +0000 | [diff] [blame] | 108 | EXPECT_EQ("#include <string>\nstd::string foo;\n", |
Benjamin Kramer | 6b23626 | 2016-04-20 12:43:43 +0000 | [diff] [blame] | 109 | runIncludeFixer("std::string foo;\n")); |
| 110 | |
Eric Liu | 516f18e | 2016-05-31 14:48:45 +0000 | [diff] [blame] | 111 | // FIXME: the current version of include-fixer does not get this test case |
| 112 | // right - header should be inserted before definition. |
Benjamin Kramer | 6b23626 | 2016-04-20 12:43:43 +0000 | [diff] [blame] | 113 | EXPECT_EQ( |
Eric Liu | 516f18e | 2016-05-31 14:48:45 +0000 | [diff] [blame] | 114 | "// comment\n#include \"foo.h\"\nstd::string foo;\n" |
| 115 | "#include \"dir/bar.h\"\n#include <string>\n", |
Benjamin Kramer | 6b23626 | 2016-04-20 12:43:43 +0000 | [diff] [blame] | 116 | runIncludeFixer("// comment\n#include \"foo.h\"\nstd::string foo;\n" |
Benjamin Kramer | 3a45fab | 2016-04-28 11:21:29 +0000 | [diff] [blame] | 117 | "#include \"dir/bar.h\"\n")); |
Benjamin Kramer | 6b23626 | 2016-04-20 12:43:43 +0000 | [diff] [blame] | 118 | |
Eric Liu | 702cfd1 | 2016-05-19 08:21:09 +0000 | [diff] [blame] | 119 | EXPECT_EQ("#include \"foo.h\"\n#include <string>\nstd::string foo;\n", |
Benjamin Kramer | 6b23626 | 2016-04-20 12:43:43 +0000 | [diff] [blame] | 120 | runIncludeFixer("#include \"foo.h\"\nstd::string foo;\n")); |
| 121 | |
| 122 | EXPECT_EQ( |
Eric Liu | 702cfd1 | 2016-05-19 08:21:09 +0000 | [diff] [blame] | 123 | "#include \"foo.h\"\n#include <string>\nstd::string::size_type foo;\n", |
Benjamin Kramer | 6b23626 | 2016-04-20 12:43:43 +0000 | [diff] [blame] | 124 | runIncludeFixer("#include \"foo.h\"\nstd::string::size_type foo;\n")); |
| 125 | |
Haojian Wu | 627ca96 | 2016-07-08 09:10:29 +0000 | [diff] [blame] | 126 | EXPECT_EQ("#include <string>\nstd::string foo;\n", |
Eric Liu | 692aca6 | 2016-05-04 08:22:35 +0000 | [diff] [blame] | 127 | runIncludeFixer("string foo;\n")); |
Benjamin Kramer | 9b15b6f | 2016-05-19 12:41:56 +0000 | [diff] [blame] | 128 | |
Benjamin Kramer | 9b15b6f | 2016-05-19 12:41:56 +0000 | [diff] [blame] | 129 | // Should not match std::string. |
| 130 | EXPECT_EQ("::string foo;\n", runIncludeFixer("::string foo;\n")); |
Benjamin Kramer | 6b23626 | 2016-04-20 12:43:43 +0000 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | TEST(IncludeFixer, IncompleteType) { |
| 134 | EXPECT_EQ( |
Eric Liu | 702cfd1 | 2016-05-19 08:21:09 +0000 | [diff] [blame] | 135 | "#include \"foo.h\"\n#include <string>\n" |
Haojian Wu | 627ca96 | 2016-07-08 09:10:29 +0000 | [diff] [blame] | 136 | "namespace std {\nclass string;\n}\nstd::string foo;\n", |
Benjamin Kramer | 6b23626 | 2016-04-20 12:43:43 +0000 | [diff] [blame] | 137 | runIncludeFixer("#include \"foo.h\"\n" |
| 138 | "namespace std {\nclass string;\n}\nstring foo;\n")); |
| 139 | } |
| 140 | |
Benjamin Kramer | 3a45fab | 2016-04-28 11:21:29 +0000 | [diff] [blame] | 141 | TEST(IncludeFixer, MinimizeInclude) { |
| 142 | std::vector<std::string> IncludePath = {"-Idir/"}; |
Eric Liu | 516f18e | 2016-05-31 14:48:45 +0000 | [diff] [blame] | 143 | EXPECT_EQ("#include \"otherdir/qux.h\"\na::b::foo bar;\n", |
Benjamin Kramer | 3a45fab | 2016-04-28 11:21:29 +0000 | [diff] [blame] | 144 | runIncludeFixer("a::b::foo bar;\n", IncludePath)); |
| 145 | |
| 146 | IncludePath = {"-isystemdir"}; |
Eric Liu | 516f18e | 2016-05-31 14:48:45 +0000 | [diff] [blame] | 147 | EXPECT_EQ("#include <otherdir/qux.h>\na::b::foo bar;\n", |
Benjamin Kramer | 3a45fab | 2016-04-28 11:21:29 +0000 | [diff] [blame] | 148 | runIncludeFixer("a::b::foo bar;\n", IncludePath)); |
| 149 | |
| 150 | IncludePath = {"-iquotedir"}; |
Eric Liu | 516f18e | 2016-05-31 14:48:45 +0000 | [diff] [blame] | 151 | EXPECT_EQ("#include \"otherdir/qux.h\"\na::b::foo bar;\n", |
Benjamin Kramer | 3a45fab | 2016-04-28 11:21:29 +0000 | [diff] [blame] | 152 | runIncludeFixer("a::b::foo bar;\n", IncludePath)); |
| 153 | |
| 154 | IncludePath = {"-Idir", "-Idir/otherdir"}; |
Eric Liu | 516f18e | 2016-05-31 14:48:45 +0000 | [diff] [blame] | 155 | EXPECT_EQ("#include \"qux.h\"\na::b::foo bar;\n", |
Benjamin Kramer | 3a45fab | 2016-04-28 11:21:29 +0000 | [diff] [blame] | 156 | runIncludeFixer("a::b::foo bar;\n", IncludePath)); |
| 157 | } |
| 158 | |
Benjamin Kramer | ad93500 | 2016-05-10 08:25:31 +0000 | [diff] [blame] | 159 | TEST(IncludeFixer, NestedName) { |
Eric Liu | 516f18e | 2016-05-31 14:48:45 +0000 | [diff] [blame] | 160 | EXPECT_EQ("#include \"dir/otherdir/qux.h\"\n" |
Benjamin Kramer | af34e06 | 2016-05-17 12:35:18 +0000 | [diff] [blame] | 161 | "int x = a::b::foo(0);\n", |
Benjamin Kramer | 04666a2 | 2016-05-19 11:39:56 +0000 | [diff] [blame] | 162 | runIncludeFixer("int x = a::b::foo(0);\n")); |
Benjamin Kramer | af34e06 | 2016-05-17 12:35:18 +0000 | [diff] [blame] | 163 | |
| 164 | // FIXME: Handle simple macros. |
| 165 | EXPECT_EQ("#define FOO a::b::foo\nint x = FOO;\n", |
| 166 | runIncludeFixer("#define FOO a::b::foo\nint x = FOO;\n")); |
| 167 | EXPECT_EQ("#define FOO(x) a::##x\nint x = FOO(b::foo);\n", |
| 168 | runIncludeFixer("#define FOO(x) a::##x\nint x = FOO(b::foo);\n")); |
| 169 | |
Eric Liu | 516f18e | 2016-05-31 14:48:45 +0000 | [diff] [blame] | 170 | // The empty namespace is cleaned up by clang-format after include-fixer |
| 171 | // finishes. |
| 172 | EXPECT_EQ("#include \"dir/otherdir/qux.h\"\n" |
| 173 | "\nint a = a::b::foo(0);\n", |
Benjamin Kramer | 04666a2 | 2016-05-19 11:39:56 +0000 | [diff] [blame] | 174 | runIncludeFixer("namespace a {}\nint a = a::b::foo(0);\n")); |
Benjamin Kramer | ad93500 | 2016-05-10 08:25:31 +0000 | [diff] [blame] | 175 | } |
| 176 | |
Benjamin Kramer | c3459a5 | 2016-05-10 08:25:28 +0000 | [diff] [blame] | 177 | TEST(IncludeFixer, MultipleMissingSymbols) { |
Eric Liu | 516f18e | 2016-05-31 14:48:45 +0000 | [diff] [blame] | 178 | EXPECT_EQ("#include <string>\nstd::string bar;\nstd::sting foo;\n", |
Benjamin Kramer | c3459a5 | 2016-05-10 08:25:28 +0000 | [diff] [blame] | 179 | runIncludeFixer("std::string bar;\nstd::sting foo;\n")); |
| 180 | } |
| 181 | |
Haojian Wu | 57cdcb0 | 2016-05-13 15:44:16 +0000 | [diff] [blame] | 182 | TEST(IncludeFixer, ScopedNamespaceSymbols) { |
Eric Liu | 516f18e | 2016-05-31 14:48:45 +0000 | [diff] [blame] | 183 | EXPECT_EQ("#include \"bar.h\"\nnamespace a {\nb::bar b;\n}", |
Eric Liu | 702cfd1 | 2016-05-19 08:21:09 +0000 | [diff] [blame] | 184 | runIncludeFixer("namespace a {\nb::bar b;\n}")); |
Eric Liu | 516f18e | 2016-05-31 14:48:45 +0000 | [diff] [blame] | 185 | EXPECT_EQ("#include \"bar.h\"\nnamespace A {\na::b::bar b;\n}", |
Eric Liu | 702cfd1 | 2016-05-19 08:21:09 +0000 | [diff] [blame] | 186 | runIncludeFixer("namespace A {\na::b::bar b;\n}")); |
Eric Liu | 516f18e | 2016-05-31 14:48:45 +0000 | [diff] [blame] | 187 | EXPECT_EQ("#include \"bar.h\"\nnamespace a {\nvoid func() { b::bar b; }\n}", |
Eric Liu | 702cfd1 | 2016-05-19 08:21:09 +0000 | [diff] [blame] | 188 | runIncludeFixer("namespace a {\nvoid func() { b::bar b; }\n}")); |
Haojian Wu | 57cdcb0 | 2016-05-13 15:44:16 +0000 | [diff] [blame] | 189 | EXPECT_EQ("namespace A { c::b::bar b; }\n", |
| 190 | runIncludeFixer("namespace A { c::b::bar b; }\n")); |
| 191 | // FIXME: The header should not be added here. Remove this after we support |
| 192 | // full match. |
Haojian Wu | 627ca96 | 2016-07-08 09:10:29 +0000 | [diff] [blame] | 193 | EXPECT_EQ("#include \"bar.h\"\nnamespace A {\na::b::bar b;\n}", |
Eric Liu | 702cfd1 | 2016-05-19 08:21:09 +0000 | [diff] [blame] | 194 | runIncludeFixer("namespace A {\nb::bar b;\n}")); |
Haojian Wu | 57cdcb0 | 2016-05-13 15:44:16 +0000 | [diff] [blame] | 195 | } |
Haojian Wu | ff6d195 | 2016-05-18 09:04:43 +0000 | [diff] [blame] | 196 | |
| 197 | TEST(IncludeFixer, EnumConstantSymbols) { |
Eric Liu | 516f18e | 2016-05-31 14:48:45 +0000 | [diff] [blame] | 198 | EXPECT_EQ("#include \"color.h\"\nint test = a::b::Green;\n", |
Haojian Wu | ff6d195 | 2016-05-18 09:04:43 +0000 | [diff] [blame] | 199 | runIncludeFixer("int test = a::b::Green;\n")); |
| 200 | } |
| 201 | |
Haojian Wu | 9c6cb03 | 2016-06-03 11:26:02 +0000 | [diff] [blame] | 202 | TEST(IncludeFixer, IgnoreSymbolFromHeader) { |
| 203 | std::string Code = "#include \"header.h\""; |
| 204 | EXPECT_EQ(Code, runIncludeFixer(Code)); |
| 205 | } |
| 206 | |
Eric Liu | 702cfd1 | 2016-05-19 08:21:09 +0000 | [diff] [blame] | 207 | // FIXME: add test cases for inserting and sorting multiple headers when |
| 208 | // include-fixer supports multiple headers insertion. |
| 209 | TEST(IncludeFixer, InsertAndSortSingleHeader) { |
| 210 | // Insert one header. |
| 211 | std::string Code = "#include \"a.h\"\n" |
| 212 | "#include \"foo.h\"\n" |
| 213 | "\n" |
| 214 | "namespace a { b::bar b; }"; |
| 215 | std::string Expected = "#include \"a.h\"\n" |
| 216 | "#include \"bar.h\"\n" |
| 217 | "#include \"foo.h\"\n" |
| 218 | "\n" |
| 219 | "namespace a { b::bar b; }"; |
| 220 | EXPECT_EQ(Expected, runIncludeFixer(Code)); |
| 221 | } |
| 222 | |
Eric Liu | a4be83a | 2016-06-13 19:05:07 +0000 | [diff] [blame] | 223 | TEST(IncludeFixer, DoNotDeleteMatchedSymbol) { |
| 224 | EXPECT_EQ("#include \"Vector.h\"\na::Vector v;", |
| 225 | runIncludeFixer("a::Vector v;")); |
| 226 | } |
| 227 | |
Haojian Wu | 627ca96 | 2016-07-08 09:10:29 +0000 | [diff] [blame] | 228 | TEST(IncludeFixer, FixNamespaceQualifiers) { |
| 229 | EXPECT_EQ("#include \"bar.h\"\na::b::bar b;\n", |
| 230 | runIncludeFixer("b::bar b;\n")); |
| 231 | EXPECT_EQ("#include \"bar.h\"\na::b::bar b;\n", |
| 232 | runIncludeFixer("a::b::bar b;\n")); |
| 233 | EXPECT_EQ("#include \"bar.h\"\na::b::bar b;\n", |
| 234 | runIncludeFixer("bar b;\n")); |
| 235 | EXPECT_EQ("#include \"bar.h\"\nnamespace a {\nb::bar b;\n}\n", |
| 236 | runIncludeFixer("namespace a {\nb::bar b;\n}\n")); |
| 237 | EXPECT_EQ("#include \"bar.h\"\nnamespace a {\nb::bar b;\n}\n", |
| 238 | runIncludeFixer("namespace a {\nbar b;\n}\n")); |
| 239 | EXPECT_EQ("#include \"bar.h\"\nnamespace a {\nnamespace b{\nbar b;\n}\n}\n", |
| 240 | runIncludeFixer("namespace a {\nnamespace b{\nbar b;\n}\n}\n")); |
| 241 | EXPECT_EQ("c::b::bar b;\n", |
| 242 | runIncludeFixer("c::b::bar b;\n")); |
Haojian Wu | 0c05e2e | 2016-07-14 09:39:12 +0000 | [diff] [blame^] | 243 | EXPECT_EQ("#include \"bar.h\"\nnamespace d {\na::b::bar b;\n}\n", |
| 244 | runIncludeFixer("namespace d {\nbar b;\n}\n")); |
| 245 | EXPECT_EQ("#include \"bar2.h\"\nnamespace c {\na::c::bar b;\n}\n", |
Haojian Wu | 627ca96 | 2016-07-08 09:10:29 +0000 | [diff] [blame] | 246 | runIncludeFixer("namespace c {\nbar b;\n}\n")); |
| 247 | |
| 248 | // Test nested classes. |
Haojian Wu | 0c05e2e | 2016-07-14 09:39:12 +0000 | [diff] [blame^] | 249 | EXPECT_EQ("#include \"bar.h\"\nnamespace d {\na::b::bar::t b;\n}\n", |
| 250 | runIncludeFixer("namespace d {\nbar::t b;\n}\n")); |
| 251 | EXPECT_EQ("#include \"bar2.h\"\nnamespace c {\na::c::bar::t b;\n}\n", |
Haojian Wu | 627ca96 | 2016-07-08 09:10:29 +0000 | [diff] [blame] | 252 | runIncludeFixer("namespace c {\nbar::t b;\n}\n")); |
| 253 | EXPECT_EQ("#include \"bar.h\"\nnamespace a {\nb::bar::t b;\n}\n", |
| 254 | runIncludeFixer("namespace a {\nbar::t b;\n}\n")); |
| 255 | |
Haojian Wu | 5d9482d | 2016-07-08 14:28:43 +0000 | [diff] [blame] | 256 | EXPECT_EQ("#include \"color.h\"\nint test = a::b::Green;\n", |
| 257 | runIncludeFixer("int test = Green;\n")); |
Haojian Wu | 627ca96 | 2016-07-08 09:10:29 +0000 | [diff] [blame] | 258 | EXPECT_EQ("#include \"color.h\"\nnamespace d {\nint test = a::b::Green;\n}\n", |
| 259 | runIncludeFixer("namespace d {\nint test = Green;\n}\n")); |
| 260 | EXPECT_EQ("#include \"color.h\"\nnamespace a {\nint test = b::Green;\n}\n", |
| 261 | runIncludeFixer("namespace a {\nint test = Green;\n}\n")); |
| 262 | |
Haojian Wu | 5d9482d | 2016-07-08 14:28:43 +0000 | [diff] [blame] | 263 | // Test global scope operator. |
| 264 | EXPECT_EQ("#include \"bar.h\"\n::a::b::bar b;\n", |
| 265 | runIncludeFixer("::a::b::bar b;\n")); |
| 266 | EXPECT_EQ("#include \"bar.h\"\nnamespace a {\n::a::b::bar b;\n}\n", |
| 267 | runIncludeFixer("namespace a {\n::a::b::bar b;\n}\n")); |
Haojian Wu | 627ca96 | 2016-07-08 09:10:29 +0000 | [diff] [blame] | 268 | } |
| 269 | |
Benjamin Kramer | 6b23626 | 2016-04-20 12:43:43 +0000 | [diff] [blame] | 270 | } // namespace |
| 271 | } // namespace include_fixer |
| 272 | } // namespace clang |