forked from OSchip/llvm-project
[SyntaxTree] Implement annotation-based test infrastructure
We add the method `SyntaxTreeTest::treeDumpEqualOnAnnotations`, which allows us to compare the treeDump of only annotated code. This will reduce a lot of noise from our `BuildTreeTest` and make them short and easier to read.
This commit is contained in:
parent
7b777ee730
commit
ab58c9ee8a
clang/unittests/Tooling/Syntax
|
@ -180,6 +180,35 @@ SyntaxTreeTest::buildTree(StringRef Code, const TestClangConfig &ClangConfig) {
|
|||
return ::testing::AssertionSuccess();
|
||||
}
|
||||
|
||||
::testing::AssertionResult
|
||||
SyntaxTreeTest::treeDumpEqualOnAnnotations(StringRef CodeWithAnnotations,
|
||||
ArrayRef<StringRef> TreeDumps) {
|
||||
SCOPED_TRACE(llvm::join(GetParam().getCommandLineArgs(), " "));
|
||||
|
||||
auto AnnotatedCode = llvm::Annotations(CodeWithAnnotations);
|
||||
auto *Root = buildTree(AnnotatedCode.code(), GetParam());
|
||||
|
||||
if (Diags->getClient()->getNumErrors() != 0) {
|
||||
return ::testing::AssertionFailure()
|
||||
<< "Source file has syntax errors, they were printed to the test "
|
||||
"log";
|
||||
}
|
||||
|
||||
bool failed = false;
|
||||
auto AnnotatedRanges = AnnotatedCode.ranges();
|
||||
assert(AnnotatedRanges.size() == TreeDumps.size());
|
||||
for (auto i = 0ul; i < AnnotatedRanges.size(); i++) {
|
||||
auto *AnnotatedNode = nodeByRange(AnnotatedRanges[i], Root);
|
||||
assert(AnnotatedNode);
|
||||
auto AnnotatedNodeDump =
|
||||
std::string(StringRef(AnnotatedNode->dump(*Arena)).trim());
|
||||
// EXPECT_EQ shows the diff between the two strings if they are different.
|
||||
EXPECT_EQ(TreeDumps[i].trim().str(), AnnotatedNodeDump);
|
||||
if (AnnotatedNodeDump != TreeDumps[i].trim().str())
|
||||
failed = true;
|
||||
}
|
||||
return failed ? ::testing::AssertionFailure() : ::testing::AssertionSuccess();
|
||||
}
|
||||
syntax::Node *SyntaxTreeTest::nodeByRange(llvm::Annotations::Range R,
|
||||
syntax::Node *Root) {
|
||||
ArrayRef<syntax::Token> Toks = tokens(Root);
|
||||
|
|
|
@ -34,6 +34,9 @@ protected:
|
|||
|
||||
::testing::AssertionResult treeDumpEqual(StringRef Code, StringRef Tree);
|
||||
|
||||
::testing::AssertionResult
|
||||
treeDumpEqualOnAnnotations(StringRef CodeWithAnnotations,
|
||||
ArrayRef<StringRef> TreeDumps);
|
||||
/// Finds the deepest node in the tree that covers exactly \p R.
|
||||
/// FIXME: implement this efficiently and move to public syntax tree API.
|
||||
syntax::Node *nodeByRange(llvm::Annotations::Range R, syntax::Node *Root);
|
||||
|
|
Loading…
Reference in New Issue