Changes from wstring to string expecting UTF-8.

This commit is contained in:
2023-04-07 12:04:12 -07:00
parent 347f311c2e
commit 2560265fcb
3 changed files with 57 additions and 52 deletions

View File

@@ -2,10 +2,10 @@
#include <stdexcept> #include <stdexcept>
namespace JTest { namespace JTest {
using std::wostream; using std::ostream;
using std::endl; using std::endl;
using std::vector; using std::vector;
using std::wstring; using std::string;
using std::runtime_error; using std::runtime_error;
using std::string; using std::string;
using std::optional; using std::optional;
@@ -34,9 +34,9 @@ namespace JTest {
return {l.total + r.total, l.skipped + r.skipped, l.passed + r.passed, l.failed + r.failed}; 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, wostream& out) { void print_test_results(const testresults_t& results, ostream& out) {
out << L"Tests: " << results.total << endl; out << "Tests: " << results.total << endl;
out << L"Failed: " << results.failed << ", Passed: " << results.passed << ", Skipped: " << results.skipped << endl; out << "Failed: " << results.failed << ", Passed: " << results.passed << ", Skipped: " << results.skipped << endl;
} }
testresults_t operator+(const testresults_t& left, const testresults_t& right) { testresults_t operator+(const testresults_t& left, const testresults_t& right) {
@@ -87,10 +87,14 @@ namespace JTest {
return {}; return {};
} }
testbundle_t it(const wstring& label, const make_test_fn& test_method, optional<testoptions_t> options) { // 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. // TODO: Stop ignoring options.
test_t test = test_method(); test_t test;
return make_testbundle(label, {test}); test._disabled = false;
test._label = label;
test._test_method = test_method;
return make_testbundle("", {test});
} }
testresults_t execute(testbundle_t bundle) { testresults_t execute(testbundle_t bundle) {
@@ -173,7 +177,7 @@ namespace JTest {
return bundle; return bundle;
} }
testbundle_t make_testbundle(const wstring& label, const vector<test_t>& tests) { testbundle_t make_testbundle(const string& label, const vector<test_t>& tests) {
testbundle_t bundle; testbundle_t bundle;
bundle._label = label; bundle._label = label;
bundle._tests.clear(); bundle._tests.clear();
@@ -201,9 +205,9 @@ namespace JTest {
// TODO: Use these to make the unimplemented_* errors simpler to call. // TODO: Use these to make the unimplemented_* errors simpler to call.
// For this function // For this function
// testbundle_t describe(const std::wstring& label, const make_testbundle_fn& make_tests, std::optional<describeoptions_t> options) // testbundle_t describe(const std::string& label, const make_testbundle_fn& make_tests, std::optional<describeoptions_t> options)
// __PRETTY_FUNCTION__ // __PRETTY_FUNCTION__
// Unimplemented function: JTest::testbundle_t JTest::describe(const std::wstring &, const JTest::make_testbundle_fn &, std::optional<describeoptions_t>) // 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++ // __FUNCSIG__ is not defined on clang++
// __func__ // __func__
// describe // describe

View File

@@ -1,12 +1,12 @@
#include <iostream> // TODO: Maybe just ostream. #include <iostream> // TODO: Maybe just ostream.
namespace JTest { namespace JTest {
using std::wostream; 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) // 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 testbundle_t;
struct test_t; struct test_t;
typedef std::function<test_t()> make_test_fn; typedef std::function<void()> test_fn;
typedef std::function<testbundle_t()> make_testbundle_fn; typedef std::function<testbundle_t()> make_testbundle_fn;
typedef std::function<void()> configure_fn; typedef std::function<void()> configure_fn;
@@ -20,15 +20,20 @@ namespace JTest {
// vector<testmethod_t> skipped; // vector<testmethod_t> skipped;
}; };
struct test_t {}; std::string _label;
test_fn _test_method;
bool _disabled;
};
struct testbundle_t { struct testbundle_t {
std::wstring _label; std::string _label;
std::vector<test_t> _tests; std::vector<test_t> _tests;
std::vector<testbundle_t> _children; std::vector<testbundle_t> _children;
std::optional<configure_fn> _beforeEach; std::optional<configure_fn> _beforeEach;
std::optional<configure_fn> _afterEach; std::optional<configure_fn> _afterEach;
std::optional<configure_fn> _beforeAll; std::optional<configure_fn> _beforeAll;
std::optional<configure_fn> _afterAll; std::optional<configure_fn> _afterAll;
bool _disabled;
}; };
struct describeoptions_t { struct describeoptions_t {
@@ -56,25 +61,24 @@ namespace JTest {
testresults_t make_testresults(); testresults_t make_testresults();
testresults_t make_testresults(uint32_t total, uint32_t skipped, uint32_t passed, uint32_t failed); 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&); testresults_t add(const testresults_t&, const testresults_t&);
void print_test_results(const testresults_t&, wostream&); void print_test_results(const testresults_t&, ostream&);
// 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);
testresults_t execute(test_t test);
// //
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::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); 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);
// 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. testbundle_t it(const std::string& label, const test_fn& test_method, std::optional<testoptions_t> options = std::nullopt);
// The label should be extracted from the test_t it returns. testbundle_t xit(const std::string& label, const test_fn& test_method, std::optional<testoptions_t> options = std::nullopt);
// The testresults_t should be constructed based on the try/catch block and whether this was called as it/xit.
testbundle_t it(const std::wstring& label, const make_test_fn& test_method, std::optional<testoptions_t> options = std::nullopt);
describeoptions_t make_describeoptions(); describeoptions_t make_describeoptions();
testbundle_t make_testbundle(const std::wstring& label, const std::vector<test_t>& tests); testbundle_t make_testbundle(const std::string& label, const std::vector<test_t>& tests);
} }

View File

@@ -9,19 +9,19 @@
using namespace JTest; using namespace JTest;
using namespace MyNS; using namespace MyNS;
using std::wstring; using std::string;
using std::vector; using std::vector;
using std::wcout; using std::cout;
using std::exception; using std::exception;
using std::endl; using std::endl;
// const vector<wstring>& might stop being a reference // const vector<string>& might stop being a reference
testresults_t test_ClassToTest_main(int argc, const vector<wstring>& argv) { testresults_t test_ClassToTest_main(const vector<string>& argv) {
return execute( return execute(
describe(L"ClassToTest", [](){ describe("ClassToTest", [](){
return make_testbundle( return make_testbundle(
{ {
it(L"should do the thing", [](){ it("should do the thing", [](){
// Throw exception if somethings goes wrong // Throw exception if somethings goes wrong
@@ -29,16 +29,13 @@ testresults_t test_ClassToTest_main(int argc, const vector<wstring>& argv) {
return (test_t){}; return (test_t){};
}), }),
it(L"should do the other thing", [](){ it("should do the other thing", [](){
return (test_t){};
}), }),
it(L"should not do the bad thing", [](){ it("should not do the bad thing", [](){
return (test_t){};
}), }),
it(L"should throw an exception if we do the other bad thing", [](){ it("should throw an exception if we do the other bad thing", [](){
return (test_t){};
}), }),
}, },
make_describeoptions() make_describeoptions()
@@ -50,16 +47,16 @@ testresults_t test_ClassToTest_main(int argc, const vector<wstring>& argv) {
); );
} }
testresults_t test_temp(int argc, const vector<wstring>& argv) { testresults_t test_temp(const vector<string>& argv) {
return execute( return execute(
describe(L"ClassToTest", [](){ describe("ClassToTest", [](){
return make_testbundle({ return make_testbundle({
describe(L"FeatureToTest", [](){ describe("FeatureToTest", [](){
return make_testbundle({ return make_testbundle({
// it(L"should do the thing", [](){ // it("should do the thing", [](){
// }), // }),
// it(L"should not do the other thing", [](){ // it("should not do the other thing", [](){
// }), // }),
}, make_describeoptions()); }, make_describeoptions());
@@ -70,11 +67,11 @@ testresults_t test_temp(int argc, const vector<wstring>& argv) {
} }
// Exmple of nested describes. // Exmple of nested describes.
testresults_t test_something(int argc, const vector<wstring>& argv) { testresults_t test_something(const vector<string>& argv) {
return execute( return execute(
describe(L"", [](){ describe("", [](){
return make_testbundle({ return make_testbundle({
describe(L"", [](){ describe("", [](){
return make_testbundle({ return make_testbundle({
}, make_describeoptions()); }, make_describeoptions());
@@ -84,9 +81,9 @@ testresults_t test_something(int argc, const vector<wstring>& argv) {
); );
} }
testresults_t test_ClassToTest_2(int argc, const vector<wstring>& argv) { testresults_t test_ClassToTest_2(const vector<string>& argv) {
return execute( return execute(
describe(L"ClassToTest", [](){ describe("ClassToTest", [](){
return make_testbundle({ return make_testbundle({
}, },
make_describeoptions() make_describeoptions()
@@ -101,15 +98,15 @@ testresults_t test_ClassToTest_2(int argc, const vector<wstring>& argv) {
// Dummy test harness // Dummy test harness
int main(int argc, char* argv[]) { int main(int argc, char* argv[]) {
try { try {
testresults_t results = make_testresults(); vector<string> args;
testresults_t results;
results = add(results, test_ClassToTest_main(0, vector<wstring>())); results = add(results, test_ClassToTest_main(args));
print_test_results(results, wcout); print_test_results(results, cout);
} }
catch (std::runtime_error ex) { catch (std::runtime_error ex) {
std::cout << ex.what() << endl; std::cout << "Unhandled exception: " << ex.what() << endl;
// wcout << L"Unhandled exception: " << ex.what() << endl;
} }
return 0; return 0;