diff --git a/src/tags_handlers/removal_tags_handler.cc b/src/tags_handlers/removal_tags_handler.cc new file mode 100644 index 0000000..f3d31ff --- /dev/null +++ b/src/tags_handlers/removal_tags_handler.cc @@ -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; +} diff --git a/src/tags_handlers/removal_tags_handler.h b/src/tags_handlers/removal_tags_handler.h new file mode 100644 index 0000000..0ffc081 --- /dev/null +++ b/src/tags_handlers/removal_tags_handler.h @@ -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; + }; + +} diff --git a/src/tags_handlers_errors.cc b/src/tags_handlers_errors.cc index 1f33c6d..bd0d403 100644 --- a/src/tags_handlers_errors.cc +++ b/src/tags_handlers_errors.cc @@ -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) +{ +} diff --git a/src/tags_handlers_errors.h b/src/tags_handlers_errors.h index 99e5d56..7bbb5e5 100644 --- a/src/tags_handlers_errors.h +++ b/src/tags_handlers_errors.h @@ -9,4 +9,9 @@ namespace opustags { TagAlreadyExistsError(const std::string &tag_key); }; + struct TagDoesNotExistError : std::runtime_error + { + TagDoesNotExistError(const std::string &tag_key); + }; + } diff --git a/tests/tags_handlers/removal_tags_handler_test.cc b/tests/tags_handlers/removal_tags_handler_test.cc new file mode 100644 index 0000000..bc1b54e --- /dev/null +++ b/tests/tags_handlers/removal_tags_handler_test.cc @@ -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)); +}