Skip to content

Commit

Permalink
fix: remove key from prenote dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
AndyTWF committed Jul 31, 2022
1 parent 5368347 commit b6d2c70
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 45 deletions.
5 changes: 2 additions & 3 deletions src/plugin/prenote/PublishedPrenote.cpp
Expand Up @@ -2,9 +2,8 @@
#include "controller/ControllerPositionHierarchy.h"

namespace UKControllerPlugin::Prenote {
PublishedPrenote::PublishedPrenote(
int id, std::string key, std::unique_ptr<Controller::ControllerPositionHierarchy> controllers)
: id(id), key(std::move(key)), controllers(std::move(controllers))
PublishedPrenote::PublishedPrenote(int id, std::unique_ptr<Controller::ControllerPositionHierarchy> controllers)
: id(id), controllers(std::move(controllers))
{
}

Expand Down
5 changes: 1 addition & 4 deletions src/plugin/prenote/PublishedPrenote.h
Expand Up @@ -12,7 +12,7 @@ namespace UKControllerPlugin::Prenote {
*/
using PublishedPrenote = struct PublishedPrenote
{
PublishedPrenote(int id, std::string key, std::unique_ptr<Controller::ControllerPositionHierarchy> controllers);
PublishedPrenote(int id, std::unique_ptr<Controller::ControllerPositionHierarchy> controllers);
~PublishedPrenote();
PublishedPrenote(const PublishedPrenote&) = delete;
PublishedPrenote(PublishedPrenote&&) noexcept;
Expand All @@ -22,9 +22,6 @@ namespace UKControllerPlugin::Prenote {
// The id of the prenote
int id;

// A string key to identify the prenote
std::string key;

// The controllers that this prenote refers to
std::unique_ptr<Controller::ControllerPositionHierarchy> controllers;
};
Expand Down
8 changes: 3 additions & 5 deletions src/plugin/prenote/PublishedPrenoteCollectionFactory.cpp
Expand Up @@ -22,9 +22,7 @@ namespace UKControllerPlugin::Prenote {
}

collection->Add(std::make_shared<PublishedPrenote>(
prenote.at("id").get<int>(),
prenote.at("key").get<std::string>(),
hierarchyFactory.CreateFromJsonById(prenote.at("controller_positions"))));
prenote.at("id").get<int>(), hierarchyFactory.CreateFromJsonById(prenote.at("controller_positions"))));
}

