mirror of
https://github.com/fmang/opustags.git
synced 2025-07-07 10:04:30 +02:00
Add StreamTagsHandler
This commit is contained in:
44
src/tags_handlers/stream_tags_handler.cc
Normal file
44
src/tags_handlers/stream_tags_handler.cc
Normal file
@ -0,0 +1,44 @@
|
||||
#include "tags_handlers/stream_tags_handler.h"
|
||||
|
||||
using namespace opustags;
|
||||
|
||||
StreamTagsHandler::StreamTagsHandler(const int streamno)
|
||||
: streamno(streamno), work_finished(false)
|
||||
{
|
||||
}
|
||||
|
||||
bool StreamTagsHandler::relevant(const int streamno)
|
||||
{
|
||||
return streamno == this->streamno;
|
||||
}
|
||||
|
||||
void StreamTagsHandler::list(const int streamno, const Tags &tags)
|
||||
{
|
||||
if (streamno != this->streamno)
|
||||
return;
|
||||
list_impl(tags);
|
||||
work_finished = true;
|
||||
}
|
||||
|
||||
bool StreamTagsHandler::edit(const int streamno, Tags &tags)
|
||||
{
|
||||
if (streamno != this->streamno)
|
||||
return false;
|
||||
const auto ret = edit_impl(tags);
|
||||
work_finished = true;
|
||||
return ret;
|
||||
}
|
||||
|
||||
bool StreamTagsHandler::done()
|
||||
{
|
||||
return work_finished;
|
||||
}
|
||||
|
||||
void StreamTagsHandler::list_impl(const Tags &)
|
||||
{
|
||||
}
|
||||
|
||||
bool StreamTagsHandler::edit_impl(Tags &)
|
||||
{
|
||||
return false;
|
||||
}
|
28
src/tags_handlers/stream_tags_handler.h
Normal file
28
src/tags_handlers/stream_tags_handler.h
Normal file
@ -0,0 +1,28 @@
|
||||
#pragma once
|
||||
|
||||
#include "tags_handler.h"
|
||||
|
||||
namespace opustags {
|
||||
|
||||
// Base handler that holds the stream number it's supposed to work with
|
||||
// and performs the usual boilerplate.
|
||||
class StreamTagsHandler : public ITagsHandler
|
||||
{
|
||||
public:
|
||||
StreamTagsHandler(const int streamno);
|
||||
|
||||
bool relevant(const int streamno) override;
|
||||
void list(const int streamno, const Tags &) override;
|
||||
bool edit(const int streamno, Tags &) override;
|
||||
bool done() override;
|
||||
|
||||
protected:
|
||||
virtual void list_impl(const Tags &);
|
||||
virtual bool edit_impl(Tags &);
|
||||
|
||||
private:
|
||||
const int streamno;
|
||||
bool work_finished;
|
||||
};
|
||||
|
||||
}
|
78
tests/tags_handlers/stream_tags_handler_test.cc
Normal file
78
tests/tags_handlers/stream_tags_handler_test.cc
Normal file
@ -0,0 +1,78 @@
|
||||
#include "tags_handlers/stream_tags_handler.h"
|
||||
#include "catch.h"
|
||||
|
||||
using namespace opustags;
|
||||
|
||||
namespace
|
||||
{
|
||||
class DummyTagsHandler final : public StreamTagsHandler
|
||||
{
|
||||
public:
|
||||
DummyTagsHandler(const int streamno);
|
||||
|
||||
protected:
|
||||
void list_impl(const Tags &) override;
|
||||
bool edit_impl(Tags &) override;
|
||||
|
||||
public:
|
||||
bool list_fired, edit_fired;
|
||||
};
|
||||
}
|
||||
|
||||
DummyTagsHandler::DummyTagsHandler(const int streamno)
|
||||
: StreamTagsHandler(streamno), list_fired(false), edit_fired(false)
|
||||
{
|
||||
}
|
||||
|
||||
void DummyTagsHandler::list_impl(const Tags &)
|
||||
{
|
||||
list_fired = true;
|
||||
}
|
||||
|
||||
bool DummyTagsHandler::edit_impl(Tags &)
|
||||
{
|
||||
edit_fired = true;
|
||||
return true;
|
||||
}
|
||||
|
||||
TEST_CASE("Stream-based tags handler test")
|
||||
{
|
||||
const auto relevant_stream_number = 1;
|
||||
const auto irrelevant_stream_number = 2;
|
||||
Tags dummy_tags;
|
||||
|
||||
DummyTagsHandler handler(1);
|
||||
|
||||
SECTION("Relevance") {
|
||||
REQUIRE(!handler.relevant(irrelevant_stream_number));
|
||||
REQUIRE(handler.relevant(relevant_stream_number));
|
||||
}
|
||||
|
||||
SECTION("Listing") {
|
||||
handler.list(irrelevant_stream_number, dummy_tags);
|
||||
REQUIRE(!handler.list_fired);
|
||||
handler.list(relevant_stream_number, dummy_tags);
|
||||
REQUIRE(handler.list_fired);
|
||||
}
|
||||
|
||||
SECTION("Editing") {
|
||||
REQUIRE(!handler.edit(irrelevant_stream_number, dummy_tags));
|
||||
REQUIRE(!handler.edit_fired);
|
||||
REQUIRE(handler.edit(relevant_stream_number, dummy_tags));
|
||||
REQUIRE(handler.edit_fired);
|
||||
}
|
||||
|
||||
SECTION("Finish through listing") {
|
||||
REQUIRE(!handler.edit(irrelevant_stream_number, dummy_tags));
|
||||
REQUIRE(!handler.done());
|
||||
REQUIRE(handler.edit(relevant_stream_number, dummy_tags));
|
||||
REQUIRE(handler.done());
|
||||
}
|
||||
|
||||
SECTION("Finish through editing") {
|
||||
handler.list(irrelevant_stream_number, dummy_tags);
|
||||
REQUIRE(!handler.done());
|
||||
handler.list(relevant_stream_number, dummy_tags);
|
||||
REQUIRE(handler.done());
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user