From aeca60d128756ba4dbc7859cf6cd8eebe04aa9f0 Mon Sep 17 00:00:00 2001 From: rr- Date: Wed, 24 Feb 2016 19:43:56 +0100 Subject: [PATCH] Add CompositeTagsHandler --- src/tags_handlers/composite_tags_handler.cc | 38 ++++++++ src/tags_handlers/composite_tags_handler.h | 23 +++++ .../composite_tags_handler_test.cc | 96 +++++++++++++++++++ 3 files changed, 157 insertions(+) create mode 100644 src/tags_handlers/composite_tags_handler.cc create mode 100644 src/tags_handlers/composite_tags_handler.h create mode 100644 tests/tags_handlers/composite_tags_handler_test.cc diff --git a/src/tags_handlers/composite_tags_handler.cc b/src/tags_handlers/composite_tags_handler.cc new file mode 100644 index 0000000..237ca25 --- /dev/null +++ b/src/tags_handlers/composite_tags_handler.cc @@ -0,0 +1,38 @@ +#include "tags_handlers/composite_tags_handler.h" + +using namespace opustags; + +void CompositeTagsHandler::add_handler(std::shared_ptr 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; +} diff --git a/src/tags_handlers/composite_tags_handler.h b/src/tags_handlers/composite_tags_handler.h new file mode 100644 index 0000000..513298d --- /dev/null +++ b/src/tags_handlers/composite_tags_handler.h @@ -0,0 +1,23 @@ +#pragma once + +#include +#include +#include "tags_handler.h" + +namespace opustags { + + class CompositeTagsHandler final : public ITagsHandler + { + public: + void add_handler(std::shared_ptr 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> handlers; + }; + +} diff --git a/tests/tags_handlers/composite_tags_handler_test.cc b/tests/tags_handlers/composite_tags_handler_test.cc new file mode 100644 index 0000000..ccb5f5e --- /dev/null +++ b/tests/tags_handlers/composite_tags_handler_test.cc @@ -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(); + auto handler2 = std::make_shared(); + 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()); + } +}