mirror of
https://github.com/usatiuk/backup.git
synced 2025-10-26 17:37:47 +01:00
init
This commit is contained in:
16
tests/utils/Cleaner.cpp
Normal file
16
tests/utils/Cleaner.cpp
Normal file
@@ -0,0 +1,16 @@
|
||||
//
|
||||
// Created by Stepan Usatiuk on 14.04.2023.
|
||||
//
|
||||
|
||||
#include "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);
|
||||
}
|
||||
}
|
||||
23
tests/utils/Cleaner.h
Normal file
23
tests/utils/Cleaner.h
Normal file
@@ -0,0 +1,23 @@
|
||||
//
|
||||
// Created by Stepan Usatiuk on 14.04.2023.
|
||||
//
|
||||
|
||||
#ifndef SEMBACKUP_CLEANER_H
|
||||
#define SEMBACKUP_CLEANER_H
|
||||
|
||||
|
||||
#include <filesystem>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
class Cleaner {
|
||||
public:
|
||||
Cleaner(std::vector<std::filesystem::path> toClean);
|
||||
~Cleaner();
|
||||
|
||||
private:
|
||||
std::vector<std::filesystem::path> toClean;
|
||||
};
|
||||
|
||||
|
||||
#endif//SEMBACKUP_CLEANER_H
|
||||
43
tests/utils/HelpfulAssert.h
Normal file
43
tests/utils/HelpfulAssert.h
Normal file
@@ -0,0 +1,43 @@
|
||||
//
|
||||
// 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
|
||||
65
tests/utils/HelpfulAssertTest.cpp
Normal file
65
tests/utils/HelpfulAssertTest.cpp
Normal file
@@ -0,0 +1,65 @@
|
||||
//
|
||||
// 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;
|
||||
}
|
||||
16
tests/utils/HelpfulAssertTest.h
Normal file
16
tests/utils/HelpfulAssertTest.h
Normal file
@@ -0,0 +1,16 @@
|
||||
//
|
||||
// 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
|
||||
13
tests/utils/Runnable.cpp
Normal file
13
tests/utils/Runnable.cpp
Normal file
@@ -0,0 +1,13 @@
|
||||
//
|
||||
// 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;
|
||||
26
tests/utils/Runnable.h
Normal file
26
tests/utils/Runnable.h
Normal file
@@ -0,0 +1,26 @@
|
||||
//
|
||||
// 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
|
||||
16
tests/utils/Test.cpp
Normal file
16
tests/utils/Test.cpp
Normal file
@@ -0,0 +1,16 @@
|
||||
//
|
||||
// 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;
|
||||
25
tests/utils/Test.h
Normal file
25
tests/utils/Test.h
Normal file
@@ -0,0 +1,25 @@
|
||||
//
|
||||
// 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
|
||||
31
tests/utils/TestGroup.cpp
Normal file
31
tests/utils/TestGroup.cpp
Normal file
@@ -0,0 +1,31 @@
|
||||
//
|
||||
// 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;
|
||||
28
tests/utils/TestGroup.h
Normal file
28
tests/utils/TestGroup.h
Normal file
@@ -0,0 +1,28 @@
|
||||
//
|
||||
// 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
|
||||
23
tests/utils/TestGroupGenerator.h
Normal file
23
tests/utils/TestGroupGenerator.h
Normal file
@@ -0,0 +1,23 @@
|
||||
//
|
||||
// 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
|
||||
Reference in New Issue
Block a user