Skip to content

Commit

Permalink
Improve test coverage init BehaviorKit for timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
YannLocatelli committed Dec 18, 2023
1 parent 5558904 commit 1ba63b9
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 3 deletions.
1 change: 1 addition & 0 deletions include/interface/libs/BehaviorKit.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class BehaviorKit
public:
virtual ~BehaviorKit() = default;

virtual void init() = 0;
virtual void registerBehaviors(std::span<interface::Behavior *> behaviors) = 0;
virtual void setTimeoutDuration(std::chrono::seconds duration) = 0;
virtual void start(interface::Behavior *behavior) = 0;
Expand Down
7 changes: 6 additions & 1 deletion libs/BehaviorKit/include/BehaviorKit.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<interface::Behavior *> behaviors) final;
void setTimeoutDuration(std::chrono::seconds duration) final;

Expand Down
3 changes: 1 addition & 2 deletions libs/BehaviorKit/source/BehaviorKit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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(); });
Expand Down
22 changes: 22 additions & 0 deletions libs/BehaviorKit/tests/BehaviorKit_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<interface::Behavior *>({&mock_behavior_a});
behaviorkit.registerBehaviors(behaviors);

Expand All @@ -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);

Expand All @@ -57,6 +70,9 @@ TEST_F(BehaviorKitTest, startBehaviorNullPtr)

TEST_F(BehaviorKitTest, startFirstBehaviorID)
{
EXPECT_CALL(mock_timeout, onTimeout);
behaviorkit.init();

auto behaviors = std::to_array<interface::Behavior *>({
&mock_behavior_a,
&mock_behavior_b,
Expand All @@ -77,6 +93,9 @@ TEST_F(BehaviorKitTest, startFirstBehaviorID)

TEST_F(BehaviorKitTest, startBehaviorIDNotRegistered)
{
EXPECT_CALL(mock_timeout, onTimeout);
behaviorkit.init();

auto behaviors = std::to_array<interface::Behavior *>({
&mock_behavior_a,
&mock_behavior_b,
Expand All @@ -96,6 +115,9 @@ TEST_F(BehaviorKitTest, startBehaviorIDNotRegistered)

TEST_F(BehaviorKitTest, startAnyBehavior)
{
EXPECT_CALL(mock_timeout, onTimeout);
behaviorkit.init();

auto behaviors = std::to_array<interface::Behavior *>({
&mock_behavior_a,
&mock_behavior_b,
Expand Down
1 change: 1 addition & 0 deletions spikes/lk_auto_charge/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ auto main() -> int
imukit.init();
imukit.start();

behavior_kit.init();
behavior_kit.registerBehaviors(behaviors);

battery::cells.onChargeDidStart([] { behavior_kit.stop(); });
Expand Down

0 comments on commit 1ba63b9

Please sign in to comment.