Skip to content

Commit

Permalink
mrdox-test supports yml configs.
Browse files Browse the repository at this point in the history
  • Loading branch information
klemens-morgenstern committed Apr 26, 2023
1 parent a672ff8 commit bb83035
Show file tree
Hide file tree
Showing 6 changed files with 153 additions and 6 deletions.
16 changes: 12 additions & 4 deletions source/test/SingleFile.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include <string>
#include <utility>
#include <vector>
#include "TestConfig.hpp"

namespace clang {
namespace mrdox {
Expand All @@ -25,22 +26,29 @@ class SingleFile
: public tooling::CompilationDatabase
{
std::vector<tooling::CompileCommand> cc_;

public:
SingleFile(
llvm::StringRef dir,
llvm::StringRef file)
llvm::StringRef file,
const TestConfig &tc)
{
std::vector<std::string> cmds;
cmds.emplace_back("clang");
cmds.emplace_back("-std=c++20");
{
char buf[64];
snprintf(buf, 63, "-std=%s", tc.cxxstd.c_str());
cmds.emplace_back(buf);
}
for (const auto & fl : tc.compile_flags)
cmds.emplace_back(fl);

cmds.emplace_back(file);
cc_.emplace_back(
dir,
file,
std::move(cmds),
dir);
cc_.back().Heuristic = "unit test";
cc_.back().Heuristic = tc.heuristics;
}

std::vector<tooling::CompileCommand>
Expand Down
72 changes: 72 additions & 0 deletions source/test/TestConfig.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
`// This is a derivative work. originally part of the LLVM Project.
// Licensed under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
// Copyright (c) 2023 Klemens D. Morgenstern
//

#include "TestConfig.hpp"
#include <mrdox/Error.hpp>
#include <llvm/Support/FileSystem.h>
#include <llvm/Support/Path.h>
#include <llvm/Support/YAMLParser.h>
#include <llvm/Support/YAMLTraits.h>


template<>
struct llvm::yaml::MappingTraits<
clang::mrdox::TestConfig>
{
static void mapping(IO &io,
clang::mrdox::TestConfig& f)
{
io.mapOptional("cxxstd", f.cxxstd);
io.mapOptional("compile-flags", f.compile_flags);
io.mapOptional("should-fail", f.should_fail);
io.mapOptional("heuristics", f.heuristics);
}
};

namespace clang {
namespace mrdox {

llvm::Expected<std::vector<TestConfig>>
TestConfig::loadForTest(
llvm::StringRef dir,
llvm::StringRef file)
{
namespace fs = llvm::sys::fs;
namespace path = llvm::sys::path;

llvm::SmallString<256> filePath = file;
path::replace_extension(filePath, "yml");

if (!fs::exists(filePath))
{
filePath = dir;
filePath.append("/mrdox-test.yml");
}

if (fs::exists(filePath))
{
auto fileText = llvm::MemoryBuffer::getFile(filePath);
if(! fileText)
return makeError(fileText.getError().message(), " when loading file '", filePath, "' ");
llvm::yaml::Input yin(**fileText);

std::vector<TestConfig> res;
do
{
yin >> res.emplace_back();
if (auto ec = yin.error())
return makeError(ec.message(), " when parsing file '", filePath, "' ");
}
while (yin.nextDocument());
return res;
}
return std::vector<TestConfig>{TestConfig{}};
}

}
}
43 changes: 43 additions & 0 deletions source/test/TestConfig.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// This is a derivative work. originally part of the LLVM Project.
// Licensed under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
// Copyright (c) 2023 Klemens D. Morgenstern
//

#ifndef MRDOX_TEST_CONFIG_HPP
#define MRDOX_TEST_CONFIG_HPP

#include <mrdox/Platform.hpp>

#include <llvm/ADT/StringRef.h>
#include <llvm/Support/Error.h>

#include <string>
#include <vector>

namespace clang {
namespace mrdox {

struct TestConfig
{
std::string cxxstd = "c++20";
std::vector<std::string> compile_flags;
bool should_fail = false;
std::string heuristics = "unit test";

MRDOX_DECL
static
llvm::Expected<std::vector<TestConfig>>
loadForTest(
llvm::StringRef dir,
llvm::StringRef file);
};


}
}


#endif //MRDOX_TEST_CONFIG_HPP
20 changes: 18 additions & 2 deletions source/test/TestMain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

#include "Options.hpp"
#include "SingleFile.hpp"
#include "TestConfig.hpp"
#include <mrdox/Config.hpp>
#include <mrdox/Debug.hpp>
#include <mrdox/Error.hpp>
Expand Down Expand Up @@ -77,6 +78,8 @@ class Instance
Generator const* xmlGen_;
Generator const* adocGen_;

TestConfig testConfig_;

void
setConfig(
std::shared_ptr<Config> config);
Expand Down Expand Up @@ -184,11 +187,24 @@ handleFile(

// Build Corpus
std::unique_ptr<Corpus> corpus;

auto tcs = TestConfig::loadForTest(dirPath, filePath);
if (!tcs)
return tcs.takeError();

for (const auto & tc : *tcs)
{
SingleFile db(dirPath, filePath);
SingleFile db(dirPath, filePath, tc);
tooling::StandaloneToolExecutor ex(db, { std::string(filePath) });
auto result = Corpus::build(ex, config_, R_);
if(R_.error(result, "build Corpus for '", filePath, "'"))

if (tc.should_fail && result)
{
R_.failed("build Corpus for '", filePath, "' should have failed");
results_.numberOfErrors++;
return llvm::Error::success(); // keep going
}
else if (R_.error(result, "build Corpus for '", filePath, "'"))
{
results_.numberOfErrors++;
return llvm::Error::success(); // keep going
Expand Down
4 changes: 4 additions & 0 deletions test-files/old-tests/attributes_1.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
cxxstd: c++20
...
---
cxxstd: c++17
4 changes: 4 additions & 0 deletions test-files/old-tests/mrdox-test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
cxxstd: c++20
...
---
cxxstd: c++17

0 comments on commit bb83035

Please sign in to comment.