refactor
This commit is contained in:
parent
6a144b40e1
commit
2c50e3ee7f
@ -1,5 +1,5 @@
|
||||
#include <eedb/EEDB.hpp>
|
||||
#include <eedb/Session.hpp>
|
||||
#include <eedb/WebSession.hpp>
|
||||
|
||||
#include <eedb/auth/PgUserAuth.hpp>
|
||||
#include <eedb/auth/Services.hpp>
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
file(GLOB SOURCE
|
||||
EEDB.cpp
|
||||
Session.cpp
|
||||
WebSession.cpp
|
||||
|
||||
auth/PgUserAuth.cpp
|
||||
auth/Services.cpp
|
||||
|
||||
@ -39,17 +39,15 @@ EEDB::EEDB(std::unique_ptr< Session > session, AuthPageFactory authPageFactory,
|
||||
setTheme(_theme->create());
|
||||
|
||||
_authPage = _authPageFactory();
|
||||
_authPage->registerNeedVerification([] {});
|
||||
_authPage->registerOnUserWeakLogin([=] { authEventLogin(LoginState::weak); });
|
||||
_authPage->registerOnUserStrongLogin([=] { authEventLogin(LoginState::strong); });
|
||||
_authPage->registerOnNeedVerification([] {});
|
||||
_authPage->registerOnUserWeakLogin([&] { authEventLogin(LoginState::weak); });
|
||||
_authPage->registerOnUserStrongLogin([&] { authEventLogin(LoginState::strong); });
|
||||
_authPage->registerOnUserLogout([=] { authEventLogout(); });
|
||||
_authPage->attachTo(root());
|
||||
_authPage->processEnvironment();
|
||||
}
|
||||
|
||||
void EEDB::authEventLogin(EEDB::LoginState state) {
|
||||
// using namespace Wt;
|
||||
|
||||
root()->removeStyleClass("container");
|
||||
_authPage->detach();
|
||||
_homePage = _homePageFactory();
|
||||
|
||||
@ -1,8 +1,5 @@
|
||||
#pragma once
|
||||
|
||||
#include <Wt/Auth/Login.h>
|
||||
#include <Wt/WEnvironment.h>
|
||||
|
||||
namespace eedb::auth {
|
||||
class PgUserAuth;
|
||||
}
|
||||
@ -11,9 +8,15 @@ namespace eedb::db {
|
||||
class PgConnection;
|
||||
};
|
||||
|
||||
namespace Wt{
|
||||
class WEnvironment;
|
||||
}
|
||||
|
||||
namespace Wt::Auth {
|
||||
class AbstractUserDatabase;
|
||||
class Login;
|
||||
}
|
||||
|
||||
namespace eedb {
|
||||
|
||||
class Session {
|
||||
@ -25,17 +28,4 @@ class Session {
|
||||
virtual Wt::Auth::Login & login() = 0;
|
||||
};
|
||||
|
||||
class WebSession final : public Session {
|
||||
public:
|
||||
WebSession(std::unique_ptr< eedb::db::PgConnection > dbConnection, const Wt::WEnvironment & env);
|
||||
|
||||
eedb::db::PgConnection & db() override;
|
||||
const Wt::WEnvironment & enviroment() const override;
|
||||
Wt::Auth::Login & login() override;
|
||||
|
||||
private:
|
||||
std::unique_ptr< eedb::db::PgConnection > _dbConnection;
|
||||
Wt::Auth::Login _login;
|
||||
const Wt::WEnvironment & _env;
|
||||
};
|
||||
} // namespace eedb
|
||||
|
||||
@ -1,11 +1,13 @@
|
||||
#include <eedb/Session.hpp>
|
||||
#include <eedb/WebSession.hpp>
|
||||
|
||||
#include <eedb/auth/PgUserAuth.hpp>
|
||||
#include <eedb/db/connection.hpp>
|
||||
|
||||
namespace eedb {
|
||||
WebSession::WebSession(std::unique_ptr< eedb::db::PgConnection > dbConnection, const Wt::WEnvironment & env)
|
||||
: _dbConnection(std::move(dbConnection)), _env(env) {}
|
||||
: _dbConnection(std::move(dbConnection)), _env(env) {}
|
||||
|
||||
WebSession::~WebSession() =default;
|
||||
|
||||
db::PgConnection & WebSession::db() {
|
||||
return *_dbConnection;
|
||||
35
src/eedb/WebSession.hpp
Normal file
35
src/eedb/WebSession.hpp
Normal file
@ -0,0 +1,35 @@
|
||||
#pragma once
|
||||
|
||||
#include <Wt/Auth/Login.h>
|
||||
#include <Wt/WEnvironment.h>
|
||||
|
||||
#include <eedb/Session.hpp>
|
||||
|
||||
namespace eedb::auth {
|
||||
class PgUserAuth;
|
||||
}
|
||||
|
||||
namespace eedb::db {
|
||||
class PgConnection;
|
||||
};
|
||||
|
||||
namespace Wt::Auth {
|
||||
class AbstractUserDatabase;
|
||||
}
|
||||
namespace eedb {
|
||||
|
||||
class WebSession final : public Session {
|
||||
public:
|
||||
explicit WebSession(std::unique_ptr< eedb::db::PgConnection > dbConnection, const Wt::WEnvironment & env);
|
||||
~WebSession() override;
|
||||
|
||||
eedb::db::PgConnection & db() override;
|
||||
const Wt::WEnvironment & enviroment() const override;
|
||||
Wt::Auth::Login & login() override;
|
||||
|
||||
private:
|
||||
std::unique_ptr< eedb::db::PgConnection > _dbConnection;
|
||||
Wt::Auth::Login _login;
|
||||
const Wt::WEnvironment & _env;
|
||||
};
|
||||
} // namespace eedb
|
||||
@ -16,7 +16,7 @@ class AuthPage {
|
||||
virtual void detach() = 0;
|
||||
virtual void processEnvironment() = 0;
|
||||
|
||||
virtual void registerNeedVerification(std::function< void() > f) = 0;
|
||||
virtual void registerOnNeedVerification(std::function< void() > f) = 0;
|
||||
virtual void registerOnUserWeakLogin(std::function< void() > f) = 0;
|
||||
virtual void registerOnUserStrongLogin(std::function< void() > f) = 0;
|
||||
virtual void registerOnUserLogout(std::function< void() > f) = 0;
|
||||
|
||||
@ -43,7 +43,7 @@ struct DefaultAuthPage::DefaultAuthPagePriv {
|
||||
DefaultAuthPage::DefaultAuthPage(const auth::Services & baseAuth,
|
||||
std::unique_ptr< Wt::Auth::AbstractUserDatabase > userDatabase,
|
||||
Wt::Auth::Login & _login)
|
||||
: _priv(std::make_unique< DefaultAuthPagePriv >(baseAuth, std::move(userDatabase), _login)) {
|
||||
: _priv(spimpl::make_unique_impl< DefaultAuthPagePriv >(baseAuth, std::move(userDatabase), _login)) {
|
||||
_priv->_authWidget->login().changed().connect([this]() {
|
||||
if(_priv->_authWidget->login().state() == Wt::Auth::LoginState::Strong) {
|
||||
this->notifyUserStrongLogin();
|
||||
@ -57,15 +57,6 @@ DefaultAuthPage::DefaultAuthPage(const auth::Services & baseAuth,
|
||||
});
|
||||
}
|
||||
|
||||
DefaultAuthPage::DefaultAuthPage(DefaultAuthPage && rhs) : _priv(std::move(rhs._priv)) {}
|
||||
|
||||
DefaultAuthPage::~DefaultAuthPage() = default;
|
||||
|
||||
DefaultAuthPage & DefaultAuthPage::operator=(DefaultAuthPage && rhs) {
|
||||
_priv = std::move(rhs._priv);
|
||||
return *this;
|
||||
}
|
||||
|
||||
void DefaultAuthPage::attachTo(Wt::WContainerWidget * parent) {
|
||||
parent->addWidget(std::move(_priv->_authWidget));
|
||||
}
|
||||
@ -81,7 +72,7 @@ void DefaultAuthPage::processEnvironment() {
|
||||
_priv->_authWidget->processEnvironment();
|
||||
}
|
||||
|
||||
void DefaultAuthPage::registerNeedVerification(std::function< void() > f) {
|
||||
void DefaultAuthPage::registerOnNeedVerification(std::function< void() > f) {
|
||||
_priv->_onNeedEmailVerification.connect([=]() { f(); });
|
||||
}
|
||||
|
||||
|
||||
@ -2,7 +2,7 @@
|
||||
|
||||
#include <eedb/widgets/AuthPage.hpp>
|
||||
|
||||
#include <memory>
|
||||
#include <utils/spimpl.hpp>
|
||||
|
||||
namespace Wt::Auth {
|
||||
class AbstractUserDatabase;
|
||||
@ -16,24 +16,16 @@ class Services;
|
||||
namespace eedb {
|
||||
class DefaultAuthPage final : public AuthPage {
|
||||
public:
|
||||
DefaultAuthPage(const eedb::auth::Services & baseAuth,
|
||||
DefaultAuthPage( //
|
||||
const auth::Services & baseAuth,
|
||||
std::unique_ptr< Wt::Auth::AbstractUserDatabase > userDatabase,
|
||||
Wt::Auth::Login & session);
|
||||
DefaultAuthPage(const DefaultAuthPage &) = delete;
|
||||
DefaultAuthPage(DefaultAuthPage &&);
|
||||
|
||||
~DefaultAuthPage() override;
|
||||
|
||||
// clang-format off
|
||||
DefaultAuthPage & operator=(const DefaultAuthPage &) = delete;
|
||||
DefaultAuthPage & operator=(DefaultAuthPage &&);
|
||||
// clang-format on
|
||||
|
||||
void attachTo(Wt::WContainerWidget * parent) override;
|
||||
void detach() override;
|
||||
void processEnvironment() override;
|
||||
|
||||
void registerNeedVerification(std::function< void() > f) override;
|
||||
void registerOnNeedVerification(std::function< void() > f) override;
|
||||
void registerOnUserWeakLogin(std::function< void() > f) override;
|
||||
void registerOnUserStrongLogin(std::function< void() > f) override;
|
||||
void registerOnUserLogout(std::function< void() > f) override;
|
||||
@ -46,6 +38,6 @@ class DefaultAuthPage final : public AuthPage {
|
||||
|
||||
private:
|
||||
struct DefaultAuthPagePriv;
|
||||
std::unique_ptr< DefaultAuthPagePriv > _priv;
|
||||
spimpl::unique_impl_ptr< DefaultAuthPagePriv > _priv;
|
||||
};
|
||||
} // namespace eedb
|
||||
|
||||
446
src/utils/spimpl.hpp
Normal file
446
src/utils/spimpl.hpp
Normal file
@ -0,0 +1,446 @@
|
||||
/*
|
||||
====================================================================
|
||||
A Smart Pointer to IMPLementation (i.e. Smart PIMPL or just SPIMPL).
|
||||
====================================================================
|
||||
|
||||
Version: 1.1
|
||||
|
||||
Latest version:
|
||||
https://github.com/oliora/samples/blob/master/spimpl.h
|
||||
Rationale and description:
|
||||
http://oliora.github.io/2015/12/29/pimpl-and-rule-of-zero.html
|
||||
|
||||
Copyright (c) 2015 Andrey Upadyshev (oliora@gmail.com)
|
||||
|
||||
Distributed under the Boost Software License, Version 1.0.
|
||||
See http://www.boost.org/LICENSE_1_0.txt
|
||||
|
||||
Changes history
|
||||
---------------
|
||||
v1.1:
|
||||
- auto_ptr support is disabled by default for C++17 compatibility
|
||||
v1.0:
|
||||
- Released
|
||||
*/
|
||||
|
||||
#ifndef SPIMPL_H_
|
||||
#define SPIMPL_H_
|
||||
|
||||
#include <memory>
|
||||
#include <type_traits>
|
||||
#include <cassert>
|
||||
|
||||
|
||||
#if defined _MSC_VER && _MSC_VER < 1900 // MS Visual Studio before VS2015
|
||||
#define SPIMPL_NO_CPP11_NOEXCEPT
|
||||
#define SPIMPL_NO_CPP11_CONSTEXPR
|
||||
#define SPIMPL_NO_CPP11_DEFAULT_MOVE_SPEC_FUNC
|
||||
#endif
|
||||
|
||||
#if ! defined SPIMPL_NO_CPP11_NOEXCEPT
|
||||
#define SPIMPL_NOEXCEPT noexcept
|
||||
#else
|
||||
#define SPIMPL_NOEXCEPT
|
||||
#endif
|
||||
|
||||
#if ! defined SPIMPL_NO_CPP11_CONSTEXPR
|
||||
#define SPIMPL_CONSTEXPR constexpr
|
||||
#else
|
||||
#define SPIMPL_CONSTEXPR
|
||||
#endif
|
||||
|
||||
// define SPIMPL_HAS_AUTO_PTR to enable constructor and assignment operator that accept std::auto_ptr
|
||||
// TODO: auto detect std::auto_ptr support
|
||||
|
||||
|
||||
namespace spimpl {
|
||||
namespace details {
|
||||
template<class T>
|
||||
T *default_copy(T *src)
|
||||
{
|
||||
static_assert(sizeof(T) > 0, "default_copy cannot copy incomplete type");
|
||||
static_assert(!std::is_void<T>::value, "default_copy cannot copy incomplete type");
|
||||
return new T(*src);
|
||||
}
|
||||
|
||||
template<class T>
|
||||
void default_delete(T *p) SPIMPL_NOEXCEPT
|
||||
{
|
||||
static_assert(sizeof(T) > 0, "default_delete cannot delete incomplete type");
|
||||
static_assert(!std::is_void<T>::value, "default_delete cannot delete incomplete type");
|
||||
delete p;
|
||||
}
|
||||
|
||||
template<class T>
|
||||
struct default_deleter {
|
||||
using type = void (*)(T*);
|
||||
};
|
||||
|
||||
template<class T>
|
||||
using default_deleter_t = typename default_deleter<T>::type;
|
||||
|
||||
template<class T>
|
||||
struct default_copier {
|
||||
using type = T* (*)(T*);
|
||||
};
|
||||
|
||||
template<class T>
|
||||
using default_copier_t = typename default_copier<T>::type;
|
||||
|
||||
template<class T, class D, class C>
|
||||
struct is_default_manageable: public std::integral_constant<bool,
|
||||
std::is_same<D, default_deleter_t<T>>::value &&
|
||||
std::is_same<C, default_copier_t<T>>::value
|
||||
> {};
|
||||
}
|
||||
|
||||
|
||||
template<class T, class Deleter = details::default_deleter_t<T>, class Copier = details::default_copier_t<T>>
|
||||
class impl_ptr
|
||||
{
|
||||
private:
|
||||
static_assert(!std::is_array<T>::value, "impl_ptr specialization for arrays is not implemented");
|
||||
struct dummy_t_ {int dummy__;};
|
||||
|
||||
public:
|
||||
using pointer = T*;
|
||||
using element_type = T;
|
||||
using copier_type = typename std::decay<Copier>::type;
|
||||
using deleter_type = typename std::decay<Deleter>::type;
|
||||
using unique_ptr_type = std::unique_ptr<T, deleter_type>;
|
||||
using is_default_manageable = details::is_default_manageable<T, deleter_type, copier_type>;
|
||||
|
||||
SPIMPL_CONSTEXPR impl_ptr() SPIMPL_NOEXCEPT
|
||||
: ptr_(nullptr, deleter_type{}), copier_(copier_type{}) {}
|
||||
|
||||
SPIMPL_CONSTEXPR impl_ptr(std::nullptr_t) SPIMPL_NOEXCEPT
|
||||
: impl_ptr() {}
|
||||
|
||||
template<class D, class C>
|
||||
impl_ptr(pointer p, D&& d, C&& c,
|
||||
typename std::enable_if<
|
||||
std::is_convertible<D, deleter_type>::value
|
||||
&& std::is_convertible<C, copier_type>::value,
|
||||
dummy_t_
|
||||
>::type = dummy_t_()) SPIMPL_NOEXCEPT
|
||||
: ptr_(std::move(p), std::forward<D>(d)), copier_(std::forward<C>(c)) {}
|
||||
|
||||
template<class U>
|
||||
impl_ptr(U *u,
|
||||
typename std::enable_if<
|
||||
std::is_convertible<U*, pointer>::value
|
||||
&& is_default_manageable::value,
|
||||
dummy_t_
|
||||
>::type = dummy_t_()) SPIMPL_NOEXCEPT
|
||||
: impl_ptr(u, &details::default_delete<T>, &details::default_copy<T>) {}
|
||||
|
||||
impl_ptr(const impl_ptr& r)
|
||||
: impl_ptr(r.clone()) {}
|
||||
|
||||
#ifndef SPIMPL_NO_CPP11_DEFAULT_MOVE_SPEC_FUNC
|
||||
impl_ptr(impl_ptr&& r) SPIMPL_NOEXCEPT = default;
|
||||
#else
|
||||
impl_ptr(impl_ptr&& r) SPIMPL_NOEXCEPT
|
||||
: ptr_(std::move(r.ptr_)), copier_(std::move(r.copier_)) {}
|
||||
#endif
|
||||
|
||||
#ifdef SPIMPL_HAS_AUTO_PTR
|
||||
template<class U>
|
||||
impl_ptr(std::auto_ptr<U>&& u,
|
||||
typename std::enable_if<
|
||||
std::is_convertible<U*, pointer>::value
|
||||
&& is_default_manageable::value,
|
||||
dummy_t_
|
||||
>::type = dummy_t_()) SPIMPL_NOEXCEPT
|
||||
: ptr_(u.release(), &details::default_delete<T>), copier_(&details::default_copy<T>) {}
|
||||
#endif
|
||||
|
||||
template<class U>
|
||||
impl_ptr(std::unique_ptr<U>&& u,
|
||||
typename std::enable_if<
|
||||
std::is_convertible<U*, pointer>::value
|
||||
&& is_default_manageable::value,
|
||||
dummy_t_
|
||||
>::type = dummy_t_()) SPIMPL_NOEXCEPT
|
||||
: ptr_(u.release(), &details::default_delete<T>), copier_(&details::default_copy<T>) {}
|
||||
|
||||
template<class U, class D, class C>
|
||||
impl_ptr(std::unique_ptr<U, D>&& u, C&& c,
|
||||
typename std::enable_if<
|
||||
std::is_convertible<U*, pointer>::value
|
||||
&& std::is_convertible<D, deleter_type>::value
|
||||
&& std::is_convertible<C, copier_type>::value,
|
||||
dummy_t_
|
||||
>::type = dummy_t_()) SPIMPL_NOEXCEPT
|
||||
: ptr_(std::move(u)), copier_(std::forward<C>(c)) {}
|
||||
|
||||
template<class U, class D, class C>
|
||||
impl_ptr(impl_ptr<U, D, C>&& u,
|
||||
typename std::enable_if<
|
||||
std::is_convertible<U*, pointer>::value
|
||||
&& std::is_convertible<D, deleter_type>::value
|
||||
&& std::is_convertible<C, copier_type>::value,
|
||||
dummy_t_
|
||||
>::type = dummy_t_()) SPIMPL_NOEXCEPT
|
||||
: ptr_(std::move(u.ptr_)), copier_(std::move(u.copier_)) {}
|
||||
|
||||
impl_ptr& operator= (const impl_ptr& r)
|
||||
{
|
||||
if (this == &r)
|
||||
return *this;
|
||||
|
||||
return operator=(r.clone());
|
||||
}
|
||||
|
||||
#ifndef SPIMPL_NO_CPP11_DEFAULT_MOVE_SPEC_FUNC
|
||||
impl_ptr& operator= (impl_ptr&& r) SPIMPL_NOEXCEPT = default;
|
||||
#else
|
||||
impl_ptr& operator= (impl_ptr&& r) SPIMPL_NOEXCEPT {
|
||||
ptr_ = std::move(r.ptr_);
|
||||
copier_ = std::move(r.copier_);
|
||||
return *this;
|
||||
}
|
||||
#endif
|
||||
|
||||
template<class U, class D, class C>
|
||||
typename std::enable_if<
|
||||
std::is_convertible<U*, pointer>::value
|
||||
&& std::is_convertible<D, deleter_type>::value
|
||||
&& std::is_convertible<C, copier_type>::value,
|
||||
impl_ptr&
|
||||
>::type operator= (impl_ptr<U, D, C>&& u) SPIMPL_NOEXCEPT
|
||||
{
|
||||
ptr_ = std::move(u.ptr_);
|
||||
copier_ = std::move(u.copier_);
|
||||
return *this;
|
||||
}
|
||||
|
||||
template<class U, class D, class C>
|
||||
typename std::enable_if<
|
||||
std::is_convertible<U*, pointer>::value
|
||||
&& std::is_convertible<D, deleter_type>::value
|
||||
&& std::is_convertible<C, copier_type>::value,
|
||||
impl_ptr&
|
||||
>::type operator= (const impl_ptr<U, D, C>& u)
|
||||
{
|
||||
return operator=(u.clone());
|
||||
}
|
||||
|
||||
//
|
||||
|
||||
#ifdef SPIMPL_HAS_AUTO_PTR
|
||||
template<class U>
|
||||
typename std::enable_if<
|
||||
std::is_convertible<U*, pointer>::value
|
||||
&& is_default_manageable::value,
|
||||
impl_ptr&
|
||||
>::type operator= (std::auto_ptr<U>&& u) SPIMPL_NOEXCEPT
|
||||
{
|
||||
return operator=(impl_ptr(std::move(u)));
|
||||
}
|
||||
#endif
|
||||
|
||||
template<class U>
|
||||
typename std::enable_if<
|
||||
std::is_convertible<U*, pointer>::value
|
||||
&& is_default_manageable::value,
|
||||
impl_ptr&
|
||||
>::type operator= (std::unique_ptr<U>&& u) SPIMPL_NOEXCEPT
|
||||
{
|
||||
return operator=(impl_ptr(std::move(u)));
|
||||
}
|
||||
|
||||
impl_ptr clone() const
|
||||
{
|
||||
return impl_ptr(
|
||||
ptr_ ? copier_(ptr_.get()) : nullptr,
|
||||
ptr_.get_deleter(),
|
||||
copier_);
|
||||
}
|
||||
|
||||
typename std::remove_reference<T>::type & operator*() const { return *ptr_; }
|
||||
pointer operator->() const SPIMPL_NOEXCEPT { return get(); }
|
||||
pointer get() const SPIMPL_NOEXCEPT { return ptr_.get(); }
|
||||
|
||||
void swap(impl_ptr& u) SPIMPL_NOEXCEPT
|
||||
{
|
||||
using std::swap;
|
||||
ptr_.swap(u.ptr_);
|
||||
swap(copier_, u.copier_);
|
||||
}
|
||||
|
||||
pointer release() SPIMPL_NOEXCEPT { return ptr_.release(); }
|
||||
|
||||
unique_ptr_type release_unique() SPIMPL_NOEXCEPT { return std::move(ptr_); }
|
||||
|
||||
explicit operator bool() const SPIMPL_NOEXCEPT { return static_cast<bool>(ptr_); }
|
||||
|
||||
typename std::remove_reference<deleter_type>::type& get_deleter() SPIMPL_NOEXCEPT { return ptr_.get_deleter(); }
|
||||
const typename std::remove_reference<deleter_type>::type& get_deleter() const SPIMPL_NOEXCEPT { return ptr_.get_deleter(); }
|
||||
|
||||
typename std::remove_reference<copier_type>::type& get_copier() SPIMPL_NOEXCEPT { return copier_; }
|
||||
const typename std::remove_reference<copier_type>::type& get_copier() const SPIMPL_NOEXCEPT { return copier_; }
|
||||
|
||||
private:
|
||||
unique_ptr_type ptr_;
|
||||
copier_type copier_;
|
||||
};
|
||||
|
||||
|
||||
template<class T, class D, class C>
|
||||
inline void swap(impl_ptr<T, D, C>& l, impl_ptr<T, D, C>& r) SPIMPL_NOEXCEPT
|
||||
{
|
||||
l.swap(r);
|
||||
}
|
||||
|
||||
|
||||
template <class T1, class D1, class C1, class T2, class D2, class C2>
|
||||
inline bool operator==(const impl_ptr<T1, D1, C1>& l, const impl_ptr<T2, D2, C2>& r)
|
||||
{
|
||||
return l.get() == r.get();
|
||||
}
|
||||
|
||||
template <class T1, class D1, class C1, class T2, class D2, class C2>
|
||||
inline bool operator!=(const impl_ptr<T1, D1, C1>& l, const impl_ptr<T2, D2, C2>& r)
|
||||
{
|
||||
return !(l == r);
|
||||
}
|
||||
|
||||
template <class T1, class D1, class C1, class T2, class D2, class C2>
|
||||
inline bool operator< (const impl_ptr<T1, D1, C1>& l, const impl_ptr<T2, D2, C2>& r)
|
||||
{
|
||||
using P1 = typename impl_ptr<T1, D1, C1>::pointer;
|
||||
using P2 = typename impl_ptr<T2, D2, C2>::pointer;
|
||||
using CT = typename std::common_type<P1, P2>::type;
|
||||
return std::less<CT>()(l.get(), r.get());
|
||||
}
|
||||
|
||||
template <class T1, class D1, class C1, class T2, class D2, class C2>
|
||||
inline bool operator> (const impl_ptr<T1, D1, C1>& l, const impl_ptr<T2, D2, C2>& r)
|
||||
{
|
||||
return r < l;
|
||||
}
|
||||
|
||||
template <class T1, class D1, class C1, class T2, class D2, class C2>
|
||||
inline bool operator<=(const impl_ptr<T1, D1, C1>& l, const impl_ptr<T2, D2, C2>& r)
|
||||
{
|
||||
return !(r < l);
|
||||
}
|
||||
|
||||
template <class T1, class D1, class C1, class T2, class D2, class C2>
|
||||
inline bool operator>=(const impl_ptr<T1, D1, C1>& l, const impl_ptr<T2, D2, C2>& r)
|
||||
{
|
||||
return !(l < r);
|
||||
}
|
||||
|
||||
template <class T, class D, class C>
|
||||
inline bool operator==(const impl_ptr<T, D, C>& p, std::nullptr_t) SPIMPL_NOEXCEPT
|
||||
{
|
||||
return !p;
|
||||
}
|
||||
|
||||
template <class T, class D, class C>
|
||||
inline bool operator==(std::nullptr_t, const impl_ptr<T, D, C>& p) SPIMPL_NOEXCEPT
|
||||
{
|
||||
return !p;
|
||||
}
|
||||
|
||||
template <class T, class D, class C>
|
||||
inline bool operator!=(const impl_ptr<T, D, C>& p, std::nullptr_t) SPIMPL_NOEXCEPT
|
||||
{
|
||||
return static_cast<bool>(p);
|
||||
}
|
||||
|
||||
template <class T, class D, class C>
|
||||
inline bool operator!=(std::nullptr_t, const impl_ptr<T, D, C>& p) SPIMPL_NOEXCEPT
|
||||
{
|
||||
return static_cast<bool>(p);
|
||||
}
|
||||
|
||||
template <class T, class D, class C>
|
||||
inline bool operator< (const impl_ptr<T, D, C>& l, std::nullptr_t)
|
||||
{
|
||||
using P = typename impl_ptr<T, D, C>::pointer;
|
||||
return std::less<P>()(l.get(), nullptr);
|
||||
}
|
||||
|
||||
template <class T, class D, class C>
|
||||
inline bool operator< (std::nullptr_t, const impl_ptr<T, D, C>& p)
|
||||
{
|
||||
using P = typename impl_ptr<T, D, C>::pointer;
|
||||
return std::less<P>()(nullptr, p.get());
|
||||
}
|
||||
|
||||
template <class T, class D, class C>
|
||||
inline bool operator> (const impl_ptr<T, D, C>& p, std::nullptr_t)
|
||||
{
|
||||
return nullptr < p;
|
||||
}
|
||||
|
||||
template <class T, class D, class C>
|
||||
inline bool operator> (std::nullptr_t, const impl_ptr<T, D, C>& p)
|
||||
{
|
||||
return p < nullptr;
|
||||
}
|
||||
|
||||
template <class T, class D, class C>
|
||||
inline bool operator<=(const impl_ptr<T, D, C>& p, std::nullptr_t)
|
||||
{
|
||||
return !(nullptr < p);
|
||||
}
|
||||
|
||||
template <class T, class D, class C>
|
||||
inline bool operator<=(std::nullptr_t, const impl_ptr<T, D, C>& p)
|
||||
{
|
||||
return !(p < nullptr);
|
||||
}
|
||||
|
||||
template <class T, class D, class C>
|
||||
inline bool operator>=(const impl_ptr<T, D, C>& p, std::nullptr_t)
|
||||
{
|
||||
return !(p < nullptr);
|
||||
}
|
||||
|
||||
template <class T, class D, class C>
|
||||
inline bool operator>=(std::nullptr_t, const impl_ptr<T, D, C>& p)
|
||||
{
|
||||
return !(nullptr < p);
|
||||
}
|
||||
|
||||
|
||||
template<class T, class... Args>
|
||||
inline impl_ptr<T> make_impl(Args&&... args)
|
||||
{
|
||||
return impl_ptr<T>(new T(std::forward<Args>(args)...), &details::default_delete<T>, &details::default_copy<T>);
|
||||
}
|
||||
|
||||
|
||||
// Helpers to manage unique impl, stored in std::unique_ptr
|
||||
|
||||
template<class T, class Deleter = void(*)(T*)>
|
||||
using unique_impl_ptr = std::unique_ptr<T, Deleter>;
|
||||
|
||||
template<class T, class... Args>
|
||||
inline unique_impl_ptr<T> make_unique_impl(Args&&... args)
|
||||
{
|
||||
static_assert(!std::is_array<T>::value, "unique_impl_ptr does not support arrays");
|
||||
return unique_impl_ptr<T>(new T(std::forward<Args>(args)...), &details::default_delete<T>);
|
||||
}
|
||||
}
|
||||
|
||||
namespace std {
|
||||
template <class T, class D, class C>
|
||||
struct hash<spimpl::impl_ptr<T, D, C>>
|
||||
{
|
||||
using argument_type = spimpl::impl_ptr<T, D, C> ;
|
||||
using result_type = size_t;
|
||||
|
||||
result_type operator()(const argument_type& p) const SPIMPL_NOEXCEPT
|
||||
{
|
||||
return hash<typename argument_type::pointer>()(p.get());
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
#endif // SPIMPL_H_
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
project(eedb_tests)
|
||||
|
||||
add_subdirectory(utils)
|
||||
add_subdirectory(mocks)
|
||||
add_subdirectory(unit)
|
||||
|
||||
@ -1,10 +0,0 @@
|
||||
cmake_minimum_required(VERSION 3.0.2)
|
||||
|
||||
project(Mocks)
|
||||
|
||||
set(TEST_EXECUTABLE_NAME eedb_test_mocks )
|
||||
|
||||
#add test files
|
||||
file(GLOB_RECURSE TEST_FILES *.h* *.cpp )
|
||||
add_library(${TEST_EXECUTABLE_NAME} ${TEST_FILES})
|
||||
set_target_properties(${TEST_EXECUTABLE_NAME} PROPERTIES LINKER_LANGUAGE CXX)
|
||||
@ -3,6 +3,11 @@
|
||||
|
||||
#include <eedb/Session.hpp>
|
||||
|
||||
#include <eedb/db/connection.hpp>
|
||||
|
||||
#include <Wt/Auth/Login.h>
|
||||
#include <Wt/WEnvironment.h>
|
||||
|
||||
namespace eedb {
|
||||
class SessionMock final : public Session {
|
||||
// Session interface
|
||||
|
||||
11
tests/mocks/db/UserDatabaseMock.hpp
Normal file
11
tests/mocks/db/UserDatabaseMock.hpp
Normal file
@ -0,0 +1,11 @@
|
||||
#include <Wt/Auth/AbstractUserDatabase.h>
|
||||
|
||||
class AbstracUserDatabaseMock : public Wt::Auth::AbstractUserDatabase {
|
||||
// AbstractUserDatabase interface
|
||||
public:
|
||||
Wt::Auth::User findWithId(const std::string & id) const {}
|
||||
Wt::Auth::User findWithIdentity(const std::string & provider, const Wt::WString & identity) const {}
|
||||
void addIdentity(const Wt::Auth::User & user, const std::string & provider, const Wt::WString & id) {}
|
||||
Wt::WString identity(const Wt::Auth::User & user, const std::string & provider) const {}
|
||||
void removeIdentity(const Wt::Auth::User & user, const std::string & provider) {}
|
||||
};
|
||||
@ -1,2 +0,0 @@
|
||||
// added only to make cmake happy :(
|
||||
///@todo remove if can
|
||||
@ -12,7 +12,7 @@ class AuthPageMock final : public AuthPage {
|
||||
MOCK_METHOD1(attachTo, void(Wt::WContainerWidget * parent));
|
||||
MOCK_METHOD0(detach, void());
|
||||
|
||||
MOCK_METHOD1(registerNeedVerification, void(std::function< void() > f));
|
||||
MOCK_METHOD1(registerOnNeedVerification, void(std::function< void() > f));
|
||||
MOCK_METHOD1(registerOnUserWeakLogin, void(std::function< void() > f));
|
||||
MOCK_METHOD1(registerOnUserStrongLogin, void(std::function< void() > f));
|
||||
MOCK_METHOD1(registerOnUserLogout, void(std::function< void() > f));
|
||||
|
||||
@ -2,9 +2,8 @@ cmake_minimum_required(VERSION 3.0.2)
|
||||
|
||||
project(Tests LANGUAGES CXX)
|
||||
|
||||
include_directories( . )
|
||||
include_directories( ../ )
|
||||
include_directories( ../../share )
|
||||
|
||||
include_directories( ${eedb_app_SOURCE_DIR} )
|
||||
include_directories( ${gtest_SOURCE_DIR}/include)
|
||||
include_directories( ${gmock_SOURCE_DIR}/include)
|
||||
@ -14,8 +13,10 @@ include_directories( ${gmock_SOURCE_DIR}/include)
|
||||
|
||||
#add test files
|
||||
file(GLOB_RECURSE TEST_FILES test_*.cpp )
|
||||
file(GLOB_RECURSE MOCK_FILES ../mocks/* )
|
||||
|
||||
INCLUDE_DIRECTORIES(${PostgreSQL_INCLUDE_DIRS})
|
||||
add_executable( ${TEST_EXECUTABLE_NAME} ${TEST_FILES})
|
||||
add_executable( ${TEST_EXECUTABLE_NAME} ${TEST_FILES} ${MOCK_FILES} )
|
||||
|
||||
target_link_libraries( ${TEST_EXECUTABLE_NAME} GMock::main wt wttest eedb_db auth ${Boost_LIBRARIES} )
|
||||
|
||||
|
||||
@ -1,8 +1,8 @@
|
||||
#pragma once
|
||||
#include <gmock/gmock.h>
|
||||
|
||||
#include <Wt/Test/WTestEnvironment>
|
||||
#include <Wt/WApplication>
|
||||
#include <Wt/Test/WTestEnvironment.h>
|
||||
#include <Wt/WApplication.h>
|
||||
|
||||
class WidgetTest : public testing::Test {
|
||||
public:
|
||||
|
||||
@ -3,12 +3,15 @@
|
||||
#include "utils/UniquePtrMockWrapper.hpp"
|
||||
|
||||
#include "mocks/SessionMock.hpp"
|
||||
#include "mocks/db/UserDatabaseMock.hpp"
|
||||
#include "mocks/widgets/MainPageMock.hpp"
|
||||
|
||||
#include <Wt/Test/WTestEnvironment.h>
|
||||
#include <Wt/WApplication.h>
|
||||
#include <Wt/WPopupMenu.h>
|
||||
|
||||
#include <eedb/db/connection.hpp>
|
||||
///TODO change to mocked version
|
||||
#include <eedb/auth/Services.hpp>
|
||||
|
||||
#include <eedb/widgets/DefaultAuthPage.hpp>
|
||||
|
||||
@ -21,7 +24,7 @@ class DefaultAuthPageTest : public Test {
|
||||
}
|
||||
|
||||
void createApp() {
|
||||
// auto services = eedb::auth::Services();
|
||||
auto services = eedb::auth::Services();
|
||||
// auto session = std::make_unique< eedb::SessionMock >();
|
||||
|
||||
// const eedb::auth::Services & baseAuth,
|
||||
@ -35,21 +38,24 @@ class DefaultAuthPageTest : public Test {
|
||||
|
||||
// EXPECT_CALL(*session, enviroment()).WillOnce(ReturnRef(env));
|
||||
// sut = new eedb::AuthPageImpl(services, session.login());
|
||||
sut = std::make_unique< eedb::DefaultAuthPage >(services, std::move(usedDatabaseMock), login);
|
||||
}
|
||||
|
||||
protected:
|
||||
Wt::Test::WTestEnvironment env;
|
||||
// Wt::Auth::Login login;
|
||||
Wt::Auth::Login login;
|
||||
|
||||
// std::function< void() > strongLoginCallback;
|
||||
std::unique_ptr<AbstracUserDatabaseMock> usedDatabaseMock;
|
||||
|
||||
std::function< void() > strongLoginCallback;
|
||||
Wt::WApplication app;
|
||||
eedb::DefaultAuthPage * sut;
|
||||
std::unique_ptr< eedb::DefaultAuthPage > sut;
|
||||
};
|
||||
|
||||
TEST_F(DefaultAuthPageTest, createApp) {
|
||||
// auto menu = dynamic_cast< Wt::WPopupMenu * >(eedb->findWidget("home.navigation.session_menu_popup"));
|
||||
// auto menuItem = dynamic_cast< Wt::WMenuItem * >(eedb->findWidget("home.navigation.session_menu.logout"));
|
||||
// menu->triggered().emit(menuItem);
|
||||
auto menu = dynamic_cast< Wt::WPopupMenu * >(app.findWidget("home.navigation.session_menu_popup"));
|
||||
auto menuItem = dynamic_cast< Wt::WMenuItem * >(app.findWidget("home.navigation.session_menu.logout"));
|
||||
// menu->triggered().emit(menuItem);
|
||||
}
|
||||
|
||||
// TEST(a, b) {
|
||||
|
||||
@ -8,6 +8,8 @@
|
||||
#include <eedb/widgets/DefaultHomePage.hpp>
|
||||
#include <eedb/db/connection.hpp>
|
||||
|
||||
#include <Wt/Auth/Login.h>
|
||||
|
||||
class DefaultHomePageTests : public WidgetTest {
|
||||
public:
|
||||
DefaultHomePageTests() : WidgetTest(), sut(std::make_unique< eedb::DefaultHomePage >(session, navigationBar.getPtr())) {
|
||||
|
||||
@ -3,9 +3,10 @@
|
||||
#include "mocks/SessionMock.hpp"
|
||||
#include "mocks/widgets/AuthPageMock.hpp"
|
||||
#include "mocks/widgets/MainPageMock.hpp"
|
||||
#include "utils/UniquePtrMockWrapper.hpp"
|
||||
|
||||
#include <Wt/Test/WTestEnvironment>
|
||||
#include "utils/UniquePtrMockWrapper.hpp"
|
||||
#include <Wt/Auth/Login.h>
|
||||
#include <Wt/Test/WTestEnvironment.h>
|
||||
|
||||
#include <eedb/EEDB.hpp>
|
||||
#include <eedb/Session.hpp>
|
||||
@ -47,10 +48,10 @@ class EedbApplicationTest : public Test {
|
||||
void expectCreateAuthPage() {
|
||||
EXPECT_CALL(authPageFactory, impl()).WillOnce(Return(ByMove(authPage.getPtr())));
|
||||
|
||||
EXPECT_CALL(*authPage, registerNeedVerification(_)).WillOnce(SaveArg< 0 >(&callbacks[0]));
|
||||
EXPECT_CALL(*authPage, registerOnUserWeakLogin(_)).WillOnce(SaveArg< 0 >(&callbacks[1]));
|
||||
EXPECT_CALL(*authPage, registerOnUserStrongLogin(_)).WillOnce(SaveArg< 0 >(&callbacks[2]));
|
||||
EXPECT_CALL(*authPage, registerOnUserLogout(_)).WillOnce(SaveArg< 0 >(&callbacks[3]));
|
||||
EXPECT_CALL(*authPage, registerOnNeedVerification(_)).WillOnce(SaveArg< 0 >(&_needVerificationCB));
|
||||
EXPECT_CALL(*authPage, registerOnUserWeakLogin(_)).WillOnce(SaveArg< 0 >(&_weakLoginCB));
|
||||
EXPECT_CALL(*authPage, registerOnUserStrongLogin(_)).WillOnce(SaveArg< 0 >(&_stringLoginCB));
|
||||
EXPECT_CALL(*authPage, registerOnUserLogout(_)).WillOnce(SaveArg< 0 >(&_userLogoutCB));
|
||||
EXPECT_CALL(*authPage, attachTo(Ne(nullptr)));
|
||||
EXPECT_CALL(*authPage, processEnvironment());
|
||||
}
|
||||
@ -64,7 +65,10 @@ class EedbApplicationTest : public Test {
|
||||
protected:
|
||||
Wt::Test::WTestEnvironment env;
|
||||
|
||||
std::array< std::function< void() >, 4 > callbacks;
|
||||
std::function< void() > _stringLoginCB;
|
||||
std::function< void() > _weakLoginCB;
|
||||
std::function< void() > _userLogoutCB;
|
||||
std::function< void() > _needVerificationCB;
|
||||
|
||||
UniquePtrMockWrapper< eedb::AuthPageMock > authPage;
|
||||
AuthPageFactory authPageFactory;
|
||||
@ -79,10 +83,10 @@ TEST_F(EedbApplicationTest, createApp) {}
|
||||
|
||||
TEST_F(EedbApplicationTest, strongLoginCreatesMainPage) {
|
||||
expectCreateHomePage();
|
||||
callbacks[2]();
|
||||
_stringLoginCB();
|
||||
}
|
||||
|
||||
TEST_F(EedbApplicationTest, weakLoginCreatesMainPage) {
|
||||
expectCreateHomePage();
|
||||
callbacks[1]();
|
||||
_weakLoginCB();
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user