light/modules/test/test.test.cpp

216 lines
5 KiB
C++
Raw Normal View History

import test.test;
import test.expects;
import std;
using lt::test::Case;
using lt::test::Suite;
using lt::test::operator""_suite;
2025-09-30 06:44:09 +03:30
Suite expects = "expects"_suite = []() {
using lt::test::expect_unreachable;
2025-07-16 13:20:36 +03:30
using lt::test::expect_true;
using lt::test::expect_false;
using lt::test::expect_eq;
using lt::test::expect_ne;
using lt::test::expect_le;
using lt::test::expect_throw;
Case { "" } = [] {
};
Case { "expect_unreachable" } = [] {
auto unhappy = false;
// clang-format off
try { expect_unreachable(); }
2026-01-09 21:53:37 +03:30
catch (const std::exception &) { unhappy = true; }
// clang-format on
if (!unhappy)
{
throw std::runtime_error { "expect_unreachable" };
}
};
Case { "expect_true - happy" } = [] {
auto oongaboonga = int {};
auto *oongaboonga_ptr_here = &oongaboonga;
expect_true(oongaboonga_ptr_here);
expect_true(true);
expect_true(1); // NOLINT
};
Case { "expect_true - unhappy" } = [] {
auto unhappy_counter = 0u;
auto *where_oongaboonga_ptr = (int *)nullptr;
// clang-format off
try { expect_true(where_oongaboonga_ptr); }
2026-01-09 21:53:37 +03:30
catch (const std::exception&) { ++unhappy_counter; }
try { expect_true(!true); }
2026-01-09 21:53:37 +03:30
catch (const std::exception&) { ++unhappy_counter; }
try { expect_true(false); }
2026-01-09 21:53:37 +03:30
catch (const std::exception&) { ++unhappy_counter; }
try { expect_true(0); } // NOLINT
2026-01-09 21:53:37 +03:30
catch (const std::exception&) { ++unhappy_counter; }
// clang-format on
};
Case { "expect_false - happy" } = [] {
auto *oongaboonga_is_slacking = (int *)nullptr;
expect_false(oongaboonga_is_slacking);
expect_false(false);
};
Case { "expect_false - unhappy" } = [] {
auto *oonga_oonga_can_rest_now = (int *)nullptr;
auto unhappy_counter = 0u;
// clang-format off
try { expect_false(oonga_oonga_can_rest_now); }
2026-01-09 21:53:37 +03:30
catch (const std::exception&) { ++unhappy_counter; }
try { expect_false(true); }
2026-01-09 21:53:37 +03:30
catch (const std::exception&) { ++unhappy_counter; }
try { expect_false(!false); }
2026-01-09 21:53:37 +03:30
catch (const std::exception&) { ++unhappy_counter; }
2026-01-09 21:53:37 +03:30
try { expect_false(!!1); }
catch (const std::exception&) { ++unhappy_counter; }
// clang-format on
2026-01-09 21:53:37 +03:30
if (unhappy_counter != 4)
{
throw std::runtime_error { "expect_false - unhappy" };
}
};
Case { "expect_true - unhappy" } = [] {
auto unhappy_counter = 0u;
auto *where_oongaboonga_ptr = (int *)nullptr;
// clang-format off
try { expect_true(where_oongaboonga_ptr); }
2026-01-09 21:53:37 +03:30
catch (const std::exception&) { ++unhappy_counter; }
try { expect_true(!true); }
2026-01-09 21:53:37 +03:30
catch (const std::exception&) { ++unhappy_counter; }
try { expect_true(false); }
2026-01-09 21:53:37 +03:30
catch (const std::exception&) { ++unhappy_counter; }
2026-01-09 21:53:37 +03:30
try { expect_true(!!0); }
catch (const std::exception&) { ++unhappy_counter; }
// clang-format on
2026-01-09 21:53:37 +03:30
if (unhappy_counter != 4)
{
throw std::runtime_error { "expect_true - unhappy" };
}
};
Case { "expect_eq - happy" } = [] {
expect_eq(5, 5);
expect_eq(20.0, 20.0);
2026-01-09 21:53:37 +03:30
expect_eq(true, true);
};
Case { "expect_eq - unhappy" } = [] {
auto unhappy = false;
2025-07-16 13:20:36 +03:30
// clang-format off
try { expect_eq(true, false); }
2026-01-09 21:53:37 +03:30
catch (const std::exception &) { unhappy = true; }
// clang-format on
if (!unhappy)
2025-07-16 13:20:36 +03:30
{
throw std::runtime_error { "expect_eq unhappy" };
2025-07-16 13:20:36 +03:30
}
};
Case { "expect_ne - happy " } = [] {
expect_ne(5, 5.0000001);
expect_ne(20.0, 69.0);
2026-01-09 21:53:37 +03:30
expect_ne(true, true);
expect_ne(false, false);
};
Case { "expect_ne - unhappy" } = [] {
auto unhappy_counter = 0u;
// clang-format off
try { expect_ne(5, 5); }
2026-01-09 21:53:37 +03:30
catch (const std::exception &) { ++unhappy_counter; }
try { expect_ne(20.0, 20.0); }
2026-01-09 21:53:37 +03:30
catch (const std::exception &) { ++unhappy_counter; }
try { expect_ne(true, true); }
catch (const std::exception &) { ++unhappy_counter; }
2026-01-09 21:53:37 +03:30
try { expect_ne(false, false); }
catch (const std::exception &) { ++unhappy_counter; }
// clang-format on
2026-01-09 21:53:37 +03:30
if (unhappy_counter != 4)
2025-07-16 13:20:36 +03:30
{
throw std::runtime_error { "expect_ne unhappy" };
2025-07-16 13:20:36 +03:30
}
};
2025-07-16 13:20:36 +03:30
Case { "expect_throw - happy" } = [] {
expect_throw([] { throw std::runtime_error { "nonsense" }; });
};
Case { "expect_throw - unhappy" } = [] {
auto unhappy = false;
// clang-format off
try { expect_throw([] {}); }
2026-01-09 21:53:37 +03:30
catch (const std::exception &) { unhappy = true; }
// clang-format on
if (!unhappy)
{
throw std::runtime_error { "expect_throw - unhappy" };
}
};
Case { "expect_le - happy" } = [] {
expect_le(69, 420);
expect_le(19.694206942069420, 20.0);
2026-01-09 21:53:37 +03:30
expect_le(false, !!1);
};
Case { "expect_le - unhappy" } = [] {
auto unhappy_counter = 0u;
// clang-format off
try { expect_le(20020619 + 23, 20020619 ); }
2026-01-09 21:53:37 +03:30
catch (const std::exception &) { ++unhappy_counter; }
try { expect_le(420, 69); }
2026-01-09 21:53:37 +03:30
catch (const std::exception &) { ++unhappy_counter; }
try { expect_le(20.0, 19.694206942069420); }
2026-01-09 21:53:37 +03:30
catch (const std::exception &) { ++unhappy_counter; }
2026-01-09 21:53:37 +03:30
try { expect_le(true, false); }
catch (const std::exception &) { ++unhappy_counter; }
// clang-format on
if (unhappy_counter != 4)
{
throw std::runtime_error { "expect_le - unhappy" };
}
};
};