From 893efa76e2193b71b65b75a1ea2e3fb913a3db77 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 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@329231 91177308-0d34-0410-b5e6-96231b3b80d8 --- unittests/Format/FormatTest.cpp | 2 ++ unittests/Format/FormatTestComments.cpp | 1 + unittests/Format/FormatTestJS.cpp | 4 ++++ unittests/Format/FormatTestJava.cpp | 1 + unittests/Format/FormatTestObjC.cpp | 1 + unittests/Format/FormatTestProto.cpp | 1 + unittests/Format/FormatTestTextProto.cpp | 1 + 7 files changed, 11 insertions(+) diff --git a/unittests/Format/FormatTest.cpp b/unittests/Format/FormatTest.cpp index 2d20bc0365..4322530467 100644 --- a/unittests/Format/FormatTest.cpp +++ b/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/unittests/Format/FormatTestComments.cpp b/unittests/Format/FormatTestComments.cpp index ed11fbdb1f..005039698b 100644 --- a/unittests/Format/FormatTestComments.cpp +++ b/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/unittests/Format/FormatTestJS.cpp b/unittests/Format/FormatTestJS.cpp index 5fb47a7d32..347118353e 100644 --- a/unittests/Format/FormatTestJS.cpp +++ b/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/unittests/Format/FormatTestJava.cpp b/unittests/Format/FormatTestJava.cpp index 1d471b73ee..36b72fda47 100644 --- a/unittests/Format/FormatTestJava.cpp +++ b/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/unittests/Format/FormatTestObjC.cpp b/unittests/Format/FormatTestObjC.cpp index ed5d1aa256..b54b1c0b63 100644 --- a/unittests/Format/FormatTestObjC.cpp +++ b/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/unittests/Format/FormatTestProto.cpp b/unittests/Format/FormatTestProto.cpp index 66c5d3778d..672fd5914d 100644 --- a/unittests/Format/FormatTestProto.cpp +++ b/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/unittests/Format/FormatTestTextProto.cpp b/unittests/Format/FormatTestTextProto.cpp index f4e09cf9e2..2cd7fb7bdf 100644 --- a/unittests/Format/FormatTestTextProto.cpp +++ b/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)); } -- 2.40.0