Commit 6bd4bdf1 authored by Laurent Montel's avatar Laurent Montel 😁
Browse files

Improve job

parent 84714d95
......@@ -19,6 +19,7 @@
#include "confirmaddresscheckjobtest.h"
#include "../confirmaddresscheckjob.h"
#include <QTest>
ConfirmAddressCheckJobTest::ConfirmAddressCheckJobTest(QObject *parent)
......@@ -32,4 +33,81 @@ ConfirmAddressCheckJobTest::~ConfirmAddressCheckJobTest()
}
void ConfirmAddressCheckJobTest::shouldHaveEmptyResultByDefault()
{
ConfirmAddressCheckJob job;
job.start();
QVERIFY(job.invalidEmails().isEmpty());
QVERIFY(job.validEmails().isEmpty());
}
void ConfirmAddressCheckJobTest::shouldReturnEmptyResultWhenNoAddress()
{
ConfirmAddressCheckJob job;
const QStringList domains{QStringLiteral("foo.com"), QStringLiteral("bla.com")};
const QStringList whiteList{QStringLiteral("foo@kde.org"), QStringLiteral("bla@kde.org")};
job.setCheckSettings(domains, whiteList);
job.start();
QVERIFY(job.invalidEmails().isEmpty());
QVERIFY(job.validEmails().isEmpty());
}
void ConfirmAddressCheckJobTest::shouldReturnAllValidEmailsDomain()
{
ConfirmAddressCheckJob job;
const QStringList domains{QStringLiteral("foo.com"), QStringLiteral("bla.com")};
const QStringList whiteList{QStringLiteral("foo@kde.org"), QStringLiteral("bla@kde.org")};
const QStringList emails{QStringLiteral("toto@foo.com"), QStringLiteral("blabla@foo.com")};
job.setCheckSettings(domains, whiteList);
job.setAddressList(emails);
job.start();
QVERIFY(job.invalidEmails().isEmpty());
QVERIFY(!job.validEmails().isEmpty());
QCOMPARE(job.validEmails(), emails);
}
void ConfirmAddressCheckJobTest::shouldReturnAllInValidEmailsDomain()
{
ConfirmAddressCheckJob job;
const QStringList domains{QStringLiteral("foo.fr"), QStringLiteral("bla.com")};
const QStringList whiteList{QStringLiteral("foo@kde.org"), QStringLiteral("bla@kde.org")};
const QStringList emails{QStringLiteral("toto@foo.com"), QStringLiteral("blabla@foo.com")};
job.setCheckSettings(domains, whiteList);
job.setAddressList(emails);
job.start();
QVERIFY(!job.invalidEmails().isEmpty());
QVERIFY(job.validEmails().isEmpty());
QCOMPARE(job.invalidEmails(), emails);
}
void ConfirmAddressCheckJobTest::shouldReturnValidAndInvalid()
{
ConfirmAddressCheckJob job;
const QStringList domains{QStringLiteral("foo.fr"), QStringLiteral("bla.com")};
const QStringList whiteList{QStringLiteral("foo@kde.org"), QStringLiteral("bla@kde.org")};
const QStringList emails{QStringLiteral("toto@bla.com"), QStringLiteral("blabla@foo.com")};
job.setCheckSettings(domains, whiteList);
job.setAddressList(emails);
job.start();
QVERIFY(!job.invalidEmails().isEmpty());
QVERIFY(!job.validEmails().isEmpty());
QCOMPARE(job.invalidEmails(), QStringList() << QStringLiteral("blabla@foo.com"));
QCOMPARE(job.validEmails(), QStringList() << QStringLiteral("toto@bla.com"));
}
void ConfirmAddressCheckJobTest::shouldNotDuplicateValue()
{
ConfirmAddressCheckJob job;
const QStringList domains{QStringLiteral("foo.com"), QStringLiteral("bla.com")};
const QStringList whiteList{QStringLiteral("foo@kde.org"), QStringLiteral("bla@kde.org")};
const QStringList emails{QStringLiteral("toto@foo.com"), QStringLiteral("blabla@foo.com")};
job.setCheckSettings(domains, whiteList);
job.setAddressList(QStringList() << emails << emails);
job.start();
QVERIFY(job.invalidEmails().isEmpty());
QVERIFY(!job.validEmails().isEmpty());
QCOMPARE(job.validEmails(), emails);
}
QTEST_MAIN(ConfirmAddressCheckJobTest)
......@@ -29,6 +29,13 @@ class ConfirmAddressCheckJobTest : public QObject
public:
explicit ConfirmAddressCheckJobTest(QObject *parent = Q_NULLPTR);
~ConfirmAddressCheckJobTest();
private Q_SLOTS:
void shouldHaveEmptyResultByDefault();
void shouldReturnEmptyResultWhenNoAddress();
void shouldReturnAllValidEmailsDomain();
void shouldReturnAllInValidEmailsDomain();
void shouldReturnValidAndInvalid();
void shouldNotDuplicateValue();
};
#endif // CONFIRMADDRESSCHECKJOBTEST_H
......@@ -23,3 +23,65 @@ ConfirmAddressCheckJob::ConfirmAddressCheckJob()
{
}
ConfirmAddressCheckJob::~ConfirmAddressCheckJob()
{
}
void ConfirmAddressCheckJob::start()
{
bool foundValidEmail = false;
Q_FOREACH (const QString &email, mAddressList) {
if (email.isEmpty()) {
continue;
}
foundValidEmail = false;
Q_FOREACH(const QString &domain, mDomains) {
if (email.contains(domain)) {
if (!mValidEmails.contains(email)) {
mValidEmails.append(email);
}
foundValidEmail = true;
break;
}
}
if (!foundValidEmail) {
Q_FOREACH(const QString &whiteEmail, mWhiteEmails) {
if (email.contains(whiteEmail)) {
if (!mValidEmails.contains(email)) {
mValidEmails.append(email);
}
foundValidEmail = true;
break;
}
}
}
if (!foundValidEmail) {
if (!mInvalidEmails.contains(email)) {
mInvalidEmails.append(email);
}
}
}
}
void ConfirmAddressCheckJob::setCheckSettings(const QStringList &domains, const QStringList &whiteEmails)
{
mDomains = domains;
mWhiteEmails = whiteEmails;
}
void ConfirmAddressCheckJob::setAddressList(const QStringList &addressList)
{
mAddressList = addressList;
}
QStringList ConfirmAddressCheckJob::validEmails() const
{
return mValidEmails;
}
QStringList ConfirmAddressCheckJob::invalidEmails() const
{
return mInvalidEmails;
}
......@@ -27,6 +27,24 @@ class ConfirmAddressCheckJob
{
public:
ConfirmAddressCheckJob();
~ConfirmAddressCheckJob();
void start();
void setCheckSettings(const QStringList &domains, const QStringList &whiteEmails);
void setAddressList(const QStringList &addressList);
QStringList validEmails() const;
QStringList invalidEmails() const;
private:
QStringList mAddressList;
QStringList mValidEmails;
QStringList mInvalidEmails;
QStringList mDomains;
QStringList mWhiteEmails;
};
#endif // CONFIRMADDRESSCHECKJOB_H
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment