Add InsertionTagsHandler + a place for errors

This commit is contained in:
rr-
2016-02-24 20:13:38 +01:00
parent 2ee7702b9d
commit 6832cbf3f5
5 changed files with 83 additions and 0 deletions

View File

@@ -0,0 +1,21 @@
#include "tags_handlers/insertion_tags_handler.h"
#include "tags_handlers_errors.h"
using namespace opustags;
InsertionTagsHandler::InsertionTagsHandler(
const int streamno,
const std::string &tag_key,
const std::string &tag_value)
: StreamTagsHandler(streamno), tag_key(tag_key), tag_value(tag_value)
{
}
bool InsertionTagsHandler::edit_impl(Tags &tags)
{
if (tags.find(tag_key) != tags.end())
throw TagAlreadyExistsError(tag_key);
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 InsertionTagsHandler : public StreamTagsHandler
{
public:
InsertionTagsHandler(
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,8 @@
#include "tags_handlers_errors.h"
using namespace opustags;
TagAlreadyExistsError::TagAlreadyExistsError(const std::string &tag_key)
: std::runtime_error("Tag already exists: " + tag_key)
{
}

View File

@@ -0,0 +1,12 @@
#pragma once
#include <stdexcept>
namespace opustags {
struct TagAlreadyExistsError : std::runtime_error
{
TagAlreadyExistsError(const std::string &tag_key);
};
}

View File

@@ -0,0 +1,19 @@
#include "tags_handlers/insertion_tags_handler.h"
#include "catch.h"
using namespace opustags;
TEST_CASE("Insertion tags handler test")
{
Tags tags;
const auto streamno = 1;
const auto expected_tag_key = "tag_key";
const auto expected_tag_value = "tag_value";
InsertionTagsHandler handler(
streamno, expected_tag_key, expected_tag_value);
REQUIRE(handler.edit(streamno, tags));
REQUIRE(tags.size() == 1);
REQUIRE(tags[expected_tag_key] == expected_tag_value);
REQUIRE_THROWS(handler.edit(streamno, tags));
}