Skip to content

Commit

Permalink
[clang-format] In tests, expected code should be format-stable
Browse files Browse the repository at this point in the history
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
  • Loading branch information
mzeren-vmw committed Apr 4, 2018
1 parent a5531f2 commit c9a918c
Show file tree
Hide file tree
Showing 7 changed files with 11 additions and 0 deletions.
2 changes: 2 additions & 0 deletions clang/unittests/Format/FormatTest.cpp
Expand Up @@ -72,6 +72,8 @@ class FormatTest : public ::testing::Test {

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++
Expand Down
1 change: 1 addition & 0 deletions clang/unittests/Format/FormatTestComments.cpp
Expand Up @@ -70,6 +70,7 @@ class FormatTestComments : public ::testing::Test {

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));
}

Expand Down
4 changes: 4 additions & 0 deletions clang/unittests/Format/FormatTestJS.cpp
Expand Up @@ -49,6 +49,8 @@ class FormatTestJS : public ::testing::Test {
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;
}
Expand All @@ -57,6 +59,8 @@ class FormatTestJS : public ::testing::Test {
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;
}
Expand Down
1 change: 1 addition & 0 deletions clang/unittests/Format/FormatTestJava.cpp
Expand Up @@ -46,6 +46,7 @@ class FormatTestJava : public ::testing::Test {
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));
}
};
Expand Down
1 change: 1 addition & 0 deletions clang/unittests/Format/FormatTestObjC.cpp
Expand Up @@ -58,6 +58,7 @@ class FormatTestObjC : public ::testing::Test {
}

void verifyFormat(StringRef Code) {
EXPECT_EQ(Code.str(), format(Code)) << "Expected code is not stable";
EXPECT_EQ(Code.str(), format(test::messUp(Code)));
}

Expand Down
1 change: 1 addition & 0 deletions clang/unittests/Format/FormatTestProto.cpp
Expand Up @@ -38,6 +38,7 @@ class FormatTestProto : public ::testing::Test {
}

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)));
}
};
Expand Down
1 change: 1 addition & 0 deletions clang/unittests/Format/FormatTestTextProto.cpp
Expand Up @@ -36,6 +36,7 @@ class FormatTestTextProto : public ::testing::Test {
}

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));
}

Expand Down

0 comments on commit c9a918c

Please sign in to comment.