forked from OSchip/llvm-project
Revert "[clangd] Migrate last tweak tests to TweakTesting.h and remove old helpers. NFC"
This reverts commit 8f85685b5c
, which
breaks on old gcc that have the macro + raw strings bug.
llvm-svn: 370262
This commit is contained in:
parent
856f3fe5bb
commit
7bb847478b
|
@ -23,6 +23,8 @@
|
||||||
#include "gtest/gtest.h"
|
#include "gtest/gtest.h"
|
||||||
#include <cassert>
|
#include <cassert>
|
||||||
|
|
||||||
|
using llvm::Failed;
|
||||||
|
using llvm::Succeeded;
|
||||||
using ::testing::AllOf;
|
using ::testing::AllOf;
|
||||||
using ::testing::HasSubstr;
|
using ::testing::HasSubstr;
|
||||||
using ::testing::StartsWith;
|
using ::testing::StartsWith;
|
||||||
|
@ -31,6 +33,100 @@ namespace clang {
|
||||||
namespace clangd {
|
namespace clangd {
|
||||||
namespace {
|
namespace {
|
||||||
|
|
||||||
|
// FIXME(sammccall): migrate the rest of the tests to use TweakTesting.h and
|
||||||
|
// remove these helpers.
|
||||||
|
std::string markRange(llvm::StringRef Code, Range R) {
|
||||||
|
size_t Begin = llvm::cantFail(positionToOffset(Code, R.start));
|
||||||
|
size_t End = llvm::cantFail(positionToOffset(Code, R.end));
|
||||||
|
assert(Begin <= End);
|
||||||
|
if (Begin == End) // Mark a single point.
|
||||||
|
return (Code.substr(0, Begin) + "^" + Code.substr(Begin)).str();
|
||||||
|
// Mark a range.
|
||||||
|
return (Code.substr(0, Begin) + "[[" + Code.substr(Begin, End - Begin) +
|
||||||
|
"]]" + Code.substr(End))
|
||||||
|
.str();
|
||||||
|
}
|
||||||
|
|
||||||
|
void checkAvailable(StringRef ID, llvm::StringRef Input, bool Available) {
|
||||||
|
Annotations Code(Input);
|
||||||
|
ASSERT_TRUE(0 < Code.points().size() || 0 < Code.ranges().size())
|
||||||
|
<< "no points of interest specified";
|
||||||
|
TestTU TU;
|
||||||
|
TU.Filename = "foo.cpp";
|
||||||
|
TU.Code = Code.code();
|
||||||
|
|
||||||
|
ParsedAST AST = TU.build();
|
||||||
|
|
||||||
|
auto CheckOver = [&](Range Selection) {
|
||||||
|
unsigned Begin = cantFail(positionToOffset(Code.code(), Selection.start));
|
||||||
|
unsigned End = cantFail(positionToOffset(Code.code(), Selection.end));
|
||||||
|
auto T = prepareTweak(ID, Tweak::Selection(AST, Begin, End));
|
||||||
|
if (Available)
|
||||||
|
EXPECT_THAT_EXPECTED(T, Succeeded())
|
||||||
|
<< "code is " << markRange(Code.code(), Selection);
|
||||||
|
else
|
||||||
|
EXPECT_THAT_EXPECTED(T, Failed())
|
||||||
|
<< "code is " << markRange(Code.code(), Selection);
|
||||||
|
};
|
||||||
|
for (auto P : Code.points())
|
||||||
|
CheckOver(Range{P, P});
|
||||||
|
for (auto R : Code.ranges())
|
||||||
|
CheckOver(R);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Checks action is available at every point and range marked in \p Input.
|
||||||
|
void checkAvailable(StringRef ID, llvm::StringRef Input) {
|
||||||
|
return checkAvailable(ID, Input, /*Available=*/true);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Same as checkAvailable, but checks the action is not available.
|
||||||
|
void checkNotAvailable(StringRef ID, llvm::StringRef Input) {
|
||||||
|
return checkAvailable(ID, Input, /*Available=*/false);
|
||||||
|
}
|
||||||
|
|
||||||
|
llvm::Expected<Tweak::Effect> apply(StringRef ID, llvm::StringRef Input) {
|
||||||
|
Annotations Code(Input);
|
||||||
|
Range SelectionRng;
|
||||||
|
if (Code.points().size() != 0) {
|
||||||
|
assert(Code.ranges().size() == 0 &&
|
||||||
|
"both a cursor point and a selection range were specified");
|
||||||
|
SelectionRng = Range{Code.point(), Code.point()};
|
||||||
|
} else {
|
||||||
|
SelectionRng = Code.range();
|
||||||
|
}
|
||||||
|
TestTU TU;
|
||||||
|
TU.Filename = "foo.cpp";
|
||||||
|
TU.Code = Code.code();
|
||||||
|
|
||||||
|
ParsedAST AST = TU.build();
|
||||||
|
unsigned Begin = cantFail(positionToOffset(Code.code(), SelectionRng.start));
|
||||||
|
unsigned End = cantFail(positionToOffset(Code.code(), SelectionRng.end));
|
||||||
|
Tweak::Selection S(AST, Begin, End);
|
||||||
|
|
||||||
|
auto T = prepareTweak(ID, S);
|
||||||
|
if (!T)
|
||||||
|
return T.takeError();
|
||||||
|
return (*T)->apply(S);
|
||||||
|
}
|
||||||
|
|
||||||
|
llvm::Expected<std::string> applyEdit(StringRef ID, llvm::StringRef Input) {
|
||||||
|
auto Effect = apply(ID, Input);
|
||||||
|
if (!Effect)
|
||||||
|
return Effect.takeError();
|
||||||
|
if (!Effect->ApplyEdit)
|
||||||
|
return llvm::createStringError(llvm::inconvertibleErrorCode(),
|
||||||
|
"No replacements");
|
||||||
|
Annotations Code(Input);
|
||||||
|
return applyAllReplacements(Code.code(), *Effect->ApplyEdit);
|
||||||
|
}
|
||||||
|
|
||||||
|
void checkTransform(llvm::StringRef ID, llvm::StringRef Input,
|
||||||
|
std::string Output) {
|
||||||
|
auto Result = applyEdit(ID, Input);
|
||||||
|
ASSERT_TRUE(bool(Result)) << llvm::toString(Result.takeError()) << Input;
|
||||||
|
EXPECT_EQ(Output, std::string(*Result)) << Input;
|
||||||
|
}
|
||||||
|
|
||||||
TWEAK_TEST(SwapIfBranches);
|
TWEAK_TEST(SwapIfBranches);
|
||||||
TEST_F(SwapIfBranchesTest, Test) {
|
TEST_F(SwapIfBranchesTest, Test) {
|
||||||
Context = Function;
|
Context = Function;
|
||||||
|
@ -119,9 +215,9 @@ TEST_F(DumpRecordLayoutTest, Test) {
|
||||||
AllOf(StartsWith("message:"), HasSubstr("0 | int x")));
|
AllOf(StartsWith("message:"), HasSubstr("0 | int x")));
|
||||||
}
|
}
|
||||||
|
|
||||||
TWEAK_TEST(ExtractVariable);
|
TEST(TweaksTest, ExtractVariable) {
|
||||||
TEST_F(ExtractVariableTest, Test) {
|
llvm::StringLiteral ID = "ExtractVariable";
|
||||||
EXPECT_AVAILABLE(R"cpp(
|
checkAvailable(ID, R"cpp(
|
||||||
int xyz(int a = 1) {
|
int xyz(int a = 1) {
|
||||||
struct T {
|
struct T {
|
||||||
int bar(int a = 1);
|
int bar(int a = 1);
|
||||||
|
@ -161,14 +257,14 @@ TEST_F(ExtractVariableTest, Test) {
|
||||||
}
|
}
|
||||||
)cpp");
|
)cpp");
|
||||||
// Should not crash.
|
// Should not crash.
|
||||||
EXPECT_UNAVAILABLE(R"cpp(
|
checkNotAvailable(ID, R"cpp(
|
||||||
template<typename T, typename ...Args>
|
template<typename T, typename ...Args>
|
||||||
struct Test<T, Args...> {
|
struct Test<T, Args...> {
|
||||||
Test(const T &v) :val[[(^]]) {}
|
Test(const T &v) :val[[(^]]) {}
|
||||||
T val;
|
T val;
|
||||||
};
|
};
|
||||||
)cpp");
|
)cpp");
|
||||||
EXPECT_UNAVAILABLE(R"cpp(
|
checkNotAvailable(ID, R"cpp(
|
||||||
int xyz(int a = [[1]]) {
|
int xyz(int a = [[1]]) {
|
||||||
struct T {
|
struct T {
|
||||||
int bar(int a = [[1]]);
|
int bar(int a = [[1]]);
|
||||||
|
@ -386,35 +482,38 @@ TEST_F(ExtractVariableTest, Test) {
|
||||||
})cpp"},
|
})cpp"},
|
||||||
};
|
};
|
||||||
for (const auto &IO : InputOutputs) {
|
for (const auto &IO : InputOutputs) {
|
||||||
EXPECT_EQ(IO.second, apply(IO.first)) << IO.first;
|
checkTransform(ID, IO.first, IO.second);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
TWEAK_TEST(AnnotateHighlightings);
|
TEST(TweaksTest, AnnotateHighlightings) {
|
||||||
TEST_F(AnnotateHighlightingsTest, Test) {
|
llvm::StringLiteral ID = "AnnotateHighlightings";
|
||||||
EXPECT_AVAILABLE("^vo^id^ ^f(^) {^}^"); // available everywhere.
|
checkAvailable(ID, "^vo^id^ ^f(^) {^}^"); // available everywhere.
|
||||||
EXPECT_AVAILABLE("[[int a; int b;]]");
|
checkAvailable(ID, "[[int a; int b;]]");
|
||||||
EXPECT_EQ("/* storage.type.primitive.cpp */void "
|
const char *Input = "void ^f() {}";
|
||||||
"/* entity.name.function.cpp */f() {}",
|
const char *Output = "/* storage.type.primitive.cpp */void /* entity.name.function.cpp */f() {}";
|
||||||
apply("void ^f() {}"));
|
checkTransform(ID, Input, Output);
|
||||||
|
|
||||||
EXPECT_EQ(apply(R"cpp(
|
checkTransform(ID,
|
||||||
[[void f1();
|
R"cpp(
|
||||||
void f2();]]
|
[[void f1();
|
||||||
)cpp"),
|
void f2();]]
|
||||||
R"cpp(
|
)cpp",
|
||||||
/* storage.type.primitive.cpp */void /* entity.name.function.cpp */f1();
|
R"cpp(
|
||||||
/* storage.type.primitive.cpp */void /* entity.name.function.cpp */f2();
|
/* storage.type.primitive.cpp */void /* entity.name.function.cpp */f1();
|
||||||
)cpp");
|
/* storage.type.primitive.cpp */void /* entity.name.function.cpp */f2();
|
||||||
|
)cpp");
|
||||||
|
|
||||||
EXPECT_EQ(apply(R"cpp(
|
checkTransform(ID,
|
||||||
void f1();
|
R"cpp(
|
||||||
void f2() {^};
|
void f1();
|
||||||
)cpp"),
|
void f2() {^};
|
||||||
R"cpp(
|
)cpp",
|
||||||
void f1();
|
|
||||||
/* storage.type.primitive.cpp */void /* entity.name.function.cpp */f2() {};
|
R"cpp(
|
||||||
)cpp");
|
void f1();
|
||||||
|
/* storage.type.primitive.cpp */void /* entity.name.function.cpp */f2() {};
|
||||||
|
)cpp");
|
||||||
}
|
}
|
||||||
|
|
||||||
TWEAK_TEST(ExpandMacro);
|
TWEAK_TEST(ExpandMacro);
|
||||||
|
|
Loading…
Reference in New Issue