Skip to content

Commit 32fc1bd

Browse files
committed
tests compare expected XML versus actual
1 parent 77027b9 commit 32fc1bd

File tree

5 files changed

+86
-55
lines changed

5 files changed

+86
-55
lines changed

include/mrdox/Reporter.hpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,25 @@ struct Reporter
3030
return failed_;
3131
}
3232

33+
void test_failure()
34+
{
35+
failed_ = true;
36+
}
37+
3338
bool success(llvm::Error&& err);
39+
bool success(std::error_code const& ec);
40+
41+
template<class T>
42+
bool
43+
success(T& t, llvm::ErrorOr<T>&& ev)
44+
{
45+
if(ev)
46+
{
47+
t = std::move(ev.get());
48+
return true;
49+
}
50+
return false;
51+
}
3452

3553
bool
3654
success(

include/mrdox/XML.hpp

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,16 @@
1414

1515
#include <mrdox/Config.hpp>
1616
#include <llvm/ADT/StringRef.h>
17+
#include <llvm/Support/Error.h>
1718
#include <string>
1819

1920
namespace clang {
2021
namespace mrdox {
2122

22-
// Algorithms for rendering the corpus as XML.
23-
// The XML output is unaffected by the structure
24-
// of the input data and thus, represents a
25-
// canonical form.
26-
27-
bool
28-
renderCodeAsXML(
23+
void
24+
renderToXMLString(
2925
std::string& xml,
30-
llvm::StringRef cppCode,
26+
Corpus const& corpus,
3127
Config const& cfg);
3228

3329
} // mrdox

source/lib/Reporter.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,18 @@ success(llvm::Error&& err)
2929
return false;
3030
}
3131

32+
bool
33+
Reporter::
34+
success(std::error_code const& ec)
35+
{
36+
if(! ec)
37+
return true;
38+
// VFALCO TODO Source file, line number, and what
39+
llvm::errs() << ec.message() << "\n";
40+
failed_ = true;
41+
return false;
42+
}
43+
3244
bool
3345
Reporter::
3446
success(

source/lib/XML.cpp

Lines changed: 12 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -555,34 +555,20 @@ char const*
555555
XMLGenerator::
556556
Format = "xml";
557557

558-
/** A visitor which keeps its own map of tool results.
559-
*/
560-
class TestVisitor
561-
: public BasicVisitor
562-
{
563-
TestVisitor& corpus_;
564-
tooling::InMemoryToolResults results_;
565-
566-
public:
567-
TestVisitor(
568-
TestVisitor& corpus,
569-
Config const& cfg) noexcept
570-
: BasicVisitor(cfg)
571-
, corpus_(corpus)
572-
{
573-
}
558+
} // (anon)
574559

575-
private:
576-
void
577-
reportResult(
578-
StringRef Key,
579-
StringRef Value) override
580-
{
581-
results_.addResult(Key, Value);
582-
}
583-
};
560+
//------------------------------------------------
584561

585-
} // (anon)
562+
void
563+
renderToXMLString(
564+
std::string& xml,
565+
Corpus const& corpus,
566+
Config const& cfg)
567+
{
568+
XMLGenerator G(cfg);
569+
G.render(xml, corpus, cfg);
570+
//return llvm::Error::success();
571+
}
586572

587573
//------------------------------------------------
588574

source/tests/TestAction.cpp

Lines changed: 40 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010

1111
#include "TestAction.hpp"
1212
#include <mrdox/Config.hpp>
13+
#include <mrdox/XML.hpp>
14+
#include <llvm/Support/FileSystem.h>
15+
#include <llvm/Support/Path.h>
1316

1417
namespace clang {
1518
namespace mrdox {
@@ -18,30 +21,46 @@ void
1821
TestAction::
1922
EndSourceFileAction()
2023
{
24+
namespace fs = llvm::sys::fs;
25+
namespace path = llvm::sys::path;
26+
2127
Corpus corpus;
2228
if(! R_.success(buildIndex(corpus, results_, cfg_)))
2329
return;
24-
#if 0
25-
bool
26-
renderCodeAsXML(
27-
std::string& xml,
28-
llvm::StringRef cppCode,
29-
Config const& cfg)
30-
{
31-
std::unique_ptr<ASTUnit> astUnit =
32-
clang::tooling::buildASTFromCodeWithArgs(cppCode, {});
33-
Corpus corpus;
34-
CorpusVisitor visitor(corpus, cfg);
35-
visitor.HandleTranslationUnit(astUnit->getASTContext());
36-
if(llvm::Error err = buildIndex(corpus, cfg))
37-
return ! err;
38-
bool success = XMLGenerator(cfg).render(xml, corpus, cfg);
39-
// VFALCO oops, cfg.Executor->getToolResults()
40-
// holds information from the previous corpus...
41-
//cfg.Executor->getToolResults()->
42-
return success;
43-
}
44-
#endif
30+
std::string xml;
31+
renderToXMLString(xml, corpus, cfg_);
32+
llvm::SmallString<256> xmlPath(this->getCurrentFile());
33+
path::replace_extension(xmlPath, "xml");
34+
std::error_code ec;
35+
fs::file_status stat;
36+
ec = fs::status(xmlPath, stat, false);
37+
if(ec == std::errc::no_such_file_or_directory)
38+
{
39+
// create the xml file and write to it
40+
}
41+
else if(R_.success(ec))
42+
{
43+
if(stat.type() == fs::file_type::regular_file)
44+
{
45+
std::unique_ptr<llvm::MemoryBuffer> pb;
46+
if(! R_.success(pb, llvm::MemoryBuffer::getFile(xmlPath, true)))
47+
return;
48+
if(xml != pb->getBuffer())
49+
{
50+
llvm::errs() <<
51+
"File: \"" << this->getCurrentFile() << "\" failed.\n"
52+
"Expected:\n" <<
53+
pb->getBuffer() << "\n" <<
54+
"Got:\n" <<
55+
xml << "\n";
56+
R_.test_failure();
57+
}
58+
}
59+
else
60+
{
61+
// VFALCO report that it is not a regular file
62+
}
63+
}
4564
}
4665

4766
} // mrdox

0 commit comments

Comments
 (0)