From 347f311c2ed9b2a7b261b84da90756ed5390f8ca Mon Sep 17 00:00:00 2001 From: Tom Hicks Date: Mon, 3 Apr 2023 00:55:03 -0700 Subject: [PATCH] Implemented methods up to execute. --- examples/JTest.cpp | 144 +++++++++++++++++++++++++++++++++++++++++---- examples/JTest.h | 66 +++++++++++---------- 2 files changed, 167 insertions(+), 43 deletions(-) diff --git a/examples/JTest.cpp b/examples/JTest.cpp index 4b78bf8..fbde060 100644 --- a/examples/JTest.cpp +++ b/examples/JTest.cpp @@ -48,37 +48,155 @@ namespace JTest { return left; } - testbundle_t make_testbundle(const vector&, const describeoptions_t& options) { - throw unimplemented_function_error("make_testbundle(const vector&, const describeoptions_t&)"); + testbundle_t make_testbundle(const vector& 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) { - throw unimplemented_method_error("describeoptions_t", "beforeEach(configure_fn)"); + describeoptions_t& describeoptions_t::beforeEach(configure_fn fn) { + this->_beforeEach = fn; + return *this; } - describeoptions_t& describeoptions_t::afterEach(configure_fn) { - throw unimplemented_method_error("describeoptions_t", "afterEach(configure_fn)"); + describeoptions_t& describeoptions_t::afterEach(configure_fn fn) { + this->_afterEach = fn; + return *this; } - describeoptions_t& describeoptions_t::beforeAll(configure_fn) { - throw unimplemented_method_error("describeoptions_t", "beforeAll(configure_fn)"); + describeoptions_t& describeoptions_t::beforeAll(configure_fn fn) { + this->_beforeAll = fn; + return *this; } - describeoptions_t& describeoptions_t::afterAll(configure_fn) { - throw unimplemented_method_error("describeoptions_t", "afterAll(configure_fn)"); + describeoptions_t& describeoptions_t::afterAll(configure_fn fn) { + this->_afterAll = fn; + return *this; } describeoptions_t make_describeoptions() { return {}; } testbundle_t it(const wstring& label, const make_test_fn& test_method, optional options) { - throw unimplemented_function_error("it(const wstring&, const make_test_fn&, optional)"); + // 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)"); } + 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 combine(optional first, optional 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 options) { - throw unimplemented_function_error("describe(const wstring&, const make_testbundle_fn&, optional)"); + 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& 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 describeoptions_t::getAfterAll() const { + return this->_afterAll; + } + + optional describeoptions_t::getAfterEach() const { + return this->_afterEach; + } + + optional describeoptions_t::getBeforeAll() const { + return this->_beforeAll; + } + + optional describeoptions_t::getBeforeEach() const { + return this->_beforeEach; } // TODO: Use these to make the unimplemented_* errors simpler to call. diff --git a/examples/JTest.h b/examples/JTest.h index 0c83c37..0d3a34e 100644 --- a/examples/JTest.h +++ b/examples/JTest.h @@ -4,6 +4,12 @@ namespace JTest { using std::wostream; // TODO: Consider making testresults_t a class so we can hide the vectors behind accessor methods void add(...), T get(), vector get(uint32_t index) + struct testbundle_t; + struct test_t; + typedef std::function make_test_fn; + typedef std::function make_testbundle_fn; + typedef std::function configure_fn; + struct testresults_t { uint32_t total; uint32_t skipped; @@ -14,6 +20,35 @@ namespace JTest { // vector skipped; }; + struct test_t {}; + struct testbundle_t { + std::wstring _label; + std::vector _tests; + std::vector _children; + std::optional _beforeEach; + std::optional _afterEach; + std::optional _beforeAll; + std::optional _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 getBeforeEach() const; + std::optional getAfterEach() const; + std::optional getBeforeAll() const; + std::optional getAfterAll() const; + private: + std::optional _beforeEach; + std::optional _afterEach; + std::optional _beforeAll; + std::optional _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); @@ -23,41 +58,15 @@ namespace JTest { testresults_t add(const testresults_t&, const testresults_t&); 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. testresults_t execute(testbundle_t tests); - - typedef std::function make_testbundle_fn; - - typedef std::function 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 options = std::nullopt); - struct testoptions_t {}; - - - struct test_t {}; - testbundle_t make_testbundle(const std::vector& tests, const describeoptions_t& options); // testbundle_t make_testbundle( initializer_list tests, const testoptions_t& options); - typedef std::function make_test_fn; - // 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. // The label should be extracted from the test_t it returns. @@ -66,9 +75,6 @@ namespace JTest { describeoptions_t make_describeoptions(); - - - - + testbundle_t make_testbundle(const std::wstring& label, const std::vector& tests); }