Add CompositeTagsHandler

This commit is contained in:
rr- 2016-02-24 19:43:56 +01:00
parent 1d8d96cdee
commit aeca60d128
3 changed files with 157 additions and 0 deletions

View File

@ -0,0 +1,38 @@
#include "tags_handlers/composite_tags_handler.h"
using namespace opustags;
void CompositeTagsHandler::add_handler(std::shared_ptr<ITagsHandler> handler)
{
handlers.push_back(std::move(handler));
}
bool CompositeTagsHandler::relevant(const int streamno)
{
for (const auto &handler : handlers)
if (handler->relevant(streamno))
return true;
return false;
}
void CompositeTagsHandler::list(const int streamno, const Tags &tags)
{
for (const auto &handler : handlers)
handler->list(streamno, tags);
}
bool CompositeTagsHandler::edit(const int streamno, Tags &tags)
{
bool modified = false;
for (const auto &handler : handlers)
modified |= handler->edit(streamno, tags);
return modified;
}
bool CompositeTagsHandler::done()
{
bool done = true;
for (const auto &handler : handlers)
done &= handler->done();
return done;
}

View File

@ -0,0 +1,23 @@
#pragma once
#include <memory>
#include <vector>
#include "tags_handler.h"
namespace opustags {
class CompositeTagsHandler final : public ITagsHandler
{
public:
void add_handler(std::shared_ptr<ITagsHandler> handler);
bool relevant(const int streamno) override;
void list(const int streamno, const Tags &) override;
bool edit(const int streamno, Tags &) override;
bool done() override;
private:
std::vector<std::shared_ptr<ITagsHandler>> handlers;
};
}

View File

@ -0,0 +1,96 @@
#include "tags_handlers/composite_tags_handler.h"
#include "catch.h"
using namespace opustags;
namespace
{
struct DummyTagsHandler : ITagsHandler
{
DummyTagsHandler();
bool relevant(const int streamno) override;
void list(const int streamno, const Tags &) override;
bool edit(const int streamno, Tags &) override;
bool done() override;
bool relevant_ret, edit_ret, done_ret, list_fired;
};
}
DummyTagsHandler::DummyTagsHandler()
: relevant_ret(true), edit_ret(true), done_ret(true), list_fired(false)
{
}
bool DummyTagsHandler::relevant(const int streamno)
{
return relevant_ret;
}
void DummyTagsHandler::list(const int streamno, const Tags &)
{
list_fired = true;
}
bool DummyTagsHandler::edit(const int streamno, Tags &)
{
return edit_ret;
}
bool DummyTagsHandler::done()
{
return done_ret;
}
TEST_CASE("Composite tags handler test")
{
auto handler1 = std::make_shared<DummyTagsHandler>();
auto handler2 = std::make_shared<DummyTagsHandler>();
CompositeTagsHandler composite_handler;
composite_handler.add_handler(handler1);
composite_handler.add_handler(handler2);
SECTION("Relevance") {
const int dummy_streamno = 1;
handler1->relevant_ret = true;
handler2->relevant_ret = true;
REQUIRE(composite_handler.relevant(dummy_streamno));
handler1->relevant_ret = false;
REQUIRE(composite_handler.relevant(dummy_streamno));
handler2->relevant_ret = false;
REQUIRE(!composite_handler.relevant(dummy_streamno));
}
SECTION("Listing") {
const int dummy_streamno = 1;
Tags dummy_tags;
REQUIRE(!handler1->list_fired);
REQUIRE(!handler2->list_fired);
composite_handler.list(dummy_streamno, dummy_tags);
REQUIRE(handler1->list_fired);
REQUIRE(handler2->list_fired);
}
SECTION("Editing") {
const int dummy_streamno = 1;
Tags dummy_tags;
handler1->edit_ret = true;
handler2->edit_ret = true;
REQUIRE(composite_handler.edit(dummy_streamno, dummy_tags));
handler1->edit_ret = false;
REQUIRE(composite_handler.edit(dummy_streamno, dummy_tags));
handler2->edit_ret = false;
REQUIRE(!composite_handler.edit(dummy_streamno, dummy_tags));
}
SECTION("Finish") {
handler1->done_ret = true;
handler2->done_ret = true;
REQUIRE(composite_handler.done());
handler1->done_ret = false;
REQUIRE(!composite_handler.done());
handler2->done_ret = false;
REQUIRE(!composite_handler.done());
}
}