Observer¶
Exercise¶
Now that we have implemented several iterations of, for example, Adapter, Exercise: Proxy (Rounding Thermometer), etc., driven by tests, lets implement yet another pattern in the same way.
#include <gtest/gtest.h>
#include <social-db-observable.h>
namespace {
class MyObserver : public SocialDB_Observable::NewCitizenObserver
{
public:
const std::string& new_citizen() const
{
return _new_citizen_svnr;
}
void new_citizen_added(const std::string& svnr) override
{
_new_citizen_svnr = svnr;
}
private:
std::string _new_citizen_svnr;
};
}
TEST(observer_suite, register_one)
{
SocialDB_Observable db;
MyObserver observer;
db.register_new_citizen_notification(&observer);
db.insert("1037190666", "Joerg", "Faschingbauer");
ASSERT_EQ(observer.new_citizen(), "1037190666");
}
#include <gtest/gtest.h>
#include <social-db-observable.h>
namespace {
class MyObserver : public SocialDB_Observable::NewCitizenObserver
{
public:
const std::string& new_citizen() const
{
return _new_citizen_svnr;
}
void new_citizen_added(const std::string& svnr) override
{
_new_citizen_svnr = svnr;
}
private:
std::string _new_citizen_svnr;
};
}
TEST(observer_suite, register_many)
{
SocialDB_Observable db;
MyObserver observer1;
MyObserver observer2;
db.register_new_citizen_notification(&observer1);
db.register_new_citizen_notification(&observer2);
db.insert("1037190666", "Joerg", "Faschingbauer");
ASSERT_EQ(observer1.new_citizen(), "1037190666");
ASSERT_EQ(observer2.new_citizen(), "1037190666");
}
#include <gtest/gtest.h>
#include <social-db-observable.h>
namespace {
class MyObserver : public SocialDB_Observable::NewCitizenObserver
{
public:
void new_citizen_added(const std::string&) override {}
};
}
TEST(observer_suite, register_duplicate)
{
SocialDB_Observable db;
MyObserver observer;
db.register_new_citizen_notification(&observer);
try {
db.register_new_citizen_notification(&observer);
FAIL();
}
catch (const SocialDB_Observable::ObserverAlreadyRegistered&) {}
// -Werror=restrict
// ASSERT_THROW(db.register_new_citizen_notification(&observer), SocialDB_Observable::ObserverAlreadyRegistered);
}
#include <gtest/gtest.h>
#include <social-db-observable.h>
namespace {
class MyObserver : public SocialDB_Observable::NewCitizenObserver
{
public:
const std::string& new_citizen() const
{
return _new_citizen_svnr;
}
void reset()
{
_new_citizen_svnr.clear();
}
void new_citizen_added(const std::string& svnr) override
{
_new_citizen_svnr = svnr;
}
private:
std::string _new_citizen_svnr;
};
}
TEST(observer_suite, unregister)
{
SocialDB_Observable db;
MyObserver observer;
db.register_new_citizen_notification(&observer);
db.insert("1037190666", "Joerg", "Faschingbauer");
ASSERT_EQ(observer.new_citizen(), "1037190666");
observer.reset();
db.unregister_new_citizen_notification(&observer);
db.insert("1234250497", "Caro", "Faschingbauer");
ASSERT_EQ(observer.new_citizen().size(), 0);
}
#include <gtest/gtest.h>
#include <social-db-observable.h>
namespace {
class MyObserver : public SocialDB_Observable::NewCitizenObserver
{
public:
void new_citizen_added(const std::string& svnr) override {}
};
}
TEST(observer_suite, unregister_not_registered)
{
SocialDB_Observable db;
MyObserver observer;
try {
db.unregister_new_citizen_notification(&observer);
FAIL();
}
catch (const SocialDB_Observable::ObserverNotRegistered&) {}
// -Werror=restrict
// ASSERT_THROW(db.unregister_new_citizen_notification(&observer), SocialDB_Observable::ObserverNotRegistered);
}