LogInfo("Loaded " + std::to_string(collection->Count()) + " published prenotes");
Expand All @@ -36,8 +34,8 @@ namespace UKControllerPlugin::Prenote {
-> bool
{
return prenote.is_object() && prenote.contains("id") && prenote.at("id").is_number_integer() &&
prenote.contains("key") && prenote.at("key").is_string() && prenote.contains("controller_positions") &&
prenote.at("controller_positions").is_array() && !prenote.at("controller_positions").empty() &&
prenote.contains("controller_positions") && prenote.at("controller_positions").is_array() &&
!prenote.at("controller_positions").empty() &&
hierarchyFactory.CreateFromJsonById(prenote.at("controller_positions")) != nullptr;
}
} // namespace UKControllerPlugin::Prenote
45 changes: 12 additions & 33 deletions test/plugin/prenote/PublishedPrenoteCollectionFactoryTest.cpp
@@ -1,7 +1,7 @@
#include "controller/ControllerPosition.h"
#include "controller/ControllerPositionCollection.h"
#include "controller/ControllerPositionHierarchyFactory.h"
#include "controller/ControllerPositionHierarchy.h"
#include "controller/ControllerPositionHierarchyFactory.h"
#include "prenote/PublishedPrenote.h"
#include "prenote/PublishedPrenoteCollection.h"
#include "prenote/PublishedPrenoteCollectionFactory.h"
Expand Down Expand Up @@ -32,66 +32,49 @@ namespace UKControllerPluginTest::Prenote {

TEST_F(PublishedPrenoteCollectionFactoryTest, APrenoteIsValid)
{
nlohmann::json data = {
{"id", 1}, {"key", "prenote_one"}, {"controller_positions", nlohmann::json::array({1, 2})}};
nlohmann::json data = {{"id", 1}, {"controller_positions", nlohmann::json::array({1, 2})}};

EXPECT_TRUE(PrenoteValid(data, factory));
}

TEST_F(PublishedPrenoteCollectionFactoryTest, APrenoteIsInvalidIfIdMissing)
{
nlohmann::json data = {{"key", "prenote_one"}, {"controller_positions", nlohmann::json::array({1, 2})}};
nlohmann::json data = {{"controller_positions", nlohmann::json::array({1, 2})}};

EXPECT_FALSE(PrenoteValid(data, factory));
}

TEST_F(PublishedPrenoteCollectionFactoryTest, APrenoteIsInvalidIfIdNotInterger)
{
nlohmann::json data = {
{"id", "abc"}, {"key", "prenote_one"}, {"controller_positions", nlohmann::json::array({1, 2})}};

EXPECT_FALSE(PrenoteValid(data, factory));
}

TEST_F(PublishedPrenoteCollectionFactoryTest, APrenoteIsInvalidIfKeyMissing)
{
nlohmann::json data = {{"id", 1}, {"controller_positions", nlohmann::json::array({1, 2})}};

EXPECT_FALSE(PrenoteValid(data, factory));
}

TEST_F(PublishedPrenoteCollectionFactoryTest, APrenoteIsInvalidIfKeyNotString)
{
nlohmann::json data = {{"id", 1}, {"key", 123}, {"controller_positions", nlohmann::json::array({1, 2})}};
nlohmann::json data = {{"id", "abc"}, {"controller_positions", nlohmann::json::array({1, 2})}};

EXPECT_FALSE(PrenoteValid(data, factory));
}

TEST_F(PublishedPrenoteCollectionFactoryTest, APrenoteIsInvalidIfControllersMissing)
{
nlohmann::json data = {{"id", 1}, {"key", "prenote_one"}};
nlohmann::json data = {{"id", 1}};

EXPECT_FALSE(PrenoteValid(data, factory));
}

TEST_F(PublishedPrenoteCollectionFactoryTest, APrenoteIsInvalidIfControllersNotArray)
{
nlohmann::json data = {{"id", 1}, {"key", "prenote_one"}, {"controller_positions", nlohmann::json::object()}};
nlohmann::json data = {{"id", 1}, {"controller_positions", nlohmann::json::object()}};

EXPECT_FALSE(PrenoteValid(data, factory));
}

TEST_F(PublishedPrenoteCollectionFactoryTest, APrenoteIsInvalidIfControllersInvalid)
{
nlohmann::json data = {
{"id", 1}, {"key", "prenote_one"}, {"controller_positions", nlohmann::json::array({"ab", "cd"})}};
nlohmann::json data = {{"id", 1}, {"controller_positions", nlohmann::json::array({"ab", "cd"})}};

EXPECT_FALSE(PrenoteValid(data, factory));
}

TEST_F(PublishedPrenoteCollectionFactoryTest, APrenoteIsInvalidIfControllersEmpty)
{
nlohmann::json data = {{"id", 1}, {"key", "prenote_one"}, {"controller_positions", nlohmann::json::array()}};
nlohmann::json data = {{"id", 1}, {"controller_positions", nlohmann::json::array()}};

EXPECT_FALSE(PrenoteValid(data, factory));
}
Expand All @@ -104,34 +87,30 @@ namespace UKControllerPluginTest::Prenote {
TEST_F(PublishedPrenoteCollectionFactoryTest, ItReturnsACollection)
{
auto data = nlohmann::json::array();
data.push_back({{"id", 1}, {"key", "prenote_one"}, {"controller_positions", nlohmann::json::array({1, 2})}});
data.push_back({{"id", 2}, {"key", "prenote_two"}, {"controller_positions", nlohmann::json::array({1})}});
data.push_back({{"id", 1}, {"controller_positions", nlohmann::json::array({1, 2})}});
data.push_back({{"id", 2}, {"controller_positions", nlohmann::json::array({1})}});

auto collection = CreatePublishedPrenoteCollection(data, factory);
EXPECT_EQ(2, collection->Count());
auto prenoteOne = collection->Get(1);
EXPECT_EQ(1, prenoteOne->id);
EXPECT_EQ("prenote_one", prenoteOne->key);
EXPECT_EQ(2, prenoteOne->controllers->CountPositions());

auto prenoteTwo = collection->Get(2);
EXPECT_EQ(2, prenoteTwo->id);
EXPECT_EQ("prenote_two", prenoteTwo->key);
EXPECT_EQ(1, prenoteTwo->controllers->CountPositions());
}

TEST_F(PublishedPrenoteCollectionFactoryTest, ItIgnoresInvalidPrenotes)
{
auto data = nlohmann::json::array();
data.push_back({{"id", 1}, {"key", "prenote_one"}, {"controller_positions", nlohmann::json::array({1, 2})}});
data.push_back(
{{"id", "invalid"}, {"key", "prenote_two"}, {"controller_positions", nlohmann::json::array({1})}});
data.push_back({{"id", 1}, {"controller_positions", nlohmann::json::array()}});
data.push_back({{"id", "invalid"}, {"controller_positions", nlohmann::json::array({1})}});

auto collection = CreatePublishedPrenoteCollection(data, factory);
EXPECT_EQ(1, collection->Count());
auto prenoteOne = collection->Get(1);
EXPECT_EQ(1, prenoteOne->id);
EXPECT_EQ("prenote_one", prenoteOne->key);
EXPECT_EQ(2, prenoteOne->controllers->CountPositions());
}

Expand Down

0 comments on commit b6d2c70

Please sign in to comment.