navid/veins / 6169bc0f55b5c2ca7a3d19b3405cfaa6fa103a02cf0867e5daa279487e292194 / xveins/tests/test_support.h
test_support.h
← xveins/tests · 4484 bytes · raw · download
#pragma once
#include <iostream>
#include <source_location>
#include <sstream>
#include <stdexcept>
#include <string>
#include <string_view>
#include <type_traits>
#include <vector>
namespace vn::test {
class Failure : public std::runtime_error {
public:
using std::runtime_error::runtime_error;
};
inline std::string location_text(const std::source_location& location) {
std::ostringstream out;
out << location.file_name() << ":" << location.line();
return out.str();
}
template <typename T> std::string display(const T& value) {
if constexpr (std::is_enum_v<T>) {
std::ostringstream out;
out << static_cast<long long>(value);
return out.str();
} else if constexpr (std::is_same_v<T, bool>) {
return value ? "true" : "false";
} else {
std::ostringstream out;
out << value;
return out.str();
}
}
struct Context {
[[noreturn]] void
fail(std::string message,
const std::source_location& location = std::source_location::current()) const {
throw Failure(location_text(location) + ": " + std::move(message));
}
void expect(bool condition,
const std::source_location& location = std::source_location::current()) const {
if (!condition)
fail("expected condition to be true", location);
}
template <typename A, typename B>
void expect_eq(const A& actual, const B& expected,
const std::source_location& location = std::source_location::current()) const {
if (!(actual == expected))
fail("expected " + display(actual) + " == " + display(expected), location);
}
template <typename A, typename B>
void expect_ne(const A& actual, const B& expected,
const std::source_location& location = std::source_location::current()) const {
if (actual == expected)
fail("expected " + display(actual) + " != " + display(expected), location);
}
void
expect_contains(std::string_view text, std::string_view needle,
const std::source_location& location = std::source_location::current()) const {
if (text.find(needle) == std::string_view::npos)
fail("expected text to contain \"" + std::string(needle) + "\", got:\n" +
std::string(text),
location);
}
void expect_not_contains(
std::string_view text, std::string_view needle,
const std::source_location& location = std::source_location::current()) const {
if (text.find(needle) != std::string_view::npos)
fail("expected text not to contain \"" + std::string(needle) + "\", got:\n" +
std::string(text),
location);
}
void expect_starts_with(
std::string_view text, std::string_view prefix,
const std::source_location& location = std::source_location::current()) const {
if (!text.starts_with(prefix))
fail("expected text to start with \"" + std::string(prefix) + "\", got:\n" +
std::string(text),
location);
}
};
struct TestCase {
std::string name;
void (*run)(Context&) = nullptr;
};
struct Suite {
std::string name;
std::vector<TestCase> tests;
};
inline int run_tests(const std::vector<Suite>& suites, std::string_view only = {}) {
int failures = 0;
int count = 0;
for (const Suite& suite : suites) {
if (!only.empty() && suite.name != only)
continue;
for (const TestCase& test : suite.tests) {
++count;
Context context;
try {
test.run(context);
std::cout << "[ok] " << suite.name << "." << test.name << "\n";
} catch (const Failure& failure) {
++failures;
std::cerr << "[FAIL] " << suite.name << "." << test.name << ": " << failure.what()
<< "\n";
} catch (const std::exception& error) {
++failures;
std::cerr << "[FAIL] " << suite.name << "." << test.name
<< ": unexpected exception: " << error.what() << "\n";
}
}
}
if (count == 0) {
std::cerr << "no tests selected\n";
return 1;
}
std::cout << count - failures << "/" << count << " tests passed\n";
return failures == 0 ? 0 : 1;
}
} // namespace vn::test