diff options
| author | Antonio Maiorano <amaiorano@gmail.com> | 2017-01-17 00:12:27 +0000 |
|---|---|---|
| committer | Antonio Maiorano <amaiorano@gmail.com> | 2017-01-17 00:12:27 +0000 |
| commit | 3adfb6a3eed268a04275334147a57e165ceb5669 (patch) | |
| tree | d9e2df760d8962b208a846fb77da1f30e8edb99e /clang/unittests/Format | |
| parent | 2aab1d45ff6e76c851524b1933666681c6a87fc5 (diff) | |
| download | bcm5719-llvm-3adfb6a3eed268a04275334147a57e165ceb5669.tar.gz bcm5719-llvm-3adfb6a3eed268a04275334147a57e165ceb5669.zip | |
clang-format: Make GetStyle return Expected<FormatStyle> instead of FormatStyle
Change the contract of GetStyle so that it returns an error when an error occurs
(i.e. when it writes to stderr), and only returns the fallback style when it
can't find a configuration file.
Differential Revision: https://reviews.llvm.org/D28081
llvm-svn: 292174
Diffstat (limited to 'clang/unittests/Format')
| -rw-r--r-- | clang/unittests/Format/FormatTest.cpp | 35 | ||||
| -rw-r--r-- | clang/unittests/Format/FormatTestObjC.cpp | 16 |
2 files changed, 42 insertions, 9 deletions
diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp index 9bf7fb060f7..38095402f0c 100644 --- a/clang/unittests/Format/FormatTest.cpp +++ b/clang/unittests/Format/FormatTest.cpp @@ -10975,13 +10975,15 @@ TEST(FormatStyle, GetStyleOfFile) { ASSERT_TRUE( FS.addFile("/a/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;"))); auto Style1 = getStyle("file", "/a/.clang-format", "Google", "", &FS); - ASSERT_EQ(Style1, getLLVMStyle()); + ASSERT_TRUE((bool)Style1); + ASSERT_EQ(*Style1, getLLVMStyle()); // Test 2: fallback to default. ASSERT_TRUE( FS.addFile("/b/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;"))); auto Style2 = getStyle("file", "/b/test.cpp", "Mozilla", "", &FS); - ASSERT_EQ(Style2, getMozillaStyle()); + ASSERT_TRUE((bool)Style2); + ASSERT_EQ(*Style2, getMozillaStyle()); // Test 3: format file in parent directory. ASSERT_TRUE( @@ -10990,7 +10992,34 @@ TEST(FormatStyle, GetStyleOfFile) { ASSERT_TRUE(FS.addFile("/c/sub/sub/sub/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;"))); auto Style3 = getStyle("file", "/c/sub/sub/sub/test.cpp", "LLVM", "", &FS); - ASSERT_EQ(Style3, getGoogleStyle()); + ASSERT_TRUE((bool)Style3); + ASSERT_EQ(*Style3, getGoogleStyle()); + + // Test 4: error on invalid fallback style + auto Style4 = getStyle("file", "a.h", "KungFu", "", &FS); + ASSERT_FALSE((bool)Style4); + llvm::consumeError(Style4.takeError()); + + // Test 5: error on invalid yaml on command line + auto Style5 = getStyle("{invalid_key=invalid_value}", "a.h", "LLVM", "", &FS); + ASSERT_FALSE((bool)Style5); + llvm::consumeError(Style5.takeError()); + + // Test 6: error on invalid style + auto Style6 = getStyle("KungFu", "a.h", "LLVM", "", &FS); + ASSERT_FALSE((bool)Style6); + llvm::consumeError(Style6.takeError()); + + // Test 7: found config file, error on parsing it + ASSERT_TRUE( + FS.addFile("/d/.clang-format", 0, + llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: LLVM\n" + "InvalidKey: InvalidValue"))); + ASSERT_TRUE( + FS.addFile("/d/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;"))); + auto Style7 = getStyle("file", "/d/.clang-format", "LLVM", "", &FS); + ASSERT_FALSE((bool)Style7); + llvm::consumeError(Style7.takeError()); } TEST_F(ReplacementTest, FormatCodeAfterReplacements) { diff --git a/clang/unittests/Format/FormatTestObjC.cpp b/clang/unittests/Format/FormatTestObjC.cpp index 6a530f921e6..680ff92f75e 100644 --- a/clang/unittests/Format/FormatTestObjC.cpp +++ b/clang/unittests/Format/FormatTestObjC.cpp @@ -68,17 +68,21 @@ protected: FormatStyle Style; }; -TEST_F(FormatTestObjC, DetectsObjCInHeaders) { - Style = getStyle("LLVM", "a.h", "none", "@interface\n" - "- (id)init;"); - EXPECT_EQ(FormatStyle::LK_ObjC, Style.Language); +TEST(FormatTestObjCStyle, DetectsObjCInHeaders) { + auto Style = getStyle("LLVM", "a.h", "none", "@interface\n" + "- (id)init;"); + ASSERT_TRUE((bool)Style); + EXPECT_EQ(FormatStyle::LK_ObjC, Style->Language); + Style = getStyle("LLVM", "a.h", "none", "@interface\n" "+ (id)init;"); - EXPECT_EQ(FormatStyle::LK_ObjC, Style.Language); + ASSERT_TRUE((bool)Style); + EXPECT_EQ(FormatStyle::LK_ObjC, Style->Language); // No recognizable ObjC. Style = getStyle("LLVM", "a.h", "none", "void f() {}"); - EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language); + ASSERT_TRUE((bool)Style); + EXPECT_EQ(FormatStyle::LK_Cpp, Style->Language); } TEST_F(FormatTestObjC, FormatObjCTryCatch) { |

