Renames tinytest files again.
This commit is contained in:
7
BUILD
7
BUILD
@@ -2,6 +2,9 @@ load("@rules_cc//cc:defs.bzl", "cc_library")
|
||||
|
||||
cc_library(
|
||||
name = "tinytest",
|
||||
srcs = ["test.cpp"],
|
||||
hdrs = ["test.h"],
|
||||
srcs = ["tinytest.cpp"],
|
||||
hdrs = ["tinytest.h"],
|
||||
visibility = ["//visibility:public"],
|
||||
)
|
||||
|
||||
)
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
#define _XOPEN_SOURCE_EXTENDED
|
||||
#include "test.h"
|
||||
#include "tinytest.h"
|
||||
|
||||
#include <cstdint>
|
||||
#include <cstdio>
|
||||
@@ -7,7 +7,7 @@
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace Test {
|
||||
namespace TinyTest {
|
||||
namespace {
|
||||
using std::endl;
|
||||
using std::string;
|
||||
@@ -143,23 +143,34 @@ using std::vector;
|
||||
// Default to (string, _FnToTest, vector<tuple<"", _T1, [](a,b){return a==b;},
|
||||
// make_tuple()) Also allow make_tuple(T2) if the last param is not a tuple.
|
||||
|
||||
TestResults::TestResults()
|
||||
: errors_(0), failed_(0), passed_(0), skipped_(0), total_(0) {}
|
||||
TestResults::TestResults() : errors_(0), failed_(0), passed_(0), skipped_(0), total_(0) {}
|
||||
|
||||
TestResults::TestResults(const TestResults& other)
|
||||
: error_messages_(other.error_messages_), errors_(other.errors_),
|
||||
failed_(other.failed_), failure_messages_(other.failure_messages_),
|
||||
passed_(other.passed_), skip_messages_(other.skip_messages_),
|
||||
skipped_(other.skipped_), total_(other.total_) {}
|
||||
: error_messages_(other.error_messages_),
|
||||
errors_(other.errors_),
|
||||
failed_(other.failed_),
|
||||
failure_messages_(other.failure_messages_),
|
||||
passed_(other.passed_),
|
||||
skip_messages_(other.skip_messages_),
|
||||
skipped_(other.skipped_),
|
||||
total_(other.total_) {}
|
||||
|
||||
TestResults::TestResults(uint32_t errors, uint32_t failed, uint32_t passed,
|
||||
uint32_t skipped, uint32_t total,
|
||||
TestResults::TestResults(uint32_t errors,
|
||||
uint32_t failed,
|
||||
uint32_t passed,
|
||||
uint32_t skipped,
|
||||
uint32_t total,
|
||||
vector<string> error_messages,
|
||||
vector<string> failure_messages,
|
||||
vector<string> skip_messages)
|
||||
: error_messages_(error_messages), errors_(errors), failed_(failed),
|
||||
failure_messages_(failure_messages), passed_(passed),
|
||||
skip_messages_(skip_messages), skipped_(skipped), total_(total) {}
|
||||
: error_messages_(error_messages),
|
||||
errors_(errors),
|
||||
failed_(failed),
|
||||
failure_messages_(failure_messages),
|
||||
passed_(passed),
|
||||
skip_messages_(skip_messages),
|
||||
skipped_(skipped),
|
||||
total_(total) {}
|
||||
|
||||
TestResults& TestResults::error() {
|
||||
errors_++;
|
||||
@@ -185,7 +196,9 @@ TestResults &TestResults::fail(const string &message) {
|
||||
return *this;
|
||||
}
|
||||
|
||||
vector<string> TestResults::failure_messages() { return failure_messages_; }
|
||||
vector<string> TestResults::failure_messages() {
|
||||
return failure_messages_;
|
||||
}
|
||||
|
||||
TestResults& TestResults::pass() {
|
||||
total_++;
|
||||
@@ -206,55 +219,62 @@ TestResults &TestResults::skip(const string &message) {
|
||||
return *this;
|
||||
}
|
||||
|
||||
vector<string> TestResults::skip_messages() { return skip_messages_; }
|
||||
vector<string> TestResults::skip_messages() {
|
||||
return skip_messages_;
|
||||
}
|
||||
|
||||
vector<string> TestResults::error_messages() { return error_messages_; }
|
||||
vector<string> TestResults::error_messages() {
|
||||
return error_messages_;
|
||||
}
|
||||
|
||||
uint32_t TestResults::errors() { return errors_; }
|
||||
uint32_t TestResults::errors() {
|
||||
return errors_;
|
||||
}
|
||||
|
||||
uint32_t TestResults::failed() { return failed_; }
|
||||
uint32_t TestResults::failed() {
|
||||
return failed_;
|
||||
}
|
||||
|
||||
uint32_t TestResults::passed() { return passed_; }
|
||||
uint32_t TestResults::passed() {
|
||||
return passed_;
|
||||
}
|
||||
|
||||
uint32_t TestResults::skipped() { return skipped_; }
|
||||
uint32_t TestResults::skipped() {
|
||||
return skipped_;
|
||||
}
|
||||
|
||||
uint32_t TestResults::total() { return total_; }
|
||||
uint32_t TestResults::total() {
|
||||
return total_;
|
||||
}
|
||||
|
||||
TestResults TestResults::operator+(const TestResults& other) const {
|
||||
vector<string> error_messages;
|
||||
error_messages.insert(error_messages.end(), error_messages_.begin(),
|
||||
error_messages_.end());
|
||||
error_messages.insert(error_messages.end(), other.error_messages_.begin(),
|
||||
other.error_messages_.end());
|
||||
error_messages.insert(error_messages.end(), error_messages_.begin(), error_messages_.end());
|
||||
error_messages.insert(error_messages.end(), other.error_messages_.begin(), other.error_messages_.end());
|
||||
vector<string> failure_messages;
|
||||
failure_messages.insert(failure_messages.end(), failure_messages_.begin(),
|
||||
failure_messages_.end());
|
||||
failure_messages.insert(failure_messages.end(),
|
||||
other.failure_messages_.begin(),
|
||||
other.failure_messages_.end());
|
||||
failure_messages.insert(failure_messages.end(), failure_messages_.begin(), failure_messages_.end());
|
||||
failure_messages.insert(failure_messages.end(), other.failure_messages_.begin(), other.failure_messages_.end());
|
||||
vector<string> skip_messages;
|
||||
skip_messages.insert(skip_messages.end(), skip_messages_.begin(),
|
||||
skip_messages_.end());
|
||||
skip_messages.insert(skip_messages.end(), other.skip_messages_.begin(),
|
||||
other.skip_messages_.end());
|
||||
skip_messages.insert(skip_messages.end(), skip_messages_.begin(), skip_messages_.end());
|
||||
skip_messages.insert(skip_messages.end(), other.skip_messages_.begin(), other.skip_messages_.end());
|
||||
|
||||
return TestResults(errors_ + other.errors_, failed_ + other.failed_,
|
||||
passed_ + other.passed_, skipped_ + other.skipped_,
|
||||
total_ + other.total_, error_messages, failure_messages,
|
||||
return TestResults(errors_ + other.errors_,
|
||||
failed_ + other.failed_,
|
||||
passed_ + other.passed_,
|
||||
skipped_ + other.skipped_,
|
||||
total_ + other.total_,
|
||||
error_messages,
|
||||
failure_messages,
|
||||
skip_messages);
|
||||
}
|
||||
|
||||
TestResults& TestResults::operator+=(const TestResults& other) {
|
||||
error_messages_.insert(error_messages_.end(), other.error_messages_.begin(),
|
||||
other.error_messages_.end());
|
||||
error_messages_.insert(error_messages_.end(), other.error_messages_.begin(), other.error_messages_.end());
|
||||
errors_ += other.errors_;
|
||||
failed_ += other.failed_;
|
||||
failure_messages_.insert(failure_messages_.end(),
|
||||
other.failure_messages_.begin(),
|
||||
other.failure_messages_.end());
|
||||
failure_messages_.insert(failure_messages_.end(), other.failure_messages_.begin(), other.failure_messages_.end());
|
||||
passed_ += other.passed_;
|
||||
skip_messages_.insert(skip_messages_.end(), other.skip_messages_.begin(),
|
||||
other.skip_messages_.end());
|
||||
skip_messages_.insert(skip_messages_.end(), other.skip_messages_.begin(), other.skip_messages_.end());
|
||||
skipped_ += other.skipped_;
|
||||
total_ += other.total_;
|
||||
return *this;
|
||||
@@ -264,25 +284,23 @@ void PrintResults(std::ostream &os, TestResults results) {
|
||||
auto skip_messages = results.skip_messages();
|
||||
if (skip_messages.size() > 0) {
|
||||
os << "Skipped:" << endl;
|
||||
for_each(skip_messages.begin(), skip_messages.end(),
|
||||
[&os](const string &message) {
|
||||
for_each(skip_messages.begin(), skip_messages.end(), [&os](const string& message) {
|
||||
os << "🚧Skipped: " << message << endl;
|
||||
});
|
||||
}
|
||||
auto failure_messages = results.failure_messages();
|
||||
if (failure_messages.size() > 0) {
|
||||
os << "Failures:" << endl;
|
||||
for_each(failure_messages.begin(), failure_messages.end(),
|
||||
[&os](const string &message) {
|
||||
for_each(failure_messages.begin(), failure_messages.end(), [&os](const string& message) {
|
||||
os << "❌FAILED: " << message << endl;
|
||||
});
|
||||
}
|
||||
auto error_messages = results.error_messages();
|
||||
if (error_messages.size() > 0) {
|
||||
os << "Errors:" << endl;
|
||||
for_each(
|
||||
error_messages.begin(), error_messages.end(),
|
||||
[&os](const string &message) { os << "🔥ERROR: " << message << endl; });
|
||||
for_each(error_messages.begin(), error_messages.end(), [&os](const string& message) {
|
||||
os << "🔥ERROR: " << message << endl;
|
||||
});
|
||||
}
|
||||
os << "Total tests: " << results.total() << endl;
|
||||
os << "Passed: " << results.passed() << " ✅" << endl;
|
||||
@@ -294,4 +312,4 @@ void PrintResults(std::ostream &os, TestResults results) {
|
||||
MaybeTestConfigureFunction DefaultTestConfigureFunction() {
|
||||
return std::nullopt;
|
||||
}
|
||||
} // End namespace Test
|
||||
} // namespace TinyTest
|
||||
@@ -52,8 +52,7 @@
|
||||
// Tuple printer from:
|
||||
// https://stackoverflow.com/questions/6245735/pretty-print-stdtuple/31116392#58417285
|
||||
template <typename TChar, typename TTraits, typename... TArgs>
|
||||
auto &operator<<(std::basic_ostream<TChar, TTraits> &os,
|
||||
std::tuple<TArgs...> const &t) {
|
||||
auto& operator<<(std::basic_ostream<TChar, TTraits>& os, std::tuple<TArgs...> const& t) {
|
||||
std::apply([&os](auto&&... args) { ((os << args << " "), ...); }, t);
|
||||
return os;
|
||||
}
|
||||
@@ -73,26 +72,24 @@ auto &operator<<(std::basic_ostream<TChar, TTraits> &os, std::vector<TItem> v) {
|
||||
|
||||
template <typename TChar, typename TTraits, typename TItem>
|
||||
auto& compare(std::basic_ostream<TChar, TTraits>& error_message,
|
||||
std::vector<TItem> expected, std::vector<TItem> actual) {
|
||||
std::vector<TItem> expected,
|
||||
std::vector<TItem> actual) {
|
||||
if (expected.size() != actual.size()) {
|
||||
error_message << "size mismatch expected: " << expected.size()
|
||||
<< ", actual: " << actual.size();
|
||||
error_message << "size mismatch expected: " << expected.size() << ", actual: " << actual.size();
|
||||
return error_message;
|
||||
}
|
||||
|
||||
for (size_t index = 0; index < expected.size(); index++) {
|
||||
if (expected[index] != actual[index]) {
|
||||
error_message << "vectors differ at index " << index << ", \""
|
||||
<< expected[index] << "\" != \"" << actual[index]
|
||||
<< "\", expected: \"" << expected << "\", actual: \""
|
||||
<< actual << "\"";
|
||||
error_message << "vectors differ at index " << index << ", \"" << expected[index] << "\" != \"" << actual[index]
|
||||
<< "\", expected: \"" << expected << "\", actual: \"" << actual << "\"";
|
||||
return error_message;
|
||||
}
|
||||
}
|
||||
return error_message;
|
||||
}
|
||||
|
||||
namespace Test {
|
||||
namespace TinyTest {
|
||||
using std::string;
|
||||
using std::tuple;
|
||||
using std::vector;
|
||||
@@ -117,8 +114,11 @@ public:
|
||||
/// @param error_messages The list of error messages.
|
||||
/// @param failure_messages The list of failure messages.
|
||||
/// @param skip_messages The list of skip messages.
|
||||
TestResults(uint32_t errors, uint32_t failed, uint32_t passed,
|
||||
uint32_t skipped, uint32_t total,
|
||||
TestResults(uint32_t errors,
|
||||
uint32_t failed,
|
||||
uint32_t passed,
|
||||
uint32_t skipped,
|
||||
uint32_t total,
|
||||
std::vector<std::string> error_messages,
|
||||
std::vector<std::string> failure_messages,
|
||||
std::vector<std::string> skip_messages);
|
||||
@@ -214,8 +214,7 @@ private:
|
||||
/// @brief
|
||||
/// @tparam TResult
|
||||
template <typename TResult>
|
||||
using TestCompareFunction =
|
||||
std::function<bool(const TResult &expected, const TResult &actual)>;
|
||||
using TestCompareFunction = std::function<bool(const TResult& expected, const TResult& actual)>;
|
||||
|
||||
/// @brief
|
||||
/// @tparam TResult
|
||||
@@ -263,11 +262,13 @@ using TestTuple =
|
||||
/// @tparam TFunctionToTest
|
||||
/// @tparam ...TInputParams
|
||||
template <typename TResult, typename... TInputParams>
|
||||
using TestSuite =
|
||||
std::tuple<std::string, std::function<TResult(TInputParams...)>,
|
||||
using TestSuite = std::tuple<std::string,
|
||||
std::function<TResult(TInputParams...)>,
|
||||
std::vector<TestTuple<TResult, TInputParams...>>,
|
||||
MaybeTestCompareFunction<TResult>, MaybeTestConfigureFunction,
|
||||
MaybeTestConfigureFunction, bool>;
|
||||
MaybeTestCompareFunction<TResult>,
|
||||
MaybeTestConfigureFunction,
|
||||
MaybeTestConfigureFunction,
|
||||
bool>;
|
||||
|
||||
// This function is called to execute a test suite. You provide it with some
|
||||
// configuration info, optional utility callback functions, and test data (input
|
||||
@@ -366,8 +367,7 @@ using TestSuite =
|
||||
/// @param is_enabled If false none of these tests are run and they are all
|
||||
/// reported as skipped.
|
||||
template <typename TResult, typename... TInputParams>
|
||||
TestResults
|
||||
execute_suite(std::string suite_label,
|
||||
TestResults execute_suite(std::string suite_label,
|
||||
std::function<TResult(TInputParams...)> function_to_test,
|
||||
vector<TestTuple<TResult, TInputParams...>> tests,
|
||||
MaybeTestCompareFunction<TResult> suite_compare = std::nullopt,
|
||||
@@ -384,21 +384,18 @@ execute_suite(std::string suite_label,
|
||||
}
|
||||
|
||||
// Step 2: Execute Tests
|
||||
for_each(
|
||||
tests.begin(), tests.end(),
|
||||
[&suite_label, &function_to_test, &results,
|
||||
&suite_compare](TestTuple<TResult, TInputParams...> test_data) {
|
||||
for_each(tests.begin(),
|
||||
tests.end(),
|
||||
[&suite_label, &function_to_test, &results, &suite_compare](TestTuple<TResult, TInputParams...> test_data) {
|
||||
// Step 2a: Extract our variables from the TestTuple.
|
||||
const std::string& test_name = std::get<0>(test_data);
|
||||
const std::string qualified_test_name = suite_label + "::" + test_name;
|
||||
const TResult& expected_output = std::get<1>(test_data);
|
||||
std::tuple<TInputParams...> input_params = std::get<2>(test_data);
|
||||
MaybeTestCompareFunction<TResult> maybe_compare_function =
|
||||
std::get<3>(test_data);
|
||||
MaybeTestCompareFunction<TResult> maybe_compare_function = std::get<3>(test_data);
|
||||
TestCompareFunction<TResult> compare_function =
|
||||
maybe_compare_function.has_value() ? *maybe_compare_function
|
||||
: suite_compare.has_value()
|
||||
? *suite_compare
|
||||
: suite_compare.has_value() ? *suite_compare
|
||||
: [](const TResult& l, const TResult& r) { return l == r; };
|
||||
MaybeTestConfigureFunction before_each = std::get<4>(test_data);
|
||||
MaybeTestConfigureFunction after_each = std::get<5>(test_data);
|
||||
@@ -431,7 +428,8 @@ execute_suite(std::string suite_label,
|
||||
results.error(qualified_test_name + " " + os.str());
|
||||
std::cout << " 🔥ERROR: " << os.str() << std::endl;
|
||||
} catch (...) {
|
||||
string message = "Caught something that is neither an std::exception "
|
||||
string message =
|
||||
"Caught something that is neither an std::exception "
|
||||
"nor an std::string.";
|
||||
results.error(qualified_test_name + " " + message);
|
||||
std::cout << " 🔥ERROR: " << message << std::endl;
|
||||
@@ -443,8 +441,7 @@ execute_suite(std::string suite_label,
|
||||
std::cout << " ✅PASSED" << std::endl;
|
||||
} else {
|
||||
std::ostringstream os;
|
||||
os << "expected: \"" << expected_output << "\", actual: \"" << actual
|
||||
<< "\"";
|
||||
os << "expected: \"" << expected_output << "\", actual: \"" << actual << "\"";
|
||||
results.fail(qualified_test_name + " " + os.str());
|
||||
std::cout << " ❌FAILED: " << os.str() << std::endl;
|
||||
}
|
||||
@@ -482,8 +479,7 @@ execute_suite(std::string suite_label,
|
||||
/// @param is_enabled If false the test is reported as skipped. If true the test
|
||||
/// is run as normal.
|
||||
template <typename TResult, typename... TInputParams>
|
||||
TestResults
|
||||
execute_suite(std::string suite_label,
|
||||
TestResults execute_suite(std::string suite_label,
|
||||
std::function<TResult(TInputParams...)> function_to_test,
|
||||
std::initializer_list<TestTuple<TResult, TInputParams...>> tests,
|
||||
MaybeTestCompareFunction<TResult> suite_compare = std::nullopt,
|
||||
@@ -491,8 +487,7 @@ execute_suite(std::string suite_label,
|
||||
MaybeTestConfigureFunction after_all = std::nullopt,
|
||||
bool is_enabled = true) {
|
||||
std::vector test_data = std::vector(tests);
|
||||
return execute_suite(suite_label, function_to_test, tests, suite_compare,
|
||||
before_all, after_all, is_enabled);
|
||||
return execute_suite(suite_label, function_to_test, tests, suite_compare, before_all, after_all, is_enabled);
|
||||
}
|
||||
|
||||
/// @brief
|
||||
@@ -516,15 +511,14 @@ execute_suite(std::string suite_label,
|
||||
/// skipped for reporting purposes.
|
||||
/// @return A TestTuple suitable for use as a test run when calling test_fn.
|
||||
template <typename TResult, typename... TInputParams>
|
||||
TestTuple<TResult, TInputParams...>
|
||||
make_test(const string &test_name, const TResult &expected,
|
||||
TestTuple<TResult, TInputParams...> make_test(const string& test_name,
|
||||
const TResult& expected,
|
||||
tuple<TInputParams...> input_params,
|
||||
MaybeTestCompareFunction<TResult> test_compare_fn = std::nullopt,
|
||||
MaybeTestConfigureFunction before_each = std::nullopt,
|
||||
MaybeTestConfigureFunction after_each = std::nullopt,
|
||||
bool is_enabled = true) {
|
||||
return make_tuple(test_name, expected, input_params, test_compare_fn,
|
||||
before_each, after_each, is_enabled);
|
||||
return make_tuple(test_name, expected, input_params, test_compare_fn, before_each, after_each, is_enabled);
|
||||
}
|
||||
|
||||
/// @brief
|
||||
@@ -540,27 +534,26 @@ make_test(const string &test_name, const TResult &expected,
|
||||
/// @param is_enabled
|
||||
/// @return
|
||||
template <typename TResult, typename TFunctionToTest, typename... TInputParams>
|
||||
TestSuite<TResult, TInputParams...>
|
||||
make_test_suite(const string &suite_name, TFunctionToTest function_to_test,
|
||||
TestSuite<TResult, TInputParams...> make_test_suite(const string& suite_name,
|
||||
TFunctionToTest function_to_test,
|
||||
vector<TestTuple<TResult, TInputParams...>> test_data,
|
||||
MaybeTestCompareFunction<TResult> compare = std::nullopt,
|
||||
MaybeTestConfigureFunction before_each = std::nullopt,
|
||||
MaybeTestConfigureFunction after_each = std::nullopt,
|
||||
bool is_enabled = true) {
|
||||
return make_tuple(suite_name, function_to_test, test_data, compare,
|
||||
before_each, after_each, is_enabled);
|
||||
return make_tuple(suite_name, function_to_test, test_data, compare, before_each, after_each, is_enabled);
|
||||
}
|
||||
|
||||
template <typename TResult, typename TFunctionToTest, typename... TInputParams>
|
||||
TestSuite<TResult, TInputParams...> make_test_suite(
|
||||
const string &suite_name, TFunctionToTest function_to_test,
|
||||
const string& suite_name,
|
||||
TFunctionToTest function_to_test,
|
||||
std::initializer_list<TestTuple<TResult, TInputParams...>> test_data,
|
||||
MaybeTestCompareFunction<TResult> compare = std::nullopt,
|
||||
MaybeTestConfigureFunction before_each = std::nullopt,
|
||||
MaybeTestConfigureFunction after_each = std::nullopt,
|
||||
bool is_enabled = true) {
|
||||
return make_tuple(suite_name, function_to_test, test_data, compare,
|
||||
before_each, after_each, is_enabled);
|
||||
return make_tuple(suite_name, function_to_test, test_data, compare, before_each, after_each, is_enabled);
|
||||
}
|
||||
|
||||
/// @brief
|
||||
@@ -568,8 +561,7 @@ TestSuite<TResult, TInputParams...> make_test_suite(
|
||||
/// @tparam TInputParams... The types of parameters sent to the test function.
|
||||
/// @param test_suite A tuple representing the test suite configuration.
|
||||
template <typename TResult, typename... TInputParams>
|
||||
TestResults
|
||||
execute_suite(const TestSuite<TResult, TInputParams...> &test_suite) {
|
||||
TestResults execute_suite(const TestSuite<TResult, TInputParams...>& test_suite) {
|
||||
return execute_suite<TResult, TInputParams...>(
|
||||
std::get<0>(test_suite), std::get<1>(test_suite), std::get<2>(test_suite)
|
||||
// TODO: make this work for the optional parts of the tuple too.
|
||||
@@ -582,8 +574,7 @@ execute_suite(const TestSuite<TResult, TInputParams...> &test_suite) {
|
||||
/// @param second
|
||||
/// @return
|
||||
template <typename... TInputParams>
|
||||
MaybeTestConfigureFunction coalesce(MaybeTestConfigureFunction first,
|
||||
MaybeTestConfigureFunction second) {
|
||||
MaybeTestConfigureFunction coalesce(MaybeTestConfigureFunction first, MaybeTestConfigureFunction second) {
|
||||
if (first.has_value()) {
|
||||
if (second.has_value()) {
|
||||
// This is the only place we actually need to combine them.
|
||||
@@ -603,7 +594,7 @@ MaybeTestConfigureFunction coalesce(MaybeTestConfigureFunction first,
|
||||
/// @param os The stream to write to.
|
||||
/// @param results The TestResults to write.
|
||||
void PrintResults(std::ostream& os, TestResults results);
|
||||
} // End namespace Test
|
||||
} // End namespace TinyTest
|
||||
|
||||
// TODO: Add TShared(*)(string /*test_name*/, UUID /*test_run_id*/)
|
||||
// allocate_shared_data to the test tuple to make some shared data that can be
|
||||
Reference in New Issue
Block a user