Skip to content

Commit

Permalink
✅ (BehaviorKit): Add init to cover all unexpected calls
Browse files Browse the repository at this point in the history
  • Loading branch information
YannLocatelli committed Dec 19, 2023
1 parent 9a29261 commit edd60e0
Show file tree
Hide file tree
Showing 4 changed files with 37 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
29 changes: 29 additions & 0 deletions libs/BehaviorKit/tests/BehaviorKit_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "stubs/leka/EventLoopKit.h"

using namespace leka;
using namespace std::chrono_literals;

using ::testing::Return;

Expand All @@ -35,8 +36,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 +60,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 +71,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 +94,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 +116,9 @@ TEST_F(BehaviorKitTest, startBehaviorIDNotRegistered)

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

auto behaviors = std::to_array<interface::Behavior *>({
&mock_behavior_a,
});
Expand All @@ -110,6 +133,9 @@ TEST_F(BehaviorKitTest, startBehaviorNotRegistered)

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 All @@ -132,6 +158,9 @@ TEST_F(BehaviorKitTest, startAnyBehavior)

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

auto behaviors = std::to_array<interface::Behavior *>({&mock_behavior_a});
behaviorkit.registerBehaviors(behaviors);

Expand Down

0 comments on commit edd60e0

Please sign in to comment.