From 29086ca1039130b11c2658065622788e6ef63e50 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B3n=20Bjarni=20Bjarnason?= Date: Mon, 24 Jun 2024 08:55:29 +0000 Subject: [PATCH] formatting and adding test with arguments --- exes/themis/tests/themis_integration_test.cpp | 90 ++++++++++++------- 1 file changed, 57 insertions(+), 33 deletions(-) diff --git a/exes/themis/tests/themis_integration_test.cpp b/exes/themis/tests/themis_integration_test.cpp index 5f6776f30c..f2a2a7c428 100644 --- a/exes/themis/tests/themis_integration_test.cpp +++ b/exes/themis/tests/themis_integration_test.cpp @@ -3,13 +3,13 @@ #include #include -#include "alarm_database.hpp" -#include "dbus_interface.hpp" #include #include #include #include #include +#include "alarm_database.hpp" +#include "dbus_interface.hpp" namespace ut = boost::ut; namespace asio = boost::asio; @@ -20,12 +20,12 @@ using boost::ut::operator|; using boost::ut::operator/; using boost::ut::expect; using boost::ut::throws; +using boost::ut::fatal; using tfc::snitch::error; using tfc::snitch::info; using tfc::snitch::warning; using tfc::snitch::warning_latched; using tfc::snitch::warning_resettable; -using tfc::snitch::error; using tfc::snitch::detail::dbus_client; using tfc::themis::alarm_database; using tfc::themis::interface; @@ -51,6 +51,13 @@ struct test_setup { } }; +struct formattable_type { + constexpr operator std::string_view() const noexcept { return "42"; } +}; +constexpr auto format_as(formattable_type const& val) -> std::string_view { + return std::string_view{ val }; +} + int main(int argc, char** argv) { tfc::base::init(argc, argv); "Test some dbus functions for signature errors and general correctness"_test = [] { @@ -62,7 +69,7 @@ int main(int argc, char** argv) { }); t.ctx.run_for(2ms); expect(t.ran[0]); - t.client.list_alarms([&](const std::error_code& err, std::vector alarms){ + t.client.list_alarms([&](const std::error_code& err, std::vector alarms) { expect(!err) << err.message(); expect(alarms.size() == 1); t.ran[5] = true; @@ -112,14 +119,12 @@ int main(int argc, char** argv) { expect(t.ran[3]); expect(t.ran[4]); }; - "Test if the try_reset signal makes it in this world"_test = []{ + "Test if the try_reset signal makes it in this world"_test = [] { test_setup t; warning_latched<"desc", "details"> w(t.connection, "first_test"); t.ctx.run_for(1ms); - w.on_try_reset([&]() { - t.ran[1] = true; - }); - t.client.try_reset_alarm(w.alarm_id().value(), [&](const std::error_code& err){ + w.on_try_reset([&]() { t.ran[1] = true; }); + t.client.try_reset_alarm(w.alarm_id().value(), [&](const std::error_code& err) { expect(!!err); t.ran[2] = true; }); @@ -134,10 +139,8 @@ int main(int argc, char** argv) { t.ctx.run_for(2ms); expect(t.ran[0]); expect(w.alarm_id().has_value()); - w.on_try_reset([&]() { - t.ran[3] = true; - }); - t.client.try_reset_alarm(w.alarm_id().value(), [&](const std::error_code& err){ + w.on_try_reset([&]() { t.ran[3] = true; }); + t.client.try_reset_alarm(w.alarm_id().value(), [&](const std::error_code& err) { expect(!err) << err.message(); t.ran[4] = true; }); @@ -145,21 +148,15 @@ int main(int argc, char** argv) { expect(t.ran[3]); expect(t.ran[4]); }; - "Test is the try_reset_all signal tickles these fancies"_test = []{ + "Test is the try_reset_all signal tickles these fancies"_test = [] { test_setup t; warning_latched<"desc1", "details"> wl(t.connection, "first_test"); warning_resettable<"desc2", "details"> wr(t.connection, "first_test"); error<"desc3", "details"> e(t.connection, "first_test"); - wl.on_try_reset([&]() { - t.ran[1] = true; - }); - wr.on_try_reset([&]() { - t.ran[2] = true; - }); - e.on_try_reset([&]() { - t.ran[3] = true; - }); - t.client.try_reset_all_alarms([&](const std::error_code& err){ + wl.on_try_reset([&]() { t.ran[1] = true; }); + wr.on_try_reset([&]() { t.ran[2] = true; }); + e.on_try_reset([&]() { t.ran[3] = true; }); + t.client.try_reset_all_alarms([&](const std::error_code& err) { expect(!!err); t.ran[0] = true; }); @@ -173,7 +170,7 @@ int main(int argc, char** argv) { expect(!err) << err.message(); t.ran[4] = true; }); - t.client.try_reset_all_alarms([&](const std::error_code& err){ + t.client.try_reset_all_alarms([&](const std::error_code& err) { expect(!err) << err.message(); t.ran[5] = true; }); @@ -191,7 +188,7 @@ int main(int argc, char** argv) { expect(!err) << err.message(); t.ran[7] = true; }); - t.client.try_reset_all_alarms([&](const std::error_code& err){ + t.client.try_reset_all_alarms([&](const std::error_code& err) { expect(!err) << err.message(); t.ran[8] = true; }); @@ -207,13 +204,40 @@ int main(int argc, char** argv) { "alarm with declared params but no params provided"_test = [] { test_setup t; info<"desc {foo}", "details {bar} {foo}"> i(t.connection, "first_test"); - try { - i.set(); - expect(false) << "Should have thrown"; - } - catch (std::exception&) { - expect(true); - } + expect(throws([&]{ i.set(); })); + }; + + "alarm with wrong named params"_test = [] { + test_setup t; + info<"desc {foo}", "details {bar} {foo}"> i(t.connection, "first_test"); + expect(throws([&]{ i.set(fmt::arg("ababa", 1), fmt::arg("some_other name", 2)); })); + }; + + "alarm with params"_test = [] { + test_setup t; + info<"desc {foo}", "details {bar} {foo}"> i(t.connection, "first_test"); + i.set( + [&](auto err) { + expect(!err) << fmt::format("Received error: {}", err.message()); + t.ran[0] = true; + + t.client.list_activations( + "en", 0, 10000, tfc::snitch::level_e::info, tfc::snitch::api::active_e::active, + tfc::themis::alarm_database::timepoint_from_milliseconds(0), + tfc::themis::alarm_database::timepoint_from_milliseconds(std::numeric_limits::max()), + [&](auto list_err, std::vector const& act) { + expect(!list_err) << list_err.message(); + expect(fatal(act.size() == 1)); + auto const& alarm = act.at(0); + expect(alarm.description == "desc 1337"); + expect(alarm.details == "details 42 1337"); + t.ran[1] = true; + }); + }, + fmt::arg("foo", 1337), fmt::arg("bar", formattable_type{})); + t.ctx.run_for(2ms); + expect(t.ran[0]); + expect(t.ran[1]); }; return 0;