From 3288759c6b5f16ea73c2b572e5465a31670207ef Mon Sep 17 00:00:00 2001 From: GreenWizard Date: Fri, 29 Dec 2023 22:08:46 +0100 Subject: [PATCH] fix everything? --- .../lib/WaterPump/WaterPumpScheduler.cpp | 10 ++++- .../WaterPumpController.cpp | 0 .../WaterPumpController.h | 2 +- controller/tea_poor/platformio.ini | 20 +++++++-- .../WaterPumpScheduler_test.cpp | 42 +++++++------------ 5 files changed, 41 insertions(+), 33 deletions(-) rename controller/tea_poor/lib/{WaterPump => WaterPumpController}/WaterPumpController.cpp (100%) rename controller/tea_poor/lib/{WaterPump => WaterPumpController}/WaterPumpController.h (95%) rename controller/tea_poor/test/{test_local => test_native}/WaterPumpScheduler_test.cpp (69%) diff --git a/controller/tea_poor/lib/WaterPump/WaterPumpScheduler.cpp b/controller/tea_poor/lib/WaterPump/WaterPumpScheduler.cpp index 21bb9fe..888b4bf 100644 --- a/controller/tea_poor/lib/WaterPump/WaterPumpScheduler.cpp +++ b/controller/tea_poor/lib/WaterPump/WaterPumpScheduler.cpp @@ -1,10 +1,16 @@ #include "WaterPumpScheduler.h" -WaterPumpScheduler::WaterPumpScheduler(IWaterPump* waterPump, unsigned long forceStopIntervalMs) { +WaterPumpScheduler::WaterPumpScheduler(IWaterPump* waterPump, unsigned long forceStopIntervalMs) : + _waterPump(waterPump), + _forceStopIntervalMs(forceStopIntervalMs) +{ } WaterPumpScheduler::~WaterPumpScheduler() { - delete _waterPump; + // TODO: find better way to manage memory + // for now it's not a big deal, because Arduino will never stop + // and tests are manage memory by themselves + // delete _waterPump; } void WaterPumpScheduler::setup() { diff --git a/controller/tea_poor/lib/WaterPump/WaterPumpController.cpp b/controller/tea_poor/lib/WaterPumpController/WaterPumpController.cpp similarity index 100% rename from controller/tea_poor/lib/WaterPump/WaterPumpController.cpp rename to controller/tea_poor/lib/WaterPumpController/WaterPumpController.cpp diff --git a/controller/tea_poor/lib/WaterPump/WaterPumpController.h b/controller/tea_poor/lib/WaterPumpController/WaterPumpController.h similarity index 95% rename from controller/tea_poor/lib/WaterPump/WaterPumpController.h rename to controller/tea_poor/lib/WaterPumpController/WaterPumpController.h index 511560c..2e55cac 100644 --- a/controller/tea_poor/lib/WaterPump/WaterPumpController.h +++ b/controller/tea_poor/lib/WaterPumpController/WaterPumpController.h @@ -1,6 +1,6 @@ #ifndef WATERPUMPCONTROLLER_H #define WATERPUMPCONTROLLER_H -#include "IWaterPump.h" +#include class WaterPumpController: public IWaterPump { private: diff --git a/controller/tea_poor/platformio.ini b/controller/tea_poor/platformio.ini index a8fe5b5..10b8810 100644 --- a/controller/tea_poor/platformio.ini +++ b/controller/tea_poor/platformio.ini @@ -12,9 +12,23 @@ platform = renesas-ra board = uno_r4_wifi framework = arduino -test_framework = unity lib_deps = lasselukkari/aWOT@^3.5.0 - ; throwtheswitch/Unity@^2.5.2 -test_ignore = local +test_ignore = test_native +[env:native] +platform = native +test_build_src = no +test_framework = googletest +build_src_filter = +<*> - +lib_ldf_mode = deep +check_flags = --verbose --enable=all --std=c++11 +build_flags = + -std=c++11 + -Wall -Wextra -Wunused + -static -static-libgcc -static-libstdc++ +; ignore libraries that are only for the Arduino +lib_ignore = + RemoteControl + WaterPumpController +test_ignore = test_uno_r4_wifi \ No newline at end of file diff --git a/controller/tea_poor/test/test_local/WaterPumpScheduler_test.cpp b/controller/tea_poor/test/test_native/WaterPumpScheduler_test.cpp similarity index 69% rename from controller/tea_poor/test/test_local/WaterPumpScheduler_test.cpp rename to controller/tea_poor/test/test_native/WaterPumpScheduler_test.cpp index 3417fe9..8e2cd96 100644 --- a/controller/tea_poor/test/test_local/WaterPumpScheduler_test.cpp +++ b/controller/tea_poor/test/test_native/WaterPumpScheduler_test.cpp @@ -1,11 +1,9 @@ // I wasn't able to run tests at all. Run them locally and confirm that they are working. // Its either a local problem or a problem with the configuration of the project. // Further goes a sketch of the tests, but I wasn't able to run them. -#include +#include #include - - // Fake water pump class FakeWaterPump : public IWaterPump { private: @@ -19,14 +17,8 @@ class FakeWaterPump : public IWaterPump { }; // End of fake water pump - -/* Empty functions required by unity framework*/ -void setUp() { /* Setup code here */ } -void tearDown() { /* Teardown code here */ } - - // test that pump is stopping after given time -void test_pump_stops_after_given_time() { +TEST(WaterPumpScheduler, test_pump_stops_after_given_time) { // random time between 1 and 10 seconds const unsigned long runTimeMs = 1000 + (rand() % 10) * 1000; FakeWaterPump fakeWaterPump; @@ -37,21 +29,21 @@ void test_pump_stops_after_given_time() { waterPumpScheduler.start(runTimeMs, currentTimeMs); // check status auto status = waterPumpScheduler.status(); - TEST_ASSERT_TRUE(status.isRunning); - TEST_ASSERT_EQUAL(status.stopTime, runTimeMs); + ASSERT_TRUE(status.isRunning); + ASSERT_EQ(status.stopTime, runTimeMs); while (currentTimeMs < runTimeMs) { waterPumpScheduler.tick(currentTimeMs); - TEST_ASSERT_TRUE(fakeWaterPump.isRunning()); + ASSERT_TRUE(fakeWaterPump.isRunning()); currentTimeMs += 100; } // pump should be stopped after given time waterPumpScheduler.tick(runTimeMs + 1); - TEST_ASSERT_FALSE(fakeWaterPump.isRunning()); + ASSERT_FALSE(fakeWaterPump.isRunning()); } // test that pump is periodically forced to stop after given time -void test_pump_is_periodically_forced_to_stop_after_given_time () { +TEST(WaterPumpScheduler, test_pump_is_periodically_forced_to_stop_after_given_time) { FakeWaterPump fakeWaterPump; WaterPumpScheduler waterPumpScheduler(&fakeWaterPump, 1000); // force stop each 1 second waterPumpScheduler.setup(); @@ -60,25 +52,21 @@ void test_pump_is_periodically_forced_to_stop_after_given_time () { waterPumpScheduler.start(1, currentTimeMs); currentTimeMs += 1; waterPumpScheduler.tick(currentTimeMs); - TEST_ASSERT_FALSE(fakeWaterPump.isRunning()); // pump should be stopped after given time + ASSERT_FALSE(fakeWaterPump.isRunning()); // pump should be stopped after given time for(int i = 0; i < 10; i++) { // emulate that pump was started again fakeWaterPump.start(); currentTimeMs += 1000; waterPumpScheduler.tick(currentTimeMs); - TEST_ASSERT_FALSE(fakeWaterPump.isRunning()); // pump should be stopped + ASSERT_FALSE(fakeWaterPump.isRunning()); // pump should be stopped } } -void setup() { - UNITY_BEGIN(); - RUN_TEST(test_pump_stops_after_given_time); - RUN_TEST(test_pump_is_periodically_forced_to_stop_after_given_time); - UNITY_END(); -} - - -void loop() { - +int main(int argc, char **argv) { + ::testing::InitGoogleTest(&argc, argv); + int result = RUN_ALL_TESTS(); // Intentionally ignoring the return value + (void)result; // Silence unused variable warning + // Always return zero-code and allow PlatformIO to parse results + return 0; } \ No newline at end of file