Skip to content

Commit

Permalink
Extract Class Tool and derive most tools from it
Browse files Browse the repository at this point in the history
Drop-comment doesn't interact with test cases or test results.
  • Loading branch information
LegalizeAdulthood committed Jan 28, 2024
1 parent a7442bb commit 70f76d7
Show file tree
Hide file tree
Showing 11 changed files with 171 additions and 155 deletions.
27 changes: 6 additions & 21 deletions Tools/AddTestAlias/AddTestAlias.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <Main.h>
#include <StringScanner.h>
#include <TestCases.h>
#include <Tool.h>
#include <ToolResults.h>

#include <algorithm>
Expand All @@ -19,17 +20,17 @@
namespace
{

class AddTestAlias
class AddTestAlias : public testCases::Tool
{
public:
AddTestAlias(std::string_view testCaseDir,
std::string_view resultsDir,
std::string_view prefix,
int master,
std::vector<int> aliases) :
m_resultsDir(resultsDir),
Tool(testCaseDir, resultsDir),
m_prefix(prefix),
m_test(readTestCases(testCaseDir)),
m_test(testCases::getTestForPrefix(prefix)),
m_master(m_prefix + std::to_string(master))
{
m_aliases.reserve(aliases.size());
Expand All @@ -43,30 +44,14 @@ class AddTestAlias
void updateResults();

private:
const testCases::Test &readTestCases(std::string_view testCaseDir);
void updateFile(const testCases::FileContents &file);

std::filesystem::path m_resultsDir;
std::string m_prefix;
const testCases::Test &m_test;
std::string m_master;
std::vector<std::string> m_aliases;
};

const testCases::Test &AddTestAlias::readTestCases(std::string_view testCaseDir)
{
const std::vector<std::string> errors = testCases::Test::scanTestDirectory(testCaseDir);
if (!errors.empty())
{
for (const std::string &error : errors)
{
std::cerr << "error: " << error << '\n';
}
throw std::runtime_error("Test cases contain errors");
}
return testCases::getTestForPrefix(m_prefix);
}

void AddTestAlias::updateFile(const testCases::FileContents &file)
{
auto updateLabel = [this](const std::string &line)
Expand Down Expand Up @@ -99,11 +84,11 @@ void AddTestAlias::updateSourceFiles()

void AddTestAlias::updateResults()
{
for (const testCases::FileContents &diff : testCases::readCaseDiffs(m_resultsDir / "diffs", m_prefix))
for (const testCases::FileContents &diff : getCaseDiffsForPrefix(m_prefix))
{
updateFile(diff);
}
if (std::filesystem::path fileDiff(m_resultsDir / "file-diffs" / (m_prefix + ".txt")); exists(fileDiff))
if (std::filesystem::path fileDiff(getFileDiffForPrefix(m_prefix)); exists(fileDiff))
{
updateFile(testCases::FileContents(fileDiff));
}
Expand Down
62 changes: 28 additions & 34 deletions Tools/AddTests/AddTests.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <FileContents.h>
#include <Main.h>
#include <TestCases.h>
#include <Tool.h>
#include <ToolResults.h>

#include <algorithm>
Expand All @@ -16,45 +17,32 @@
namespace
{

const testCases::Test &getTestCase(const std::filesystem::path &testCaseDir, std::string_view prefix)
{
const std::vector<std::string> errors = testCases::Test::scanTestDirectory(testCaseDir);
if (!errors.empty())
{
for (const std::string &error : errors)
{
std::cerr << "error: " << error << '\n';
}
throw std::runtime_error("Test cases contain errors:");
}

return testCases::getTestForPrefix(prefix);
}

class AddTests
class AddTests : public testCases::Tool
{
public:
AddTests(const std::filesystem::path &testCaseDir,
AddTests(std::string_view testCaseDir, std::string_view resultsDir,
std::string_view prefix,
const std::filesystem::path &sourceFile,
const std::filesystem::path &resultsDir);
const std::filesystem::path &sourceFile);

bool readSourceFile(std::string_view sourceFile);
void writeSourceFile();
void updateResultsFile(const std::filesystem::path &file);
void updateResults();
void writePlaceholderDiffs();

private:
void updateResultsDir(const std::filesystem::path &dir);
void writePlaceholderDiffs(const std::filesystem::path &diffDir);
void updateResultsFile(const std::filesystem::path &file);

const testCases::Test &m_test;
testCases::FileContents m_sourceContents;
std::vector<std::string> m_newLabels;
};

AddTests::AddTests(const std::filesystem::path &testCaseDir,
AddTests::AddTests(std::string_view testCaseDir,
std::string_view resultsDir,
std::string_view prefix,
const std::filesystem::path &sourceFile,
const std::filesystem::path &resultsDir) :
m_test(getTestCase(testCaseDir, prefix)),
const std::filesystem::path &sourceFile) :
Tool(testCaseDir, resultsDir),
m_test(testCases::getTestForPrefix(prefix)),
m_sourceContents(sourceFile)
{
}
Expand All @@ -79,6 +67,11 @@ void AddTests::writeSourceFile()
m_sourceContents.transform(replaceMarkers);
}

void AddTests::updateResults()
{
updateResultsDir(getResultsDir());
}

void AddTests::updateResultsFile(const std::filesystem::path &file)
{
testCases::ToolResults results(file);
Expand All @@ -104,8 +97,9 @@ void AddTests::updateResultsDir(const std::filesystem::path &dir)
}
}

void AddTests::writePlaceholderDiffs(const std::filesystem::path &diffDir)
void AddTests::writePlaceholderDiffs()
{
const std::filesystem::path diffDir(getDiffsDir());
for (const std::string &label : m_newLabels)
{
std::ofstream str(diffDir / (label + ".txt"));
Expand All @@ -127,14 +121,14 @@ int toolMain(std::vector<std::string_view> args)
{
return usage(args[0]);
}
const std::filesystem::path testCaseDir{args[1]};
if (!is_directory(testCaseDir))
const std::string_view testCaseDir{args[1]};
if (!is_directory(std::filesystem::path(testCaseDir)))
{
std::cerr << "Test case directory " << testCaseDir << " does not exist.\n";
return 1;
}
const std::filesystem::path resultsDir{args[2]};
if (!is_directory(resultsDir))
const std::string_view resultsDir{args[2]};
if (!is_directory(std::filesystem::path(resultsDir)))
{
std::cerr << "Results case directory " << resultsDir << " does not exist.\n";
return 1;
Expand All @@ -147,9 +141,9 @@ int toolMain(std::vector<std::string_view> args)
return 1;
}

AddTests tool(testCaseDir, testPrefix, sourceFile, resultsDir);
AddTests tool(testCaseDir, resultsDir, testPrefix, sourceFile);
tool.writeSourceFile();
tool.updateResultsDir(resultsDir);
tool.writePlaceholderDiffs(resultsDir / "diffs");
tool.updateResults();
tool.writePlaceholderDiffs();
return 0;
}
54 changes: 21 additions & 33 deletions Tools/DeleteTests/DeleteTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <Main.h>
#include <StringScanner.h>
#include <TestCases.h>
#include <Tool.h>
#include <ToolResults.h>

#include <algorithm>
Expand All @@ -19,56 +20,43 @@
namespace
{

class DeleteTests
class DeleteTests : public testCases::Tool
{
public:
DeleteTests(std::string_view testCaseDir,
std::string_view resultsDir,
std::string_view prefix,
std::vector<int> deletions) :
m_resultsDir(resultsDir),
m_prefix(prefix),
m_test(readTestCases(testCaseDir))
{
m_deletions.reserve(deletions.size());
std::transform(deletions.begin(),
deletions.end(),
std::back_inserter(m_deletions),
[this](int alias) { return m_prefix + std::to_string(alias); });
}
std::string_view resultsDir,
std::string_view prefix,
std::vector<int> deletions);

void updateSourceFiles();
void updateResults();

private:
const testCases::Test &readTestCases(std::string_view testCaseDir);
std::string removeLabel(const std::string &line);
void updateFile(const testCases::FileContents &file);
void updateToolResults(const std::filesystem::path &dir);
void updateDiffs();

std::filesystem::path m_resultsDir;
std::string m_prefix;
const testCases::Test &m_test;
std::vector<std::string> m_deletions;
bool m_updatingDIffs{};
};

const testCases::Test &DeleteTests::readTestCases(std::string_view testCaseDir)
DeleteTests::DeleteTests(std::string_view testCaseDir,
std::string_view resultsDir,
std::string_view prefix,
std::vector<int> deletions) :
Tool(testCaseDir, resultsDir),
m_prefix(prefix),
m_test(testCases::getTestForPrefix(m_prefix))
{
const std::vector<std::string> errors = testCases::Test::scanTestDirectory(testCaseDir);
if (!errors.empty())
{
for (const std::string &error : errors)
{
std::cerr << "error: " << error << '\n';
}
throw std::runtime_error("Test cases contain errors");
}
return testCases::getTestForPrefix(m_prefix);
m_deletions.reserve(deletions.size());
std::transform(deletions.begin(),
deletions.end(),
std::back_inserter(m_deletions),
[this](int alias) { return m_prefix + std::to_string(alias); });
}


void DeleteTests::updateFile(const testCases::FileContents &file)
{
const auto removeLabel = [this](const std::string &line)
Expand Down Expand Up @@ -120,17 +108,17 @@ void DeleteTests::updateSourceFiles()

void DeleteTests::updateDiffs()
{
for (const testCases::FileContents &diff : testCases::readCaseDiffs(m_resultsDir / "diffs", m_prefix))
for (const testCases::FileContents &diff : getCaseDiffsForPrefix(m_prefix))
{
updateFile(diff);
}

if (std::filesystem::path fileDiff(m_resultsDir / "file-diffs" / (m_prefix + ".txt")); exists(fileDiff))
if (std::filesystem::path fileDiff(getFileDiffForPrefix(m_prefix)); exists(fileDiff))
{
updateFile(testCases::FileContents(fileDiff));
}

for (const auto &entry : std::filesystem::directory_iterator(m_resultsDir / "diffs"))
for (const auto &entry : std::filesystem::directory_iterator(getDiffsDir()))
{
if (is_directory(entry))
{
Expand Down Expand Up @@ -178,7 +166,7 @@ void DeleteTests::updateToolResults(const std::filesystem::path &dir)
void DeleteTests::updateResults()
{
m_updatingDIffs = false;
updateToolResults(m_resultsDir);
updateToolResults(getResultsDir());
m_updatingDIffs = true;
updateDiffs();
}
Expand Down
Loading

0 comments on commit 70f76d7

Please sign in to comment.