diff options
author | Daniel Jasper <djasper@google.com> | 2015-09-23 08:30:47 +0000 |
---|---|---|
committer | Daniel Jasper <djasper@google.com> | 2015-09-23 08:30:47 +0000 |
commit | d89ae9d3accb4cb8b7357932c3dfba8f4ae383c4 (patch) | |
tree | ed7b06902aba907fb09f8524a1b1edbe897e69b2 /clang/unittests/Format/SortIncludesTest.cpp | |
parent | 9fcf72ef9bdc047a7ab24d0fc00604cddbd5ddf5 (diff) | |
download | bcm5719-llvm-d89ae9d3accb4cb8b7357932c3dfba8f4ae383c4.tar.gz bcm5719-llvm-d89ae9d3accb4cb8b7357932c3dfba8f4ae383c4.zip |
clang-format: Add initial #include sorting capabilities.
To implement this nicely, add a function that merges two sets of
replacements that are meant to be done in sequence. This functionality
will also be useful for other applications, e.g. formatting the result
of clang-tidy fixes.
llvm-svn: 248367
Diffstat (limited to 'clang/unittests/Format/SortIncludesTest.cpp')
-rw-r--r-- | clang/unittests/Format/SortIncludesTest.cpp | 108 |
1 files changed, 108 insertions, 0 deletions
diff --git a/clang/unittests/Format/SortIncludesTest.cpp b/clang/unittests/Format/SortIncludesTest.cpp new file mode 100644 index 00000000000..cbbc2baad97 --- /dev/null +++ b/clang/unittests/Format/SortIncludesTest.cpp @@ -0,0 +1,108 @@ +//===- unittest/Format/SortIncludesTest.cpp - Include sort unit tests -----===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#include "FormatTestUtils.h" +#include "clang/Format/Format.h" +#include "llvm/Support/Debug.h" +#include "gtest/gtest.h" + +#define DEBUG_TYPE "format-test" + +namespace clang { +namespace format { +namespace { + +class SortIncludesTest : public ::testing::Test { +protected: + std::string sort(llvm::StringRef Code) { + std::vector<tooling::Range> Ranges(1, tooling::Range(0, Code.size())); + std::string Sorted = applyAllReplacements( + Code, sortIncludes(getLLVMStyle(), Code, Ranges, "input.cpp")); + return applyAllReplacements( + Sorted, reformat(getLLVMStyle(), Sorted, Ranges, "input.cpp")); + } +}; + +TEST_F(SortIncludesTest, BasicSorting) { + EXPECT_EQ("#include \"a.h\"\n" + "#include \"b.h\"\n" + "#include \"c.h\"\n", + sort("#include \"a.h\"\n" + "#include \"c.h\"\n" + "#include \"b.h\"\n")); +} + +TEST_F(SortIncludesTest, FixTrailingComments) { + EXPECT_EQ("#include \"a.h\" // comment\n" + "#include \"bb.h\" // comment\n" + "#include \"ccc.h\"\n", + sort("#include \"a.h\" // comment\n" + "#include \"ccc.h\"\n" + "#include \"bb.h\" // comment\n")); +} + +TEST_F(SortIncludesTest, LeadingWhitespace) { + EXPECT_EQ("#include \"a.h\"\n" + "#include \"b.h\"\n" + "#include \"c.h\"\n", + sort(" #include \"a.h\"\n" + " #include \"c.h\"\n" + " #include \"b.h\"\n")); + EXPECT_EQ("#include \"a.h\"\n" + "#include \"b.h\"\n" + "#include \"c.h\"\n", + sort("# include \"a.h\"\n" + "# include \"c.h\"\n" + "# include \"b.h\"\n")); +} + +TEST_F(SortIncludesTest, GreaterInComment) { + EXPECT_EQ("#include \"a.h\"\n" + "#include \"b.h\" // >\n" + "#include \"c.h\"\n", + sort("#include \"a.h\"\n" + "#include \"c.h\"\n" + "#include \"b.h\" // >\n")); +} + +TEST_F(SortIncludesTest, SortsLocallyInEachBlock) { + EXPECT_EQ("#include \"a.h\"\n" + "#include \"c.h\"\n" + "\n" + "#include \"b.h\"\n", + sort("#include \"c.h\"\n" + "#include \"a.h\"\n" + "\n" + "#include \"b.h\"\n")); +} + +TEST_F(SortIncludesTest, HandlesAngledIncludesAsSeparateBlocks) { + EXPECT_EQ("#include <b.h>\n" + "#include <d.h>\n" + "#include \"a.h\"\n" + "#include \"c.h\"\n", + sort("#include <d.h>\n" + "#include <b.h>\n" + "#include \"c.h\"\n" + "#include \"a.h\"\n")); +} + +TEST_F(SortIncludesTest, HandlesMultilineIncludes) { + EXPECT_EQ("#include \"a.h\"\n" + "#include \"b.h\"\n" + "#include \"c.h\"\n", + sort("#include \"a.h\"\n" + "#include \\\n" + "\"c.h\"\n" + "#include \"b.h\"\n")); +} + +} // end namespace +} // end namespace format +} // end namespace clang |