Finally have something useful.
This commit is contained in:
@@ -1,251 +0,0 @@
|
||||
#include "JTest.h"
|
||||
#include <stdexcept>
|
||||
|
||||
namespace JTest {
|
||||
using std::ostream;
|
||||
using std::endl;
|
||||
using std::vector;
|
||||
using std::string;
|
||||
using std::runtime_error;
|
||||
using std::string;
|
||||
using std::optional;
|
||||
|
||||
runtime_error unimplemented_function_error(string method_name) {
|
||||
return runtime_error("Unimplemented function: " + method_name);
|
||||
}
|
||||
|
||||
runtime_error unimplemented_method_error(string class_name, string method_name) {
|
||||
return runtime_error("Unimplemented method in class " + class_name + ": " + method_name);
|
||||
}
|
||||
|
||||
runtime_error unimplemented_feature_error(string feature_name) {
|
||||
return runtime_error("Unimplemented feature: " + feature_name);
|
||||
}
|
||||
|
||||
testresults_t make_testresults() {
|
||||
return {0, 0, 0, 0};
|
||||
}
|
||||
|
||||
testresults_t make_testresults(uint32_t total, uint32_t skipped, uint32_t passed, uint32_t failed) {
|
||||
return {total, skipped, passed, failed};
|
||||
}
|
||||
|
||||
testresults_t add(const testresults_t& l, const testresults_t& r) {
|
||||
return {l.total + r.total, l.skipped + r.skipped, l.passed + r.passed, l.failed + r.failed};
|
||||
}
|
||||
|
||||
void print_test_results(const testresults_t& results, ostream& out) {
|
||||
out << "Tests: " << results.total << endl;
|
||||
out << "Failed: " << results.failed << ", Passed: " << results.passed << ", Skipped: " << results.skipped << endl;
|
||||
}
|
||||
|
||||
testresults_t operator+(const testresults_t& left, const testresults_t& right) {
|
||||
return add(left, right);
|
||||
}
|
||||
|
||||
testresults_t& operator+=(testresults_t& left, const testresults_t& right) {
|
||||
left = left + right;
|
||||
return left;
|
||||
}
|
||||
|
||||
testbundle_t make_testbundle(const vector<testbundle_t>& bundles, const describeoptions_t& options) {
|
||||
testbundle_t b;
|
||||
|
||||
for_each(bundles.begin(), bundles.end(), [&b](testbundle_t bundle){
|
||||
if (bundle._tests.size() == 0 && bundle._children.size() == 1 && bundle._label.size() == 0) {
|
||||
b._tests.push_back(bundle._tests.at(0));
|
||||
} else {
|
||||
b._children.push_back(bundle);
|
||||
}
|
||||
});
|
||||
|
||||
b._beforeEach = options.getBeforeEach();
|
||||
b._afterEach = options.getAfterEach();
|
||||
b._beforeAll = options.getBeforeAll();
|
||||
b._afterAll = options.getAfterAll();
|
||||
return b;
|
||||
}
|
||||
|
||||
describeoptions_t& describeoptions_t::beforeEach(configure_fn fn) {
|
||||
this->_beforeEach = fn;
|
||||
return *this;
|
||||
}
|
||||
|
||||
describeoptions_t& describeoptions_t::afterEach(configure_fn fn) {
|
||||
this->_afterEach = fn;
|
||||
return *this;
|
||||
}
|
||||
describeoptions_t& describeoptions_t::beforeAll(configure_fn fn) {
|
||||
this->_beforeAll = fn;
|
||||
return *this;
|
||||
}
|
||||
describeoptions_t& describeoptions_t::afterAll(configure_fn fn) {
|
||||
this->_afterAll = fn;
|
||||
return *this;
|
||||
}
|
||||
describeoptions_t make_describeoptions() {
|
||||
return {};
|
||||
}
|
||||
|
||||
// TODO test_method is the actual test method. We should save it to the test instead of executing it.
|
||||
testbundle_t it(const string& label, const test_fn& test_method, optional<testoptions_t> options) {
|
||||
// TODO: Stop ignoring options.
|
||||
test_t test;
|
||||
test._disabled = false;
|
||||
test._label = label;
|
||||
test._test_method = test_method;
|
||||
return make_testbundle("", {test});
|
||||
}
|
||||
|
||||
testbundle_t xit(const string& label, const test_fn& test_method, optional<testoptions_t> options) {
|
||||
// TODO: replace this with a call to it and setting _disabled to true after it and xit are made to return test_t.
|
||||
test_t test;
|
||||
test._disabled = true;
|
||||
test._label = label;
|
||||
test._test_method = test_method;
|
||||
return make_testbundle("", {test});
|
||||
}
|
||||
|
||||
testresults_t execute(testbundle_t bundle) {
|
||||
testresults_t results;
|
||||
|
||||
if (bundle._disabled) {
|
||||
// TODO: recursively report all tests as skipped.
|
||||
} else {
|
||||
try {
|
||||
// TODO: Try to do this in parallel where possible
|
||||
if (bundle._beforeAll.has_value()) {
|
||||
bundle._beforeAll.value()();
|
||||
}
|
||||
for_each(bundle._children.begin(), bundle._children.end(), [&results](testbundle_t bundle) {
|
||||
// TODO: Find a way to make child tests get our beforeEach and afterEach callbacks.
|
||||
results += execute(bundle);
|
||||
});
|
||||
// TODO: Consider capturing these callbacks differently. Without the bundle? By value?
|
||||
for_each(bundle._tests.begin(), bundle._tests.end(), [&results, &bundle](test_t test) {
|
||||
if (bundle._beforeEach.has_value()) {
|
||||
bundle._beforeEach.value()();
|
||||
}
|
||||
results += execute(test);
|
||||
if (bundle._afterEach.has_value()) {
|
||||
bundle._afterEach.value()();
|
||||
}
|
||||
});
|
||||
if (bundle._afterAll.has_value()) {
|
||||
bundle._afterAll.value()();
|
||||
}
|
||||
} catch(...) {
|
||||
// TODO: Log this and mark the tests as failed.
|
||||
// Report as much info as possible. This likely means something happened in beforeAll() or afterAll().
|
||||
}
|
||||
}
|
||||
throw unimplemented_function_error("execute(testbundle_t)");
|
||||
}
|
||||
|
||||
testresults_t execute(test_t test) {
|
||||
int status = 0;
|
||||
if (test._disabled) {
|
||||
|
||||
}
|
||||
|
||||
/*
|
||||
status = PASSED (PASSED, FAILED, SKIPPED (PENDING | DISABLED), QUEUED, RUNNING) QUEUED and RUNNING might not make sense.
|
||||
If the test is marked as disabled (xit) then record it as a skipped test and return.
|
||||
try {
|
||||
test.beforeEach();
|
||||
run the test function
|
||||
// Successful completion of the test method is recorded as a passed test.
|
||||
// Pending tests throw a test_pending exception - These are recorded as skipped tests.
|
||||
// Failed expects throw a test_failed exception - These are recorded as failed tests.
|
||||
// Runtime errors and catchall should be caught last of all. - These are recorded as failed tests.
|
||||
test.afterEach();
|
||||
|
||||
} catch (test_pending_exception ex) {
|
||||
status = SKIPPED;
|
||||
} catch (test_failed_exception ex) {
|
||||
status = FAILED
|
||||
} catch (exception ex) {
|
||||
status = FAILED
|
||||
} catch (...) {
|
||||
status = FAILED
|
||||
} finally {
|
||||
maybe some kind of cleanup
|
||||
}
|
||||
|
||||
return make_testresults(...) // based on the status.
|
||||
*/
|
||||
throw unimplemented_function_error("execute(test_t)");
|
||||
}
|
||||
|
||||
optional<configure_fn> combine(optional<configure_fn> first, optional<configure_fn> second) {
|
||||
if (first.has_value()) {
|
||||
auto first_v = first.value();
|
||||
if (second.has_value()) {
|
||||
auto second_v = second.value();
|
||||
return [first_v, second_v]() {
|
||||
first_v();
|
||||
second_v();
|
||||
};
|
||||
} else {
|
||||
return first;
|
||||
}
|
||||
} else {
|
||||
return second;
|
||||
}
|
||||
}
|
||||
|
||||
testbundle_t describe(const string& label, const make_testbundle_fn& make_tests, optional<describeoptions_t> options) {
|
||||
testbundle_t bundle = make_tests();
|
||||
bundle._label = label;
|
||||
bundle._disabled = false;
|
||||
if (options.has_value()) {
|
||||
describeoptions_t options_v = options.value();
|
||||
bundle._afterAll = combine(options_v.getAfterAll(), bundle._afterAll);
|
||||
bundle._afterEach = combine(options_v.getAfterEach(), bundle._afterEach);
|
||||
bundle._beforeAll = combine(options_v.getBeforeAll(), bundle._beforeAll);
|
||||
bundle._beforeEach = combine(options_v.getBeforeEach(), bundle._beforeEach);
|
||||
}
|
||||
return bundle;
|
||||
}
|
||||
|
||||
testbundle_t make_testbundle(const string& label, const vector<test_t>& tests) {
|
||||
testbundle_t bundle;
|
||||
bundle._label = label;
|
||||
bundle._tests.clear();
|
||||
for_each(tests.begin(), tests.end(), [&bundle](test_t test){
|
||||
bundle._tests.push_back(test);
|
||||
});
|
||||
return bundle;
|
||||
}
|
||||
|
||||
optional<configure_fn> describeoptions_t::getAfterAll() const {
|
||||
return this->_afterAll;
|
||||
}
|
||||
|
||||
optional<configure_fn> describeoptions_t::getAfterEach() const {
|
||||
return this->_afterEach;
|
||||
}
|
||||
|
||||
optional<configure_fn> describeoptions_t::getBeforeAll() const {
|
||||
return this->_beforeAll;
|
||||
}
|
||||
|
||||
optional<configure_fn> describeoptions_t::getBeforeEach() const {
|
||||
return this->_beforeEach;
|
||||
}
|
||||
|
||||
// TODO: Use these to make the unimplemented_* errors simpler to call.
|
||||
// For this function
|
||||
// testbundle_t describe(const std::string& label, const make_testbundle_fn& make_tests, std::optional<describeoptions_t> options)
|
||||
// __PRETTY_FUNCTION__
|
||||
// Unimplemented function: JTest::testbundle_t JTest::describe(const std::string &, const JTest::make_testbundle_fn &, std::optional<describeoptions_t>)
|
||||
// __FUNCSIG__ is not defined on clang++
|
||||
// __func__
|
||||
// describe
|
||||
// __LINE__ is an integer
|
||||
//
|
||||
// __FILE__
|
||||
// examples/JSTest.cpp
|
||||
// __FUNCTION__
|
||||
// describe
|
||||
}
|
||||
@@ -1,85 +0,0 @@
|
||||
#include <iostream> // TODO: Maybe just ostream.
|
||||
|
||||
namespace JTest {
|
||||
using std::ostream;
|
||||
// TODO: Consider making testresults_t a class so we can hide the vectors behind accessor methods void add(...), T get(), vector<T> get(uint32_t index)
|
||||
|
||||
struct testbundle_t;
|
||||
struct test_t;
|
||||
typedef std::function<void()> test_fn;
|
||||
typedef std::function<testbundle_t()> make_testbundle_fn;
|
||||
typedef std::function<void()> configure_fn;
|
||||
|
||||
struct testresults_t {
|
||||
uint32_t total;
|
||||
uint32_t skipped;
|
||||
uint32_t passed;
|
||||
uint32_t failed;
|
||||
// vector<error_t> errors;
|
||||
// vector<testfailure_t> failures;
|
||||
// vector<testmethod_t> skipped;
|
||||
};
|
||||
|
||||
struct test_t {
|
||||
std::string _label;
|
||||
test_fn _test_method;
|
||||
bool _disabled;
|
||||
};
|
||||
|
||||
struct testbundle_t {
|
||||
std::string _label;
|
||||
std::vector<test_t> _tests;
|
||||
std::vector<testbundle_t> _children;
|
||||
std::optional<configure_fn> _beforeEach;
|
||||
std::optional<configure_fn> _afterEach;
|
||||
std::optional<configure_fn> _beforeAll;
|
||||
std::optional<configure_fn> _afterAll;
|
||||
bool _disabled;
|
||||
};
|
||||
|
||||
struct describeoptions_t {
|
||||
// TODO: Should these configure_fn params be const and/or &?
|
||||
describeoptions_t& beforeEach(configure_fn);
|
||||
describeoptions_t& afterEach(configure_fn);
|
||||
describeoptions_t& beforeAll(configure_fn);
|
||||
describeoptions_t& afterAll(configure_fn);
|
||||
std::optional<configure_fn> getBeforeEach() const;
|
||||
std::optional<configure_fn> getAfterEach() const;
|
||||
std::optional<configure_fn> getBeforeAll() const;
|
||||
std::optional<configure_fn> getAfterAll() const;
|
||||
private:
|
||||
std::optional<configure_fn> _beforeEach;
|
||||
std::optional<configure_fn> _afterEach;
|
||||
std::optional<configure_fn> _beforeAll;
|
||||
std::optional<configure_fn> _afterAll;
|
||||
};
|
||||
struct testoptions_t {};
|
||||
|
||||
testresults_t operator+(const testresults_t& left, const testresults_t& right);
|
||||
|
||||
testresults_t& operator+=(testresults_t& left, const testresults_t& right);
|
||||
|
||||
testresults_t make_testresults();
|
||||
testresults_t make_testresults(uint32_t total, uint32_t skipped, uint32_t passed, uint32_t failed);
|
||||
testresults_t add(const testresults_t&, const testresults_t&);
|
||||
void print_test_results(const testresults_t&, ostream&);
|
||||
|
||||
// Executes the tests in tests. Possibly in parallel. Will block until all async tests have completed.
|
||||
testresults_t execute(testbundle_t tests);
|
||||
testresults_t execute(test_t test);
|
||||
|
||||
//
|
||||
testbundle_t describe(const std::string& label, const make_testbundle_fn& make_tests, std::optional<describeoptions_t> options = std::nullopt);
|
||||
testbundle_t xdescribe(const std::string& label, const make_testbundle_fn& make_tests, std::optional<describeoptions_t> options = std::nullopt);
|
||||
|
||||
testbundle_t make_testbundle(const std::vector<testbundle_t>& tests, const describeoptions_t& options);
|
||||
|
||||
// TODO: Make this return a test_t instead.
|
||||
testbundle_t it(const std::string& label, const test_fn& test_method, std::optional<testoptions_t> options = std::nullopt);
|
||||
testbundle_t xit(const std::string& label, const test_fn& test_method, std::optional<testoptions_t> options = std::nullopt);
|
||||
|
||||
describeoptions_t make_describeoptions();
|
||||
|
||||
testbundle_t make_testbundle(const std::string& label, const std::vector<test_t>& tests);
|
||||
|
||||
}
|
||||
@@ -1,12 +1,10 @@
|
||||
// #include <JTest.h>
|
||||
#include "JTest.h"
|
||||
#include "ClassToTest.h"
|
||||
#include <JTest/JTest.h>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <tuple>
|
||||
#include <stdexcept>
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
#include "ClassToTest.h"
|
||||
|
||||
using namespace JTest;
|
||||
using namespace MyNS;
|
||||
@@ -16,27 +14,31 @@ using std::vector;
|
||||
using std::cout;
|
||||
using std::exception;
|
||||
using std::endl;
|
||||
using std::runtime_error;
|
||||
|
||||
// const vector<string>& might stop being a reference
|
||||
testresults_t test_ClassToTest_main(const vector<string>& argv) {
|
||||
TestResults test_ClassToTest_main(const vector<string>& argv) {
|
||||
return execute(
|
||||
describe("ClassToTest", [](){
|
||||
return make_testbundle(
|
||||
return TestBundle(
|
||||
{
|
||||
it("should do the thing", [](){
|
||||
// Throw exception if somethings goes wrong
|
||||
}),
|
||||
|
||||
it("should do the other thing", [](){
|
||||
pending("we havent made the other thing yet");
|
||||
}),
|
||||
|
||||
it("should not do the bad thing", [](){
|
||||
fail("it did the bad thing");
|
||||
}),
|
||||
|
||||
it("should throw an exception if we do the other bad thing", [](){
|
||||
throw runtime_error("Bad thing happen.");
|
||||
}),
|
||||
},
|
||||
make_describeoptions()
|
||||
DescribeOptions()
|
||||
.beforeEach([](){})
|
||||
.afterEach([](){})
|
||||
.beforeAll([](){})
|
||||
@@ -45,46 +47,46 @@ testresults_t test_ClassToTest_main(const vector<string>& argv) {
|
||||
);
|
||||
}
|
||||
|
||||
testresults_t test_temp(const vector<string>& argv) {
|
||||
TestResults test_temp(const vector<string>& args) {
|
||||
return execute(
|
||||
describe("ClassToTest", [](){
|
||||
return make_testbundle({
|
||||
return TestBundle({
|
||||
describe("FeatureToTest", [](){
|
||||
return make_testbundle({
|
||||
return TestBundle({
|
||||
// it("should do the thing", [](){
|
||||
|
||||
// }),
|
||||
// it("should not do the other thing", [](){
|
||||
|
||||
// }),
|
||||
}, make_describeoptions());
|
||||
}, DescribeOptions());
|
||||
}),
|
||||
}, make_describeoptions());
|
||||
}, DescribeOptions());
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
// Exmple of nested describes.
|
||||
testresults_t test_something(const vector<string>& argv) {
|
||||
TestResults test_something(const vector<string>& argv) {
|
||||
return execute(
|
||||
describe("", [](){
|
||||
return make_testbundle({
|
||||
return TestBundle({
|
||||
describe("", [](){
|
||||
return make_testbundle({
|
||||
return TestBundle({
|
||||
|
||||
}, make_describeoptions());
|
||||
}, make_describeoptions()),
|
||||
}, make_describeoptions());
|
||||
}, DescribeOptions());
|
||||
}, DescribeOptions()),
|
||||
}, DescribeOptions());
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
testresults_t test_ClassToTest_2(const vector<string>& argv) {
|
||||
TestResults test_ClassToTest_2(const vector<string>& argv) {
|
||||
return execute(
|
||||
describe("ClassToTest", [](){
|
||||
return make_testbundle({
|
||||
return TestBundle({
|
||||
},
|
||||
make_describeoptions()
|
||||
DescribeOptions()
|
||||
.beforeEach([](){})
|
||||
.afterEach([](){})
|
||||
.beforeAll([](){})
|
||||
@@ -92,118 +94,23 @@ testresults_t test_ClassToTest_2(const vector<string>& argv) {
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
class Expectable {
|
||||
public:
|
||||
Expectable(const T& actual);
|
||||
virtual ~Expectable();
|
||||
virtual void toEqual(const T& value);
|
||||
virtual void toNotEqual(const T& value);
|
||||
// Maybe these funcs should return tuple<bool, string> instead.
|
||||
virtual void toBe(std::function<std::tuple<bool, std::optional<std::string>>(const T& actual)> evaluator);
|
||||
//virtual void toBeNull();
|
||||
//virtual void toNotBeNull();
|
||||
//virtual void toThrow(...);
|
||||
private:
|
||||
const T& actual_;
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
Expectable<T>::Expectable(const T& actual)
|
||||
: actual_(actual) {}
|
||||
|
||||
template<typename T>
|
||||
Expectable<T>::~Expectable() {}
|
||||
|
||||
class FailedExpectation: std::runtime_error {
|
||||
public:
|
||||
FailedExpectation(const std::string& message);
|
||||
virtual ~FailedExpectation();
|
||||
};
|
||||
|
||||
FailedExpectation::FailedExpectation(const std::string& message)
|
||||
: runtime_error(message) {
|
||||
}
|
||||
|
||||
FailedExpectation::~FailedExpectation() {}
|
||||
|
||||
template<typename T>
|
||||
void Expectable<T>::toEqual(const T& value) {
|
||||
if (actual_ != value) {
|
||||
std::ostringstream os;
|
||||
os << "Expected: " << actual_ << " to be " << value;
|
||||
throw(FailedExpectation(os.str()));
|
||||
}
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void Expectable<T>::toNotEqual(const T& value) {
|
||||
if (actual_ == value) {
|
||||
std::ostringstream os;
|
||||
os << "Expected: " << actual_ << " to not be " << value;
|
||||
throw(FailedExpectation(os.str()));
|
||||
}
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void Expectable<T>::toBe(std::function<std::tuple<bool, std::optional<std::string>>(const T& actual)> evaluator) {
|
||||
auto result = evaluator(actual_);
|
||||
if (!std::get<0>(result)) {
|
||||
std::ostringstream os;
|
||||
// std::optional<std::string> message = std::get<1>;
|
||||
std::optional<std::string> message = std::get<1>(result);
|
||||
if (message.has_value()) {
|
||||
os << "Expected: " << actual_ << " to pass validation, but " << message.value();
|
||||
} else {
|
||||
os << "Expected: " << actual_ << " to pass validation.";
|
||||
}
|
||||
throw(FailedExpectation(os.str()));
|
||||
}
|
||||
}
|
||||
|
||||
// template<typename T>
|
||||
// void Expectable<T>::toBeNull() {
|
||||
// if (actual_ != nullptr) {
|
||||
// std::ostringstream os;
|
||||
// os << "Expected null, but got " << actual_;
|
||||
// throw(FailedExpectation(os.str()));
|
||||
// }
|
||||
// }
|
||||
|
||||
// template<typename T>
|
||||
// void Expectable<T>::toNotBeNull() {
|
||||
// if (actual_ == nullptr) {
|
||||
// throw(FailedExpectation("Expected non-null, but got null"));
|
||||
// }
|
||||
// }
|
||||
|
||||
// template<typename T>
|
||||
// void Expectable<T>::toThrow(...) {}
|
||||
|
||||
template<typename T>
|
||||
Expectable<T> expect(const T& actual);
|
||||
|
||||
template<typename T>
|
||||
Expectable<T> expect(const T& actual) {
|
||||
return Expectable<T>(actual);
|
||||
}
|
||||
|
||||
int MyAddFunction(int a, int b) {
|
||||
return 0;
|
||||
return a+b;
|
||||
}
|
||||
|
||||
testresults_t test_BasicExpectable_main(const vector<string>& argv) {
|
||||
TestResults test_BasicExpectable_main(const vector<string>& argv) {
|
||||
return execute(
|
||||
describe("MyAddFunction", [](){
|
||||
return make_testbundle({
|
||||
return TestBundle({
|
||||
it("should add 2 and 2 to get 4", [](){
|
||||
expect(MyAddFunction(2, 2)).toEqual(4);
|
||||
}),
|
||||
it("should ", [](){
|
||||
it("should fail", [](){
|
||||
fail("because I said so");
|
||||
// Throw exception if somethings goes wrong
|
||||
}),
|
||||
}, make_describeoptions());
|
||||
}, DescribeOptions());
|
||||
}));
|
||||
}
|
||||
|
||||
@@ -211,12 +118,15 @@ testresults_t test_BasicExpectable_main(const vector<string>& argv) {
|
||||
int main(int argc, char* argv[]) {
|
||||
try {
|
||||
vector<string> args;
|
||||
testresults_t results;
|
||||
TestResults results;
|
||||
|
||||
results = add(results, test_ClassToTest_main(args));
|
||||
results = add(results, test_BasicExpectable_main(args));
|
||||
results += test_ClassToTest_main(args);
|
||||
// results += test_BasicExpectable_main(args);
|
||||
// results += test_ClassToTest_2(args);
|
||||
// results += test_something(args);
|
||||
// results += test_temp(args);
|
||||
|
||||
print_test_results(results, cout);
|
||||
cout << results << endl;
|
||||
}
|
||||
catch (std::runtime_error ex) {
|
||||
std::cout << "Unhandled exception: " << ex.what() << endl;
|
||||
|
||||
Binary file not shown.
Reference in New Issue
Block a user