refactor: make test a header
This commit is contained in:
parent
e58bdb01ba
commit
92c39f13a6
1 changed files with 13 additions and 2 deletions
81
test.hpp
Normal file
81
test.hpp
Normal file
|
|
@ -0,0 +1,81 @@
|
|||
#ifndef RENDO_TEST_HPP
|
||||
#define RENDO_TEST_HPP
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <functional>
|
||||
|
||||
class Test {
|
||||
private:
|
||||
std::function<bool()> complete;
|
||||
public:
|
||||
std::string test_name;
|
||||
Test(std::string name, std::function<bool()> complete): test_name(name), complete(complete){};
|
||||
bool evaluate();
|
||||
};
|
||||
|
||||
|
||||
class TestSuite {
|
||||
private:
|
||||
std::string suite_name;
|
||||
std::vector<Test> tests;
|
||||
public:
|
||||
TestSuite(std::string);
|
||||
|
||||
void run();
|
||||
|
||||
template<class T>
|
||||
TestSuite& eq(std::string, T, T);
|
||||
template<class T>
|
||||
TestSuite& neq(std::string, T, T);
|
||||
TestSuite& custom(std::string, std::function<bool()>);
|
||||
};
|
||||
|
||||
|
||||
#ifdef RENDO_TEST_IMPLEMENTATION
|
||||
|
||||
#include <iostream>
|
||||
bool Test::evaluate() { return complete(); }
|
||||
|
||||
TestSuite::TestSuite(std::string name): suite_name(name) {
|
||||
tests = {};
|
||||
}
|
||||
|
||||
template<class T>
|
||||
TestSuite& TestSuite::eq(std::string test_name, T test_value, T expected_value) {
|
||||
tests.push_back(Test(test_name, [=](){return test_value == expected_value;}));
|
||||
return *this;
|
||||
}
|
||||
|
||||
template<class T>
|
||||
TestSuite& TestSuite::neq(std::string test_name, T test_value, T expected_value) {
|
||||
tests.push_back(Test(test_name, [=](){return test_value != expected_value;}));
|
||||
return *this;
|
||||
}
|
||||
|
||||
TestSuite& TestSuite::custom(std::string test_name, std::function<bool()> completness_function) {
|
||||
tests.push_back(Test(test_name, completness_function));
|
||||
return *this;
|
||||
}
|
||||
|
||||
void TestSuite::run() {
|
||||
std::cout << "=== " << suite_name << " ===" << std::endl;
|
||||
int completed = 0;
|
||||
int amount = tests.size();
|
||||
for (int i = 0; i < amount; i++) {
|
||||
try {
|
||||
bool result = tests[i].evaluate();
|
||||
std::cout << "Running Test " << i + 1 << "/" << amount << " \"" << tests[i].test_name << "\": " << (result ? "SUCCESS" : "FAILED") << std::endl;
|
||||
if (result)
|
||||
completed++;
|
||||
} catch (...) {
|
||||
std::cout << "Test " << i + 1 << "/" << amount << " finished with exception.";
|
||||
}
|
||||
}
|
||||
std::cout << "Tests completed: " << completed << "/" << amount << (completed == amount ? ". Well done!" : "... You've got a job to do") << std::endl;
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
#endif
|
||||
Loading…
Add table
Add a link
Reference in a new issue