mirror of
https://github.com/fmang/opustags.git
synced 2025-10-27 06:58:12 +01:00
Add InsertionTagsHandler + a place for errors
This commit is contained in:
21
src/tags_handlers/insertion_tags_handler.cc
Normal file
21
src/tags_handlers/insertion_tags_handler.cc
Normal 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;
|
||||
}
|
||||
23
src/tags_handlers/insertion_tags_handler.h
Normal file
23
src/tags_handlers/insertion_tags_handler.h
Normal 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;
|
||||
};
|
||||
|
||||
}
|
||||
8
src/tags_handlers_errors.cc
Normal file
8
src/tags_handlers_errors.cc
Normal 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)
|
||||
{
|
||||
}
|
||||
12
src/tags_handlers_errors.h
Normal file
12
src/tags_handlers_errors.h
Normal file
@@ -0,0 +1,12 @@
|
||||
#pragma once
|
||||
|
||||
#include <stdexcept>
|
||||
|
||||
namespace opustags {
|
||||
|
||||
struct TagAlreadyExistsError : std::runtime_error
|
||||
{
|
||||
TagAlreadyExistsError(const std::string &tag_key);
|
||||
};
|
||||
|
||||
}
|
||||
19
tests/tags_handlers/insertion_tags_handler_test.cc
Normal file
19
tests/tags_handlers/insertion_tags_handler_test.cc
Normal 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));
|
||||
}
|
||||
Reference in New Issue
Block a user