Finally have something useful.

This commit is contained in:
2023-04-11 23:42:27 -07:00
parent 1a4a19b938
commit cdbe51a3cc
34 changed files with 1065 additions and 475 deletions

1
.gitignore vendored
View File

@@ -2,3 +2,4 @@
.vscode
build
tmp
*.o

View File

@@ -1,32 +1,39 @@
BUILD_DIR ?= build
ARCHS = -arch arm64 # -arch x86_64
INCLUDES = -I./include
CC = clang++
CCFLAGS += -std=c++17 -finput-charset=utf-8 -fdiagnostics-show-template-tree -fno-elide-type -g $(ARCHS) --stdlib=libc++
CCFLAGS += -std=c++17 -finput-charset=utf-8 -fdiagnostics-show-template-tree -fno-elide-type -g $(ARCHS) $(INCLUDES)
LD = clang++
LDFLAGS += $(ARCHS)
JTEST_HEADERS := $(wildcard include/JTest/*.h)
JTEST_SOURCES := $(wildcard src/JTest/*.cpp)
JTEST_OBJECTS := $(patsubst src/%.cpp, $(BUILD_DIR)/%.o, $(JTEST_SOURCES))
#$(patsubst src/%.cpp, $(BUILD_DIR)/%.o, $(JTEST_SOURCES))
.Phony: clean all $(BUILD_DIR)/test.txt
.Phony: clean all run
all: $(BUILD_DIR)/test.txt $(BUILD_DIR)/examples/example
@echo $(BUILD_DIR)
all: $(BUILD_DIR)/examples/example
clean:
rm -rf $(BUILD_DIR)
mkdir -p $(BUILD_DIR)/examples
@echo CC=$(CC)
@echo CCFLAGS=$(CCFLAGS)
@echo LD=$(LD)
@echo LDFLAGS=$(LDFLAGS)
mkdir -p $(BUILD_DIR)/JTest
# @echo JTEST_HEADERS = $(JTEST_HEADERS)
# @echo JTEST_SOURCES = $(JTEST_SOURCES)
# @echo JTEST_OBJECTS = $(JTEST_OBJECTS)
$(BUILD_DIR)/test.txt:
@echo $(BUILD_DIR)/test.txt
run: $(BUILD_DIR)/examples/example
$(BUILD_DIR)/examples/example
$(BUILD_DIR)/examples/example: $(BUILD_DIR)/examples/example.o $(BUILD_DIR)/examples/JTest.o
$(BUILD_DIR)/examples/example: $(BUILD_DIR)/examples/example.o $(JTEST_OBJECTS)
$(LD) $(LDFLAGS) -o $@ $^
$(BUILD_DIR)/examples/example.o: examples/example.cpp examples/ClassToTest.h examples/JTest.h
$(BUILD_DIR)/examples/%.o: examples/%.cpp examples/ClassToTest.h $(JTEST_HEADERS)
$(CC) $(CCFLAGS) -c -o $@ $<
$(BUILD_DIR)/examples/JTest.o: examples/JTest.cpp examples/JTest.h
$(BUILD_DIR)/examples/JTest.o: examples/JTest.cpp examples/JTest.h $(JTEST_HEADERS)
$(CC) $(CCFLAGS) -c -o $@ $<
$(BUILD_DIR)/JTest/%.o: src/JTest/%.cpp $(JTEST_HEADERS)
$(CC) $(CCFLAGS) -c -o $@ $<

View File

@@ -0,0 +1,33 @@
## TODO:
* Make Expectable toThrow methods work with arguments, class methods, static class methods, and possibly constructors/destructors.
* Report all errors at the end instead of printing them inline. This is needed for parallel support.
* Figure out how to make JTest self testing. Maybe use some namespace magic to run a special instance in another namespace.
* Mocks
* Design them.
* Add them.
* Spies
* Design them.
* Add them.
* Expectable<T>
* Clean up failure messages in Expectable.
* Make failure messages in Expectable only use ostreams if operator<< is defined for T.
* Pick a name for Expectable<T>::not()
* DescribeOptions
* Add operator<< and print for DescribeOptions
* JTest
* Make it and xit return a Test instead of a TestBundle
* Make describe and xdescribe take a variant of Test or TestBundle instead of just TestBundle
* Make TestOptions useful
* Find a way to set label/path from it/xit/describe/xdescribe instead of when we execute them.
* If executing a disabled bundle report all of it's child tests as skipped/disabled.
* Execute tests in parallel.
* Recheck that bundles/tests inherit before/after methods from their parents.
* Before* of parents should be called before that of children.
* After* of parents should be called after that of children.
* When reporting Unhandled exceptions in tests, report the fully qualified test name and label.
* TestResults
* Add commented out vectors to hold skipped, failed, errored test info.
* FailedExpectation
* Figure out a way to get label and path when creating the expectation from an Expect method.
* We need to get the label and path from the parent method (execute) that is calling expect<T>(T) to create the Expectable.
* We will likely be creating the FailedExpectation exception where the test fails and adding the label and path when we catch it before reporting the failure.

View File

@@ -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
}

View File

@@ -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);
}

View File

@@ -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.

View File

@@ -0,0 +1,10 @@
#ifndef JTEST__CONFIGUREFUNCTION_H__
#define JTEST__CONFIGUREFUNCTION_H__
#include <functional>
#include <optional>
namespace JTest {
typedef std::function<void()> configure_fn;
typedef std::optional<configure_fn> maybe_configure_fn;
maybe_configure_fn combine(maybe_configure_fn first, maybe_configure_fn second);
}
#endif

View File

@@ -0,0 +1,29 @@
#ifndef JTEST__DESCRIBEOPTIONS_H__
#define JTEST__DESCRIBEOPTIONS_H__
namespace JTest {
class DescribeOptions;
}
#include <optional>
#include <JTest/ConfigureFunction.h>
//TODO: Add operator<< and maybe print(ostream&)
namespace JTest {
class DescribeOptions {
public:
DescribeOptions();
maybe_configure_fn getAfterAll() const;
maybe_configure_fn getAfterEach() const;
maybe_configure_fn getBeforeAll() const;
maybe_configure_fn getBeforeEach() const;
DescribeOptions& afterAll(maybe_configure_fn fn);
DescribeOptions& afterEach(maybe_configure_fn fn);
DescribeOptions& beforeAll(maybe_configure_fn fn);
DescribeOptions& beforeEach(maybe_configure_fn fn);
private:
maybe_configure_fn afterAll_;
maybe_configure_fn afterEach_;
maybe_configure_fn beforeAll_;
maybe_configure_fn beforeEach_;
};
}
#endif

259
include/JTest/Expectable.h Normal file
View File

@@ -0,0 +1,259 @@
#ifndef JTEST__EXPECTABLE_H__
#define JTEST__EXPECTABLE_H__
namespace JTest {
template<typename T>
class Expectable;
}
#include <functional>
#include <string>
#include <sstream>
#include <optional>
#include <tuple>
#include <JTest/FailedExpectation.h>
#include <type_traits>
namespace JTest {
template<typename T>
class Expectable {
public:
Expectable(const T& actual)
: actual_(actual)
, is_inverted_(false) {}
Expectable(const T& actual, bool is_inverted)
: actual_(actual)
, is_inverted_(is_inverted) {}
virtual ~Expectable() {}
const T& actual() {
return actual_;
}
bool isInverted() {
return is_inverted_;
}
virtual Expectable<T> nevermore() {
return Expectable(actual_, !is_inverted_);
}
void toBe(std::function<std::optional<std::string>(const T&)> matcher) {
auto reason = matcher(actual_);
if (is_false(reason.has_value())) {
throw FailedExpectation(make_failure_message(*reason));
}
}
template<typename T2 = T>
typename std::enable_if<std::is_convertible<T2, bool>::value, void>::type
toBeFalse() {
if (is_true(actual_)) {
throw FailedExpectation(make_failure_message("to be false"));
}
}
template<typename T2 = T>
typename std::enable_if<std::is_pointer<T2>::value, void>::type
toBeNull() {
if (is_false(actual_ == nullptr)) {
throw FailedExpectation(make_failure_message("to be null"));
}
}
template<typename T2 = T>
typename std::enable_if<std::is_convertible<T2, bool>::value, bool>::type
toBeTrue() {
if (is_false(actual_)) {
throw FailedExpectation(make_failure_message("to be true"));
}
}
template<typename T2 = T>
typename std::enable_if<decltype(std::declval<std::equal_to <>>()(std::declval<T2>(), std::declval<T2>()), void(), std::true_type())::type::value, void>::type
toEqual(const T& value) {
if (is_false(actual_ == value)) {
throw FailedExpectation(make_failure_message("to equal", value));
}
}
template<typename T2 = T>
typename std::enable_if<std::is_invocable<T2>::value, bool>::type
toThrow() {
bool has_failed;
std::string reason;
try {
// TODO: Do some magic here to allow for args and member functions.
std::invoke(actual_);
has_failed = is_false();
if (has_failed) {
reason = "to throw, but did not";
}
} catch(...) {
has_failed = is_true();
if (has_failed) {
reason = "to throw, but threw something";
}
}
if (has_failed) {
throw FailedExpectation(make_failure_message(reason));
}
}
template<typename T2=T>
typename std::enable_if<std::is_invocable<T2>::value, bool>::type
toThrow(const std::exception& expected) {
bool has_failed;
std::string reason = "";
try {
// TODO: Do some magic here to allow for args and member functions.
std::invoke(actual_);
has_failed = is_false();
if (has_failed) {
reason = ", but did not throw";
}
} catch(const std::exception& actual) {
has_failed = is_true(strcmp(expected.what(), actual.what()) == 0);
if (has_failed) {
throw FailedExpectation(make_failure_message("to throw", expected.what(), actual.what()));
reason = string(is_inverted_ ? "" : ", but threw ") + actual.what();
}
} catch(...) {
has_failed = is_false();
reason = "but threw something else";
reason = is_inverted_ ? "" : ", but threw something else";
}
if (has_failed) {
throw FailedExpectation(make_failure_message("to throw " + (expected.what() + reason)));
}
}
template<typename T2=T>
typename std::enable_if<std::is_invocable<T2>::value, bool>::type
toThrow(std::function<std::optional<std::string>(const std::exception&)> matcher) {
bool has_failed;
std::optional<std::string> reason = std::nullopt;
try {
// TODO: Do some magic here to allow for args and member functions.
std::invoke(actual_);
has_failed = is_false();
if (has_failed) {
reason = "did not throw";
}
} catch(const std::exception& actual) {
reason = matcher(actual);
has_failed = is_true(reason.has_value());
} catch(...) {
has_failed = is_false();
if (has_failed) {
reason = "threw something else";
}
}
if (has_failed) {
if (reason.has_value()) {
throw FailedExpectation(make_failure_message(is_inverted_ ? "to throw, and " : "to throw, but " + *reason));
}
// This should never be hit.
throw FailedExpectation(make_failure_message("to throw"));
}
}
template<typename T2=T>
typename std::enable_if<std::is_invocable<T2>::value, bool>::type
toThrow(const std::string& what) {
bool has_failed;
std::string reason;
try {
// TODO: Do some magic here to allow for args and member functions.
std::invoke(actual_);
has_failed = is_false();
if (has_failed) {
reason = "to throw " + what + ", but did not throw anything";
}
} catch(const std::exception& actual) {
has_failed = is_true(what == actual.what());
if (has_failed) {
reason = "to throw " + what + ", but actually threw " + actual.what();
}
} catch(...) {
has_failed = is_false();
if (has_failed) {
reason = "to throw " + what + ", but threw something that is not an exception";
}
}
if (has_failed) {
throw FailedExpectation(make_failure_message(reason));
}
}
private:
const T& actual_;
const bool is_inverted_;
bool is_true(bool condition = true) {
return condition != is_inverted_;
}
bool is_false(bool condition = true) {
return condition == is_inverted_;
}
// std::string make_failure_message(const T& expected, const T& actual) {
// ostringstream os;
// os << "expected: " << value << ", actual: " << actual_;
// return os.str();
// }
// Failed Expectation:
// to be equal with expected = 12, and actual = 0
// Failed Expectation:
// not to be equal with expected = 12, and actual = 12
std::string make_failure_message(const std::string reason) {
std::ostringstream os;
os << (is_inverted_ ? "not " : "") << reason << " with actual = " << actual_;
return os.str();
}
std::string make_failure_message(const std::string reason, const T& expected) {
std::ostringstream os;
os << (is_inverted_ ? "not " : "") << reason << " with expected = " << expected << ", and actual = " << actual_;
return os.str();
}
std::string make_failure_message(const std::string reason, std::string expected, std::string actual) {
std::ostringstream os;
os << (is_inverted_ ? "not " : "") << reason
<< " with expected message = \"" << expected
<< "\", and actual message = \"" << actual_ << "\"";
return os.str();
}
// TODO: Fix the make_failure_message methods so they only exist if operator<< is defined for (ostream&, T)
// Maybe have it generate a shorter message.
// Maybe also check for string + T and use that if operator<< does not exist.
// If neither of those exist this should at least say "expected not equal to actual"
// Also this should take is_inverted_ into account.
};
// Other possible names since Expectable<T> not() is not an option
// Wanted, but can't have: expect(2+2).not().toBe(5);
// expect(2+2).absolutelyNot().toBe(5);
// expect(2+2).definitelyNot().toBe(5);
// expect(2+2).isNot().toBe(5);
// expect(2+2).isNotGoing().toBe(5);
// expect(2+2).nay().toBe(5);
// expect(2+2).neer().toBe(5);
// expect(2+2).neither().toBe(5);
// expect(2+2).never().toBe(5);
// expect(2+2).nevermore().toBe(5);
// expect(2+2).nix().toBe(5);
// expect(2+2).no().toBe(5);
// expect(2+2).notAtAll().toBe(5);
// expect(2+2).notByAnyMeans().toBe(5);
// expect(2+2).surelyNot().toBe(5);
// This is for T actual(){...} since Expectable<void()> has no default constructor.
// Expectable<void()> won't compile no default constructor exists for class "Expectable<void ()>"
// template<typename T2 = T>
// typename std::enable_if<std::is_default_constructible<T2>::value, T>::type
}
#endif

View File

@@ -0,0 +1,16 @@
#ifndef JTEST__FAILEDEXPECTATION_H__
#define JTEST__FAILEDEXPECTATION_H__
namespace JTest {
class FailedExpectation;
}
#include <string>
#include <JTest/TestFailedException.h>
namespace JTest {
class FailedExpectation: public TestFailedException {
public:
FailedExpectation(const std::string& label);
FailedExpectation(const std::string& label, const std::string& path, const std::string& reason);
virtual ~FailedExpectation();
};
}
#endif

39
include/JTest/JTest.h Normal file
View File

@@ -0,0 +1,39 @@
#ifndef JTEST_H__
#define JTEST_H__
namespace JTest {
}
#include <string>
#include <optional>
#include <JTest/TestResults.h>
#include <JTest/TestBundle.h>
#include <JTest/Test.h>
#include <JTest/Expectable.h>
#include <JTest/TestOptions.h>
#include <JTest/DescribeOptions.h>
#include <JTest/TestFunction.h>
#include <JTest/TestBundle.h>
namespace JTest {
// Jasmine methods
// Executes the tests in tests. Possibly in parallel. Will block until all async tests have completed.
TestResults execute(TestBundle tests, const std::string& path="");
TestResults execute(Test test, const std::string& bundle_label);
//
TestBundle describe(const std::string& label, const make_testbundle_fn& make_tests, std::optional<DescribeOptions> options = std::nullopt);
TestBundle xdescribe(const std::string& label, const make_testbundle_fn& make_tests, std::optional<DescribeOptions> options = std::nullopt);
// TODO: Make this return a Test instead.
TestBundle it(const std::string& label, const test_fn& test_method, std::optional<TestOptions> options = std::nullopt);
TestBundle xit(const std::string& label, const test_fn& test_method, std::optional<TestOptions> options = std::nullopt);
void fail(const std::string& reason);
void pending(const std::string& reason);
template<typename T>
Expectable<T> expect(const T& actual) {
return Expectable(actual);
}
}
#endif

17
include/JTest/Test.h Normal file
View File

@@ -0,0 +1,17 @@
#ifndef JTEST__TEST_H__
#define JTEST__TEST_H__
namespace JTest {
class Test;
}
#include <string>
#include <JTest/TestFunction.h>
namespace JTest {
class Test {
public:
std::string label_;
test_fn test_method_;
bool disabled_;
private:
};
}
#endif

View File

@@ -0,0 +1,33 @@
#ifndef JEST__TESTBUNDLE_H__
#define JEST__TESTBUNDLE_H__
#include <functional>
namespace JTest {
class TestBundle;
typedef std::function<TestBundle()> make_testbundle_fn;
}
#include <vector>
#include <optional>
#include <string>
#include <JTest/ConfigureFunction.h>
#include <JTest/DescribeOptions.h>
#include <JTest/TestBundle.h>
#include <JTest/Test.h>
namespace JTest {
class TestBundle {
public:
TestBundle(const std::vector<TestBundle>& bundles, const DescribeOptions& options);
TestBundle(const std::string& label, const std::vector<Test>& tests);
std::string fully_qualified_label() const;
std::string label_;
std::string fully_qualified_path_;
std::vector<Test> tests_;
std::vector<TestBundle> children_;
maybe_configure_fn afterAll_;
maybe_configure_fn afterEach_;
maybe_configure_fn beforeAll_;
maybe_configure_fn beforeEach_;
bool disabled_;
private:
};
}
#endif

View File

@@ -0,0 +1,28 @@
#ifndef JTEST__TESTEXCEPTION_H__
#define JTEST__TESTEXCEPTION_H__
namespace JTest {
class TestException;
}
#include <string>
#include <stdexcept>
#include <JTest/TestStatus.h>
namespace JTest {
class TestException: public std::runtime_error {
public:
TestException(const std::string& label, const std::string& path);
TestException(const std::string& label, const std::string& path, const std::string& what);
TestException(const std::string& function, const std::string& file, int line, const std::string& reason);
virtual ~TestException();
virtual std::string test_label() const;
virtual std::string fully_qualified_test_label() const;
virtual TestStatus status() const;
private:
const std::string test_label_;
const std::string fully_qualified_test_label_;
const std::string function_;
const std::string file_;
const int line_;
const std::string reason_;
};
}
#endif

View File

@@ -0,0 +1,20 @@
#ifndef JTEST__TESTFAILEDEXCEPTION_H__
#define JTEST__TESTFAILEDEXCEPTION_H__
namespace JTest{
class TestFailedException;
}
#include <string>
#include <JTest/TestStatus.h>
#include <JTest/TestException.h>
namespace JTest{
class TestFailedException: public TestException {
public:
TestFailedException(std::string label, std::string path, std::string reason);
virtual ~TestFailedException();
virtual TestStatus status() const;
virtual std::string reason() const;
private:
std::string reason_;
};
}
#endif

View File

@@ -0,0 +1,7 @@
#ifndef JTEST__TESTFUNCTION_H__
#define JTEST__TESTFUNCTION_H__
#include <functional>
namespace JTest {
typedef std::function<void()> test_fn;
}
#endif

View File

@@ -0,0 +1,12 @@
#ifndef JTEST__TESTOPTIONS_H__
#define JTEST__TESTOPTIONS_H__
namespace JTest {
class TestOptions;
}
namespace JTest {
class TestOptions {
};
}
#endif

View File

@@ -0,0 +1,17 @@
#ifndef JTEST__TESTPENDINGEXCEPTION_H__
#define JTEST__TESTPENDINGEXCEPTION_H__
namespace JTest {
class TestPendingException;
}
#include <JTest/TestException.h>
#include <JTest/TestStatus.h>
namespace JTest {
class TestPendingException: public TestException {
public:
TestPendingException(const std::string& label, const std::string& path, const std::string& reason);
virtual ~TestPendingException();
virtual TestStatus status() const;
};
}
#endif

View File

@@ -0,0 +1,28 @@
#ifndef JTEST_TESTRESULTS_H__
#define JTEST_TESTRESULTS_H__
namespace JTest {
class TestResults;
}
#include <cstdint>
#include <ostream>
namespace JTest {
// TODO: Consider hiding the vectors behind accessor methods void add(...), T get(), vector<T> get(uint32_t index)
class TestResults {
public:
TestResults();
TestResults(uint32_t total, uint32_t skipped, uint32_t passed, uint32_t failed);
TestResults operator+(const TestResults& right) const;
TestResults& operator+=(const TestResults& right);
friend std::ostream& operator<<(std::ostream& os, const TestResults& results);
// TODO: Make these private, make operator++ increment total, add methods pass, fail, and skip to increment passed, failed, and skipped
uint32_t total;
uint32_t skipped;
uint32_t passed;
uint32_t failed;
private:
// vector<TestError> errors;
// vector<TestFailure> failures;
// vector<TestSkip> skipped;
};
}
#endif

View File

@@ -0,0 +1,11 @@
#ifndef JTEST__TESTSTATUS_H__
#define JTEST__TESTSTATUS_H__
namespace JTest {
enum TestStatus {
Unknown,
Passed,
Failed,
Skipped,
};
}
#endif

View File

@@ -0,0 +1,20 @@
#include <JTest/ConfigureFunction.h>
#include <optional>
namespace JTest {
maybe_configure_fn combine(maybe_configure_fn first, maybe_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;
}
}
}

View File

@@ -0,0 +1,44 @@
#include <JTest/DescribeOptions.h>
namespace JTest {
DescribeOptions::DescribeOptions()
: afterAll_(std::nullopt)
, afterEach_(std::nullopt)
, beforeAll_(std::nullopt)
, beforeEach_(std::nullopt) {}
maybe_configure_fn DescribeOptions::getAfterAll() const {
return afterAll_;
}
DescribeOptions& DescribeOptions::afterAll(maybe_configure_fn fn) {
this->afterAll_ = fn;
return *this;
}
maybe_configure_fn DescribeOptions::getAfterEach() const {
return afterEach_;
}
DescribeOptions& DescribeOptions::afterEach(maybe_configure_fn fn) {
this->afterEach_ = fn;
return *this;
}
maybe_configure_fn DescribeOptions::getBeforeAll() const {
return beforeAll_;
}
DescribeOptions& DescribeOptions::beforeAll(maybe_configure_fn fn) {
this->beforeAll_ = fn;
return *this;
}
maybe_configure_fn DescribeOptions::getBeforeEach() const {
return beforeEach_;
}
DescribeOptions& DescribeOptions::beforeEach(maybe_configure_fn fn) {
this->beforeEach_ = fn;
return *this;
}
}

View File

@@ -0,0 +1,11 @@
#include <JTest/FailedExpectation.h>
namespace JTest{
FailedExpectation::~FailedExpectation() {}
FailedExpectation::FailedExpectation(const std::string& reason)
: TestFailedException("", "", reason) {}
// TODO: Figure out if we can get label and path from here without passing them in.
FailedExpectation::FailedExpectation(const std::string& label, const std::string& path, const std::string& reason)
: TestFailedException(label, path, reason) {}
}

201
src/JTest/JTest.cpp Normal file
View File

@@ -0,0 +1,201 @@
#include <JTest/JTest.h>
#include <stdexcept>
#include <string>
#include <iostream>
#include <algorithm>
#include <JTest/TestFunction.h>
#include <JTest/TestBundle.h>
#include <JTest/Test.h>
#include <JTest/TestResults.h>
#include <JTest/TestOptions.h>
#include <JTest/DescribeOptions.h>
#include <JTest/TestPendingException.h>
#include <JTest/TestFailedException.h>
namespace JTest {
using std::ostream;
using std::endl;
using std::vector;
using std::string;
using std::runtime_error;
using std::string;
using std::optional;
using std::cout;
using std::exception;
using std::for_each;
void throw_unimplemented_function_error(string method_name) {
throw runtime_error("Unimplemented function: " + method_name);
}
void throw_unimplemented_method_error(string class_name, string method_name) {
throw runtime_error("Unimplemented method in class " + class_name + ": " + method_name);
}
void throw_unimplemented_feature_error(string feature_name) {
throw runtime_error("Unimplemented feature: " + feature_name);
}
void print_current_function(ostream & out, string function_name = __builtin_FUNCTION()) {
out << "\033[32m" << function_name << "\033[39m" << endl;
}
TestBundle it(const string& label, const test_fn& test_method, optional<TestOptions> options) {
// TODO: Stop ignoring options.
// TODO: Return a Test instead of a TestBundle.
Test test;
test.disabled_ = false;
test.label_ = label;
test.test_method_ = test_method;
return TestBundle("", {test});
}
TestBundle xit(const string& label, const test_fn& test_method, optional<TestOptions> options) {
TestBundle bundle = it(label, test_method, options);
bundle.tests_.at(0).disabled_ = true;
return bundle;
}
TestResults execute(TestBundle bundle, const string& path) {
TestResults results;
// TODO: Set this during bundle creation or parsing.
// bundle.fully_qualified_path_ = path.length() > 0 ? path + "::" : "";
// cout << "executing TestBundle: " << bundle.fully_qualified_label() << endl;
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, &bundle](TestBundle child) {
// TODO: Find a way to make child tests get our beforeEach and afterEach callbacks without modifying child.
child.beforeEach_ = combine(bundle.beforeEach_, child.beforeEach_);
child.afterEach_ = combine(bundle.afterEach_, child.afterEach_);
child.fully_qualified_path_ = bundle.fully_qualified_label();
results += execute(child);
});
// TODO: Consider capturing these callbacks differently. Without the bundle? By value?
for_each(bundle.tests_.begin(), bundle.tests_.end(), [&results, &bundle](Test test) {
if (bundle.beforeEach_.has_value()) {
bundle.beforeEach_.value()();
}
results += execute(test, bundle.fully_qualified_label());
if (bundle.afterEach_.has_value()) {
bundle.afterEach_.value()();
}
});
if (bundle.afterAll_.has_value()) {
bundle.afterAll_.value()();
}
} catch(const exception& ex) {
// TODO: This should include the fully qualified test name.
cout << "🔥 Unhandled exception running test bundle: " << bundle.label_ << " what: " << ex.what() << endl;
results.failed++;
} catch(...) {
// TODO: This should include the fully qualified test name.
cout << "🔥 Unhandled exception running test bundle: " << bundle.label_ << endl;
results.failed++;
}
}
return results;
}
TestResults execute(Test test, const string& bundle_label) {
// cout << "executing Test:" << endl
// << " bundle_label = " << bundle_label << endl
// << " test.label = " << test.label_ << endl;
TestResults results;
results.total++;
int status = 0;
if (test.disabled_) {
results.skipped++;
} else {
try {
test.test_method_();
results.passed++;
} catch (const TestPendingException& ex) {
results.skipped++;
// TODO: This should be ex.fully_qualified_test_label() but that isn't set yet.
cout << "🚧 Pending Test: " << bundle_label + "::" + test.label_ << endl;
} catch (const TestFailedException& ex) {
results.failed++;
// TODO: This should be ex.fully_qualified_test_label() but that isn't set yet.
cout << "❌ Failed Test: " << bundle_label + "::" + test.label_ << " reason: " << ex.reason() << endl;
} catch(const exception& ex) {
results.failed++;
// TODO: This should include the fully qualified test name.
cout << "🔥 Unhandled exception running test: " << bundle_label + "::" + test.label_ << " what: " << ex.what() << endl;
} catch(...) {
results.failed++;
// TODO: This should include the fully qualified test name.
cout << "🔥 Unhandled exception running test: " << bundle_label + "::" + test.label_ << endl;
}
// throw; can still escape these catch clauses because it calls std::terminate and we can't catch/interrupt that.
}
return results;
}
TestBundle describe(const string& label, const make_testbundle_fn& maketests_fn, optional<DescribeOptions> options) {
TestBundle bundle = maketests_fn();
// vector<TestBundle> newChildren;
// for_each(bundle.children_.begin(), bundle.children_.end(), [&bundle, &newChildren](TestBundle& child) {
// if (child.label_.length() == 0 && !child.afterAll_.has_value() && !child.afterEach_.has_value()
// && !child.beforeAll_.has_value() && !child.beforeEach_.has_value()) {
// for_each(child.tests_.begin(), child.tests_.end(), [&bundle](Test test) {
// bundle.tests_.push_back(test);
// });
// } else {
// newChildren.push_back(bundle);
// }
// });
// bundle.children_ = newChildren;
bundle.label_ = label;
bundle.disabled_ = false;
if (options.has_value()) {
DescribeOptions 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_);
}
// TODO: go through all child bundles and tests to update the fully qualified path/label
// cout << "describe: bundle_label = " << bundle.label_
// << ", bundle.tests.size() = " << bundle.tests_.size()
// << ", bundle.children.size() = " << bundle.children_.size() << endl;
return bundle;
}
void fail(const std::string& reason) {
// TODO: Figure out how to get label, and path
string label = "";
string path = "";
throw TestFailedException(label, path, reason);
}
void pending(const std::string& reason) {
// TODO: Figure out how to get label, and path
string label = "";
string path = "";
throw TestPendingException(label, path, reason);
}
// TODO: Use these to make the unimplemented_* errors simpler to call.
// For this function
// TestBundle describe(const std::string& label, const make_testbundle_fn& maketests_, std::optional<DescribeOptions> options)
// __PRETTY_FUNCTION__
// Unimplemented function: JTest::TestBundle JTest::describe(const std::string &, const JTest::make_testbundle_fn &, std::optional<DescribeOptions>)
// __FUNCSIG__ is not defined on clang++
// __func__
// describe
// __LINE__ is an integer
//
// __FILE__
// examples/JSTest.cpp
// __FUNCTION__
// describe
}

52
src/JTest/TestBundle.cpp Normal file
View File

@@ -0,0 +1,52 @@
#include <JTest/TestBundle.h>
#include <algorithm>
#include <vector>
#include <JTest/DescribeOptions.h>
#include <JTest/Test.h>
#include <string>
#include <optional>
#include <iostream>
namespace JTest {
using std::string;
using std::vector;
using std::nullopt;
TestBundle::TestBundle(const vector<TestBundle>& bundles, const DescribeOptions& options)
: label_("")
, fully_qualified_path_("")
, tests_({})
, children_({})
, afterAll_(options.getAfterAll())
, afterEach_(options.getAfterEach())
, beforeAll_(options.getBeforeAll())
, beforeEach_(options.getBeforeEach())
, disabled_(false) {
for_each(bundles.begin(), bundles.end(), [this](TestBundle bundle) {
if (bundle.tests_.size() == 0 && bundle.children_.size() == 1 && bundle.label_.size() == 0) {
tests_.push_back(bundle.tests_.at(0));
} else {
children_.push_back(bundle);
}
});
}
TestBundle::TestBundle(const string& label, const vector<Test>& tests)
: label_(label)
, fully_qualified_path_("")
, tests_({})
, children_({})
, afterAll_(nullopt)
, afterEach_(nullopt)
, beforeAll_(nullopt)
, beforeEach_(nullopt)
, disabled_(false) {
for_each(tests.begin(), tests.end(), [this](Test test) {
tests_.push_back(test);
});
}
string TestBundle::fully_qualified_label() const {
return (fully_qualified_path_.length()>0?fully_qualified_path_ + "::":"") + (label_.length()>0?label_:"<anonymous>");
}
}

View File

@@ -0,0 +1,47 @@
#include <JTest/TestException.h>
#include <string>
namespace JTest {
using std::string;
TestException::TestException(const string& label, const string& path)
: runtime_error("Exception in test: " + path + "::" + label)
, test_label_(label)
, fully_qualified_test_label_(path + "::" + label)
, function_("")
, file_("")
, line_(-1)
, reason_("") {}
TestException::TestException(const string& label, const string& path, const string& what)
: runtime_error(what)
, test_label_(label)
, fully_qualified_test_label_(path + "::" + label)
, function_("")
, file_("")
, line_(-1)
, reason_("") {}
TestException::TestException(const std::string& function, const std::string& file, int line, const std::string& reason)
: runtime_error(reason)
, test_label_("")
, fully_qualified_test_label_("")
, function_(function)
, file_(file)
, line_(line)
, reason_(reason) {}
TestException::~TestException() {}
string TestException::test_label() const {
return test_label_;
}
string TestException::fully_qualified_test_label() const {
return fully_qualified_test_label_;
}
TestStatus TestException::status() const {
return TestStatus::Unknown;
}
}

View File

@@ -0,0 +1,22 @@
#include <JTest/TestFailedException.h>
#include <string>
#include <JTest/TestException.h>
#include <JTest/TestStatus.h>
namespace JTest {
using std::string;
TestFailedException::TestFailedException(string label, string path, string reason)
: TestException(label, path, "Failed Test: " +fully_qualified_test_label())
, reason_(reason) {}
TestFailedException::~TestFailedException() {}
TestStatus TestFailedException::status() const {
return TestStatus::Failed;
}
string TestFailedException::reason() const {
return reason_;
}
}

View File

View File

@@ -0,0 +1,17 @@
#include <JTest/TestPendingException.h>
#include <string>
#include <JTest/TestStatus.h>
#include <JTest/TestException.h>
namespace JTest {
using std::string;
TestPendingException::TestPendingException(const string& label, const string& path, const string& reason)
: TestException(label, path, "Pending Test: " + fully_qualified_test_label()) {}
TestPendingException::~TestPendingException() {}
TestStatus TestPendingException::status() const {
return TestStatus::Skipped;
}
}

35
src/JTest/TestResults.cpp Normal file
View File

@@ -0,0 +1,35 @@
#include <JTest/TestResults.h>
namespace JTest {
TestResults::TestResults()
: total(0)
, skipped(0)
, passed(0)
, failed(0) {}
TestResults::TestResults(uint32_t total, uint32_t skipped, uint32_t passed, uint32_t failed)
: total(total)
, skipped(skipped)
, passed(passed)
, failed(failed) {}
TestResults TestResults::operator+(const TestResults& right) const {
return TestResults(
total + right.total, skipped + right.skipped,
passed + right.passed, failed + right.failed);
}
TestResults& TestResults::operator+=(const TestResults& right) {
total += right.total;
skipped += right.skipped;
passed += right.passed;
failed += right.failed;
return *this;
}
std::ostream& operator<<(std::ostream& os, const TestResults& results) {
os << "Total: " << results.total << ", Passed: " << results.passed
<< ", Failed: " << results.failed << ", Skipped: " << results.skipped;
return os;
}
}

0
src/JTest/TestStatus.cpp Normal file
View File