Skip to content

Commit

Permalink
🎨 (ActivityKit): Use unordered_map
Browse files Browse the repository at this point in the history
  • Loading branch information
HPezz committed Sep 1, 2022
1 parent b9653c1 commit c820b63
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 18 deletions.
8 changes: 6 additions & 2 deletions libs/ActivityKit/include/ActivityKit.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,24 @@

#pragma once

#include "Activity.h"
#include <unordered_map>

#include "Activity.h"
#include "MagicCard.h"
namespace leka {

class ActivityKit
{
public:
explicit ActivityKit() = default;

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

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

} // namespace leka
13 changes: 9 additions & 4 deletions libs/ActivityKit/source/ActivityKit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,20 @@

using namespace leka;

void ActivityKit::start(interface::Activity *activity)
void ActivityKit::registerActivities(std::unordered_map<uint16_t, interface::Activity *> const &activities)
{
stop();
_activities = activities;
}

_activity = activity;
void ActivityKit::start(const MagicCard &card)
{
stop();

if (_activity == nullptr) {
if (_activities.find(card.getId()) == _activities.end()) {
return;
}
_activity = _activities.at(card.getId());

_activity->start();
}

Expand Down
28 changes: 16 additions & 12 deletions libs/ActivityKit/tests/ActivityKit_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,53 +16,57 @@ class ActivityKitTest : public ::testing::Test
protected:
ActivityKitTest() = default;

// void SetUp() override {}
void SetUp() override { activitykit.registerActivities(activity_list); }
// void TearDown() override {}

ActivityKit activitykit;

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

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

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

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

activitykit.start(&mock_activity);
activitykit.start(MagicCard::number_0);
}

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

activitykit.start(nullptr);
activitykit.start(MagicCard::none);
}

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

activitykit.stop();
}

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

activitykit.start(&mock_activity);
activitykit.start(MagicCard::number_0);
activitykit.stop();
}

TEST_F(ActivityKitTest, startNewactivitySequence)
TEST_F(ActivityKitTest, startNewActivitySequence)
{
mock::Activity mock_new_activity;

{
InSequence seq;

Expand All @@ -71,7 +75,7 @@ TEST_F(ActivityKitTest, startNewactivitySequence)
EXPECT_CALL(mock_new_activity, start).Times(1);
}

activitykit.start(&mock_activity);
activitykit.start(MagicCard::number_0);

activitykit.start(&mock_new_activity);
activitykit.start(MagicCard::number_1);
}

0 comments on commit c820b63

Please sign in to comment.