Add RemovalTagsHandler

This commit is contained in:
rr-
2016-02-24 20:18:10 +01:00
parent 6832cbf3f5
commit b362f6565f
5 changed files with 68 additions and 0 deletions

View File

@ -0,0 +1,19 @@
#include "tags_handlers/removal_tags_handler.h"
#include "tags_handlers_errors.h"
using namespace opustags;
RemovalTagsHandler::RemovalTagsHandler(
const int streamno, const std::string &tag_key)
: StreamTagsHandler(streamno), tag_key(tag_key)
{
}
bool RemovalTagsHandler::edit_impl(Tags &tags)
{
if (tags.find(tag_key) == tags.end())
throw TagDoesNotExistError(tag_key);
tags.erase(tag_key);
return true;
}

View File

@ -0,0 +1,19 @@
#pragma once
#include "tags_handlers/stream_tags_handler.h"
namespace opustags {
class RemovalTagsHandler : public StreamTagsHandler
{
public:
RemovalTagsHandler(const int streamno, const std::string &tag_key);
protected:
bool edit_impl(Tags &) override;
private:
const std::string tag_key;
};
}

View File

@ -6,3 +6,8 @@ TagAlreadyExistsError::TagAlreadyExistsError(const std::string &tag_key)
: std::runtime_error("Tag already exists: " + tag_key)
{
}
TagDoesNotExistError::TagDoesNotExistError(const std::string &tag_key)
: std::runtime_error("Tag does not exist: " + tag_key)
{
}

View File

@ -9,4 +9,9 @@ namespace opustags {
TagAlreadyExistsError(const std::string &tag_key);
};
struct TagDoesNotExistError : std::runtime_error
{
TagDoesNotExistError(const std::string &tag_key);
};
}

View File

@ -0,0 +1,20 @@
#include "tags_handlers/removal_tags_handler.h"
#include "catch.h"
using namespace opustags;
TEST_CASE("Removal tags handler test")
{
const auto streamno = 1;
const auto expected_tag_key = "tag_key";
const auto other_tag_key = "other_tag_key";
const auto dummy_value = "dummy";
RemovalTagsHandler handler(streamno, expected_tag_key);
Tags tags = {{expected_tag_key, dummy_value}, {other_tag_key, dummy_value}};
REQUIRE(tags.size() == 2);
REQUIRE(handler.edit(streamno, tags));
REQUIRE(tags.size() == 1);
REQUIRE(tags.find(other_tag_key) != tags.end());
REQUIRE_THROWS(handler.edit(streamno, tags));
}