Skip to content

Yann/feature/behaviorkit/create dependency injection structure #1343

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: yann/rename/behaviorkit-into-behaviorkitdeprecated
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions include/interface/libs/BehaviorKit.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Leka - LekaOS
// Copyright 2023 APF France handicap
// SPDX-License-Identifier: Apache-2.0

#pragma once

#include <span>

#include "interface/Behavior.h"
#include "internal/BehaviorID.h"

namespace leka::interface {

class BehaviorKit
{
public:
virtual ~BehaviorKit() = default;

virtual void registerBehaviors(std::span<interface::Behavior *> behaviors) = 0;
virtual void start(interface::Behavior *behavior) = 0;
virtual void start(BehaviorID id) = 0;
virtual void stop() = 0;
};

} // namespace leka::interface
26 changes: 26 additions & 0 deletions libs/BehaviorKit/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Leka - LekaOS
# Copyright 2023 APF France handicap
# SPDX-License-Identifier: Apache-2.0

add_library(BehaviorKit STATIC)

target_include_directories(BehaviorKit
PUBLIC
include
)

target_sources(BehaviorKit
PRIVATE
source/BehaviorKit.cpp
)

target_link_libraries(BehaviorKit
EventLoopKit
)

if(${CMAKE_PROJECT_NAME} STREQUAL "LekaOSUnitTests")
leka_unit_tests_sources(
tests/BehaviorID_test.cpp
tests/BehaviorKit_test.cpp
)
endif()
31 changes: 31 additions & 0 deletions libs/BehaviorKit/include/BehaviorKit.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Leka - LekaOS
// Copyright 2023 APF France handicap
// SPDX-License-Identifier: Apache-2.0

#pragma once

#include <span>

#include "interface/Behavior.h"
#include "interface/libs/BehaviorKit.h"
#include "internal/BehaviorID.h"

namespace leka {

class BehaviorKit : public interface::BehaviorKit
{
public:
explicit BehaviorKit() = default;

void registerBehaviors(std::span<interface::Behavior *> behaviors) final;

void start(interface::Behavior *behavior) final;
void start(BehaviorID id) final;
void stop() final;

private:
std::span<interface::Behavior *> _behaviors {};
interface::Behavior *_behavior = nullptr;
};

} // namespace leka
23 changes: 23 additions & 0 deletions libs/BehaviorKit/include/interface/Behavior.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Leka - LekaOS
// Copyright 2023 APF France handicap
// SPDX-License-Identifier: Apache-2.0

#pragma once

#include <cstddef>
#include <cstdint>

#include "../internal/BehaviorID.h"

namespace leka::interface {

struct Behavior {
virtual ~Behavior() = default;

virtual auto id() -> BehaviorID = 0;

virtual void run() = 0;
virtual void stop() = 0;
};

} // namespace leka::interface
15 changes: 15 additions & 0 deletions libs/BehaviorKit/include/internal/BehaviorID.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Leka - LekaOS
// Copyright 2023 APF France handicap
// SPDX-License-Identifier: Apache-2.0

#pragma once

#include <cstdint>

using BehaviorID = uint16_t;

namespace leka::behavior_id {

inline constexpr BehaviorID none = 0x0000;

} // namespace leka::behavior_id
52 changes: 52 additions & 0 deletions libs/BehaviorKit/source/BehaviorKit.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// Leka - LekaOS
// Copyright 2023 APF France handicap
// SPDX-License-Identifier: Apache-2.0

#include "BehaviorKit.h"
#include <algorithm>

using namespace leka;

void BehaviorKit::registerBehaviors(std::span<interface::Behavior *> behaviors)
{
_behaviors = behaviors;
}

void BehaviorKit::start(interface::Behavior *behavior)
{
stop();

_behavior = nullptr;
for (auto *b: _behaviors) {
if (b == behavior) {
_behavior = b;
}
}

if (_behavior == nullptr) {
return;
}

_behavior->run();
}

void BehaviorKit::start(BehaviorID id)
{
interface::Behavior *found_behavior = nullptr;

for (auto *behavior: _behaviors) {
if (id == behavior->id()) {
found_behavior = behavior;
break;
}
}

start(found_behavior);
}

void BehaviorKit::stop()
{
if (_behavior != nullptr) {
_behavior->stop();
}
}
29 changes: 29 additions & 0 deletions libs/BehaviorKit/tests/BehaviorID_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Leka - LekaOS
// Copyright 2023 APF France handicap
// SPDX-License-Identifier: Apache-2.0

#include <array>

#include "gtest/gtest.h"
#include "interface/Behavior.h"

using namespace leka;

