Add ModificationTagsHandler

This commit is contained in:
rr-
2016-02-24 20:32:03 +01:00
parent b362f6565f
commit fca4f81eac
3 changed files with 69 additions and 0 deletions

View File

@@ -0,0 +1,18 @@
#include "tags_handlers/modification_tags_handler.h"
#include "tags_handlers_errors.h"
using namespace opustags;
ModificationTagsHandler::ModificationTagsHandler(
const int streamno,
const std::string &tag_key,
const std::string &tag_value)
: StreamTagsHandler(streamno), tag_key(tag_key), tag_value(tag_value)
{
}
bool ModificationTagsHandler::edit_impl(Tags &tags)
{
tags[tag_key] = tag_value;
return true;
}

View File

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

View File

@@ -0,0 +1,28 @@
#include "tags_handlers/modification_tags_handler.h"
#include "catch.h"
using namespace opustags;
TEST_CASE("Modification tags handler test")
{
const auto streamno = 1;
const auto first_tag_key = "tag_key";
const auto other_tag_key = "other_tag_key";
const auto dummy_value = "dummy";
const auto new_value = "dummy 2";
Tags tags = {{first_tag_key, dummy_value}};
REQUIRE(tags.size() == 1);
// setting nonexistent keys adds them
ModificationTagsHandler handler1(streamno, other_tag_key, dummy_value);
REQUIRE(handler1.edit(streamno, tags));
REQUIRE(tags.size() == 2);
REQUIRE(tags[other_tag_key] == dummy_value);
// setting existing keys overrides their values
ModificationTagsHandler handler2(streamno, other_tag_key, new_value);
REQUIRE(handler2.edit(streamno, tags));
REQUIRE(tags.size() == 2);
REQUIRE(tags[other_tag_key] == new_value);
}