diff --git a/src/dsl/word/Pool.hpp b/src/dsl/word/Pool.hpp index 9ac0f6a1..9d47eb32 100644 --- a/src/dsl/word/Pool.hpp +++ b/src/dsl/word/Pool.hpp @@ -49,7 +49,7 @@ namespace dsl { } template - static constexpr bool check(A&&... /*unused*/) { + static constexpr bool check(const A&... /*unused*/) { return true; } diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 6a095467..728c3d9e 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -27,6 +27,7 @@ FetchContent_Declare( SYSTEM DOWNLOAD_EXTRACT_TIMESTAMP TRUE URL https://github.com/catchorg/Catch2/archive/refs/tags/v3.6.0.tar.gz ) +set(CATCH_CONFIG_CONSOLE_WIDTH 120) FetchContent_MakeAvailable(Catch2) # Silence clang-tidy warnings from catch2 diff --git a/tests/networktest.cpp b/tests/networktest.cpp index 67bad089..64c3b713 100644 --- a/tests/networktest.cpp +++ b/tests/networktest.cpp @@ -26,7 +26,6 @@ #include #include -namespace { struct PerformEmits {}; using NUClear::message::CommandLineArguments; @@ -119,8 +118,6 @@ class TestReactor : public NUClear::Reactor { }); } }; -} // namespace - // NOLINTNEXTLINE(bugprone-exception-escape) int main(int argc, const char* argv[]) { diff --git a/tests/tests/api/ReactionHandle.cpp b/tests/tests/api/ReactionHandle.cpp index a18b1d1d..ccff5f72 100644 --- a/tests/tests/api/ReactionHandle.cpp +++ b/tests/tests/api/ReactionHandle.cpp @@ -25,23 +25,18 @@ #include "test_util/TestBase.hpp" -// Anonymous namespace to keep everything file local -namespace { - -/// Events that occur during the test -std::vector events; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables) - -struct Message { - Message(int i) : i(i) {} - int i; -}; class TestReactor : public test_util::TestBase { public: + struct Message { + Message(int i) : i(i) {} + int i; + }; + TestReactor(std::unique_ptr environment) : TestBase(std::move(environment)) { // Make an always disabled reaction - a = on, Priority::HIGH>().then([](const Message& msg) { // + a = on, Priority::HIGH>().then([this](const Message& msg) { // events.push_back("Executed disabled reaction " + std::to_string(msg.i)); }); a.disable(); @@ -53,7 +48,7 @@ class TestReactor : public test_util::TestBase { emit(std::make_unique(1)); }); - on>().then([](const Message& msg) { // + on>().then([this](const Message& msg) { // events.push_back("Executed enabled reaction " + std::to_string(msg.i)); }); @@ -65,14 +60,17 @@ class TestReactor : public test_util::TestBase { ReactionHandle a; ReactionHandle b; + + /// Events that occur during the test + std::vector events; }; -} // namespace + TEST_CASE("Testing reaction handle functionality", "[api][reactionhandle]") { NUClear::Configuration config; config.thread_count = 1; NUClear::PowerPlant plant(config); - plant.install(); + const auto& reactor = plant.install(); plant.start(); const std::vector expected = { @@ -82,8 +80,8 @@ TEST_CASE("Testing reaction handle functionality", "[api][reactionhandle]") { }; // Make an info print the diff in an easy to read way if we fail - INFO(test_util::diff_string(expected, events)); + INFO(test_util::diff_string(expected, reactor.events)); // Check the events fired in order and only those events - REQUIRE(events == expected); + REQUIRE(reactor.events == expected); } diff --git a/tests/tests/api/ReactionStatistics.cpp b/tests/tests/api/ReactionStatistics.cpp index 1a7c0621..0af4650e 100644 --- a/tests/tests/api/ReactionStatistics.cpp +++ b/tests/tests/api/ReactionStatistics.cpp @@ -26,19 +26,15 @@ #include "test_util/TestBase.hpp" // This namespace is named to make things consistent with the reaction statistics test -namespace stats_test { - -/// Events that occur during the test -std::vector events; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables) - -template -struct Message {}; -struct LoopMessage {}; using NUClear::message::ReactionEvent; class TestReactor : public test_util::TestBase { public: + template + struct Message {}; + struct LoopMessage {}; + TestReactor(std::unique_ptr environment) : TestBase(std::move(environment)) { // This reaction is here to emit something from a ReactionStatistics trigger @@ -77,7 +73,7 @@ class TestReactor : public test_util::TestBase { } }); - on>>().then("Exception Handler", [] { + on>>().then("Exception Handler", [this] { events.push_back("Running Exception Handler"); throw std::runtime_error("Text in an exception"); }); @@ -92,33 +88,36 @@ class TestReactor : public test_util::TestBase { emit(std::make_unique>()); }); } + + /// Events that occur during the test + std::vector events; }; -} // namespace stats_test + TEST_CASE("Testing reaction statistics functionality", "[api][reactionstatistics]") { NUClear::Configuration config; config.thread_count = 1; NUClear::PowerPlant plant(config); - plant.install(); + const auto& reactor = plant.install(); plant.start(); const std::vector expected = { "Running Startup Handler", - "Stats for Startup Handler from stats_test::TestReactor", + "Stats for Startup Handler from TestReactor", "NUClear::Reactor::on", "Running Message Handler", - "Stats for Message Handler from stats_test::TestReactor", - "NUClear::Reactor::on>>", + "Stats for Message Handler from TestReactor", + "NUClear::Reactor::on>>", "Running Exception Handler", - "Stats for Exception Handler from stats_test::TestReactor", - "NUClear::Reactor::on>>", + "Stats for Exception Handler from TestReactor", + "NUClear::Reactor::on>>", "Exception received: \"Text in an exception\"", }; // Make an info print the diff in an easy to read way if we fail - INFO(test_util::diff_string(expected, stats_test::events)); + INFO(test_util::diff_string(expected, reactor.events)); // Check the events fired in order and only those events - REQUIRE(stats_test::events == expected); + REQUIRE(reactor.events == expected); } diff --git a/tests/tests/api/ReactionStatisticsTiming.cpp b/tests/tests/api/ReactionStatisticsTiming.cpp index b14f96cf..e2171142 100644 --- a/tests/tests/api/ReactionStatisticsTiming.cpp +++ b/tests/tests/api/ReactionStatisticsTiming.cpp @@ -26,9 +26,6 @@ #include "test_util/TestBase.hpp" #include "test_util/TimeUnit.hpp" -// Anonymous namespace to keep everything file local -namespace { - using TimeUnit = test_util::TimeUnit; /// Events that occur during the test and the time they occur @@ -118,7 +115,7 @@ class TestReactor : public test_util::TestBase { }); } }; -} // namespace + TEST_CASE("Testing reaction statistics timing", "[api][reactionstatistics][timing]") { diff --git a/tests/tests/api/ReactorArgs.cpp b/tests/tests/api/ReactorArgs.cpp index bb983755..ad262e21 100644 --- a/tests/tests/api/ReactorArgs.cpp +++ b/tests/tests/api/ReactorArgs.cpp @@ -23,9 +23,6 @@ #include #include -// Anonymous namespace to keep everything file local -namespace { - class TestReactorNoArgs : public NUClear::Reactor { public: TestReactorNoArgs(std::unique_ptr environment) : NUClear::Reactor(std::move(environment)) {} @@ -44,7 +41,6 @@ class TestReactorArgs : public NUClear::Reactor { uint32_t i{0}; }; -} // namespace TEST_CASE("Testing Reactor installation arguments", "[api][reactorargs]") { NUClear::Configuration config; diff --git a/tests/tests/api/TimeTravel.cpp b/tests/tests/api/TimeTravel.cpp index 5bbb949f..623394e7 100644 --- a/tests/tests/api/TimeTravel.cpp +++ b/tests/tests/api/TimeTravel.cpp @@ -5,8 +5,6 @@ #include "test_util/TestBase.hpp" #include "test_util/TimeUnit.hpp" -namespace { - using TimeUnit = test_util::TimeUnit; constexpr int64_t EVENT_1_TIME = 4; @@ -73,7 +71,6 @@ class TestReactor : public test_util::TestBase { Results results; }; -} // anonymous namespace TEST_CASE("Test time travel correctly changes the time for non zero rtf", "[time_travel][chrono_controller]") { diff --git a/tests/tests/api/TimeTravelFrozen.cpp b/tests/tests/api/TimeTravelFrozen.cpp index 162b6023..99328f3d 100644 --- a/tests/tests/api/TimeTravelFrozen.cpp +++ b/tests/tests/api/TimeTravelFrozen.cpp @@ -5,8 +5,6 @@ #include "test_util/TestBase.hpp" -namespace { - constexpr std::chrono::milliseconds EVENT_1_TIME = std::chrono::milliseconds(4); constexpr std::chrono::milliseconds EVENT_2_TIME = std::chrono::milliseconds(8); constexpr std::chrono::milliseconds SHUTDOWN_TIME = std::chrono::milliseconds(12); @@ -67,7 +65,6 @@ class TestReactor : public test_util::TestBase { std::vector events; }; -} // anonymous namespace TEST_CASE("Test time travel correctly changes the time for non zero rtf", "[time_travel][chrono_controller]") { @@ -112,5 +109,5 @@ TEST_CASE("Test time travel correctly changes the time for non zero rtf", "[time } INFO(test_util::diff_string(expected, reactor.events)); - CHECK(expected == reactor.events); + CHECK(reactor.events == expected); } diff --git a/tests/tests/dsl/Always.cpp b/tests/tests/dsl/Always.cpp index 0b78407c..3d32ae94 100644 --- a/tests/tests/dsl/Always.cpp +++ b/tests/tests/dsl/Always.cpp @@ -25,15 +25,9 @@ #include "test_util/TestBase.hpp" -namespace { - -/// Events that occur during the test -std::vector events; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables) - -struct SimpleMessage {}; - class TestReactor : public test_util::TestBase { public: + struct SimpleMessage {}; TestReactor(std::unique_ptr environment) : TestBase(std::move(environment), false) { on().then([this] { @@ -56,15 +50,18 @@ class TestReactor : public test_util::TestBase { /// Counter for the number of times we have run int i = 0; + + /// Events that occur during the test + std::vector events; }; -} // namespace + TEST_CASE("The Always DSL keyword runs continuously when it can", "[api][always]") { NUClear::Configuration config; config.thread_count = 1; NUClear::PowerPlant plant(config); - plant.install(); + auto& reactor = plant.install(); plant.start(); const std::vector expected = { @@ -82,8 +79,8 @@ TEST_CASE("The Always DSL keyword runs continuously when it can", "[api][always] }; // Make an info print the diff in an easy to read way if we fail - INFO(test_util::diff_string(expected, events)); + INFO(test_util::diff_string(expected, reactor.events)); // Check the events fired in order and only those events - REQUIRE(events == expected); + REQUIRE(reactor.events == expected); } diff --git a/tests/tests/dsl/ArgumentFission.cpp b/tests/tests/dsl/ArgumentFission.cpp index 511fd6dc..02cabedb 100644 --- a/tests/tests/dsl/ArgumentFission.cpp +++ b/tests/tests/dsl/ArgumentFission.cpp @@ -26,8 +26,6 @@ #include "test_util/TestBase.hpp" -namespace { - /// Events that occur during the test std::vector events; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables) @@ -84,7 +82,7 @@ class TestReactor : public test_util::TestBase { events.push_back("Bind3 returned " + c); } }; -} // namespace + TEST_CASE("Testing distributing arguments to multiple bind functions (NUClear Fission)", "[api][dsl][fission]") { diff --git a/tests/tests/dsl/BlockNoData.cpp b/tests/tests/dsl/BlockNoData.cpp index 3f01537b..e671c2a9 100644 --- a/tests/tests/dsl/BlockNoData.cpp +++ b/tests/tests/dsl/BlockNoData.cpp @@ -25,16 +25,11 @@ #include "test_util/TestBase.hpp" -namespace { - -/// Events that occur during the test -std::vector events; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables) - -struct MessageA {}; -struct MessageB {}; - class TestReactor : public test_util::TestBase { public: + struct MessageA {}; + struct MessageB {}; + TestReactor(std::unique_ptr environment) : TestBase(std::move(environment)) { on>().then([this] { @@ -43,11 +38,11 @@ class TestReactor : public test_util::TestBase { emit(std::make_unique()); }); - on, With>().then([] { // + on, With>().then([this] { // events.push_back("MessageA with MessageB triggered"); }); - on, With>().then([] { // + on, With>().then([this] { // events.push_back("MessageB with MessageA triggered"); }); @@ -61,8 +56,10 @@ class TestReactor : public test_util::TestBase { emit(std::make_unique>()); }); } + + /// Events that occur during the test + std::vector events; }; -} // namespace TEST_CASE("Testing that when an on statement does not have it's data satisfied it does not run", "[api][nodata]") { @@ -70,7 +67,7 @@ TEST_CASE("Testing that when an on statement does not have it's data satisfied i NUClear::Configuration config; config.thread_count = 1; NUClear::PowerPlant plant(config); - plant.install(); + const auto& reactor = plant.install(); plant.start(); const std::vector expected = { @@ -81,8 +78,8 @@ TEST_CASE("Testing that when an on statement does not have it's data satisfied i }; // Make an info print the diff in an easy to read way if we fail - INFO(test_util::diff_string(expected, events)); + INFO(test_util::diff_string(expected, reactor.events)); // Check the events fired in order and only those events - REQUIRE(events == expected); + REQUIRE(reactor.events == expected); } diff --git a/tests/tests/dsl/Buffer.cpp b/tests/tests/dsl/Buffer.cpp index 8e323c85..20c6630f 100644 --- a/tests/tests/dsl/Buffer.cpp +++ b/tests/tests/dsl/Buffer.cpp @@ -25,33 +25,28 @@ #include "test_util/TestBase.hpp" -namespace { - -/// Events that occur during the test -std::vector events; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables) - -struct Message { - Message(int i) : i(i) {} - int i; -}; - class TestReactor : public test_util::TestBase { public: + struct Message { + Message(int i) : i(i) {} + int i; + }; + TestReactor(std::unique_ptr environment) : TestBase(std::move(environment)) { - on>().then([](const Message& msg) { // + on>().then([this](const Message& msg) { // events.push_back("Trigger reaction " + std::to_string(msg.i)); }); - on, Single>().then([](const Message& msg) { // + on, Single>().then([this](const Message& msg) { // events.push_back("Single reaction " + std::to_string(msg.i)); }); - on, Buffer<2>>().then([](const Message& msg) { // + on, Buffer<2>>().then([this](const Message& msg) { // events.push_back("Buffer<2> reaction " + std::to_string(msg.i)); }); - on, Buffer<3>>().then([](const Message& msg) { // + on, Buffer<3>>().then([this](const Message& msg) { // events.push_back("Buffer<3> reaction " + std::to_string(msg.i)); }); - on, Buffer<4>>().then([](const Message& msg) { // + on, Buffer<4>>().then([this](const Message& msg) { // events.push_back("Buffer<4> reaction " + std::to_string(msg.i)); }); @@ -94,15 +89,18 @@ class TestReactor : public test_util::TestBase { emit(std::make_unique>()); }); } + + /// Events that occur during the test + std::vector events; }; -} // namespace + TEST_CASE("Test that Buffer and Single limit the number of concurrent executions", "[api][precondition][single]") { NUClear::Configuration config; config.thread_count = 1; NUClear::PowerPlant plant(config); - plant.install(); + const auto& reactor = plant.install(); plant.start(); const std::vector expected = { @@ -169,8 +167,8 @@ TEST_CASE("Test that Buffer and Single limit the number of concurrent executions }; // Make an info print the diff in an easy to read way if we fail - INFO(test_util::diff_string(expected, events)); + INFO(test_util::diff_string(expected, reactor.events)); // Check the events fired in order and only those events - REQUIRE(events == expected); + REQUIRE(reactor.events == expected); } diff --git a/tests/tests/dsl/CommandLineArguments.cpp b/tests/tests/dsl/CommandLineArguments.cpp index 80ecb51e..139b0ad0 100644 --- a/tests/tests/dsl/CommandLineArguments.cpp +++ b/tests/tests/dsl/CommandLineArguments.cpp @@ -26,11 +26,6 @@ #include "test_util/TestBase.hpp" -namespace { - -// Events that occur during the test -std::vector events; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables) - class TestReactor : public test_util::TestBase { private: using CommandLineArguments = NUClear::message::CommandLineArguments; @@ -38,7 +33,7 @@ class TestReactor : public test_util::TestBase { public: TestReactor(std::unique_ptr environment) : TestBase(std::move(environment)) { - on>().then([](const CommandLineArguments& args) { + on>().then([this](const CommandLineArguments& args) { std::stringstream output; for (const auto& arg : args) { output << arg << " "; @@ -46,8 +41,11 @@ class TestReactor : public test_util::TestBase { events.push_back("CommandLineArguments: " + output.str()); }); } + + /// Events that occur during the test + std::vector events; }; -} // namespace + TEST_CASE("Testing the Command Line argument capturing", "[api][command_line_arguments]") { const int argc = 2; @@ -55,14 +53,14 @@ TEST_CASE("Testing the Command Line argument capturing", "[api][command_line_arg NUClear::Configuration config; config.thread_count = 1; NUClear::PowerPlant plant(config, argc, argv); - plant.install(); + const auto& reactor = plant.install(); plant.start(); const std::vector expected = {"CommandLineArguments: Hello World "}; // Make an info print the diff in an easy to read way if we fail - INFO(test_util::diff_string(expected, events)); + INFO(test_util::diff_string(expected, reactor.events)); // Check the events fired in order and only those events - REQUIRE(events == expected); + REQUIRE(reactor.events == expected); } diff --git a/tests/tests/dsl/CustomGet.cpp b/tests/tests/dsl/CustomGet.cpp index 62e39c7f..d0e3672f 100644 --- a/tests/tests/dsl/CustomGet.cpp +++ b/tests/tests/dsl/CustomGet.cpp @@ -25,11 +25,6 @@ #include "test_util/TestBase.hpp" -namespace { - -/// Events that occur during the test -std::vector events; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables) - struct CustomGet : NUClear::dsl::operation::TypeBind { template @@ -42,7 +37,7 @@ class TestReactor : public test_util::TestBase { public: TestReactor(std::unique_ptr environment) : TestBase(std::move(environment)) { - on().then([](const std::string& x) { // + on().then([this](const std::string& x) { // events.push_back("CustomGet Triggered"); events.push_back(x); }); @@ -53,15 +48,18 @@ class TestReactor : public test_util::TestBase { emit(std::make_unique()); }); } + + /// Events that occur during the test + std::vector events; }; -} // namespace + TEST_CASE("Test a custom reactor that returns a type that needs dereferencing", "[api][custom_get]") { NUClear::Configuration config; config.thread_count = 1; NUClear::PowerPlant plant(config); - plant.install(); + const auto& reactor = plant.install(); plant.start(); const std::vector expected = { @@ -71,8 +69,8 @@ TEST_CASE("Test a custom reactor that returns a type that needs dereferencing", }; // Make an info print the diff in an easy to read way if we fail - INFO(test_util::diff_string(expected, events)); + INFO(test_util::diff_string(expected, reactor.events)); // Check the events fired in order and only those events - REQUIRE(events == expected); + REQUIRE(reactor.events == expected); } diff --git a/tests/tests/dsl/DSLOrdering.cpp b/tests/tests/dsl/DSLOrdering.cpp index e902b69f..d7cffab8 100644 --- a/tests/tests/dsl/DSLOrdering.cpp +++ b/tests/tests/dsl/DSLOrdering.cpp @@ -25,28 +25,26 @@ #include "test_util/TestBase.hpp" -namespace { - -/// Events that occur during the test -std::vector events; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables) - -template -struct Message { - Message(std::string data) : data(std::move(data)) {} - std::string data; -}; class TestReactor : public test_util::TestBase { public: + template + struct Message { + Message(std::string data) : data(std::move(data)) {} + std::string data; + }; + TestReactor(std::unique_ptr environment) : TestBase(std::move(environment)) { // Check that the lists are combined, and that the function args are in order on>, Trigger>, With>>().then( - [](const Message<1>& a, const Message<3>& c, const Message<2>& b) { + [this](const Message<1>& a, const Message<3>& c, const Message<2>& b) { events.push_back("A:" + a.data + " B:" + b.data + " C:" + c.data); }); // Make sure we can pass an empty function in here - on>, With>, With>>().then([] { events.push_back("Empty function"); }); + on>, With>, With>>().then([this] { // + events.push_back("Empty function"); + }); on>, Priority::LOW>().then([this] { events.push_back("Emitting 1"); @@ -67,15 +65,18 @@ class TestReactor : public test_util::TestBase { emit(std::make_unique>()); }); } + + /// Events that occur during the test + std::vector events; }; -} // namespace + TEST_CASE("Testing poorly ordered on arguments", "[api][dsl][order][with]") { NUClear::Configuration config; config.thread_count = 1; NUClear::PowerPlant plant(config); - plant.install(); + const auto& reactor = plant.install(); plant.start(); const std::vector expected = { @@ -86,8 +87,8 @@ TEST_CASE("Testing poorly ordered on arguments", "[api][dsl][order][with]") { }; // Make an info print the diff in an easy to read way if we fail - INFO(test_util::diff_string(expected, events)); + INFO(test_util::diff_string(expected, reactor.events)); // Check the events fired in order and only those events - REQUIRE(events == expected); + REQUIRE(reactor.events == expected); } diff --git a/tests/tests/dsl/DSLProxy.cpp b/tests/tests/dsl/DSLProxy.cpp index 6079d261..d18b928f 100644 --- a/tests/tests/dsl/DSLProxy.cpp +++ b/tests/tests/dsl/DSLProxy.cpp @@ -25,13 +25,11 @@ #include "test_util/TestBase.hpp" -namespace { struct CustomMessage1 {}; struct CustomMessage2 { CustomMessage2(int value) : value(value) {} int value; }; -} // namespace namespace NUClear { namespace dsl { @@ -45,16 +43,12 @@ namespace dsl { } // namespace dsl } // namespace NUClear -namespace { - -/// Events that occur during the test -std::vector events; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables) class TestReactor : public test_util::TestBase { public: TestReactor(std::unique_ptr environment) : TestBase(std::move(environment)) { - on().then([](const CustomMessage2& d) { + on().then([this](const CustomMessage2& d) { events.push_back("CustomMessage1 Triggered with " + std::to_string(d.value)); }); @@ -68,15 +62,18 @@ class TestReactor : public test_util::TestBase { emit(std::make_unique()); }); } + + /// Events that occur during the test + std::vector events; }; -} // namespace + TEST_CASE("Testing that the DSL proxy works as expected for binding unmodifyable types", "[api][dsl][proxy]") { NUClear::Configuration config; config.thread_count = 1; NUClear::PowerPlant plant(config); - plant.install(); + const auto& reactor = plant.install(); plant.start(); const std::vector expected = { @@ -86,8 +83,8 @@ TEST_CASE("Testing that the DSL proxy works as expected for binding unmodifyable }; // Make an info print the diff in an easy to read way if we fail - INFO(test_util::diff_string(expected, events)); + INFO(test_util::diff_string(expected, reactor.events)); // Check the events fired in order and only those events - REQUIRE(events == expected); + REQUIRE(reactor.events == expected); } diff --git a/tests/tests/dsl/Every.cpp b/tests/tests/dsl/Every.cpp index cfacd231..ab747044 100644 --- a/tests/tests/dsl/Every.cpp +++ b/tests/tests/dsl/Every.cpp @@ -26,29 +26,27 @@ #include "test_util/TestBase.hpp" -namespace { - -// Store our times -std::vector every_times; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables) -std::vector per_times; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables) -std::vector dynamic_times; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables) - class TestReactor : public test_util::TestBase { - public: TestReactor(std::unique_ptr environment) : TestBase(std::move(environment), false, std::chrono::seconds(10)) { // Trigger on 3 different types of every - on>>().then([]() { every_times.push_back(NUClear::clock::now()); }); - on>().then([]() { per_times.push_back(NUClear::clock::now()); }); - on>(std::chrono::milliseconds(1)).then([]() { dynamic_times.push_back(NUClear::clock::now()); }); + on>>().then([this]() { every_times.push_back(NUClear::clock::now()); }); + on>().then([this]() { per_times.push_back(NUClear::clock::now()); }); + on>(std::chrono::milliseconds(1)).then([this]() { dynamic_times.push_back(NUClear::clock::now()); }); // Gather data for some amount of time on>().then([this] { powerplant.shutdown(); }); } + + /// Times that the every trigger has been called + std::vector every_times; + /// Times that the every per trigger has been called + std::vector per_times; + /// Times that the dynamic every trigger has been called + std::vector dynamic_times; }; -} // namespace void test_results(const std::vector& times) { @@ -74,26 +72,27 @@ void test_results(const std::vector& times) { REQUIRE(std::abs(mean + stddev * 2) < 0.008); } + TEST_CASE("Testing the Every<> DSL word", "[api][every][per]") { NUClear::Configuration config; config.thread_count = 1; NUClear::PowerPlant plant(config); - plant.install(); + const auto& reactor = plant.install(); plant.start(); { INFO("Testing Every"); - test_results(every_times); + test_results(reactor.every_times); } { INFO("Testing Every Per"); - test_results(per_times); + test_results(reactor.per_times); } { INFO("Testing Dynamic Every every"); - test_results(dynamic_times); + test_results(reactor.dynamic_times); } } diff --git a/tests/tests/dsl/FlagMessage.cpp b/tests/tests/dsl/FlagMessage.cpp index df301d2c..b67c1125 100644 --- a/tests/tests/dsl/FlagMessage.cpp +++ b/tests/tests/dsl/FlagMessage.cpp @@ -25,20 +25,14 @@ #include "test_util/TestBase.hpp" -namespace { - -/// Events that occur during the test -std::vector events; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables) - -struct SimpleMessage {}; - -struct MessageA {}; -struct MessageB {}; - class TestReactor : public test_util::TestBase { public: - TestReactor(std::unique_ptr environment) : TestBase(std::move(environment)) { + struct SimpleMessage {}; + + struct MessageA {}; + struct MessageB {}; + TestReactor(std::unique_ptr environment) : TestBase(std::move(environment)) { on>().then([this] { events.push_back("MessageA triggered"); @@ -46,12 +40,12 @@ class TestReactor : public test_util::TestBase { emit(std::make_unique()); }); - on>().then([] { // + on>().then([this] { // events.push_back("MessageB triggered"); }); // This should never run - on, With>().then([](const MessageA&, const MessageB&) { // + on, With>().then([this](const MessageA&, const MessageB&) { // events.push_back("MessageA with MessageB triggered"); }); @@ -66,8 +60,10 @@ class TestReactor : public test_util::TestBase { emit(std::make_unique>()); }); } + + /// Events that occur during the test + std::vector events; }; -} // namespace TEST_CASE("Testing emitting types that are flag types (Have no contents)", "[api][flag]") { @@ -75,7 +71,7 @@ TEST_CASE("Testing emitting types that are flag types (Have no contents)", "[api NUClear::Configuration config; config.thread_count = 1; NUClear::PowerPlant plant(config); - plant.install(); + const auto& reactor = plant.install(); plant.start(); const std::vector expected = { @@ -88,8 +84,8 @@ TEST_CASE("Testing emitting types that are flag types (Have no contents)", "[api }; // Make an info print the diff in an easy to read way if we fail - INFO(test_util::diff_string(expected, events)); + INFO(test_util::diff_string(expected, reactor.events)); // Check the events fired in order and only those events - REQUIRE(events == expected); + REQUIRE(reactor.events == expected); } diff --git a/tests/tests/dsl/FusionInOrder.cpp b/tests/tests/dsl/FusionInOrder.cpp index 4cf28f41..e5235968 100644 --- a/tests/tests/dsl/FusionInOrder.cpp +++ b/tests/tests/dsl/FusionInOrder.cpp @@ -23,9 +23,7 @@ #include #include -namespace { - -std::vector events; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables) +std::vector events; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables,-warnings-as-errors) template struct Extension { @@ -43,8 +41,6 @@ class TestReactor : public NUClear::Reactor { } }; -} // namespace - TEST_CASE("Testing that the bind functions of extensions are executed in order", "[api][extension][bind]") { diff --git a/tests/tests/dsl/GroupPool.cpp b/tests/tests/dsl/GroupPool.cpp index 290ae804..62bde284 100644 --- a/tests/tests/dsl/GroupPool.cpp +++ b/tests/tests/dsl/GroupPool.cpp @@ -25,27 +25,15 @@ #include "test_util/TestBase.hpp" -namespace { - -/// @brief A vector of events that have happened -std::vector events; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables) - -/// @brief Add an event to the event list -void add_event(const std::string& event) { - static std::mutex events_mutex; - const std::lock_guard lock(events_mutex); - events.push_back(event); -} - -struct StartTest {}; -struct Synced {}; -template -struct PoolFinished {}; - -constexpr int POOL_COUNT = 10; - class TestReactor : public test_util::TestBase { public: + struct StartTest {}; + struct Synced {}; + template + struct PoolFinished {}; + + static constexpr int POOL_COUNT = 10; + template struct TestPool { static constexpr int thread_count = 1; @@ -89,9 +77,17 @@ class TestReactor : public test_util::TestBase { // Register all the callbacks and a final callback to gather the results register_callbacks(NUClear::util::GenerateSequence<0, POOL_COUNT>()); } -}; -} // namespace + /// Add an event to the event list + void add_event(const std::string& event) { + static std::mutex events_mutex; + const std::lock_guard lock(events_mutex); + events.push_back(event); + } + + /// A vector of events that have happened + std::vector events; +}; TEST_CASE("Test that if a pool has nothing to do because of a sync group it will recover", "[api][pool][group]") { @@ -99,21 +95,21 @@ TEST_CASE("Test that if a pool has nothing to do because of a sync group it will NUClear::Configuration config; config.thread_count = 1; NUClear::PowerPlant plant(config); - plant.install(); + const auto& reactor = plant.install(); plant.start(); std::vector expected = { "Startup", "Send Synced Message", }; - for (int i = 0; i < POOL_COUNT; ++i) { + for (int i = 0; i < TestReactor::POOL_COUNT; ++i) { expected.push_back("Pool Message"); } expected.push_back("Finished"); // Make an info print the diff in an easy to read way if we fail - INFO(test_util::diff_string(expected, events)); + INFO(test_util::diff_string(expected, reactor.events)); // Check the events fired in order and only those events - REQUIRE(events == expected); + REQUIRE(reactor.events == expected); } diff --git a/tests/tests/dsl/IO.cpp b/tests/tests/dsl/IO.cpp index 669e7252..82129c94 100644 --- a/tests/tests/dsl/IO.cpp +++ b/tests/tests/dsl/IO.cpp @@ -32,13 +32,6 @@ #include -namespace { - -/// Events that occur during the test reading -std::vector read_events; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables) -/// Events that occur during the test writing -std::vector write_events; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables) - class TestReactor : public test_util::TestBase { public: TestReactor(std::unique_ptr environment) : TestBase(std::move(environment), false) { @@ -95,15 +88,20 @@ class TestReactor : public test_util::TestBase { NUClear::util::FileDescriptor out; int char_no{0}; ReactionHandle writer; + + /// Events that occur during the test reading + std::vector read_events; + /// Events that occur during the test writing + std::vector write_events; }; -} // namespace + TEST_CASE("Testing the IO extension", "[api][io]") { NUClear::Configuration config; config.thread_count = 1; NUClear::PowerPlant plant(config); - plant.install(); + const auto& reactor = plant.install(); plant.start(); const std::vector read_expected = { @@ -116,7 +114,7 @@ TEST_CASE("Testing the IO extension", "[api][io]") { }; // Make an info print the diff in an easy to read way if we fail - INFO("Read Events\n" << test_util::diff_string(read_expected, read_events)); + INFO("Read Events\n" << test_util::diff_string(read_expected, reactor.read_events)); const std::vector write_expected{ "Wrote 1 bytes (H) to pipe", @@ -127,15 +125,16 @@ TEST_CASE("Testing the IO extension", "[api][io]") { }; // Make an info print the diff in an easy to read way if we fail - INFO("Write Events\n" << test_util::diff_string(write_expected, write_events)); + INFO("Write Events\n" << test_util::diff_string(write_expected, reactor.write_events)); // Check the events fired in order and only those events - REQUIRE(read_events == read_expected); - REQUIRE(write_events == write_expected); + REQUIRE(reactor.read_events == read_expected); + REQUIRE(reactor.write_events == write_expected); } #else + TEST_CASE("Testing the IO extension", "[api][io]") { SUCCEED("This test is not supported on Windows"); } diff --git a/tests/tests/dsl/Idle.cpp b/tests/tests/dsl/Idle.cpp index 32245aee..bdc19f89 100644 --- a/tests/tests/dsl/Idle.cpp +++ b/tests/tests/dsl/Idle.cpp @@ -24,21 +24,15 @@ #include #include "test_util/TestBase.hpp" - -namespace { - -/// A vector of events that have happened -std::vector events; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables) - -struct SimpleMessage { - SimpleMessage(int data) : data(data) {} - int data; -}; - -constexpr int time_step = 50; +#include "test_util/TimeUnit.hpp" class TestReactor : public test_util::TestBase { public: + struct SimpleMessage { + SimpleMessage(int data) : data(data) {} + int data; + }; + template struct CustomPool { static constexpr int thread_count = 2; @@ -46,7 +40,7 @@ class TestReactor : public test_util::TestBase { template void do_step(const std::string& name) { - std::this_thread::sleep_until(start_time + std::chrono::milliseconds(time_step * N)); + std::this_thread::sleep_until(start_time + test_util::TimeUnit(N)); events.push_back(name + " " + std::to_string(N)); emit(std::make_unique>()); } @@ -104,6 +98,9 @@ class TestReactor : public test_util::TestBase { }); } + /// A vector of events that have happened + std::vector events; + private: NUClear::clock::time_point start_time; NUClear::threading::ReactionHandle drh; @@ -112,15 +109,13 @@ class TestReactor : public test_util::TestBase { NUClear::threading::ReactionHandle grh; }; -} // namespace - TEST_CASE("Test that pool idle triggers when nothing is running", "[api][idle]") { NUClear::Configuration config; config.thread_count = 4; NUClear::PowerPlant plant(config); - plant.install(); + const auto& reactor = plant.install(); plant.start(); const std::vector expected = { @@ -133,8 +128,8 @@ TEST_CASE("Test that pool idle triggers when nothing is running", "[api][idle]") }; // Make an info print the diff in an easy to read way if we fail - INFO(test_util::diff_string(expected, events)); + INFO(test_util::diff_string(expected, reactor.events)); // Check the events fired in order and only those events - REQUIRE(events == expected); + REQUIRE(reactor.events == expected); } diff --git a/tests/tests/dsl/IdleCrossPool.cpp b/tests/tests/dsl/IdleCrossPool.cpp index 78437e58..c9b1060a 100644 --- a/tests/tests/dsl/IdleCrossPool.cpp +++ b/tests/tests/dsl/IdleCrossPool.cpp @@ -26,13 +26,6 @@ #include "test_util/TestBase.hpp" #include "test_util/TimeUnit.hpp" -namespace { - -/// A vector of events that have happened -std::vector events; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables) - -constexpr int time_step = 50; - class TestReactor : public test_util::TestBase { public: explicit TestReactor(std::unique_ptr environment) : TestBase(std::move(environment), false) { @@ -60,9 +53,10 @@ class TestReactor : public test_util::TestBase { std::thread::id main_thread_id; std::thread::id idle_thread_id; std::thread::id default_thread_id; -}; -} // namespace + /// A vector of events that have happened + std::vector events; +}; TEST_CASE("Test that idle can fire events for other pools but only runs once", "[api][dsl][Idle][Pool]") { @@ -86,8 +80,8 @@ TEST_CASE("Test that idle can fire events for other pools but only runs once", " }; // Make an info print the diff in an easy to read way if we fail - INFO(test_util::diff_string(expected, events)); + INFO(test_util::diff_string(expected, reactor.events)); // Check the events fired in order and only those events - REQUIRE(events == expected); + REQUIRE(reactor.events == expected); } diff --git a/tests/tests/dsl/IdleGlobal.cpp b/tests/tests/dsl/IdleGlobal.cpp index 06084e3a..b45122e8 100644 --- a/tests/tests/dsl/IdleGlobal.cpp +++ b/tests/tests/dsl/IdleGlobal.cpp @@ -26,14 +26,6 @@ #include "test_util/TestBase.hpp" #include "test_util/TimeUnit.hpp" -namespace { - -void wait_for_set(const std::atomic& flag) { - while (!flag.load(std::memory_order_acquire)) { - std::this_thread::sleep_for(test_util::TimeUnit(1)); - } -} - class TestReactor : public test_util::TestBase { public: // A pool to use for monitoring which does not interact with idleness @@ -105,6 +97,12 @@ class TestReactor : public test_util::TestBase { }); } + static void wait_for_set(const std::atomic& flag) { + while (!flag.load(std::memory_order_acquire)) { + std::this_thread::sleep_for(test_util::TimeUnit(1)); + } + } + std::atomic idles_fired{0}; std::atomic sync_obtained{false}; @@ -112,8 +110,6 @@ class TestReactor : public test_util::TestBase { std::atomic default_idle{false}; }; -} // namespace - TEST_CASE("Test that Idle won't fire when an already idle pool goes idle again", "[api][dsl][Idle][Pool]") { diff --git a/tests/tests/dsl/IdleSingle.cpp b/tests/tests/dsl/IdleSingle.cpp index 245879c3..458c640f 100644 --- a/tests/tests/dsl/IdleSingle.cpp +++ b/tests/tests/dsl/IdleSingle.cpp @@ -39,25 +39,24 @@ struct StringMaker> { } // namespace Catch -namespace { -constexpr int n_loops = 250; +class TestReactor : public test_util::TestBase { +public: + static constexpr int n_loops = 250; -struct TaskB { - explicit TaskB(int i) : i(i) {} - int i; -}; -struct TaskA { - explicit TaskA(int i) : i(i) {} - int i; -}; + struct TaskB { + explicit TaskB(int i) : i(i) {} + int i; + }; + struct TaskA { + explicit TaskA(int i) : i(i) {} + int i; + }; -struct IdlePool { - static constexpr int thread_count = 1; -}; + struct IdlePool { + static constexpr int thread_count = 1; + }; -class TestReactor : public test_util::TestBase { -public: explicit TestReactor(std::unique_ptr environment) : TestBase(std::move(environment), false) { /* @@ -104,8 +103,6 @@ class TestReactor : public test_util::TestBase { std::array, n_loops> idle_calls{}; }; -} // namespace - TEST_CASE("Test that when a global idle trigger exists it is triggered only once", "[api][dsl][Idle][Sync]") { @@ -115,6 +112,7 @@ TEST_CASE("Test that when a global idle trigger exists it is triggered only once const auto& reactor = plant.install(); plant.start(); + constexpr int n_loops = TestReactor::n_loops; std::array, n_loops> expected{}; for (int i = 0; i < n_loops; ++i) { expected[i].store(1, std::memory_order_relaxed); diff --git a/tests/tests/dsl/IdleSingleGlobal.cpp b/tests/tests/dsl/IdleSingleGlobal.cpp index db5ae131..966956e3 100644 --- a/tests/tests/dsl/IdleSingleGlobal.cpp +++ b/tests/tests/dsl/IdleSingleGlobal.cpp @@ -39,10 +39,6 @@ struct StringMaker> { } // namespace Catch -namespace { - -constexpr int n_loops = 10000; - class TestReactor : public test_util::TestBase { private: struct Loop { @@ -51,6 +47,8 @@ class TestReactor : public test_util::TestBase { }; public: + static constexpr int n_loops = 10000; + explicit TestReactor(std::unique_ptr environment) : TestBase(std::move(environment), false) { /* @@ -84,8 +82,6 @@ class TestReactor : public test_util::TestBase { std::array, n_loops> idle_calls{}; }; -} // namespace - TEST_CASE("Test that when a global idle trigger exists it is triggered only once", "[api][dsl][Idle][Sync]") { @@ -95,6 +91,7 @@ TEST_CASE("Test that when a global idle trigger exists it is triggered only once const auto& reactor = plant.install(); plant.start(); + constexpr int n_loops = TestReactor::n_loops; std::array, n_loops> expected{}; for (int i = 0; i < n_loops; ++i) { expected[i].store(1, std::memory_order_relaxed); diff --git a/tests/tests/dsl/IdleSync.cpp b/tests/tests/dsl/IdleSync.cpp index 45b00c18..e4590e4a 100644 --- a/tests/tests/dsl/IdleSync.cpp +++ b/tests/tests/dsl/IdleSync.cpp @@ -25,11 +25,6 @@ #include "test_util/TestBase.hpp" -namespace { - -/// A vector of events that have happened -std::vector events; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables) - class TestReactor : public test_util::TestBase { public: explicit TestReactor(std::unique_ptr environment) : TestBase(std::move(environment), false) { @@ -41,13 +36,13 @@ class TestReactor : public test_util::TestBase { }); // Idle testing for default thread - on>, Sync>().then([] { + on>, Sync>().then([this] { events.push_back("Default Start"); std::this_thread::sleep_for(std::chrono::milliseconds(300)); events.push_back("Default End"); }); - on>, Sync, MainThread>().then([] { events.push_back("Main Task"); }); + on>, Sync, MainThread>().then([this] { events.push_back("Main Task"); }); on>().then([this] { events.push_back("Idle Main Thread"); @@ -56,16 +51,18 @@ class TestReactor : public test_util::TestBase { on().then([this] { emit(std::make_unique>()); }); } + + /// A vector of events that have happened + std::vector events; }; -} // namespace TEST_CASE("Test that pool idle triggers when a waiting task prevents running", "[api][idle]") { NUClear::Configuration config; config.thread_count = 4; NUClear::PowerPlant plant(config); - plant.install(); + const auto& reactor = plant.install(); plant.start(); const std::vector expected = { @@ -76,8 +73,8 @@ TEST_CASE("Test that pool idle triggers when a waiting task prevents running", " }; // Make an info print the diff in an easy to read way if we fail - INFO(test_util::diff_string(expected, events)); + INFO(test_util::diff_string(expected, reactor.events)); // Check the events fired in order and only those events - REQUIRE(events == expected); + REQUIRE(reactor.events == expected); } diff --git a/tests/tests/dsl/Last.cpp b/tests/tests/dsl/Last.cpp index 2568ce47..6751bdca 100644 --- a/tests/tests/dsl/Last.cpp +++ b/tests/tests/dsl/Last.cpp @@ -25,18 +25,13 @@ #include "test_util/TestBase.hpp" -namespace { - -/// Events that occur during the test -std::vector events; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables) - -struct TestMessage { - TestMessage(int v) : value(v) {}; - int value; -}; - class TestReactor : public test_util::TestBase { public: + struct TestMessage { + TestMessage(int v) : value(v) {}; + int value; + }; + TestReactor(std::unique_ptr environment) : TestBase(std::move(environment)) { on>>().then([this](std::list> messages) { @@ -55,16 +50,18 @@ class TestReactor : public test_util::TestBase { on().then([this] { emit(std::make_unique(0)); }); } + + /// Events that occur during the test + std::vector events; }; -} // namespace TEST_CASE("Testing the last n feature", "[api][last]") { NUClear::Configuration config; config.thread_count = 1; NUClear::PowerPlant plant(config); - plant.install(); + const auto& reactor = plant.install(); plant.start(); const std::vector expected = { @@ -82,8 +79,8 @@ TEST_CASE("Testing the last n feature", "[api][last]") { }; // Make an info print the diff in an easy to read way if we fail - INFO(test_util::diff_string(expected, events)); + INFO(test_util::diff_string(expected, reactor.events)); // Check the events fired in order and only those events - REQUIRE(events == expected); + REQUIRE(reactor.events == expected); } diff --git a/tests/tests/dsl/MainThread.cpp b/tests/tests/dsl/MainThread.cpp index 4a8f3d0a..fdba7ce1 100644 --- a/tests/tests/dsl/MainThread.cpp +++ b/tests/tests/dsl/MainThread.cpp @@ -25,16 +25,11 @@ #include "test_util/TestBase.hpp" -namespace { - -/// Events that occur during the test -std::vector events; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables) - -struct MessageA {}; -struct MessageB {}; - class TestReactor : public test_util::TestBase { public: + struct MessageA {}; + struct MessageB {}; + TestReactor(std::unique_ptr environment) : TestBase(std::move(environment)) { // Run a task without MainThread to make sure it isn't on the main thread @@ -63,14 +58,17 @@ class TestReactor : public test_util::TestBase { emit(std::make_unique()); }); } + + /// Events that occur during the test + std::vector events; }; -} // namespace + TEST_CASE("Testing that the MainThread keyword runs tasks on the main thread", "[api][dsl][main_thread]") { NUClear::Configuration config; config.thread_count = 1; NUClear::PowerPlant plant(config); - plant.install(); + const auto& reactor = plant.install(); plant.start(); const std::vector expected = { @@ -81,8 +79,8 @@ TEST_CASE("Testing that the MainThread keyword runs tasks on the main thread", " }; // Make an info print the diff in an easy to read way if we fail - INFO(test_util::diff_string(expected, events)); + INFO(test_util::diff_string(expected, reactor.events)); // Check the events fired in order and only those events - REQUIRE(events == expected); + REQUIRE(reactor.events == expected); } diff --git a/tests/tests/dsl/MissingArguments.cpp b/tests/tests/dsl/MissingArguments.cpp index 3986dd84..13e64c84 100644 --- a/tests/tests/dsl/MissingArguments.cpp +++ b/tests/tests/dsl/MissingArguments.cpp @@ -25,23 +25,18 @@ #include "test_util/TestBase.hpp" -namespace { - -/// Events that occur during the test -std::vector events; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables) - -template -struct Message { - int val; - Message(int val) : val(val) {}; -}; - class TestReactor : public test_util::TestBase { public: + template + struct Message { + int val; + Message(int val) : val(val) {}; + }; + TestReactor(std::unique_ptr environment) : TestBase(std::move(environment)) { on>, With>, With>, With>>().then( - [](const Message<2>& m2, const Message<4>& m4) { + [this](const Message<2>& m2, const Message<4>& m4) { events.push_back("Message<2>: " + std::to_string(m2.val)); events.push_back("Message<4>: " + std::to_string(m4.val)); }); @@ -58,15 +53,18 @@ class TestReactor : public test_util::TestBase { emit(std::make_unique>(1 * 1)); }); } + + /// Events that occur during the test + std::vector events; }; -} // namespace + TEST_CASE("Testing that when arguments missing from the call it can still run", "[api][missing_arguments]") { NUClear::Configuration config; config.thread_count = 1; NUClear::PowerPlant plant(config); - plant.install(); + const auto& reactor = plant.install(); plant.start(); const std::vector expected = { @@ -79,8 +77,8 @@ TEST_CASE("Testing that when arguments missing from the call it can still run", }; // Make an info print the diff in an easy to read way if we fail - INFO(test_util::diff_string(expected, events)); + INFO(test_util::diff_string(expected, reactor.events)); // Check the events fired in order and only those events - REQUIRE(events == expected); + REQUIRE(reactor.events == expected); } diff --git a/tests/tests/dsl/Once.cpp b/tests/tests/dsl/Once.cpp index 4a9b460e..a81f4651 100644 --- a/tests/tests/dsl/Once.cpp +++ b/tests/tests/dsl/Once.cpp @@ -25,22 +25,17 @@ #include "test_util/TestBase.hpp" -namespace { - -// Events that occur during the test -std::vector events; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables) - -struct SimpleMessage { - SimpleMessage(int run) : run(run) {} - int run = 0; -}; - class TestReactor : public NUClear::Reactor { public: + struct SimpleMessage { + SimpleMessage(int run) : run(run) {} + int run = 0; + }; + TestReactor(std::unique_ptr environment) : Reactor(std::move(environment)) { // Make this priority high so it will always run first if it is able - on, Priority::HIGH, Once>().then([](const SimpleMessage& msg) { // + on, Priority::HIGH, Once>().then([this](const SimpleMessage& msg) { // events.push_back("Once Trigger executed " + std::to_string(msg.run)); }); @@ -61,15 +56,18 @@ class TestReactor : public NUClear::Reactor { emit(std::make_unique(0)); }); } + + /// Events that occur during the test + std::vector events; }; -} // namespace + TEST_CASE("Reactions with the Once DSL keyword only execute once", "[api][once]") { NUClear::Configuration config; config.thread_count = 1; NUClear::PowerPlant plant(config); - plant.install(); + const auto& reactor = plant.install(); plant.start(); const std::vector expected = { @@ -88,8 +86,8 @@ TEST_CASE("Reactions with the Once DSL keyword only execute once", "[api][once]" }; // Make an info print the diff in an easy to read way if we fail - INFO(test_util::diff_string(expected, events)); + INFO(test_util::diff_string(expected, reactor.events)); // Check the events fired in order and only those events - REQUIRE(events == expected); + REQUIRE(reactor.events == expected); } diff --git a/tests/tests/dsl/Optional.cpp b/tests/tests/dsl/Optional.cpp index 52839f65..0273a9b9 100644 --- a/tests/tests/dsl/Optional.cpp +++ b/tests/tests/dsl/Optional.cpp @@ -25,19 +25,14 @@ #include "test_util/TestBase.hpp" -namespace { - -/// Events that occur during the test -std::vector events; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables) - -struct MessageA {}; -struct MessageB {}; - class TestReactor : public test_util::TestBase { public: + struct MessageA {}; + struct MessageB {}; + TestReactor(std::unique_ptr environment) : TestBase(std::move(environment)) { - on, With>().then([](const MessageA&, const MessageB&) { // + on, With>().then([this](const MessageA&, const MessageB&) { // events.push_back("Executed reaction with A and B"); }); @@ -48,13 +43,13 @@ class TestReactor : public test_util::TestBase { emit(std::make_unique()); }); - on, With>().then([] { // + on, With>().then([this] { // events.push_back("Executed reaction with B and A"); }); // Double trigger test (to ensure that it can handle multiple DSL words on, Trigger>>().then( - [](const std::shared_ptr& a, const std::shared_ptr& b) { // + [this](const std::shared_ptr& a, const std::shared_ptr& b) { // events.push_back(std::string("Executed reaction with optional A and B with A") + (a ? "+" : "-") + " and B" + (b ? "+" : "-")); }); @@ -65,15 +60,18 @@ class TestReactor : public test_util::TestBase { emit(std::make_unique()); }); } + + /// Events that occur during the test + std::vector events; }; -} // namespace + TEST_CASE("Testing that optional is able to let data through even if it's invalid", "[api][optional]") { NUClear::Configuration config; config.thread_count = 1; NUClear::PowerPlant plant(config); - plant.install(); + const auto& reactor = plant.install(); plant.start(); const std::vector expected = { @@ -86,8 +84,8 @@ TEST_CASE("Testing that optional is able to let data through even if it's invali }; // Make an info print the diff in an easy to read way if we fail - INFO(test_util::diff_string(expected, events)); + INFO(test_util::diff_string(expected, reactor.events)); // Check the events fired in order and only those events - REQUIRE(events == expected); + REQUIRE(reactor.events == expected); } diff --git a/tests/tests/dsl/Priority.cpp b/tests/tests/dsl/Priority.cpp index 370f8345..fe299c6d 100644 --- a/tests/tests/dsl/Priority.cpp +++ b/tests/tests/dsl/Priority.cpp @@ -26,33 +26,28 @@ #include "test_util/TestBase.hpp" -namespace { - -/// Events that occur during the test -std::vector events; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables) - -template -struct Message {}; - class TestReactor : public test_util::TestBase { public: + template + struct Message {}; + TestReactor(std::unique_ptr environment) : TestBase(std::move(environment)) { // Declare in the order you'd expect them to fire - on>, Priority::REALTIME>().then([] { events.push_back("Realtime Message<1>"); }); - on>, Priority::HIGH>().then("High", [] { events.push_back("High Message<1>"); }); - on>>().then([] { events.push_back("Default Message<1>"); }); - on>, Priority::NORMAL>().then("Normal", [] { events.push_back("Normal Message<1>"); }); - on>, Priority::LOW>().then("Low", [] { events.push_back("Low Message<1>"); }); - on>, Priority::IDLE>().then([] { events.push_back("Idle Message<1>"); }); + on>, Priority::REALTIME>().then([this] { events.push_back("Realtime Message<1>"); }); + on>, Priority::HIGH>().then("High", [this] { events.push_back("High Message<1>"); }); + on>>().then([this] { events.push_back("Default Message<1>"); }); + on>, Priority::NORMAL>().then("Normal", [this] { events.push_back("Normal Message<1>"); }); + on>, Priority::LOW>().then("Low", [this] { events.push_back("Low Message<1>"); }); + on>, Priority::IDLE>().then([this] { events.push_back("Idle Message<1>"); }); // Declare in the opposite order to what you'd expect them to fire - on>, Priority::IDLE>().then([] { events.push_back("Idle Message<2>"); }); - on>, Priority::LOW>().then([] { events.push_back("Low Message<2>"); }); - on>, Priority::NORMAL>().then([] { events.push_back("Normal Message<2>"); }); - on>>().then([] { events.push_back("Default Message<2>"); }); - on>, Priority::HIGH>().then([] { events.push_back("High Message<2>"); }); - on>, Priority::REALTIME>().then([] { events.push_back("Realtime Message<2>"); }); + on>, Priority::IDLE>().then([this] { events.push_back("Idle Message<2>"); }); + on>, Priority::LOW>().then([this] { events.push_back("Low Message<2>"); }); + on>, Priority::NORMAL>().then([this] { events.push_back("Normal Message<2>"); }); + on>>().then([this] { events.push_back("Default Message<2>"); }); + on>, Priority::HIGH>().then([this] { events.push_back("High Message<2>"); }); + on>, Priority::REALTIME>().then([this] { events.push_back("Realtime Message<2>"); }); // Declare in a random order std::array order = {0, 1, 2, 3, 4}; @@ -60,20 +55,21 @@ class TestReactor : public test_util::TestBase { for (const auto& i : order) { switch (i) { case 0: - on>, Priority::REALTIME>().then([] { events.push_back("Realtime Message<3>"); }); + on>, Priority::REALTIME>().then( + [this] { events.push_back("Realtime Message<3>"); }); break; case 1: - on>, Priority::HIGH>().then([] { events.push_back("High Message<3>"); }); + on>, Priority::HIGH>().then([this] { events.push_back("High Message<3>"); }); break; case 2: - on>, Priority::NORMAL>().then([] { events.push_back("Normal Message<3>"); }); - on>>().then([] { events.push_back("Default Message<3>"); }); + on>, Priority::NORMAL>().then([this] { events.push_back("Normal Message<3>"); }); + on>>().then([this] { events.push_back("Default Message<3>"); }); break; case 3: - on>, Priority::LOW>().then([] { events.push_back("Low Message<3>"); }); + on>, Priority::LOW>().then([this] { events.push_back("Low Message<3>"); }); break; case 4: - on>, Priority::IDLE>().then([] { events.push_back("Idle Message<3>"); }); + on>, Priority::IDLE>().then([this] { events.push_back("Idle Message<3>"); }); break; default: throw std::invalid_argument("Should be impossible"); } @@ -85,8 +81,10 @@ class TestReactor : public test_util::TestBase { emit(std::make_unique>()); }); } + + /// Events that occur during the test + std::vector events; }; -} // namespace TEST_CASE("Tests that priority orders the tasks appropriately", "[api][priority]") { @@ -94,7 +92,7 @@ TEST_CASE("Tests that priority orders the tasks appropriately", "[api][priority] NUClear::Configuration config; config.thread_count = 1; NUClear::PowerPlant plant(config); - plant.install(); + const auto& reactor = plant.install(); plant.start(); const std::vector expected = { @@ -119,8 +117,8 @@ TEST_CASE("Tests that priority orders the tasks appropriately", "[api][priority] }; // Make an info print the diff in an easy to read way if we fail - INFO(test_util::diff_string(expected, events)); + INFO(test_util::diff_string(expected, reactor.events)); // Check the events fired in order and only those events - REQUIRE(events == expected); + REQUIRE(reactor.events == expected); } diff --git a/tests/tests/dsl/Raw.cpp b/tests/tests/dsl/Raw.cpp index 371763c1..f63cb3de 100644 --- a/tests/tests/dsl/Raw.cpp +++ b/tests/tests/dsl/Raw.cpp @@ -23,22 +23,18 @@ #include #include -namespace { - -struct TypeA { - TypeA(int x) : x(x) {} - - int x; -}; - -struct TypeB { - TypeB(int x) : x(x) {} - - int x; -}; - class TestReactor : public NUClear::Reactor { private: + struct TypeA { + TypeA(int x) : x(x) {} + int x; + }; + + struct TypeB { + TypeB(int x) : x(x) {} + int x; + }; + std::vector> stored; public: @@ -72,7 +68,7 @@ class TestReactor : public NUClear::Reactor { on().then([this] { emit(std::make_unique(0)); }); } }; -} // namespace + TEST_CASE("Testing the raw type conversions work properly", "[api][raw]") { diff --git a/tests/tests/dsl/RawFunction.cpp b/tests/tests/dsl/RawFunction.cpp index 4ab7e636..de6bf04f 100644 --- a/tests/tests/dsl/RawFunction.cpp +++ b/tests/tests/dsl/RawFunction.cpp @@ -25,8 +25,6 @@ #include "test_util/TestBase.hpp" -namespace { - /// Events that occur during the test std::vector events; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables) @@ -80,7 +78,6 @@ void raw_function_test_both_args(const Message& msg, const Data& data) { events.push_back("Raw function both args: " + msg.data + " " + data.data); } - class TestReactor : public test_util::TestBase { public: TestReactor(std::unique_ptr environment) : TestBase(std::move(environment)) { @@ -103,7 +100,7 @@ class TestReactor : public test_util::TestBase { }); } }; -} // namespace + TEST_CASE("Test reaction can take a raw function instead of just a lambda", "[api][raw_function]") { diff --git a/tests/tests/dsl/Shutdown.cpp b/tests/tests/dsl/Shutdown.cpp index 8535ecfe..9547231e 100644 --- a/tests/tests/dsl/Shutdown.cpp +++ b/tests/tests/dsl/Shutdown.cpp @@ -25,17 +25,11 @@ #include "test_util/TestBase.hpp" -// Anonymous namespace to keep everything file local -namespace { - -/// Events that occur during the test -std::vector events; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables) - class TestReactor : public test_util::TestBase { public: TestReactor(std::unique_ptr environment) : TestBase(std::move(environment), false) { - on().then([] { // + on().then([this] { // events.push_back("Shutdown task executed"); }); @@ -49,15 +43,18 @@ class TestReactor : public test_util::TestBase { emit(std::make_unique>()); }); } + + /// Events that occur during the test + std::vector events; }; -} // namespace + TEST_CASE("A test that a shutdown message is emitted when the system shuts down", "[api][shutdown]") { NUClear::Configuration config; config.thread_count = 1; NUClear::PowerPlant plant(config); - plant.install(); + const auto& reactor = plant.install(); plant.start(); const std::vector expected = { @@ -67,8 +64,8 @@ TEST_CASE("A test that a shutdown message is emitted when the system shuts down" }; // Make an info print the diff in an easy to read way if we fail - INFO(test_util::diff_string(expected, events)); + INFO(test_util::diff_string(expected, reactor.events)); // Check the events fired in order and only those events - REQUIRE(events == expected); + REQUIRE(reactor.events == expected); } diff --git a/tests/tests/dsl/Startup.cpp b/tests/tests/dsl/Startup.cpp index 1b8eed64..864c37fd 100644 --- a/tests/tests/dsl/Startup.cpp +++ b/tests/tests/dsl/Startup.cpp @@ -25,21 +25,16 @@ #include "test_util/TestBase.hpp" -namespace { - -/// Events that occur during the test -std::vector events; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables) - -struct SimpleMessage { - SimpleMessage(int data) : data(data) {} - int data{0}; -}; - class TestReactor : public test_util::TestBase { public: + struct SimpleMessage { + SimpleMessage(int data) : data(data) {} + int data{0}; + }; + TestReactor(std::unique_ptr environment) : TestBase(std::move(environment)) { - on>().then([](const SimpleMessage& message) { // + on>().then([this](const SimpleMessage& message) { // events.push_back("SimpleMessage triggered with " + std::to_string(message.data)); }); @@ -49,14 +44,17 @@ class TestReactor : public test_util::TestBase { emit(std::make_unique(10)); }); } + + /// Events that occur during the test + std::vector events; }; -} // namespace + TEST_CASE("Testing the startup event is emitted at the start of the program", "[api][startup]") { NUClear::Configuration config; config.thread_count = 1; NUClear::PowerPlant plant(config); - plant.install(); + const auto& reactor = plant.install(); plant.start(); const std::vector expected = { @@ -66,8 +64,8 @@ TEST_CASE("Testing the startup event is emitted at the start of the program", "[ }; // Make an info print the diff in an easy to read way if we fail - INFO(test_util::diff_string(expected, events)); + INFO(test_util::diff_string(expected, reactor.events)); // Check the events fired in order and only those events - REQUIRE(events == expected); + REQUIRE(reactor.events == expected); } diff --git a/tests/tests/dsl/Sync.cpp b/tests/tests/dsl/Sync.cpp index e1851b3b..0167060f 100644 --- a/tests/tests/dsl/Sync.cpp +++ b/tests/tests/dsl/Sync.cpp @@ -25,19 +25,14 @@ #include "test_util/TestBase.hpp" -namespace { - -/// Events that occur during the test -std::vector events; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables) - -template -struct Message { - Message(std::string data) : data(std::move(data)) {}; - std::string data; -}; - class TestReactor : public test_util::TestBase { public: + template + struct Message { + Message(std::string data) : data(std::move(data)) {}; + std::string data; + }; + TestReactor(std::unique_ptr environment) : TestBase(std::move(environment)) { on>, Sync>().then([this](const Message<0>& m) { @@ -95,14 +90,17 @@ class TestReactor : public test_util::TestBase { emit(std::make_unique>("From Startup")); }); } + + /// Events that occur during the test + std::vector events; }; -} // namespace + TEST_CASE("Testing that the Sync word works correctly", "[api][sync]") { NUClear::Configuration config; config.thread_count = 4; NUClear::PowerPlant plant(config); - plant.install(); + const auto& reactor = plant.install(); plant.start(); const std::vector expected = { @@ -121,8 +119,8 @@ TEST_CASE("Testing that the Sync word works correctly", "[api][sync]") { }; // Make an info print the diff in an easy to read way if we fail - INFO(test_util::diff_string(expected, events)); + INFO(test_util::diff_string(expected, reactor.events)); // Check the events fired in order and only those events - REQUIRE(events == expected); + REQUIRE(reactor.events == expected); } diff --git a/tests/tests/dsl/SyncMulti.cpp b/tests/tests/dsl/SyncMulti.cpp index f65f1e92..972609be 100644 --- a/tests/tests/dsl/SyncMulti.cpp +++ b/tests/tests/dsl/SyncMulti.cpp @@ -26,17 +26,11 @@ #include "test_util/TestBase.hpp" #include "test_util/TimeUnit.hpp" -namespace { - -/// Events that occur during the test -std::vector events; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables) - -struct A {}; -struct B {}; - - class TestReactor : public test_util::TestBase { public: + struct A {}; + struct B {}; + void do_task(const std::string& event) { auto start = test_util::round_to_test_units(std::chrono::steady_clock::now() - start_time); events.push_back(event + " started @ " + std::to_string(start.count())); @@ -61,14 +55,17 @@ class TestReactor : public test_util::TestBase { } std::chrono::steady_clock::time_point start_time; + + /// Events that occur during the test + std::vector events; }; -} // namespace + TEST_CASE("Test that sync works when one thread has multiple groups", "[api][sync][multi]") { NUClear::Configuration config; config.thread_count = 4; NUClear::PowerPlant plant(config); - plant.install(); + const auto& reactor = plant.install(); plant.start(); const std::vector expected = { @@ -81,8 +78,8 @@ TEST_CASE("Test that sync works when one thread has multiple groups", "[api][syn }; // Make an info print the diff in an easy to read way if we fail - INFO(test_util::diff_string(expected, events)); + INFO(test_util::diff_string(expected, reactor.events)); // Check the events fired in order and only those events - REQUIRE(events == expected); + REQUIRE(reactor.events == expected); } diff --git a/tests/tests/dsl/SyncOrder.cpp b/tests/tests/dsl/SyncOrder.cpp index 270ba200..89c6508b 100644 --- a/tests/tests/dsl/SyncOrder.cpp +++ b/tests/tests/dsl/SyncOrder.cpp @@ -28,28 +28,23 @@ #include "test_util/TestBase.hpp" -namespace { - -constexpr int N_EVENTS = 1000; - -template -struct Message { - int val; - Message(int val) : val(val) {}; -}; - -/// Events that occur during the test -std::vector> events; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables) - class TestReactor : public test_util::TestBase { public: + static constexpr int N_EVENTS = 1000; + + template + struct Message { + int val; + Message(int val) : val(val) {}; + }; + TestReactor(std::unique_ptr environment) : TestBase(std::move(environment)) { - on>, Sync>().then([](const Message<'A'>& m) { // + on>, Sync>().then([this](const Message<'A'>& m) { // events.emplace_back('A', m.val); }); - on>, Sync, MainThread>().then([](const Message<'B'>& m) { // + on>, Sync, MainThread>().then([this](const Message<'B'>& m) { // events.emplace_back('B', m.val); }); @@ -60,23 +55,26 @@ class TestReactor : public test_util::TestBase { } }); } + + /// Events that occur during the test + std::vector> events; }; -} // namespace + TEST_CASE("Sync events execute in order", "[api][sync][priority]") { NUClear::Configuration config; config.thread_count = 4; NUClear::PowerPlant plant(config); - plant.install(); + const auto& reactor = plant.install(); plant.start(); // Expect interleaved A and B events in order - std::vector> expected(events.size()); - for (int i = 0; i < N_EVENTS; ++i) { + std::vector> expected(reactor.events.size()); + for (int i = 0; i < TestReactor::N_EVENTS; ++i) { expected[2 * i] = {'A', i}; expected[2 * i + 1] = {'B', i}; } - REQUIRE(events == expected); + REQUIRE(reactor.events == expected); } diff --git a/tests/tests/dsl/TCP.cpp b/tests/tests/dsl/TCP.cpp index 6b2f4236..f9e10733 100644 --- a/tests/tests/dsl/TCP.cpp +++ b/tests/tests/dsl/TCP.cpp @@ -26,8 +26,6 @@ #include "test_util/TestBase.hpp" #include "test_util/has_ipv6.hpp" -namespace { - /// Events that occur during the test std::vector events; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables) @@ -200,7 +198,6 @@ class TestReactor : public test_util::TestBase { NUClear::util::FileDescriptor ephemeral_port_fd; }; -} // namespace TEST_CASE("Testing listening for TCP connections and receiving data messages", "[api][network][tcp]") { diff --git a/tests/tests/dsl/Transient.cpp b/tests/tests/dsl/Transient.cpp index 33240297..ecdb2e7b 100644 --- a/tests/tests/dsl/Transient.cpp +++ b/tests/tests/dsl/Transient.cpp @@ -25,8 +25,6 @@ #include "test_util/TestBase.hpp" -namespace { - struct Message { Message(std::string msg) : msg(std::move(msg)) {} std::string msg; @@ -41,7 +39,6 @@ struct TransientMessage { return valid; } }; -} // namespace namespace NUClear { namespace dsl { @@ -52,11 +49,6 @@ namespace dsl { } // namespace dsl } // namespace NUClear -namespace { - -/// Events that occur during the test -std::vector events; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables) - struct TransientGetter : NUClear::dsl::operation::TypeBind { template @@ -75,8 +67,9 @@ class TestReactor : public test_util::TestBase { public: TestReactor(std::unique_ptr environment) : TestBase(std::move(environment)) { - on, TransientGetter>().then( - [](const Message& m, const TransientMessage& t) { events.push_back(m.msg + " : " + t.msg); }); + on, TransientGetter>().then([this](const Message& m, const TransientMessage& t) { // + events.push_back(m.msg + " : " + t.msg); + }); on>, Priority::LOW>().then([this] { events.push_back("Emitting Message 1"); @@ -127,14 +120,17 @@ class TestReactor : public test_util::TestBase { emit(std::make_unique>()); }); } + + /// Events that occur during the test + std::vector events; }; -} // namespace + TEST_CASE("Testing whether getters that return transient data can cache between calls", "[api][transient]") { NUClear::Configuration config; config.thread_count = 1; NUClear::PowerPlant plant(config); - plant.install(); + const auto& reactor = plant.install(); plant.start(); const std::vector expected = { @@ -158,8 +154,8 @@ TEST_CASE("Testing whether getters that return transient data can cache between }; // Make an info print the diff in an easy to read way if we fail - INFO(test_util::diff_string(expected, events)); + INFO(test_util::diff_string(expected, reactor.events)); // Check the events fired in order and only those events - REQUIRE(events == expected); + REQUIRE(reactor.events == expected); } diff --git a/tests/tests/dsl/Trigger.cpp b/tests/tests/dsl/Trigger.cpp index a8ee8dc4..a435d507 100644 --- a/tests/tests/dsl/Trigger.cpp +++ b/tests/tests/dsl/Trigger.cpp @@ -25,21 +25,16 @@ #include "test_util/TestBase.hpp" -namespace { - -/// A vector of events that have happened -std::vector events; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables) - -struct SimpleMessage { - SimpleMessage(int data) : data(data) {} - int data; -}; - class TestReactor : public test_util::TestBase { public: + struct SimpleMessage { + SimpleMessage(int data) : data(data) {} + int data; + }; + TestReactor(std::unique_ptr environment) : TestBase(std::move(environment)) { - on>().then([](const SimpleMessage& message) { // + on>().then([this](const SimpleMessage& message) { // events.push_back("Trigger " + std::to_string(message.data)); }); @@ -50,8 +45,10 @@ class TestReactor : public test_util::TestBase { } }); } + + /// A vector of events that have happened + std::vector events; }; -} // namespace TEST_CASE("Test that Trigger statements get the correct data", "[api][trigger]") { @@ -59,7 +56,7 @@ TEST_CASE("Test that Trigger statements get the correct data", "[api][trigger]") NUClear::Configuration config; config.thread_count = 1; NUClear::PowerPlant plant(config); - plant.install(); + const auto& reactor = plant.install(); plant.start(); const std::vector expected = { @@ -76,8 +73,8 @@ TEST_CASE("Test that Trigger statements get the correct data", "[api][trigger]") }; // Make an info print the diff in an easy to read way if we fail - INFO(test_util::diff_string(expected, events)); + INFO(test_util::diff_string(expected, reactor.events)); // Check the events fired in order and only those events - REQUIRE(events == expected); + REQUIRE(reactor.events == expected); } diff --git a/tests/tests/dsl/UDP.cpp b/tests/tests/dsl/UDP.cpp index 23adea72..5fca06bd 100644 --- a/tests/tests/dsl/UDP.cpp +++ b/tests/tests/dsl/UDP.cpp @@ -26,8 +26,6 @@ #include "test_util/TestBase.hpp" #include "test_util/has_ipv6.hpp" -namespace { - /// Events that occur during the test std::vector events; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables) @@ -348,7 +346,6 @@ class TestReactor : public test_util::TestBase { size_t test_no = 0; }; -} // namespace TEST_CASE("Testing sending and receiving of UDP messages", "[api][network][udp]") { diff --git a/tests/tests/dsl/Watchdog.cpp b/tests/tests/dsl/Watchdog.cpp index 03ce6658..0122660f 100644 --- a/tests/tests/dsl/Watchdog.cpp +++ b/tests/tests/dsl/Watchdog.cpp @@ -27,16 +27,11 @@ #include "test_util/TestBase.hpp" -namespace { - -/// Events that occur during the test -std::vector events; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables) - -template -struct Flag {}; - class TestReactor : public test_util::TestBase { public: + template + struct Flag {}; + TestReactor(std::unique_ptr environment) : TestBase(std::move(environment), false), start(NUClear::clock::now()) { @@ -92,16 +87,18 @@ class TestReactor : public test_util::TestBase { int flag3a{0}; int flag3b{0}; int flag4{0}; + + /// Events that occur during the test + std::vector events; }; -} // namespace TEST_CASE("Testing the Watchdog Smart Type", "[api][watchdog]") { NUClear::Configuration config; config.thread_count = 1; NUClear::PowerPlant plant(config); - plant.install(); + const auto& reactor = plant.install(); plant.start(); const std::vector expected = { @@ -121,8 +118,8 @@ TEST_CASE("Testing the Watchdog Smart Type", "[api][watchdog]") { }; // Make an info print the diff in an easy to read way if we fail - INFO(test_util::diff_string(expected, events)); + INFO(test_util::diff_string(expected, reactor.events)); // Check the events fired in order and only those events - REQUIRE(events == expected); + REQUIRE(reactor.events == expected); } diff --git a/tests/tests/dsl/With.cpp b/tests/tests/dsl/With.cpp index 07fab6b7..620d08e7 100644 --- a/tests/tests/dsl/With.cpp +++ b/tests/tests/dsl/With.cpp @@ -25,25 +25,20 @@ #include "test_util/TestBase.hpp" -namespace { - -/// Events that occur during the test -std::vector events; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables) - -struct Message { - Message(std::string data) : data(std::move(data)) {} - std::string data; -}; -struct Data { - Data(std::string data) : data(std::move(data)) {} - std::string data; -}; - class TestReactor : public test_util::TestBase { public: + struct Message { + Message(std::string data) : data(std::move(data)) {} + std::string data; + }; + struct Data { + Data(std::string data) : data(std::move(data)) {} + std::string data; + }; + TestReactor(std::unique_ptr environment) : TestBase(std::move(environment)) { // Check that the lists are combined, and that the function args are in order - on, With>().then([](const Message& m, const Data& d) { // + on, With>().then([this](const Message& m, const Data& d) { // events.push_back("Message: " + m.data + " Data: " + d.data); }); @@ -80,15 +75,18 @@ class TestReactor : public test_util::TestBase { emit(std::make_unique>()); }); } + + /// Events that occur during the test + std::vector events; }; -} // namespace + TEST_CASE("Testing the with dsl keyword", "[api][with]") { NUClear::Configuration config; config.thread_count = 1; NUClear::PowerPlant plant(config); - plant.install(); + const auto& reactor = plant.install(); plant.start(); const std::vector expected = { @@ -102,8 +100,8 @@ TEST_CASE("Testing the with dsl keyword", "[api][with]") { }; // Make an info print the diff in an easy to read way if we fail - INFO(test_util::diff_string(expected, events)); + INFO(test_util::diff_string(expected, reactor.events)); // Check the events fired in order and only those events - REQUIRE(events == expected); + REQUIRE(reactor.events == expected); } diff --git a/tests/tests/dsl/emit/Delay.cpp b/tests/tests/dsl/emit/Delay.cpp index cefc17e7..535452d0 100644 --- a/tests/tests/dsl/emit/Delay.cpp +++ b/tests/tests/dsl/emit/Delay.cpp @@ -26,38 +26,31 @@ #include "test_util/TestBase.hpp" #include "test_util/TimeUnit.hpp" -// Anonymous namespace to keep everything file local -namespace { - -using TimeUnit = test_util::TimeUnit; - -/// Events that occur during the test -std::vector events; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables) - -/// Perform this many different time points for the test -constexpr int test_loops = 5; +class TestReactor : public test_util::TestBase { +public: + using TimeUnit = test_util::TimeUnit; -struct DelayedMessage { - DelayedMessage(const NUClear::clock::duration& delay) : time(NUClear::clock::now()), delay(delay) {} - NUClear::clock::time_point time; - NUClear::clock::duration delay; -}; + /// Perform this many different time points for the test + static constexpr int test_loops = 5; -struct TargetTimeMessage { - TargetTimeMessage(const NUClear::clock::time_point& target) : time(NUClear::clock::now()), target(target) {} - NUClear::clock::time_point time; - NUClear::clock::time_point target; -}; + struct DelayedMessage { + DelayedMessage(const NUClear::clock::duration& delay) : time(NUClear::clock::now()), delay(delay) {} + NUClear::clock::time_point time; + NUClear::clock::duration delay; + }; -struct FinishTest {}; + struct TargetTimeMessage { + TargetTimeMessage(const NUClear::clock::time_point& target) : time(NUClear::clock::now()), target(target) {} + NUClear::clock::time_point time; + NUClear::clock::time_point target; + }; -class TestReactor : public test_util::TestBase { -public: + struct FinishTest {}; TestReactor(std::unique_ptr environment) : TestBase(std::move(environment), false, std::chrono::seconds(2)) { // Measure when messages were sent and received and print those values - on>().then([](const DelayedMessage& m) { + on>().then([this](const DelayedMessage& m) { auto true_delta = test_util::round_to_test_units(NUClear::clock::now() - m.time); auto delta = test_util::round_to_test_units(m.delay); @@ -66,7 +59,7 @@ class TestReactor : public test_util::TestBase { + std::to_string(delta.count())); }); - on>().then([](const TargetTimeMessage& m) { + on>().then([this](const TargetTimeMessage& m) { auto true_delta = test_util::round_to_test_units(NUClear::clock::now() - m.time); auto delta = test_util::round_to_test_units(m.target - m.time); @@ -95,14 +88,17 @@ class TestReactor : public test_util::TestBase { on().then([this] { emit(std::make_unique>()); }); } + + /// Events that occur during the test + std::vector events; }; -} // namespace + TEST_CASE("Testing the delay emit", "[api][emit][delay]") { NUClear::Configuration config; config.thread_count = 1; NUClear::PowerPlant plant(config); - plant.install(); + const auto& reactor = plant.install(); plant.start(); const std::vector expected = { @@ -120,8 +116,8 @@ TEST_CASE("Testing the delay emit", "[api][emit][delay]") { }; // Make an info print the diff in an easy to read way if we fail - INFO(test_util::diff_string(expected, events)); + INFO(test_util::diff_string(expected, reactor.events)); // Check the events fired in order and only those events - REQUIRE(events == expected); + REQUIRE(reactor.events == expected); } diff --git a/tests/tests/dsl/emit/EmitFusion.cpp b/tests/tests/dsl/emit/EmitFusion.cpp index 3b7639bf..ff6b1675 100644 --- a/tests/tests/dsl/emit/EmitFusion.cpp +++ b/tests/tests/dsl/emit/EmitFusion.cpp @@ -26,11 +26,8 @@ #include "test_util/TestBase.hpp" -// Anonymous namespace to keep everything file local -namespace { - /// Events that occur during the test -std::vector events; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables) +std::vector events; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables,-warnings-as-errors) template struct E1 { @@ -81,7 +78,7 @@ class TestReactor : public test_util::TestBase { events.push_back("End test 5"); } }; -} // namespace + TEST_CASE("Testing emit function fusion", "[api][emit][fusion]") { diff --git a/tests/tests/dsl/emit/Initialise.cpp b/tests/tests/dsl/emit/Initialise.cpp index a0854c19..2516b775 100644 --- a/tests/tests/dsl/emit/Initialise.cpp +++ b/tests/tests/dsl/emit/Initialise.cpp @@ -25,24 +25,18 @@ #include "test_util/TestBase.hpp" -// Anonymous namespace to keep everything file local -namespace { - -/// Events that occur during the test -std::vector events; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables) - -struct TestMessage { - TestMessage(std::string data) : data(std::move(data)) {} - std::string data; -}; - class TestReactor : public test_util::TestBase { public: + struct TestMessage { + TestMessage(std::string data) : data(std::move(data)) {} + std::string data; + }; + TestReactor(std::unique_ptr environment) : TestBase(std::move(environment)) { emit(std::make_unique("Initialise before trigger")); emit(std::make_unique("Normal before trigger")); - on>().then([](const TestMessage& v) { // + on>().then([this](const TestMessage& v) { // events.push_back("Triggered " + v.data); }); @@ -60,14 +54,17 @@ class TestReactor : public test_util::TestBase { emit(std::make_unique>()); }); } + + /// Events that occur during the test + std::vector events; }; -} // namespace + TEST_CASE("Testing the Initialize scope", "[api][emit][initialize]") { NUClear::Configuration config; config.thread_count = 1; NUClear::PowerPlant plant(config); - plant.install(); + const auto& reactor = plant.install(); plant.start(); const std::vector expected = { @@ -78,8 +75,8 @@ TEST_CASE("Testing the Initialize scope", "[api][emit][initialize]") { }; // Make an info print the diff in an easy to read way if we fail - INFO(test_util::diff_string(expected, events)); + INFO(test_util::diff_string(expected, reactor.events)); // Check the events fired in order and only those events - REQUIRE(events == expected); + REQUIRE(reactor.events == expected); } diff --git a/tests/tests/log/Log.cpp b/tests/tests/log/Log.cpp index c0088118..a0097c69 100644 --- a/tests/tests/log/Log.cpp +++ b/tests/tests/log/Log.cpp @@ -23,9 +23,6 @@ #include #include -// Anonymous namespace to keep everything file local -namespace { - // This is a free floating function that we can use to test the log function when not in a reactor template void free_floating_log(const Args&... args) { @@ -38,7 +35,7 @@ struct LogTestOutput { bool from_reaction; }; -// Store all the log messages we received +/// All the log messages received std::vector messages; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables) // All the log levels @@ -134,7 +131,7 @@ class TestReactor : public NUClear::Reactor { }); } }; -} // namespace + TEST_CASE("Testing the Log<>() function", "[api][log]") { diff --git a/tests/tests/util/network/resolve.cpp b/tests/tests/util/network/resolve.cpp index 4f83969b..f18601ea 100644 --- a/tests/tests/util/network/resolve.cpp +++ b/tests/tests/util/network/resolve.cpp @@ -25,6 +25,7 @@ #include #include + TEST_CASE("resolve function returns expected socket address", "[util][network][resolve]") { SECTION("IPv4 address") { diff --git a/tests/tests/util/serialise/serialise.cpp b/tests/tests/util/serialise/serialise.cpp index 06357bff..50784f74 100644 --- a/tests/tests/util/serialise/serialise.cpp +++ b/tests/tests/util/serialise/serialise.cpp @@ -100,6 +100,7 @@ SCENARIO("Serialisation works correctly on single primitives", "[util][serialise } } + TEMPLATE_TEST_CASE("Scenario: Serialisation works correctly on iterables of primitives", "[util][serialise][multiple][primitive]", std::vector, @@ -177,8 +178,6 @@ TEMPLATE_TEST_CASE("Scenario: Serialisation works correctly on iterables of prim } } -namespace { - struct TriviallyCopyable { uint8_t a; int8_t b; @@ -190,8 +189,6 @@ struct TriviallyCopyable { }; static_assert(std::is_trivially_copyable::value, "This type should be trivially copyable"); -} // namespace - SCENARIO("Serialisation works correctly on single trivially copyable types", "[util][serialise][single][trivial]") { GIVEN("a trivially copyable value") {