diff --git a/etest/disabled_test_test.cpp b/etest/disabled_test_test.cpp index 6726a081..766109a9 100644 --- a/etest/disabled_test_test.cpp +++ b/etest/disabled_test_test.cpp @@ -1,23 +1,25 @@ -// SPDX-FileCopyrightText: 2022-2023 Robin Lindén +// SPDX-FileCopyrightText: 2022-2024 Robin Lindén // // SPDX-License-Identifier: BSD-2-Clause -#include "etest/etest.h" +#include "etest/etest2.h" #include #include int main() { + etest::Suite s{}; + bool ran{}; - etest::disabled_test("hi", [&] { ran = true; }); + s.disabled_test("hi", [&](etest::IActions &) { ran = true; }); - std::ignore = etest::run_all_tests(); + std::ignore = s.run(); if (ran) { std::cerr << "A disabled test ran when it shouldn't have\n"; return 1; } - std::ignore = etest::run_all_tests({.run_disabled_tests = true}); + std::ignore = s.run({.run_disabled_tests = true}); if (!ran) { std::cerr << "A disabled test didn't run when it should have\n"; return 1; diff --git a/etest/etest.cpp b/etest/etest.cpp deleted file mode 100644 index bf20cb0c..00000000 --- a/etest/etest.cpp +++ /dev/null @@ -1,56 +0,0 @@ -// SPDX-FileCopyrightText: 2021-2024 Robin Lindén -// -// SPDX-License-Identifier: BSD-2-Clause - -#include "etest/etest.h" - -#include "etest/etest2.h" - -#include -#include -#include -#include -#include -#include -#include - -namespace etest { -namespace { -Suite ®istry() { - static Suite test_registry{}; - return test_registry; -} - -// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables) -std::optional> current_actions{}; -} // namespace - -int run_all_tests(RunOptions const &opts) noexcept { - return registry().run(opts); -} - -void test(std::string name, std::function body) noexcept { - registry().add_test(std::move(name), [body = std::move(body)](IActions &a) { - current_actions = a; - body(); - }); -} - -void disabled_test(std::string name, std::function body) noexcept { - registry().disabled_test(std::move(name), [body = std::move(body)](IActions &a) { - current_actions = a; - body(); - }); -} - -void expect(bool b, std::optional log_message, std::source_location const &loc) noexcept { - assert(current_actions.has_value()); - current_actions.value().get().expect(b, std::move(log_message), loc); -} - -void require(bool b, std::optional log_message, std::source_location const &loc) { - assert(current_actions.has_value()); - current_actions.value().get().require(b, std::move(log_message), loc); -} - -} // namespace etest diff --git a/etest/etest.h b/etest/etest.h deleted file mode 100644 index 59e22850..00000000 --- a/etest/etest.h +++ /dev/null @@ -1,83 +0,0 @@ -// SPDX-FileCopyrightText: 2021-2024 Robin Lindén -// -// SPDX-License-Identifier: BSD-2-Clause - -#ifndef ETEST_TEST_H_ -#define ETEST_TEST_H_ - -#include "etest/etest2.h" - -#include -#include -#include -#include -#include -#include -#include - -namespace etest { - -[[nodiscard]] int run_all_tests(RunOptions const & = {}) noexcept; - -void test(std::string name, std::function body) noexcept; -void disabled_test(std::string name, std::function body) noexcept; - -// Weak test requirement. Allows the test to continue even if the check fails. -void expect(bool, - std::optional log_message = std::nullopt, - std::source_location const &loc = std::source_location::current()) noexcept; - -// Hard test requirement. Stops the test (using an exception) if the check fails. -void require(bool, - std::optional log_message = std::nullopt, - std::source_location const &loc = std::source_location::current()); - -// Weak test requirement. Prints the types compared on failure (if printable). -template -void expect_eq(T const &a, - U const &b, - std::optional log_message = std::nullopt, - std::source_location const &loc = std::source_location::current()) noexcept { - if (a == b) { - return; - } - - std::stringstream ss; - ss << a << " !=\n" << b; - expect(false, log_message ? std::move(log_message) : std::move(ss).str(), loc); -} - -template -void expect_eq(T const &a, - U const &b, - std::optional log_message = std::nullopt, - std::source_location const &loc = std::source_location::current()) noexcept { - expect(a == b, std::move(log_message), loc); -} - -// Hard test requirement. Prints the types compared on failure (if printable). -template -void require_eq(T const &a, - U const &b, - std::optional log_message = std::nullopt, - std::source_location const &loc = std::source_location::current()) { - if (a == b) { - return; - } - - std::stringstream ss; - ss << a << " !=\n" << b; - require(false, log_message ? std::move(log_message) : std::move(ss).str(), loc); -} - -template -void require_eq(T const &a, - U const &b, - std::optional log_message = std::nullopt, - std::source_location const &loc = std::source_location::current()) { - require(a == b, std::move(log_message), loc); -} - -} // namespace etest - -#endif diff --git a/etest/expect_eq_failure_test.cpp b/etest/expect_eq_failure_test.cpp index b8cef37e..c8034798 100644 --- a/etest/expect_eq_failure_test.cpp +++ b/etest/expect_eq_failure_test.cpp @@ -1,10 +1,11 @@ -// SPDX-FileCopyrightText: 2021-2022 Robin Lindén +// SPDX-FileCopyrightText: 2021-2024 Robin Lindén // // SPDX-License-Identifier: BSD-2-Clause -#include "etest/etest.h" +#include "etest/etest2.h" int main() { - etest::test("this should fail", [] { etest::expect_eq(1, 2); }); - return etest::run_all_tests(); + etest::Suite s{}; + s.add_test("this should fail", [](etest::IActions &a) { a.expect_eq(1, 2); }); + return s.run(); } diff --git a/etest/expect_failure_test.cpp b/etest/expect_failure_test.cpp index 313496a8..74326a80 100644 --- a/etest/expect_failure_test.cpp +++ b/etest/expect_failure_test.cpp @@ -1,12 +1,11 @@ -// SPDX-FileCopyrightText: 2021-2022 Robin Lindén +// SPDX-FileCopyrightText: 2021-2024 Robin Lindén // // SPDX-License-Identifier: BSD-2-Clause -#include "etest/etest.h" - -using etest::expect; +#include "etest/etest2.h" int main() { - etest::test("this should fail", [] { expect(false); }); - return etest::run_all_tests(); + etest::Suite s{}; + s.add_test("this should fail", [](etest::IActions &a) { a.expect(false); }); + return s.run(); } diff --git a/etest/no_tests_registered_test.cpp b/etest/no_tests_registered_test.cpp index 3eca2bc8..c6b33477 100644 --- a/etest/no_tests_registered_test.cpp +++ b/etest/no_tests_registered_test.cpp @@ -1,12 +1,12 @@ -// SPDX-FileCopyrightText: 2023 Robin Lindén +// SPDX-FileCopyrightText: 2023-2024 Robin Lindén // // SPDX-License-Identifier: BSD-2-Clause -#include "etest/etest.h" +#include "etest/etest2.h" // If you try to run tests, but none will run due to them all being filtered out // or something, that's probably an error. int main() { - bool failure = etest::run_all_tests() != 0; + bool failure = etest::Suite{}.run() != 0; return failure ? 0 : 1; } diff --git a/etest/require_eq_failure_test.cpp b/etest/require_eq_failure_test.cpp index 23cab400..54b3c93a 100644 --- a/etest/require_eq_failure_test.cpp +++ b/etest/require_eq_failure_test.cpp @@ -1,10 +1,11 @@ -// SPDX-FileCopyrightText: 2021-2022 Robin Lindén +// SPDX-FileCopyrightText: 2021-2024 Robin Lindén // // SPDX-License-Identifier: BSD-2-Clause -#include "etest/etest.h" +#include "etest/etest2.h" int main() { - etest::test("this should fail", [] { etest::require_eq(1, 2); }); - return etest::run_all_tests(); + etest::Suite s{}; + s.add_test("this should fail", [](etest::IActions &a) { a.require_eq(1, 2); }); + return s.run(); } diff --git a/etest/require_failure_test.cpp b/etest/require_failure_test.cpp index 7b68d746..946cc4cd 100644 --- a/etest/require_failure_test.cpp +++ b/etest/require_failure_test.cpp @@ -1,12 +1,11 @@ -// SPDX-FileCopyrightText: 2021-2022 Robin Lindén +// SPDX-FileCopyrightText: 2021-2024 Robin Lindén // // SPDX-License-Identifier: BSD-2-Clause -#include "etest/etest.h" - -using etest::require; +#include "etest/etest2.h" int main() { - etest::test("this should fail", [] { require(false); }); - return etest::run_all_tests(); + etest::Suite s{}; + s.add_test("this should fail", [](etest::IActions &a) { a.require(false); }); + return s.run(); } diff --git a/etest/success_test.cpp b/etest/success_test.cpp index bc71eb2a..ab244582 100644 --- a/etest/success_test.cpp +++ b/etest/success_test.cpp @@ -1,13 +1,14 @@ -// SPDX-FileCopyrightText: 2021 Robin Lindén +// SPDX-FileCopyrightText: 2021-2024 Robin Lindén // // SPDX-License-Identifier: BSD-2-Clause -#include "etest/etest.h" +#include "etest/etest2.h" int main() { - etest::test("expect", [] { etest::expect(true); }); - etest::test("expect_eq", [] { etest::expect_eq(1, 1); }); - etest::test("require", [] { etest::require(true); }); - etest::test("require_eq", [] { etest::require_eq(1, 1); }); - return etest::run_all_tests(); + etest::Suite s{}; + s.add_test("expect", [](etest::IActions &a) { a.expect(true); }); + s.add_test("expect_eq", [](etest::IActions &a) { a.expect_eq(1, 1); }); + s.add_test("require", [](etest::IActions &a) { a.require(true); }); + s.add_test("require_eq", [](etest::IActions &a) { a.require_eq(1, 1); }); + return s.run(); } diff --git a/unicode/util_test.cpp b/unicode/util_test.cpp index b573e19e..490181e5 100644 --- a/unicode/util_test.cpp +++ b/unicode/util_test.cpp @@ -104,9 +104,9 @@ int main() { }); s.add_test("CodePointView", [](etest::IActions &a) { - auto into_code_points = [](std::string_view s) { + auto into_code_points = [](std::string_view sv) { std::vector code_points{}; - for (auto cp : CodePointView{s}) { + for (auto cp : CodePointView{sv}) { code_points.push_back(cp); } return code_points;