light/modules/test/public/expects.hpp
light7734 cd886aa8c9
Some checks reported errors
continuous-integration/drone/push Build was killed
refactor: flatten directory structure
2025-07-20 04:46:15 +03:30

128 lines
2.4 KiB
C++

#pragma once
#include <concepts>
#include <format>
#include <source_location>
namespace lt::test {
template<typename T>
concept Printable = requires(std::ostream &os, T t) {
{ os << t } -> std::same_as<std::ostream &>;
};
template<typename T>
concept Testable = Printable<T> && std::equality_comparable<T>;
constexpr void expect_eq(
Testable auto lhs,
Testable auto rhs,
std::source_location source_location = std::source_location::current()
)
{
if (lhs != rhs)
{
throw std::runtime_error {
std::format(
"Failed equality expectation:\n"
"\tactual: {}\n"
"\texpected: {}\n"
"\tlocation: {}:{}",
lhs,
rhs,
source_location.file_name(),
source_location.line()
),
};
}
}
constexpr void expect_ne(
Testable auto lhs,
Testable auto rhs,
std::source_location source_location = std::source_location::current()
)
{
if (lhs == rhs)
{
throw std::runtime_error {
std::format(
"Failed un-equality expectation:\n"
"\tactual: {}\n"
"\texpected: {}\n"
"\tlocation: {}:{}",
lhs,
rhs,
source_location.file_name(),
source_location.line()
),
};
}
}
constexpr void expect_true(
bool expression,
std::source_location source_location = std::source_location::current()
)
{
if (!expression)
{
throw std::runtime_error {
std::format(
"Failed true expectation:\n"
"\tactual: {}\n"
"\texpected: true\n"
"\tlocation: {}:{}",
expression,
source_location.file_name(),
source_location.line()
),
};
}
}
constexpr void expect_false(
bool expression,
std::source_location source_location = std::source_location::current()
)
{
if (expression)
{
throw std::runtime_error {
std::format(
"Failed false expectation:\n"
"\tactual: {}\n"
"\texpected: true\n"
"\tlocation: {}:{}",
expression,
source_location.file_name(),
source_location.line()
),
};
}
}
constexpr void expect_le(
Testable auto lhs,
Testable auto rhs,
std::source_location source_location = std::source_location::current()
)
{
if (lhs > rhs)
{
throw std::runtime_error {
std::format(
"Failed false expectation:\n"
"\tactual: {}\n"
"\texpected: >= {}\n"
"\tlocation: {}:{}",
lhs,
rhs,
source_location.file_name(),
source_location.line()
),
};
}
}
} // namespace lt::test