mirror of
https://github.com/usatiuk/backup.git
synced 2025-10-26 17:37:47 +01:00
proper cmake
This commit is contained in:
5
tests/utils/CMakeLists.txt
Normal file
5
tests/utils/CMakeLists.txt
Normal file
@@ -0,0 +1,5 @@
|
||||
cmake_minimum_required(VERSION 3.22)
|
||||
|
||||
add_library(testUtils srcs/Cleaner.cpp)
|
||||
|
||||
target_include_directories(testUtils PUBLIC includes)
|
||||
@@ -1,43 +0,0 @@
|
||||
//
|
||||
// Created by Stepan Usatiuk on 13.04.2023.
|
||||
//
|
||||
|
||||
#ifndef SEMBACKUP_HELPFULASSERT_H
|
||||
#define SEMBACKUP_HELPFULASSERT_H
|
||||
|
||||
#include <functional>
|
||||
#include <iostream>
|
||||
#include <ostream>
|
||||
#include <sstream>
|
||||
#include <utility>
|
||||
|
||||
#include "../../src/Exception.h"
|
||||
|
||||
template<typename T, typename comp = std::equal_to<>>
|
||||
class HelpfulAssert {
|
||||
public:
|
||||
bool operator()(const T &lhs, const T &rhs) {
|
||||
if (!comp()(lhs, rhs)) {
|
||||
std::stringstream out;
|
||||
if constexpr (has_print_op<T>::value) {
|
||||
out << "Expected lhs to compare to " << rhs << std::endl;
|
||||
out << "But lhs is " << lhs << std::endl;
|
||||
|
||||
} else {
|
||||
out << "Error comparing!" << std::endl;
|
||||
}
|
||||
throw Exception(out.str());
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private:
|
||||
template<typename, typename = void>
|
||||
struct has_print_op : std::false_type {};
|
||||
|
||||
template<typename E>
|
||||
struct has_print_op<E, std::void_t<decltype(std::declval<std::ostream>() << std::declval<E>())>> : std::true_type {};
|
||||
};
|
||||
|
||||
|
||||
#endif//SEMBACKUP_HELPFULASSERT_H
|
||||
@@ -1,65 +0,0 @@
|
||||
//
|
||||
// Created by Stepan Usatiuk on 13.04.2023.
|
||||
//
|
||||
|
||||
#include "HelpfulAssertTest.h"
|
||||
|
||||
std::unique_ptr<TestGroup> HelpfulAssertTest::operator()() {
|
||||
auto tg = std::make_unique<TestGroup>("HelpfulAssert tests");
|
||||
|
||||
tg->addTest(std::make_unique<Test>("Equal test", std::function<bool()>([]() {
|
||||
HelpfulAssert<int>()(1, 1);
|
||||
|
||||
HelpfulAssert<std::string>()("hello", "hello");
|
||||
|
||||
try {
|
||||
HelpfulAssert<std::string>()("hello", "hello2");
|
||||
} catch (const Exception &e) {
|
||||
if (std::string_view(e.what()).find("Expected lhs to compare to hello2\nBut lhs is hello\n") == std::string_view::npos)
|
||||
throw Exception("HelpfulAssert text is wrong");
|
||||
} catch (...) {
|
||||
throw Exception("HelpfulAssert throws wrong exception");
|
||||
}
|
||||
|
||||
try {
|
||||
HelpfulAssert<int>()(1, 2);
|
||||
} catch (const Exception &e) {
|
||||
if (std::string_view(e.what()).find("Expected lhs to compare to 2\nBut lhs is 1\n") == std::string_view::npos)
|
||||
throw Exception("HelpfulAssert text is wrong");
|
||||
} catch (...) {
|
||||
throw Exception("HelpfulAssert throws wrong exception");
|
||||
}
|
||||
|
||||
return true;
|
||||
})));
|
||||
|
||||
tg->addTest(std::make_unique<Test>("Greater than test", std::function<bool()>([]() {
|
||||
HelpfulAssert<int, std::greater<>>()(2, 1);
|
||||
|
||||
try {
|
||||
HelpfulAssert<int, std::greater<>>()(1, 2);
|
||||
} catch (const Exception &e) {
|
||||
if (std::string_view(e.what()).find("Expected lhs to compare to 2\nBut lhs is 1\n") == std::string_view::npos)
|
||||
throw Exception("HelpfulAssert text is wrong");
|
||||
} catch (...) {
|
||||
throw Exception("HelpfulAssert throws wrong exception");
|
||||
}
|
||||
|
||||
return true;
|
||||
})));
|
||||
tg->addTest(std::make_unique<Test>("Less than test", std::function<bool()>([]() {
|
||||
HelpfulAssert<int, std::less<>>()(1, 2);
|
||||
|
||||
try {
|
||||
HelpfulAssert<int, std::less<>>()(2, 1);
|
||||
} catch (const Exception &e) {
|
||||
if (std::string_view(e.what()).find("Expected lhs to compare to 1\nBut lhs is 2\n") == std::string_view::npos)
|
||||
throw Exception("HelpfulAssert text is wrong");
|
||||
} catch (...) {
|
||||
throw Exception("HelpfulAssert throws wrong exception");
|
||||
}
|
||||
|
||||
return true;
|
||||
})));
|
||||
return tg;
|
||||
}
|
||||
@@ -1,16 +0,0 @@
|
||||
//
|
||||
// Created by Stepan Usatiuk on 13.04.2023.
|
||||
//
|
||||
|
||||
#ifndef SEMBACKUP_HELPFULASSERTTEST_H
|
||||
#define SEMBACKUP_HELPFULASSERTTEST_H
|
||||
|
||||
#include "TestGroupGenerator.h"
|
||||
|
||||
class HelpfulAssertTest : public TestGroupGenerator {
|
||||
public:
|
||||
std::unique_ptr<TestGroup> operator()() override;
|
||||
};
|
||||
|
||||
|
||||
#endif//SEMBACKUP_HELPFULASSERTTEST_H
|
||||
@@ -1,13 +0,0 @@
|
||||
//
|
||||
// Created by Stepan Usatiuk on 13.04.2023.
|
||||
//
|
||||
|
||||
#include "Runnable.h"
|
||||
|
||||
Runnable::Runnable(std::string name) : name(std::move(name)) {}
|
||||
|
||||
std::string Runnable::getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
Runnable::~Runnable() = default;
|
||||
@@ -1,26 +0,0 @@
|
||||
//
|
||||
// Created by Stepan Usatiuk on 13.04.2023.
|
||||
//
|
||||
|
||||
#ifndef SEMBACKUP_RUNNABLE_H
|
||||
#define SEMBACKUP_RUNNABLE_H
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
class Runnable {
|
||||
public:
|
||||
Runnable(std::string name);
|
||||
|
||||
virtual bool run(std::ostream &out) = 0;
|
||||
|
||||
virtual ~Runnable();
|
||||
|
||||
std::string getName();
|
||||
|
||||
protected:
|
||||
std::string name;
|
||||
};
|
||||
|
||||
|
||||
#endif//SEMBACKUP_RUNNABLE_H
|
||||
@@ -1,16 +0,0 @@
|
||||
//
|
||||
// Created by Stepan Usatiuk on 13.04.2023.
|
||||
//
|
||||
|
||||
#include "Test.h"
|
||||
|
||||
#include <utility>
|
||||
|
||||
Test::Test(std::string name, std::function<bool()> test)
|
||||
: Runnable(std::move(name)), test(std::move(test)) {}
|
||||
|
||||
bool Test::run(std::ostream &out) {
|
||||
return test();
|
||||
}
|
||||
|
||||
Test::~Test() = default;
|
||||
@@ -1,25 +0,0 @@
|
||||
//
|
||||
// Created by Stepan Usatiuk on 13.04.2023.
|
||||
//
|
||||
|
||||
#ifndef SEMBACKUP_TEST_H
|
||||
#define SEMBACKUP_TEST_H
|
||||
|
||||
#include <functional>
|
||||
|
||||
#include "Runnable.h"
|
||||
|
||||
class Test : public Runnable {
|
||||
public:
|
||||
Test(std::string name, std::function<bool()> test);
|
||||
|
||||
bool run(std::ostream &out) override;
|
||||
|
||||
~Test() override;
|
||||
|
||||
private:
|
||||
std::function<bool()> test;
|
||||
};
|
||||
|
||||
|
||||
#endif//SEMBACKUP_TEST_H
|
||||
@@ -1,31 +0,0 @@
|
||||
//
|
||||
// Created by Stepan Usatiuk on 13.04.2023.
|
||||
//
|
||||
|
||||
#include "TestGroup.h"
|
||||
|
||||
TestGroup::TestGroup(std::string name) : Runnable(std::move(name)) {}
|
||||
|
||||
bool TestGroup::run(std::ostream &out) {
|
||||
bool ok = true;
|
||||
out << "Running test group " << getName() << std::endl;
|
||||
for (auto const &p: tests) {
|
||||
out << " Running " << p->getName() << " ";
|
||||
try {
|
||||
p->run(out);
|
||||
out << "OK";
|
||||
} catch (const std::exception &e) {
|
||||
ok = false;
|
||||
out << "ERROR" << std::endl;
|
||||
out << e.what();
|
||||
}
|
||||
out << std::endl;
|
||||
}
|
||||
return ok;
|
||||
}
|
||||
|
||||
void TestGroup::addTest(std::unique_ptr<Runnable> &&test) {
|
||||
tests.emplace_back(std::move(test));
|
||||
}
|
||||
|
||||
TestGroup::~TestGroup() = default;
|
||||
@@ -1,28 +0,0 @@
|
||||
//
|
||||
// Created by Stepan Usatiuk on 13.04.2023.
|
||||
//
|
||||
|
||||
#ifndef SEMBACKUP_TESTGROUP_H
|
||||
#define SEMBACKUP_TESTGROUP_H
|
||||
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
|
||||
#include "Runnable.h"
|
||||
|
||||
class TestGroup : public Runnable {
|
||||
public:
|
||||
TestGroup(std::string name);
|
||||
|
||||
bool run(std::ostream &out) override;
|
||||
|
||||
void addTest(std::unique_ptr<Runnable> &&test);
|
||||
|
||||
~TestGroup() override;
|
||||
|
||||
private:
|
||||
std::vector<std::unique_ptr<Runnable>> tests;
|
||||
};
|
||||
|
||||
|
||||
#endif//SEMBACKUP_TESTGROUP_H
|
||||
@@ -1,23 +0,0 @@
|
||||
//
|
||||
// Created by Stepan Usatiuk on 13.04.2023.
|
||||
//
|
||||
|
||||
#ifndef SEMBACKUP_TESTGROUPGENERATOR_H
|
||||
#define SEMBACKUP_TESTGROUPGENERATOR_H
|
||||
|
||||
#include <cassert>
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
#include <sstream>
|
||||
|
||||
#include "HelpfulAssert.h"
|
||||
#include "Runnable.h"
|
||||
#include "Test.h"
|
||||
#include "TestGroup.h"
|
||||
|
||||
class TestGroupGenerator {
|
||||
public:
|
||||
virtual std::unique_ptr<TestGroup> operator()() = 0;
|
||||
};
|
||||
|
||||
#endif//SEMBACKUP_TESTGROUPGENERATOR_H
|
||||
@@ -2,13 +2,14 @@
|
||||
// Created by Stepan Usatiuk on 14.04.2023.
|
||||
//
|
||||
|
||||
#include "Cleaner.h"
|
||||
#include "../includes/Cleaner.h"
|
||||
|
||||
Cleaner::Cleaner(std::vector<std::filesystem::path> toClean) : toClean(std::move(toClean)) {
|
||||
for (const auto &p: this->toClean) {
|
||||
std::filesystem::remove_all(p);
|
||||
}
|
||||
}
|
||||
|
||||
Cleaner::~Cleaner() {
|
||||
for (const auto &p: toClean) {
|
||||
std::filesystem::remove_all(p);
|
||||
Reference in New Issue
Block a user