Commit 8a9864c6 authored by Laurent Montel's avatar Laurent Montel 😁
Browse files

Port QRegexp + add new autotest for it

parent 687bda9e
......@@ -119,6 +119,9 @@ add_definitions(-DQT_NO_FOREACH)
set(CMAKE_CXX_STANDARD 14)
configure_file(knotes-version.h.cmake ${CMAKE_CURRENT_BINARY_DIR}/knotes-version.h @ONLY)
if(BUILD_TESTING)
add_definitions(-DBUILD_TESTING)
endif()
add_subdirectory(noteshared)
add_subdirectory(src)
......
......@@ -2,3 +2,8 @@ add_executable(noteeditorutilstest noteeditorutilstest.cpp )
add_test(NAME noteeditorutilstest COMMAND noteeditorutilstest)
ecm_mark_as_test(noteeditorutilstest)
target_link_libraries(noteeditorutilstest Qt5::Test notesharedprivate)
add_executable(noteutilstest noteutilstest.cpp )
add_test(NAME noteutilstest COMMAND noteutilstest)
ecm_mark_as_test(noteutilstest)
target_link_libraries(noteutilstest Qt5::Test notesharedprivate)
/*
Copyright (C) 2020 Laurent Montel <montel@kde.org>
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; see the file COPYING. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
#include "noteutilstest.h"
#include "noteutils.h"
#include <QTest>
QTEST_MAIN(NoteUtilsTest)
NoteUtilsTest::NoteUtilsTest(QObject *parent)
: QObject(parent)
{
}
void NoteUtilsTest::testExtractNoteText_data()
{
QTest::addColumn<QString>("titleAddons");
QTest::addColumn<QString>("noteText");
QTest::addColumn<QString>("resultNoteTitle");
QTest::addColumn<QString>("resultNoteText");
QTest::newRow("empty") << QString() << QString() << QString() << QString();
QTest::newRow("test1") << QStringLiteral("foo") << QStringLiteral("bla\r\nfoo") << QStringLiteral("blafoo") << QStringLiteral("foo");
QTest::newRow("test2") << QString() << QStringLiteral("bla\r\nfoo") << QStringLiteral("bla") << QStringLiteral("foo");
QTest::newRow("test3") << QString() << QStringLiteral("blafoo") << QStringLiteral("blafoo") << QStringLiteral("blafoo");
}
void NoteUtilsTest::testExtractNoteText()
{
QFETCH(QString, titleAddons);
QFETCH(QString, noteText);
QFETCH(QString, resultNoteTitle);
QFETCH(QString, resultNoteText);
NoteShared::NoteUtils noteUtils;
NoteShared::NoteUtils::NoteText result = noteUtils.extractNoteText(noteText, titleAddons);
QCOMPARE(result.noteText, resultNoteText);
QCOMPARE(result.noteTitle, resultNoteTitle);
}
/*
Copyright (C) 2020 Laurent Montel <montel@kde.org>
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; see the file COPYING. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
#ifndef NOTEUTILSTEST_H
#define NOTEUTILSTEST_H
#include <QObject>
class NoteUtilsTest : public QObject
{
Q_OBJECT
public:
explicit NoteUtilsTest(QObject *parent = nullptr);
~NoteUtilsTest() = default;
private Q_SLOTS:
void testExtractNoteText_data();
void testExtractNoteText();
};
#endif // NOTEUTILSTEST_H
......@@ -40,6 +40,7 @@
#include <QTimer>
#include <QTextCodec>
#include "noteshared_debug.h"
#include "noteutils.h"
#include <QLocale>
// Maximum note size in chars we are going to accept,
......@@ -144,16 +145,11 @@ void NotesNetworkReceiver::slotConnectionClosed()
QTextCodec *codec = QTextCodec::codecForLocale();
if (d->m_timer->isActive()) {
QString noteText = QString(codec->toUnicode(*d->m_buffer)).trimmed();
// First line is the note title or, in case of ATnotes, the id
const int pos = noteText.indexOf(QRegExp(QLatin1String("[\r\n]")));
const QString noteTitle = noteText.left(pos).trimmed() + d->m_titleAddon;
noteText = noteText.mid(pos).trimmed();
if (!noteText.isEmpty()) {
Q_EMIT sigNoteReceived(noteTitle, noteText);
const QString noteText = QString(codec->toUnicode(*d->m_buffer)).trimmed();
NoteUtils utils;
const NoteUtils::NoteText result = utils.extractNoteText(noteText, d->m_titleAddon);
if (!result.noteText.isEmpty()) {
Q_EMIT sigNoteReceived(result.noteTitle, result.noteText);
}
}
......
/* This file is part of the KDE project
Copyright (C) 2020 Laurent Montel <montel@kde.org>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
#ifndef NOTESHAREDPRIVATE_EXPORT_H
#define NOTESHAREDPRIVATE_EXPORT_H
#include "noteshared_export.h"
/* Classes which are exported only for unit tests */
#ifdef BUILD_TESTING
#ifndef NOTESHARED_TESTS_EXPORT
#define NOTESHARED_TESTS_EXPORT NOTESHARED_EXPORT
# endif
#else /* not compiling tests */
#define NOTESHARED_TESTS_EXPORT
#endif
#endif
......@@ -27,6 +27,7 @@
#include <KMime/KMimeMessage>
#include <QRegularExpression>
#include <QPointer>
#include <QApplication>
#include <QSslSocket>
......@@ -145,3 +146,15 @@ QString NoteUtils::createToolTip(const Akonadi::Item &item)
}
return tip;
}
NoteUtils::NoteText NoteUtils::extractNoteText(QString noteText, const QString &titleAddon)
{
const int pos = noteText.indexOf(QRegularExpression(QLatin1String("[\r\n]")));
const QString noteTitle = noteText.left(pos).trimmed() + titleAddon;
noteText = noteText.mid(pos).trimmed();
NoteUtils::NoteText noteTextResult;
noteTextResult.noteText = noteText;
noteTextResult.noteTitle = noteTitle;
return noteTextResult;
}
......@@ -27,10 +27,15 @@ namespace NoteShared {
class NOTESHARED_EXPORT NoteUtils
{
public:
struct NoteText {
QString noteTitle;
QString noteText;
};
NoteUtils();
bool sendToMail(QWidget *parent, const QString &title, const QString &message);
Q_REQUIRED_RESULT bool sendToMail(QWidget *parent, const QString &title, const QString &message);
void sendToNetwork(QWidget *parent, const QString &title, const QString &message);
QString createToolTip(const Akonadi::Item &item);
Q_REQUIRED_RESULT QString createToolTip(const Akonadi::Item &item);
Q_REQUIRED_RESULT NoteText extractNoteText(QString noteText, const QString &titleAddon);
};
}
......
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