TEST(BehaviorIDTest, behaviorsHaveUniqueID)
{
// auto all_behaviors = std::to_array<interface::Behavior *>({});
std::array<interface::Behavior *, 0> all_behaviors =
{}; // TODO: to remove, only for empty list, use above instead

auto comparator = [](interface::Behavior *a, interface::Behavior *b) { return a->id() < b->id(); };
std::sort(all_behaviors.begin(), all_behaviors.end(), comparator);

auto array_size = std::size(all_behaviors);
if (array_size <= 1) {
return;
}

for (auto i = 1; i < array_size; i++) {
ASSERT_NE(all_behaviors.at(i - 1)->id(), all_behaviors.at(i)->id());
}
}
114 changes: 114 additions & 0 deletions libs/BehaviorKit/tests/BehaviorKit_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
// Leka - LekaOS
// Copyright 2023 APF France handicap
// SPDX-License-Identifier: Apache-2.0

#include "BehaviorKit.h"

#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include "mocks/leka/Behavior.h"

using namespace leka;

using ::testing::Return;

class BehaviorKitTest : public ::testing::Test
{
protected:
BehaviorKitTest() = default;

// void SetUp() override {}
// void TearDown() override {}

BehaviorKit behaviorkit {};

mock::Behavior mock_behavior_a {};
mock::Behavior mock_behavior_b {};
};

TEST_F(BehaviorKitTest, initialization)
{
ASSERT_NE(&behaviorkit, nullptr);
}

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

EXPECT_CALL(mock_behavior_a, run);

behaviorkit.start(&mock_behavior_a);
}

TEST_F(BehaviorKitTest, startBehaviorNullPtr)
{
// nothing expected

behaviorkit.start(nullptr);
}

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

auto behavior_a_id = BehaviorID {1};
auto behavior_b_id = BehaviorID {2};

EXPECT_CALL(mock_behavior_a, id).WillRepeatedly(Return(behavior_a_id));
EXPECT_CALL(mock_behavior_b, id).WillRepeatedly(Return(behavior_b_id));
EXPECT_CALL(mock_behavior_b, run);

behaviorkit.start(behavior_b_id);
}

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

auto behavior_a_id = BehaviorID {1};
auto behavior_b_id = BehaviorID {2};

EXPECT_CALL(mock_behavior_a, id).WillRepeatedly(Return(behavior_a_id));
EXPECT_CALL(mock_behavior_b, id).WillRepeatedly(Return(behavior_b_id));

behaviorkit.start(BehaviorID {3});
}

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

EXPECT_CALL(mock_behavior_b, run).Times(0);

behaviorkit.start(&mock_behavior_b);
}

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

EXPECT_CALL(mock_behavior_a, run);

behaviorkit.start(&mock_behavior_a);

EXPECT_CALL(mock_behavior_a, stop);
EXPECT_CALL(mock_behavior_b, run);

behaviorkit.start(&mock_behavior_b);
}
1 change: 1 addition & 0 deletions libs/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

add_subdirectory(${LIBS_DIR}/ActivityKit)
add_subdirectory(${LIBS_DIR}/BatteryKit)
add_subdirectory(${LIBS_DIR}/BehaviorKit)
add_subdirectory(${LIBS_DIR}/BehaviorKitDeprecated)
add_subdirectory(${LIBS_DIR}/BLEKit)
add_subdirectory(${LIBS_DIR}/ColorKit)
Expand Down
1 change: 1 addition & 0 deletions spikes/lk_behavior_kit/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ target_sources(spike_lk_behavior_kit
)

target_link_libraries(spike_lk_behavior_kit
BehaviorKit
BehaviorKitDeprecated
CorePwm
EventLoopKit
Expand Down
1 change: 1 addition & 0 deletions tests/unit/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,7 @@ leka_register_unit_tests_for_driver(CoreVideo)
# Register libraries
leka_register_unit_tests_for_library(ActivityKit)
leka_register_unit_tests_for_library(BatteryKit)
leka_register_unit_tests_for_library(BehaviorKit)
leka_register_unit_tests_for_library(BehaviorKitDeprecated)
leka_register_unit_tests_for_library(BLEKit)
leka_register_unit_tests_for_library(ColorKit)
Expand Down
21 changes: 21 additions & 0 deletions tests/unit/mocks/mocks/leka/Behavior.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Leka - LekaOS
// Copyright 2023 APF France handicap
// SPDX-License-Identifier: Apache-2.0

#pragma once

#include "gmock/gmock.h"
#include "interface/Behavior.h"

namespace leka::mock {

class Behavior : public interface::Behavior
{
public:
MOCK_METHOD(BehaviorID, id, (), (override));

MOCK_METHOD(void, run, (), (override));
MOCK_METHOD(void, stop, (), (override));
};

} // namespace leka::mock