RemovalTagsHandler: add ability to delete all tags

This commit is contained in:
rr- 2016-02-24 21:38:24 +01:00
parent d9f123c84c
commit 4f5d33491b
3 changed files with 52 additions and 16 deletions

View File

@ -3,6 +3,11 @@
using namespace opustags;
RemovalTagsHandler::RemovalTagsHandler(const int streamno)
: StreamTagsHandler(streamno)
{
}
RemovalTagsHandler::RemovalTagsHandler(
const int streamno, const std::string &tag_key)
: StreamTagsHandler(streamno), tag_key(tag_key)
@ -11,9 +16,19 @@ RemovalTagsHandler::RemovalTagsHandler(
bool RemovalTagsHandler::edit_impl(Tags &tags)
{
if (!tags.contains(tag_key))
throw TagDoesNotExistError(tag_key);
if (tag_key.empty())
{
const auto all_tags = tags.get_all();
for (const auto &kv : all_tags)
tags.remove(std::get<0>(kv));
return !all_tags.empty();
}
else
{
if (!tags.contains(tag_key))
throw TagDoesNotExistError(tag_key);
tags.remove(tag_key);
return true;
tags.remove(tag_key);
return true;
}
}

View File

@ -7,6 +7,7 @@ namespace opustags {
class RemovalTagsHandler : public StreamTagsHandler
{
public:
RemovalTagsHandler(const int streamno);
RemovalTagsHandler(const int streamno, const std::string &tag_key);
protected:

View File

@ -6,18 +6,38 @@ 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;
tags.set(expected_tag_key, dummy_value);
tags.set(other_tag_key, dummy_value);
SECTION("Removing a single tag")
{
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);
REQUIRE(tags.get_all().size() == 2);
REQUIRE(handler.edit(streamno, tags));
REQUIRE(tags.get_all().size() == 1);
REQUIRE(tags.contains(other_tag_key));
REQUIRE_THROWS(handler.edit(streamno, tags));
Tags tags;
tags.set(expected_tag_key, dummy_value);
tags.set(other_tag_key, dummy_value);
REQUIRE(tags.get_all().size() == 2);
REQUIRE(handler.edit(streamno, tags));
REQUIRE(tags.get_all().size() == 1);
REQUIRE(tags.contains(other_tag_key));
REQUIRE_THROWS(handler.edit(streamno, tags));
}
SECTION("Removing all tags")
{
RemovalTagsHandler handler(streamno);
Tags tags;
tags.set("z", "value1");
tags.set("a", "value2");
tags.set("y", "value3");
tags.set("c", "value4");
REQUIRE(tags.get_all().size() == 4);
REQUIRE(handler.edit(streamno, tags));
REQUIRE(tags.get_all().size() == 0);
REQUIRE(!handler.edit(streamno, tags));
}
}