diff options
Diffstat (limited to 'clang/unittests/Format/FormatTest.cpp')
| -rw-r--r-- | clang/unittests/Format/FormatTest.cpp | 64 |
1 files changed, 64 insertions, 0 deletions
diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp index bb344d4..7160c7a 100644 --- a/clang/unittests/Format/FormatTest.cpp +++ b/clang/unittests/Format/FormatTest.cpp @@ -21579,6 +21579,70 @@ TEST(FormatStyle, GetStyleOfFile) { Style.IndentWidth = 7; return Style; }()); + + // Test 9.9: use inheritance from a specific config file. + Style9 = getStyle("file:/e/sub/sub/.clang-format", "/e/sub/sub/code.cpp", + "none", "", &FS); + ASSERT_TRUE(static_cast<bool>(Style9)); + ASSERT_EQ(*Style9, SubSubStyle); +} + +TEST(FormatStyle, GetStyleOfSpecificFile) { + llvm::vfs::InMemoryFileSystem FS; + // Specify absolute path to a format file in a parent directory. + ASSERT_TRUE( + FS.addFile("/e/.clang-format", 0, + llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: LLVM"))); + ASSERT_TRUE( + FS.addFile("/e/explicit.clang-format", 0, + llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: Google"))); + ASSERT_TRUE(FS.addFile("/e/sub/sub/sub/test.cpp", 0, + llvm::MemoryBuffer::getMemBuffer("int i;"))); + auto Style = getStyle("file:/e/explicit.clang-format", + "/e/sub/sub/sub/test.cpp", "LLVM", "", &FS); + ASSERT_TRUE(static_cast<bool>(Style)); + ASSERT_EQ(*Style, getGoogleStyle()); + + // Specify relative path to a format file. + ASSERT_TRUE( + FS.addFile("../../e/explicit.clang-format", 0, + llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: Google"))); + Style = getStyle("file:../../e/explicit.clang-format", + "/e/sub/sub/sub/test.cpp", "LLVM", "", &FS); + ASSERT_TRUE(static_cast<bool>(Style)); + ASSERT_EQ(*Style, getGoogleStyle()); + + // Specify path to a format file that does not exist. + Style = getStyle("file:/e/missing.clang-format", "/e/sub/sub/sub/test.cpp", + "LLVM", "", &FS); + ASSERT_FALSE(static_cast<bool>(Style)); + llvm::consumeError(Style.takeError()); + + // Specify path to a file on the filesystem. + SmallString<128> FormatFilePath; + std::error_code ECF = llvm::sys::fs::createTemporaryFile( + "FormatFileTest", "tpl", FormatFilePath); + EXPECT_FALSE((bool)ECF); + llvm::raw_fd_ostream FormatFileTest(FormatFilePath, ECF); + EXPECT_FALSE((bool)ECF); + FormatFileTest << "BasedOnStyle: Google\n"; + FormatFileTest.close(); + + SmallString<128> TestFilePath; + std::error_code ECT = + llvm::sys::fs::createTemporaryFile("CodeFileTest", "cc", TestFilePath); + EXPECT_FALSE((bool)ECT); + llvm::raw_fd_ostream CodeFileTest(TestFilePath, ECT); + CodeFileTest << "int i;\n"; + CodeFileTest.close(); + + std::string format_file_arg = std::string("file:") + FormatFilePath.c_str(); + Style = getStyle(format_file_arg, TestFilePath, "LLVM", "", nullptr); + + llvm::sys::fs::remove(FormatFilePath.c_str()); + llvm::sys::fs::remove(TestFilePath.c_str()); + ASSERT_TRUE(static_cast<bool>(Style)); + ASSERT_EQ(*Style, getGoogleStyle()); } TEST_F(ReplacementTest, FormatCodeAfterReplacements) { |
