Reorganize exceptions and add unit tests

This commit is contained in:
Simon Brunel 2019-02-23 11:42:02 +01:00
parent 6110cd40d3
commit 700098ef7b
8 changed files with 76 additions and 20 deletions

View File

@ -2,7 +2,7 @@
#define QTPROMISE_QPROMISE_H
#include "qpromise_p.h"
#include "qpromiseerror.h"
#include "qpromiseexceptions.h"
#include "qpromiseglobal.h"
#include "qpromiseresolver.h"

View File

@ -1,5 +1,5 @@
#ifndef QTPROMISE_QPROMISEERROR_H
#define QTPROMISE_QPROMISEERROR_H
#ifndef QTPROMISE_QPROMISEEXCEPTIONS_H
#define QTPROMISE_QPROMISEEXCEPTIONS_H
#include "qpromise_p.h"
#include "qpromiseglobal.h"
@ -9,6 +9,16 @@
namespace QtPromise {
class QPromiseCanceledException : public QException
{
public:
void raise() const Q_DECL_OVERRIDE { throw *this; }
QPromiseCanceledException* clone() const Q_DECL_OVERRIDE
{
return new QPromiseCanceledException(*this);
}
};
class QPromiseTimeoutException : public QException
{
public:
@ -37,4 +47,4 @@ using QPromiseError = QtPromisePrivate::PromiseError;
} // namespace QtPromise
#endif // QTPROMISE_QPROMISEERROR_H
#endif // QTPROMISE_QPROMISEEXCEPTIONS_H

View File

@ -1,24 +1,12 @@
#ifndef QTPROMISE_QPROMISEFUTURE_P_H
#define QTPROMISE_QPROMISEFUTURE_P_H
#include "qpromiseexceptions.h"
// Qt
#include <QFutureWatcher>
#include <QFuture>
namespace QtPromise {
class QPromiseCanceledException : public QException
{
public:
void raise() const Q_DECL_OVERRIDE { throw *this; }
QPromiseCanceledException* clone() const Q_DECL_OVERRIDE
{
return new QPromiseCanceledException(*this);
}
};
} // namespace QtPromise
namespace QtPromisePrivate {
template <typename T>

View File

@ -1,7 +1,7 @@
#ifndef QTPROMISE_QPROMISERESOLVER_H
#define QTPROMISE_QPROMISERESOLVER_H
#include "qpromiseerror.h"
#include "qpromiseexceptions.h"
// Qt
#include <QExplicitlySharedDataPointer>

View File

@ -2,7 +2,7 @@ HEADERS += \
$$PWD/qpromise.h \
$$PWD/qpromise.inl \
$$PWD/qpromise_p.h \
$$PWD/qpromiseerror.h \
$$PWD/qpromiseexceptions.h \
$$PWD/qpromisefuture.h \
$$PWD/qpromiseglobal.h \
$$PWD/qpromisehelpers.h \

View File

@ -0,0 +1,5 @@
QT += concurrent
TARGET = tst_exceptions
SOURCES += $$PWD/tst_exceptions.cpp
include(../qtpromise.pri)

View File

@ -0,0 +1,52 @@
#include "../shared/utils.h"
// QtPromise
#include <QtPromise>
// Qt
#include <QtConcurrent>
#include <QtTest>
using namespace QtPromise;
class tst_exceptions : public QObject
{
Q_OBJECT
private Q_SLOTS:
void canceled();
void timeout();
void undefined();
}; // class tst_exceptions
QTEST_MAIN(tst_exceptions)
#include "tst_exceptions.moc"
namespace {
template <class E>
void verify()
{
auto p = qPromise(QtConcurrent::run([]() { throw E(); }));
QCOMPARE(p.isPending(), true);
QCOMPARE(waitForRejected<E>(p), true);
QCOMPARE(p.isRejected(), true);
}
} // anonymous namespace
void tst_exceptions::canceled()
{
verify<QPromiseCanceledException>();
}
void tst_exceptions::timeout()
{
verify<QPromiseTimeoutException>();
}
void tst_exceptions::undefined()
{
verify<QPromiseUndefinedException>();
}

View File

@ -1,6 +1,7 @@
TEMPLATE = subdirs
SUBDIRS += \
benchmark \
exceptions \
future \
helpers \
qpromise \