Implemented methods up to execute.
This commit is contained in:
@@ -48,37 +48,155 @@ namespace JTest {
|
|||||||
return left;
|
return left;
|
||||||
}
|
}
|
||||||
|
|
||||||
testbundle_t make_testbundle(const vector<testbundle_t>&, const describeoptions_t& options) {
|
testbundle_t make_testbundle(const vector<testbundle_t>& bundles, const describeoptions_t& options) {
|
||||||
throw unimplemented_function_error("make_testbundle(const vector<testbundle_t>&, const describeoptions_t&)");
|
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) {
|
describeoptions_t& describeoptions_t::beforeEach(configure_fn fn) {
|
||||||
throw unimplemented_method_error("describeoptions_t", "beforeEach(configure_fn)");
|
this->_beforeEach = fn;
|
||||||
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
describeoptions_t& describeoptions_t::afterEach(configure_fn) {
|
describeoptions_t& describeoptions_t::afterEach(configure_fn fn) {
|
||||||
throw unimplemented_method_error("describeoptions_t", "afterEach(configure_fn)");
|
this->_afterEach = fn;
|
||||||
|
return *this;
|
||||||
}
|
}
|
||||||
describeoptions_t& describeoptions_t::beforeAll(configure_fn) {
|
describeoptions_t& describeoptions_t::beforeAll(configure_fn fn) {
|
||||||
throw unimplemented_method_error("describeoptions_t", "beforeAll(configure_fn)");
|
this->_beforeAll = fn;
|
||||||
|
return *this;
|
||||||
}
|
}
|
||||||
describeoptions_t& describeoptions_t::afterAll(configure_fn) {
|
describeoptions_t& describeoptions_t::afterAll(configure_fn fn) {
|
||||||
throw unimplemented_method_error("describeoptions_t", "afterAll(configure_fn)");
|
this->_afterAll = fn;
|
||||||
|
return *this;
|
||||||
}
|
}
|
||||||
describeoptions_t make_describeoptions() {
|
describeoptions_t make_describeoptions() {
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
testbundle_t it(const wstring& label, const make_test_fn& test_method, optional<testoptions_t> options) {
|
testbundle_t it(const wstring& label, const make_test_fn& test_method, optional<testoptions_t> options) {
|
||||||
throw unimplemented_function_error("it(const wstring&, const make_test_fn&, optional<testoptions_t>)");
|
// TODO: Stop ignoring options.
|
||||||
|
test_t test = test_method();
|
||||||
|
return make_testbundle(label, {test});
|
||||||
}
|
}
|
||||||
|
|
||||||
testresults_t execute(testbundle_t tests) {
|
testresults_t execute(testbundle_t bundle) {
|
||||||
|
/*
|
||||||
|
Try to do this in parallel where possible
|
||||||
|
|
||||||
|
If the bundle is marked as disabled (xdescribe) report all the tests in it and child bundles as SKIPPED.
|
||||||
|
testresults_t results;
|
||||||
|
try {
|
||||||
|
bundle.beforeAll();
|
||||||
|
for each child of bundle.children
|
||||||
|
results += execute(child)
|
||||||
|
for each test of bundle.tests
|
||||||
|
results += execute(test)
|
||||||
|
bundle.afterAll();
|
||||||
|
} catch (...) {
|
||||||
|
Report as much info as possible. This likely means something happened in beforeAll() or afterAll().
|
||||||
|
}
|
||||||
|
*/
|
||||||
throw unimplemented_function_error("execute(testbundle_t)");
|
throw unimplemented_function_error("execute(testbundle_t)");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
testresults_t execute(test_t test) {
|
||||||
|
/*
|
||||||
|
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 wstring& label, const make_testbundle_fn& make_tests, optional<describeoptions_t> options) {
|
testbundle_t describe(const wstring& label, const make_testbundle_fn& make_tests, optional<describeoptions_t> options) {
|
||||||
throw unimplemented_function_error("describe(const wstring&, const make_testbundle_fn&, optional<describeoptions_t>)");
|
testbundle_t bundle = make_tests();
|
||||||
|
bundle._label = label;
|
||||||
|
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 wstring& 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.
|
// TODO: Use these to make the unimplemented_* errors simpler to call.
|
||||||
|
|||||||
@@ -4,6 +4,12 @@ namespace JTest {
|
|||||||
using std::wostream;
|
using std::wostream;
|
||||||
// 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)
|
// 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<test_t()> make_test_fn;
|
||||||
|
typedef std::function<testbundle_t()> make_testbundle_fn;
|
||||||
|
typedef std::function<void()> configure_fn;
|
||||||
|
|
||||||
struct testresults_t {
|
struct testresults_t {
|
||||||
uint32_t total;
|
uint32_t total;
|
||||||
uint32_t skipped;
|
uint32_t skipped;
|
||||||
@@ -14,6 +20,35 @@ namespace JTest {
|
|||||||
// vector<testmethod_t> skipped;
|
// vector<testmethod_t> skipped;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
struct test_t {};
|
||||||
|
struct testbundle_t {
|
||||||
|
std::wstring _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;
|
||||||
|
};
|
||||||
|
|
||||||
|
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+(const testresults_t& left, const testresults_t& right);
|
||||||
|
|
||||||
testresults_t& operator+=(testresults_t& left, const testresults_t& right);
|
testresults_t& operator+=(testresults_t& left, const testresults_t& right);
|
||||||
@@ -23,41 +58,15 @@ namespace JTest {
|
|||||||
testresults_t add(const testresults_t&, const testresults_t&);
|
testresults_t add(const testresults_t&, const testresults_t&);
|
||||||
void print_test_results(const testresults_t&, wostream&);
|
void print_test_results(const testresults_t&, wostream&);
|
||||||
|
|
||||||
struct testbundle_t {};
|
|
||||||
|
|
||||||
// Executes the tests in tests. Possibly in parallel. Will block until all async tests have completed.
|
// 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(testbundle_t tests);
|
||||||
|
|
||||||
|
|
||||||
typedef std::function<testbundle_t()> make_testbundle_fn;
|
|
||||||
|
|
||||||
typedef std::function<void()> configure_fn;
|
|
||||||
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);
|
|
||||||
private:
|
|
||||||
configure_fn _beforeEach;
|
|
||||||
configure_fn _afterEach;
|
|
||||||
configure_fn _beforeAll;
|
|
||||||
configure_fn _afterAll;
|
|
||||||
};
|
|
||||||
|
|
||||||
//
|
//
|
||||||
testbundle_t describe(const std::wstring& label, const make_testbundle_fn& make_tests, std::optional<describeoptions_t> options = std::nullopt);
|
testbundle_t describe(const std::wstring& label, const make_testbundle_fn& make_tests, std::optional<describeoptions_t> options = std::nullopt);
|
||||||
|
|
||||||
struct testoptions_t {};
|
|
||||||
|
|
||||||
|
|
||||||
struct test_t {};
|
|
||||||
|
|
||||||
testbundle_t make_testbundle(const std::vector<testbundle_t>& tests, const describeoptions_t& options);
|
testbundle_t make_testbundle(const std::vector<testbundle_t>& tests, const describeoptions_t& options);
|
||||||
// testbundle_t make_testbundle( initializer_list tests, const testoptions_t& options);
|
// testbundle_t make_testbundle( initializer_list tests, const testoptions_t& options);
|
||||||
|
|
||||||
typedef std::function<test_t()> make_test_fn;
|
|
||||||
|
|
||||||
// TODO: Make this return a test_t instead.
|
// TODO: Make this return a test_t instead.
|
||||||
// TOOD: Bake make_test_fn not need to return testresults_t. Method calls should be surrounded with try/catch.
|
// TOOD: Bake make_test_fn not need to return testresults_t. Method calls should be surrounded with try/catch.
|
||||||
// The label should be extracted from the test_t it returns.
|
// The label should be extracted from the test_t it returns.
|
||||||
@@ -66,9 +75,6 @@ namespace JTest {
|
|||||||
|
|
||||||
describeoptions_t make_describeoptions();
|
describeoptions_t make_describeoptions();
|
||||||
|
|
||||||
|
testbundle_t make_testbundle(const std::wstring& label, const std::vector<test_t>& tests);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user