summaryrefslogtreecommitdiffstats
path: root/clang/unittests
diff options
context:
space:
mode:
authorEric Liu <ioeric@google.com>2016-07-11 13:53:12 +0000
committerEric Liu <ioeric@google.com>2016-07-11 13:53:12 +0000
commit4f8d99433da80161f85988052f96bc7dc621c4eb (patch)
treee926d386edbf05c0d8346483a53ac49678e2f284 /clang/unittests
parent4d8500396454334975ab8bb6766d6521c5140b13 (diff)
downloadbcm5719-llvm-4f8d99433da80161f85988052f96bc7dc621c4eb.tar.gz
bcm5719-llvm-4f8d99433da80161f85988052f96bc7dc621c4eb.zip
Make tooling::applyAllReplacements return llvm::Expected<string> instead of empty string to indicate potential error.
Summary: return llvm::Expected<> to carry error status and error information. This is the first step towards introducing "Error" into tooling::Replacements. Reviewers: djasper, klimek Subscribers: ioeric, klimek, cfe-commits Differential Revision: http://reviews.llvm.org/D21601 llvm-svn: 275062
Diffstat (limited to 'clang/unittests')
-rw-r--r--clang/unittests/Format/CleanupTest.cpp28
-rw-r--r--clang/unittests/Format/FormatTest.cpp24
-rw-r--r--clang/unittests/Format/FormatTestJS.cpp8
-rw-r--r--clang/unittests/Format/FormatTestJava.cpp8
-rw-r--r--clang/unittests/Format/FormatTestProto.cpp8
-rw-r--r--clang/unittests/Format/FormatTestSelective.cpp8
-rw-r--r--clang/unittests/Format/SortImportsTestJS.cpp9
-rw-r--r--clang/unittests/Format/SortIncludesTest.cpp9
-rw-r--r--clang/unittests/Tooling/RefactoringTest.cpp29
-rw-r--r--clang/unittests/Tooling/RewriterTest.cpp5
10 files changed, 83 insertions, 53 deletions
diff --git a/clang/unittests/Format/CleanupTest.cpp b/clang/unittests/Format/CleanupTest.cpp
index f51e3acf9ac..5f85c53b780 100644
--- a/clang/unittests/Format/CleanupTest.cpp
+++ b/clang/unittests/Format/CleanupTest.cpp
@@ -25,9 +25,9 @@ protected:
const FormatStyle &Style = getLLVMStyle()) {
tooling::Replacements Replaces = format::cleanup(Style, Code, Ranges);
- std::string Result = applyAllReplacements(Code, Replaces);
- EXPECT_NE("", Result);
- return Result;
+ auto Result = applyAllReplacements(Code, Replaces);
+ EXPECT_TRUE(static_cast<bool>(Result));
+ return *Result;
}
};
@@ -254,16 +254,26 @@ protected:
inline std::string apply(StringRef Code,
const tooling::Replacements Replaces) {
- return applyAllReplacements(
- Code, cleanupAroundReplacements(Code, Replaces, Style));
+ auto CleanReplaces = cleanupAroundReplacements(Code, Replaces, Style);
+ EXPECT_TRUE(static_cast<bool>(CleanReplaces))
+ << llvm::toString(CleanReplaces.takeError()) << "\n";
+ auto Result = applyAllReplacements(Code, *CleanReplaces);
+ EXPECT_TRUE(static_cast<bool>(Result));
+ return *Result;
}
inline std::string formatAndApply(StringRef Code,
const tooling::Replacements Replaces) {
- return applyAllReplacements(
- Code,
- formatReplacements(
- Code, cleanupAroundReplacements(Code, Replaces, Style), Style));
+
+ auto CleanReplaces = cleanupAroundReplacements(Code, Replaces, Style);
+ EXPECT_TRUE(static_cast<bool>(CleanReplaces))
+ << llvm::toString(CleanReplaces.takeError()) << "\n";
+ auto FormattedReplaces = formatReplacements(Code, *CleanReplaces, Style);
+ EXPECT_TRUE(static_cast<bool>(FormattedReplaces))
+ << llvm::toString(FormattedReplaces.takeError()) << "\n";
+ auto Result = applyAllReplacements(Code, *FormattedReplaces);
+ EXPECT_TRUE(static_cast<bool>(Result));
+ return *Result;
}
int getOffset(StringRef Code, int Line, int Column) {
diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index 218243380c3..8d46ba6efcf 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -47,10 +47,10 @@ protected:
EXPECT_EQ(ExpectedIncompleteFormat, IncompleteFormat) << Code << "\n\n";
}
ReplacementCount = Replaces.size();
- std::string Result = applyAllReplacements(Code, Replaces);
- EXPECT_NE("", Result);
- DEBUG(llvm::errs() << "\n" << Result << "\n\n");
- return Result;
+ auto Result = applyAllReplacements(Code, Replaces);
+ EXPECT_TRUE(static_cast<bool>(Result));
+ DEBUG(llvm::errs() << "\n" << *Result << "\n\n");
+ return *Result;
}
FormatStyle getLLVMStyleWithColumns(unsigned ColumnLimit) {
@@ -11553,8 +11553,12 @@ TEST_F(ReplacementTest, FormatCodeAfterReplacements) {
format::FormatStyle Style = format::getLLVMStyle();
Style.ColumnLimit = 20; // Set column limit to 20 to increase readibility.
- EXPECT_EQ(Expected, applyAllReplacements(
- Code, formatReplacements(Code, Replaces, Style)));
+ auto FormattedReplaces = formatReplacements(Code, Replaces, Style);
+ EXPECT_TRUE(static_cast<bool>(FormattedReplaces))
+ << llvm::toString(FormattedReplaces.takeError()) << "\n";
+ auto Result = applyAllReplacements(Code, *FormattedReplaces);
+ EXPECT_TRUE(static_cast<bool>(Result));
+ EXPECT_EQ(Expected, *Result);
}
TEST_F(ReplacementTest, SortIncludesAfterReplacement) {
@@ -11578,8 +11582,12 @@ TEST_F(ReplacementTest, SortIncludesAfterReplacement) {
format::FormatStyle Style = format::getLLVMStyle();
Style.SortIncludes = true;
- auto FinalReplaces = formatReplacements(Code, Replaces, Style);
- EXPECT_EQ(Expected, applyAllReplacements(Code, FinalReplaces));
+ auto FormattedReplaces = formatReplacements(Code, Replaces, Style);
+ EXPECT_TRUE(static_cast<bool>(FormattedReplaces))
+ << llvm::toString(FormattedReplaces.takeError()) << "\n";
+ auto Result = applyAllReplacements(Code, *FormattedReplaces);
+ EXPECT_TRUE(static_cast<bool>(Result));
+ EXPECT_EQ(Expected, *Result);
}
} // end namespace
diff --git a/clang/unittests/Format/FormatTestJS.cpp b/clang/unittests/Format/FormatTestJS.cpp
index a5f0b19577a..5fda89d45d6 100644
--- a/clang/unittests/Format/FormatTestJS.cpp
+++ b/clang/unittests/Format/FormatTestJS.cpp
@@ -28,10 +28,10 @@ protected:
tooling::Replacements Replaces =
reformat(Style, Code, Ranges, "<stdin>", &IncompleteFormat);
EXPECT_FALSE(IncompleteFormat);
- std::string Result = applyAllReplacements(Code, Replaces);
- EXPECT_NE("", Result);
- DEBUG(llvm::errs() << "\n" << Result << "\n\n");
- return Result;
+ auto Result = applyAllReplacements(Code, Replaces);
+ EXPECT_TRUE(static_cast<bool>(Result));
+ DEBUG(llvm::errs() << "\n" << *Result << "\n\n");
+ return *Result;
}
static std::string format(
diff --git a/clang/unittests/Format/FormatTestJava.cpp b/clang/unittests/Format/FormatTestJava.cpp
index 30d1bc8baf8..dfc3debc46e 100644
--- a/clang/unittests/Format/FormatTestJava.cpp
+++ b/clang/unittests/Format/FormatTestJava.cpp
@@ -25,10 +25,10 @@ protected:
DEBUG(llvm::errs() << Code << "\n\n");
std::vector<tooling::Range> Ranges(1, tooling::Range(Offset, Length));
tooling::Replacements Replaces = reformat(Style, Code, Ranges);
- std::string Result = applyAllReplacements(Code, Replaces);
- EXPECT_NE("", Result);
- DEBUG(llvm::errs() << "\n" << Result << "\n\n");
- return Result;
+ auto Result = applyAllReplacements(Code, Replaces);
+ EXPECT_TRUE(static_cast<bool>(Result));
+ DEBUG(llvm::errs() << "\n" << *Result << "\n\n");
+ return *Result;
}
static std::string
diff --git a/clang/unittests/Format/FormatTestProto.cpp b/clang/unittests/Format/FormatTestProto.cpp
index d9fb1409139..6881af4361c 100644
--- a/clang/unittests/Format/FormatTestProto.cpp
+++ b/clang/unittests/Format/FormatTestProto.cpp
@@ -25,10 +25,10 @@ protected:
DEBUG(llvm::errs() << Code << "\n\n");
std::vector<tooling::Range> Ranges(1, tooling::Range(Offset, Length));
tooling::Replacements Replaces = reformat(Style, Code, Ranges);
- std::string Result = applyAllReplacements(Code, Replaces);
- EXPECT_NE("", Result);
- DEBUG(llvm::errs() << "\n" << Result << "\n\n");
- return Result;
+ auto Result = applyAllReplacements(Code, Replaces);
+ EXPECT_TRUE(static_cast<bool>(Result));
+ DEBUG(llvm::errs() << "\n" << *Result << "\n\n");
+ return *Result;
}
static std::string format(llvm::StringRef Code) {
diff --git a/clang/unittests/Format/FormatTestSelective.cpp b/clang/unittests/Format/FormatTestSelective.cpp
index c4286d4297d..2bc60fd1e0d 100644
--- a/clang/unittests/Format/FormatTestSelective.cpp
+++ b/clang/unittests/Format/FormatTestSelective.cpp
@@ -28,10 +28,10 @@ protected:
tooling::Replacements Replaces =
reformat(Style, Code, Ranges, "<stdin>", &IncompleteFormat);
EXPECT_FALSE(IncompleteFormat) << Code << "\n\n";
- std::string Result = applyAllReplacements(Code, Replaces);
- EXPECT_NE("", Result);
- DEBUG(llvm::errs() << "\n" << Result << "\n\n");
- return Result;
+ auto Result = applyAllReplacements(Code, Replaces);
+ EXPECT_TRUE(static_cast<bool>(Result));
+ DEBUG(llvm::errs() << "\n" << *Result << "\n\n");
+ return *Result;
}
FormatStyle Style = getLLVMStyle();
diff --git a/clang/unittests/Format/SortImportsTestJS.cpp b/clang/unittests/Format/SortImportsTestJS.cpp
index e6b5273f7b1..77c37e337dd 100644
--- a/clang/unittests/Format/SortImportsTestJS.cpp
+++ b/clang/unittests/Format/SortImportsTestJS.cpp
@@ -25,10 +25,13 @@ protected:
if (Length == 0U)
Length = Code.size() - Offset;
std::vector<tooling::Range> Ranges(1, tooling::Range(Offset, Length));
- std::string Sorted =
+ auto Sorted =
applyAllReplacements(Code, sortIncludes(Style, Code, Ranges, FileName));
- return applyAllReplacements(Sorted,
- reformat(Style, Sorted, Ranges, FileName));
+ EXPECT_TRUE(static_cast<bool>(Sorted));
+ auto Formatted = applyAllReplacements(
+ *Sorted, reformat(Style, *Sorted, Ranges, FileName));
+ EXPECT_TRUE(static_cast<bool>(Formatted));
+ return *Formatted;
}
void verifySort(llvm::StringRef Expected, llvm::StringRef Code,
diff --git a/clang/unittests/Format/SortIncludesTest.cpp b/clang/unittests/Format/SortIncludesTest.cpp
index c8a43fc6240..13a1b9adeee 100644
--- a/clang/unittests/Format/SortIncludesTest.cpp
+++ b/clang/unittests/Format/SortIncludesTest.cpp
@@ -26,10 +26,13 @@ protected:
std::string sort(StringRef Code, StringRef FileName = "input.cpp") {
auto Ranges = GetCodeRange(Code);
- std::string Sorted =
+ auto Sorted =
applyAllReplacements(Code, sortIncludes(Style, Code, Ranges, FileName));
- return applyAllReplacements(Sorted,
- reformat(Style, Sorted, Ranges, FileName));
+ EXPECT_TRUE(static_cast<bool>(Sorted));
+ auto Result = applyAllReplacements(
+ *Sorted, reformat(Style, *Sorted, Ranges, FileName));
+ EXPECT_TRUE(static_cast<bool>(Result));
+ return *Result;
}
unsigned newCursor(llvm::StringRef Code, unsigned Cursor) {
diff --git a/clang/unittests/Tooling/RefactoringTest.cpp b/clang/unittests/Tooling/RefactoringTest.cpp
index fbf54134a7c..df96bb159de 100644
--- a/clang/unittests/Tooling/RefactoringTest.cpp
+++ b/clang/unittests/Tooling/RefactoringTest.cpp
@@ -640,27 +640,32 @@ protected:
StringRef Result, const Replacements &First,
const Replacements &Second) {
// These are mainly to verify the test itself and make it easier to read.
- std::string AfterFirst = applyAllReplacements(Code, First);
- std::string InSequenceRewrite = applyAllReplacements(AfterFirst, Second);
- EXPECT_EQ(Intermediate, AfterFirst);
- EXPECT_EQ(Result, InSequenceRewrite);
+ auto AfterFirst = applyAllReplacements(Code, First);
+ EXPECT_TRUE(static_cast<bool>(AfterFirst));
+ auto InSequenceRewrite = applyAllReplacements(*AfterFirst, Second);
+ EXPECT_TRUE(static_cast<bool>(InSequenceRewrite));
+ EXPECT_EQ(Intermediate, *AfterFirst);
+ EXPECT_EQ(Result, *InSequenceRewrite);
tooling::Replacements Merged = mergeReplacements(First, Second);
- std::string MergedRewrite = applyAllReplacements(Code, Merged);
- EXPECT_EQ(InSequenceRewrite, MergedRewrite);
- if (InSequenceRewrite != MergedRewrite)
+ auto MergedRewrite = applyAllReplacements(Code, Merged);
+ EXPECT_TRUE(static_cast<bool>(MergedRewrite));
+ EXPECT_EQ(*InSequenceRewrite, *MergedRewrite);
+ if (*InSequenceRewrite != *MergedRewrite)
for (tooling::Replacement M : Merged)
llvm::errs() << M.getOffset() << " " << M.getLength() << " "
<< M.getReplacementText() << "\n";
}
void mergeAndTestRewrite(StringRef Code, const Replacements &First,
const Replacements &Second) {
- std::string InSequenceRewrite =
- applyAllReplacements(applyAllReplacements(Code, First), Second);
+ auto AfterFirst = applyAllReplacements(Code, First);
+ EXPECT_TRUE(static_cast<bool>(AfterFirst));
+ auto InSequenceRewrite = applyAllReplacements(*AfterFirst, Second);
tooling::Replacements Merged = mergeReplacements(First, Second);
- std::string MergedRewrite = applyAllReplacements(Code, Merged);
- EXPECT_EQ(InSequenceRewrite, MergedRewrite);
- if (InSequenceRewrite != MergedRewrite)
+ auto MergedRewrite = applyAllReplacements(Code, Merged);
+ EXPECT_TRUE(static_cast<bool>(MergedRewrite));
+ EXPECT_EQ(*InSequenceRewrite, *MergedRewrite);
+ if (*InSequenceRewrite != *MergedRewrite)
for (tooling::Replacement M : Merged)
llvm::errs() << M.getOffset() << " " << M.getLength() << " "
<< M.getReplacementText() << "\n";
diff --git a/clang/unittests/Tooling/RewriterTest.cpp b/clang/unittests/Tooling/RewriterTest.cpp
index 93f69eb9fa0..e8afedb0117 100644
--- a/clang/unittests/Tooling/RewriterTest.cpp
+++ b/clang/unittests/Tooling/RewriterTest.cpp
@@ -41,8 +41,9 @@ TEST(Rewriter, AdjacentInsertAndDelete) {
Replacements Replaces;
Replaces.insert(Replacement("<file>", 6, 6, ""));
Replaces.insert(Replacement("<file>", 6, 0, "replaced\n"));
- EXPECT_EQ("line1\nreplaced\nline3\nline4",
- applyAllReplacements("line1\nline2\nline3\nline4", Replaces));
+ auto Rewritten = applyAllReplacements("line1\nline2\nline3\nline4", Replaces);
+ EXPECT_TRUE(static_cast<bool>(Rewritten));
+ EXPECT_EQ("line1\nreplaced\nline3\nline4", *Rewritten);
}
} // end namespace
OpenPOWER on IntegriCloud