Implement QPromise::tap(handler)

This `handler` allows to observe the value of the `input` promise, without changing the propagated value. The `output` promise will be resolved with the same value as the `input` promise (the `handler` returned value will be ignored). However, if `handler` throws, `output` is rejected with the new exception. Unlike `finally`, this handler is not called for rejections.
This commit is contained in:
Simon Brunel
2017-08-23 18:12:57 +02:00
parent 25d2bad54f
commit c55fa03e7b
5 changed files with 183 additions and 0 deletions

View File

@ -15,6 +15,7 @@ private Q_SLOTS:
void valueReject();
void valueThen();
void valueFinally();
void valueTap();
void valueDelayed();
void errorReject();
void errorThen();
@ -224,6 +225,36 @@ void tst_benchmark::valueFinally()
}
}
void tst_benchmark::valueTap()
{
{ // should not copy the value on continutation if fulfilled
int value = -1;
Data::logs().reset();
QPromise<Data>::resolve(Data(42)).tap([&](const Data& res) {
value = res.value();
}).wait();
QCOMPARE(Data::logs().ctor, 1);
QCOMPARE(Data::logs().copy, 0);
QCOMPARE(Data::logs().move, 1); // move value to the input and output promise data
QCOMPARE(Data::logs().refs, 0);
QCOMPARE(value, 42);
}
{ // should not create value on continutation if rejected
int value = -1;
Data::logs().reset();
QPromise<Data>::reject(QString("foo")).tap([&](const Data& res) {
value = res.value();
}).wait();
QCOMPARE(Data::logs().ctor, 0);
QCOMPARE(Data::logs().copy, 0);
QCOMPARE(Data::logs().move, 0);
QCOMPARE(Data::logs().refs, 0);
QCOMPARE(value, -1);
}
}
void tst_benchmark::errorReject()
{
{ // should create one copy of the error when rejected by rvalue