From c9a918c560aa2f524815dfb4ec68c3b24f3b211a Mon Sep 17 00:00:00 2001 From: Mark Zeren Date: Wed, 4 Apr 2018 21:09:00 +0000 Subject: [PATCH] [clang-format] In tests, expected code should be format-stable Summary: Extend various verifyFormat helper functions to check that the expected text is "stable". This provides some protection against bugs where formatting results are ocilating between two forms, or continually change in some other way. Testing Done: * Ran unit tests. * Reproduced a known instability in preprocessor indentation which was caught by this new check. Reviewers: krasimir Subscribers: cfe-commits Differential Revision: https://reviews.llvm.org/D42034 llvm-svn: 329231 --- clang/unittests/Format/FormatTest.cpp | 2 ++ clang/unittests/Format/FormatTestComments.cpp | 1 + clang/unittests/Format/FormatTestJS.cpp | 4 ++++ clang/unittests/Format/FormatTestJava.cpp | 1 + clang/unittests/Format/FormatTestObjC.cpp | 1 + clang/unittests/Format/FormatTestProto.cpp | 1 + clang/unittests/Format/FormatTestTextProto.cpp | 1 + 7 files changed, 11 insertions(+) diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp index 2d20bc03651c..43225304670f 100644 --- a/clang/unittests/Format/FormatTest.cpp +++ b/clang/unittests/Format/FormatTest.cpp @@ -72,6 +72,8 @@ protected: void verifyFormat(llvm::StringRef Expected, llvm::StringRef Code, const FormatStyle &Style = getLLVMStyle()) { + EXPECT_EQ(Expected.str(), format(Expected, Style)) + << "Expected code is not stable"; EXPECT_EQ(Expected.str(), format(Code, Style)); if (Style.Language == FormatStyle::LK_Cpp) { // Objective-C++ is a superset of C++, so everything checked for C++ diff --git a/clang/unittests/Format/FormatTestComments.cpp b/clang/unittests/Format/FormatTestComments.cpp index ed11fbdb1fc0..005039698bba 100644 --- a/clang/unittests/Format/FormatTestComments.cpp +++ b/clang/unittests/Format/FormatTestComments.cpp @@ -70,6 +70,7 @@ protected: void verifyFormat(llvm::StringRef Code, const FormatStyle &Style = getLLVMStyle()) { + EXPECT_EQ(Code.str(), format(Code, Style)) << "Expected code is not stable"; EXPECT_EQ(Code.str(), format(test::messUp(Code), Style)); } diff --git a/clang/unittests/Format/FormatTestJS.cpp b/clang/unittests/Format/FormatTestJS.cpp index 5fb47a7d32f0..347118353e36 100644 --- a/clang/unittests/Format/FormatTestJS.cpp +++ b/clang/unittests/Format/FormatTestJS.cpp @@ -49,6 +49,8 @@ protected: static void verifyFormat( llvm::StringRef Code, const FormatStyle &Style = getGoogleStyle(FormatStyle::LK_JavaScript)) { + EXPECT_EQ(Code.str(), format(Code, Style)) + << "Expected code is not stable"; std::string Result = format(test::messUp(Code), Style); EXPECT_EQ(Code.str(), Result) << "Formatted:\n" << Result; } @@ -57,6 +59,8 @@ protected: llvm::StringRef Expected, llvm::StringRef Code, const FormatStyle &Style = getGoogleStyle(FormatStyle::LK_JavaScript)) { + EXPECT_EQ(Expected.str(), format(Expected, Style)) + << "Expected code is not stable"; std::string Result = format(Code, Style); EXPECT_EQ(Expected.str(), Result) << "Formatted:\n" << Result; } diff --git a/clang/unittests/Format/FormatTestJava.cpp b/clang/unittests/Format/FormatTestJava.cpp index 1d471b73ee64..36b72fda475f 100644 --- a/clang/unittests/Format/FormatTestJava.cpp +++ b/clang/unittests/Format/FormatTestJava.cpp @@ -46,6 +46,7 @@ protected: static void verifyFormat( llvm::StringRef Code, const FormatStyle &Style = getGoogleStyle(FormatStyle::LK_Java)) { + EXPECT_EQ(Code.str(), format(Code, Style)) << "Expected code is not stable"; EXPECT_EQ(Code.str(), format(test::messUp(Code), Style)); } }; diff --git a/clang/unittests/Format/FormatTestObjC.cpp b/clang/unittests/Format/FormatTestObjC.cpp index ed5d1aa25609..b54b1c0b63db 100644 --- a/clang/unittests/Format/FormatTestObjC.cpp +++ b/clang/unittests/Format/FormatTestObjC.cpp @@ -58,6 +58,7 @@ protected: } void verifyFormat(StringRef Code) { + EXPECT_EQ(Code.str(), format(Code)) << "Expected code is not stable"; EXPECT_EQ(Code.str(), format(test::messUp(Code))); } diff --git a/clang/unittests/Format/FormatTestProto.cpp b/clang/unittests/Format/FormatTestProto.cpp index 66c5d3778d4a..672fd5914d07 100644 --- a/clang/unittests/Format/FormatTestProto.cpp +++ b/clang/unittests/Format/FormatTestProto.cpp @@ -38,6 +38,7 @@ protected: } static void verifyFormat(llvm::StringRef Code) { + EXPECT_EQ(Code.str(), format(Code)) << "Expected code is not stable"; EXPECT_EQ(Code.str(), format(test::messUp(Code))); } }; diff --git a/clang/unittests/Format/FormatTestTextProto.cpp b/clang/unittests/Format/FormatTestTextProto.cpp index f4e09cf9e27b..2cd7fb7bdf30 100644 --- a/clang/unittests/Format/FormatTestTextProto.cpp +++ b/clang/unittests/Format/FormatTestTextProto.cpp @@ -36,6 +36,7 @@ protected: } static void verifyFormat(llvm::StringRef Code, const FormatStyle &Style) { + EXPECT_EQ(Code.str(), format(Code, Style)) << "Expected code is not stable"; EXPECT_EQ(Code.str(), format(test::messUp(Code), Style)); }