Skip to content

Commit

Permalink
🎨 (activityKit): Turn MagicCard hashable
Browse files Browse the repository at this point in the history
  • Loading branch information
HPezz committed Sep 1, 2022
1 parent 20c0ca6 commit 8fe7ba0
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 21 deletions.
4 changes: 2 additions & 2 deletions libs/ActivityKit/include/ActivityKit.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ class ActivityKit
public:
explicit ActivityKit() = default;

void registerActivities(std::unordered_map<uint16_t, interface::Activity *> const &activities);
void registerActivities(std::unordered_map<MagicCard, interface::Activity *> const &activities);

void start(const MagicCard &card);
void stop();

private:
interface::Activity *_current_activity = nullptr;
std::unordered_map<uint16_t, interface::Activity *> _activities {};
std::unordered_map<MagicCard, interface::Activity *> _activities {};
};

} // namespace leka
12 changes: 7 additions & 5 deletions libs/ActivityKit/source/ActivityKit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

using namespace leka;

void ActivityKit::registerActivities(std::unordered_map<uint16_t, interface::Activity *> const &activities)
void ActivityKit::registerActivities(std::unordered_map<MagicCard, interface::Activity *> const &activities)
{
_activities = activities;
}
Expand All @@ -15,19 +15,21 @@ void ActivityKit::start(const MagicCard &card)
{
stop();

if (_activities.find(card.getId()) == _activities.end()) {
if (!_activities.contains(card)) {
_current_activity = nullptr;
return;
}

_current_activity = _activities.at(card.getId());
_current_activity = _activities.at(card);

_current_activity->start();
}

void ActivityKit::stop()
{
if (_current_activity != nullptr) {
_current_activity->stop();
if (_current_activity == nullptr) {
return;
}

_current_activity->stop();
}
28 changes: 14 additions & 14 deletions libs/ActivityKit/tests/ActivityKit_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,45 +21,45 @@ class ActivityKitTest : public ::testing::Test

ActivityKit activitykit;

mock::Activity mock_activity {};
mock::Activity mock_new_activity {};
mock::Activity mock_activity_0 {};
mock::Activity mock_activity_1 {};

std::unordered_map<uint16_t, interface::Activity *> activity_list = {
{MagicCard::number_0.getId(), &mock_activity},
{MagicCard::number_1.getId(), &mock_new_activity},
std::unordered_map<MagicCard, interface::Activity *> activity_list = {
{MagicCard::number_0, &mock_activity_0},
{MagicCard::number_1, &mock_activity_1},
};
};

TEST_F(ActivityKitTest, initialization)
{
EXPECT_NE(&mock_activity, nullptr);
EXPECT_NE(&mock_activity_0, nullptr);
}

TEST_F(ActivityKitTest, startActivity)
{
EXPECT_CALL(mock_activity, start).Times(1);
EXPECT_CALL(mock_activity_0, start).Times(1);

activitykit.start(MagicCard::number_0);
}

TEST_F(ActivityKitTest, startNullPtr)
{
EXPECT_CALL(mock_activity, start).Times(0);
EXPECT_CALL(mock_activity_0, start).Times(0);

activitykit.start(MagicCard::none);
}

TEST_F(ActivityKitTest, stopWithoutActivity)
{
EXPECT_CALL(mock_activity, stop).Times(0);
EXPECT_CALL(mock_activity_0, stop).Times(0);

activitykit.stop();
}

TEST_F(ActivityKitTest, stopStartedActivity)
{
EXPECT_CALL(mock_activity, start).Times(1);
EXPECT_CALL(mock_activity, stop).Times(1);
EXPECT_CALL(mock_activity_0, start).Times(1);
EXPECT_CALL(mock_activity_0, stop).Times(1);

activitykit.start(MagicCard::number_0);
activitykit.stop();
Expand All @@ -70,9 +70,9 @@ TEST_F(ActivityKitTest, startNewActivitySequence)
{
InSequence seq;

EXPECT_CALL(mock_activity, start).Times(1);
EXPECT_CALL(mock_activity, stop).Times(1);
EXPECT_CALL(mock_new_activity, start).Times(1);
EXPECT_CALL(mock_activity_0, start).Times(1);
EXPECT_CALL(mock_activity_0, stop).Times(1);
EXPECT_CALL(mock_activity_1, start).Times(1);
}

activitykit.start(MagicCard::number_0);
Expand Down
8 changes: 8 additions & 0 deletions libs/RFIDKit/include/MagicCard.h
Original file line number Diff line number Diff line change
Expand Up @@ -190,3 +190,11 @@ constexpr MagicCard MagicCard::math_arithmetic_substraction_sign_minus = MagicCa
constexpr MagicCard MagicCard::math_arithmetic_addition_sign_plus = MagicCard {0x00'3C};

} // namespace leka

// ? Make MagicCard std::hash compatible for use in associative containers (i.e. std::unordered_map)
namespace std {
template <>
struct hash<leka::MagicCard> {
auto operator()(const leka::MagicCard &card) const -> size_t { return hash<int>()(card.getId()); }
};
} // namespace std

0 comments on commit 8fe7ba0

Please sign in to comment.