From 1ba63b971ccc4d393d8d7fa2e382aad43ab87845 Mon Sep 17 00:00:00 2001 From: Yann Locatelli Date: Mon, 18 Dec 2023 11:32:36 +0100 Subject: [PATCH] Improve test coverage init BehaviorKit for timeout --- include/interface/libs/BehaviorKit.h | 1 + libs/BehaviorKit/include/BehaviorKit.h | 7 ++++++- libs/BehaviorKit/source/BehaviorKit.cpp | 3 +-- libs/BehaviorKit/tests/BehaviorKit_test.cpp | 22 +++++++++++++++++++++ spikes/lk_auto_charge/main.cpp | 1 + 5 files changed, 31 insertions(+), 3 deletions(-) diff --git a/include/interface/libs/BehaviorKit.h b/include/interface/libs/BehaviorKit.h index e5181647f3..c44c4c9d98 100644 --- a/include/interface/libs/BehaviorKit.h +++ b/include/interface/libs/BehaviorKit.h @@ -17,6 +17,7 @@ class BehaviorKit public: virtual ~BehaviorKit() = default; + virtual void init() = 0; virtual void registerBehaviors(std::span behaviors) = 0; virtual void setTimeoutDuration(std::chrono::seconds duration) = 0; virtual void start(interface::Behavior *behavior) = 0; diff --git a/libs/BehaviorKit/include/BehaviorKit.h b/libs/BehaviorKit/include/BehaviorKit.h index a89807735b..92346f7b19 100644 --- a/libs/BehaviorKit/include/BehaviorKit.h +++ b/libs/BehaviorKit/include/BehaviorKit.h @@ -17,8 +17,13 @@ namespace leka { class BehaviorKit : public interface::BehaviorKit { public: - explicit BehaviorKit(interface::EventLoop &event_loop, interface::Timeout &timeout); + explicit BehaviorKit(interface::EventLoop &event_loop, interface::Timeout &timeout) + : _event_loop(event_loop), _timeout(timeout) + { + // nothing to do + } + void init() final; void registerBehaviors(std::span behaviors) final; void setTimeoutDuration(std::chrono::seconds duration) final; diff --git a/libs/BehaviorKit/source/BehaviorKit.cpp b/libs/BehaviorKit/source/BehaviorKit.cpp index 2dc84ffeff..6dfc74ca1f 100644 --- a/libs/BehaviorKit/source/BehaviorKit.cpp +++ b/libs/BehaviorKit/source/BehaviorKit.cpp @@ -7,8 +7,7 @@ using namespace leka; -BehaviorKit::BehaviorKit(interface::EventLoop &event_loop, interface::Timeout &timeout) - : _event_loop(event_loop), _timeout(timeout) +void BehaviorKit::init() { _event_loop.registerCallback([this] { run(); }); _timeout.onTimeout([this] { stop(); }); diff --git a/libs/BehaviorKit/tests/BehaviorKit_test.cpp b/libs/BehaviorKit/tests/BehaviorKit_test.cpp index 7b6b16f1ab..690a1a3d86 100644 --- a/libs/BehaviorKit/tests/BehaviorKit_test.cpp +++ b/libs/BehaviorKit/tests/BehaviorKit_test.cpp @@ -35,8 +35,18 @@ TEST_F(BehaviorKitTest, initialization) ASSERT_NE(&behaviorkit, nullptr); } +TEST_F(BehaviorKitTest, initialize) +{ + EXPECT_CALL(mock_timeout, onTimeout); + + behaviorkit.init(); +} + TEST_F(BehaviorKitTest, startFirstBehavior) { + EXPECT_CALL(mock_timeout, onTimeout); + behaviorkit.init(); + auto behaviors = std::to_array({&mock_behavior_a}); behaviorkit.registerBehaviors(behaviors); @@ -49,6 +59,9 @@ TEST_F(BehaviorKitTest, startFirstBehavior) TEST_F(BehaviorKitTest, startBehaviorNullPtr) { + EXPECT_CALL(mock_timeout, onTimeout); + behaviorkit.init(); + EXPECT_CALL(mock_timeout, stop); EXPECT_CALL(mock_timeout, start).Times(0); @@ -57,6 +70,9 @@ TEST_F(BehaviorKitTest, startBehaviorNullPtr) TEST_F(BehaviorKitTest, startFirstBehaviorID) { + EXPECT_CALL(mock_timeout, onTimeout); + behaviorkit.init(); + auto behaviors = std::to_array({ &mock_behavior_a, &mock_behavior_b, @@ -77,6 +93,9 @@ TEST_F(BehaviorKitTest, startFirstBehaviorID) TEST_F(BehaviorKitTest, startBehaviorIDNotRegistered) { + EXPECT_CALL(mock_timeout, onTimeout); + behaviorkit.init(); + auto behaviors = std::to_array({ &mock_behavior_a, &mock_behavior_b, @@ -96,6 +115,9 @@ TEST_F(BehaviorKitTest, startBehaviorIDNotRegistered) TEST_F(BehaviorKitTest, startAnyBehavior) { + EXPECT_CALL(mock_timeout, onTimeout); + behaviorkit.init(); + auto behaviors = std::to_array({ &mock_behavior_a, &mock_behavior_b, diff --git a/spikes/lk_auto_charge/main.cpp b/spikes/lk_auto_charge/main.cpp index 0ab60d4cc8..a9a6eb42db 100644 --- a/spikes/lk_auto_charge/main.cpp +++ b/spikes/lk_auto_charge/main.cpp @@ -123,6 +123,7 @@ auto main() -> int imukit.init(); imukit.start(); + behavior_kit.init(); behavior_kit.registerBehaviors(behaviors); battery::cells.onChargeDidStart([] { behavior_kit.stop(); });