mirror of
https://github.com/fmang/opustags.git
synced 2025-07-10 11:15:10 +02:00
Add RemovalTagsHandler
This commit is contained in:
19
src/tags_handlers/removal_tags_handler.cc
Normal file
19
src/tags_handlers/removal_tags_handler.cc
Normal 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;
|
||||
}
|
19
src/tags_handlers/removal_tags_handler.h
Normal file
19
src/tags_handlers/removal_tags_handler.h
Normal 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;
|
||||
};
|
||||
|
||||
}
|
@ -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)
|
||||
{
|
||||
}
|
||||
|
@ -9,4 +9,9 @@ namespace opustags {
|
||||
TagAlreadyExistsError(const std::string &tag_key);
|
||||
};
|
||||
|
||||
struct TagDoesNotExistError : std::runtime_error
|
||||
{
|
||||
TagDoesNotExistError(const std::string &tag_key);
|
||||
};
|
||||
|
||||
}
|
||||
|
20
tests/tags_handlers/removal_tags_handler_test.cc
Normal file
20
tests/tags_handlers/removal_tags_handler_test.cc
Normal 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));
|
||||
}
|
Reference in New Issue
Block a user