Commit fe51710c authored by Nicolas Fella's avatar Nicolas Fella
Browse files

Add autotest for blank parsing

It is non-trivial string manipulation and eventually needs to be ported to QRegularExpression

Better have a test for it
parent 8b24f760
Pipeline #256033 passed with stage
in 49 seconds
......@@ -28,6 +28,7 @@ include(KDECompilerSettings NO_POLICY_SCOPE)
include(ECMInstallIcons)
include(ECMAddAppIcon)
include(ECMSetupVersion)
include(ECMAddTests)
find_package(Qt${QT_MAJOR_VERSION} ${REQUIRED_QT_VERSION} CONFIG REQUIRED Core Widgets PrintSupport)
......@@ -57,6 +58,12 @@ add_definitions(-DQT_NO_URL_CAST_FROM_STRING)
add_subdirectory(doc)
add_subdirectory(src)
if(BUILD_TESTING)
find_package(Qt${QT_MAJOR_VERSION}Test REQUIRED)
add_subdirectory(autotests)
endif()
ki18n_install(po)
if (KF5DocTools_FOUND)
kdoctools_install(po)
......
ecm_add_test(blanktest.cpp ../src/blankanswer.cpp TEST_NAME blankTest LINK_LIBRARIES Qt::Test)
/*
SPDX-FileCopyrightText: 2022 Nicolas Fella <nicolas.fella@gmx.de>
SPDX-License-Identifier: GPL-2.0-or-later
*/
#include <QTest>
#include "../src/blankanswer.h"
class BlankTest : public QObject
{
Q_OBJECT
private Q_SLOTS:
void testBlank_data();
void testBlank();
};
void BlankTest::testBlank_data()
{
QTest::addColumn<QString>("input");
QTest::addColumn<QString>("blankedAnswer");
QTest::addColumn<QString>("correctAnswer");
QTest::addRow("empty") << ""
<< ""
<< "";
QTest::addRow("no_blank") << "Just a sentence"
<< ""
<< "";
QTest::addRow("one_blank") << "This is [one] test"
<< "This is .......... test"
<< "one";
QTest::addRow("two_blank") << "This is [one] test with [two] blanks"
<< "This is .......... test with .......... blanks"
<< "one; two";
QTest::addRow("two_words") << "[Two words] in one blank"
<< ".......... in one blank"
<< "Two words";
}
void BlankTest::testBlank()
{
QFETCH(QString, input);
QFETCH(QString, blankedAnswer);
QFETCH(QString, correctAnswer);
BlankAnswer::BlankResult result = BlankAnswer::blankAnswer(input);
QCOMPARE(result.blankedAnswer, blankedAnswer);
QCOMPARE(result.correctAnswer, correctAnswer);
}
QTEST_MAIN(BlankTest)
#include "blanktest.moc"
......@@ -16,6 +16,7 @@ include_directories(
ecm_setup_version(${RELEASE_SERVICE_VERSION} VARIABLE_PREFIX KWORDQUIZ VERSION_HEADER kwordquiz_version.h)
set(kwordquiz_SRCS
blankanswer.cpp
dlglanguage.cpp
dlglanguage.h
dlgspecchar.cpp
......
/*
SPDX-FileCopyrightText: 2008-2010 Peter Hedlund <peter.hedlund@kdemail.net>
SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "blankanswer.h"
#include <QRegExp>
namespace BlankAnswer
{
BlankResult blankAnswer(const QString &input)
{
BlankResult result;
QString r;
QString tTemp = input;
r = tTemp;
QRegExp rx;
rx.setMinimal(true);
rx.setPattern(QStringLiteral("\\[.*\\]"));
r.replace(rx, QStringLiteral(".........."));
if (r != tTemp) {
result.blankedAnswer = r;
int offset = 0;
while (offset >= 0) {
offset = rx.indexIn(tTemp, offset);
if (offset >= 0) {
if (result.correctAnswer.length() > 0)
result.correctAnswer = result.correctAnswer + "; " + tTemp.mid(offset + 1, tTemp.indexOf(']', offset) - offset - 1);
else
result.correctAnswer = tTemp.mid(offset + 1, tTemp.indexOf(']', offset) - offset - 1);
offset++;
}
}
}
return result;
}
}; // namespace BlankAnswer
/*
SPDX-FileCopyrightText: 2008-2010 Peter Hedlund <peter.hedlund@kdemail.net>
SPDX-License-Identifier: GPL-2.0-or-later
*/
#ifndef BLANKANSWER_H
#define BLANKANSWER_H
#include <QString>
namespace BlankAnswer
{
struct BlankResult {
QString blankedAnswer;
QString correctAnswer;
};
BlankResult blankAnswer(const QString &input);
}; // namespace BlankAnswer
#endif
......@@ -5,9 +5,10 @@
#include "kwqquizmodel.h"
#include "blankanswer.h"
#include "krandomsequence.h"
#include "kwqsortfiltermodel.h"
#include "kwqtablemodel.h"
#include "krandomsequence.h"
KWQQuizModel::KWQQuizModel(QObject *parent) : QSortFilterProxyModel(parent)
{
......@@ -321,43 +322,23 @@ QString KWQQuizModel::question()
return s;
}
QString KWQQuizModel::blankAnswer()
{
QString r = QLatin1String("");
m_correctBlank.clear();
m_answerBlank.clear();
QString tTemp;
if (m_quizType == Prefs::EnumStartSession::QA && Prefs::enableBlanks()) {
int row = m_list.at(m_currentQuestion);
tTemp = data(index(qAbs(row), column(row), QModelIndex()), Qt::DisplayRole).toString();
r = tTemp;
QRegExp rx;
rx.setMinimal(true);
rx.setPattern(QStringLiteral("\\[.*\\]"));
r.replace(rx, QStringLiteral(".........."));
if (r != tTemp) {
m_answerBlank = r;
int offset = 0;
while (offset >= 0) {
offset = rx.indexIn(tTemp, offset);
if (offset >= 0) {
if (m_correctBlank.length() > 0)
m_correctBlank = m_correctBlank + "; " + tTemp.mid(offset + 1, tTemp.indexOf(']', offset) - offset - 1);
else
m_correctBlank = tTemp.mid(offset + 1, tTemp.indexOf(']', offset) - offset - 1);
offset++;
}
}
}
int row = m_list.at(m_currentQuestion);
const QString input = data(index(qAbs(row), column(row), QModelIndex()), Qt::DisplayRole).toString();
const BlankAnswer::BlankResult result = BlankAnswer::blankAnswer(input);
m_answerBlank = result.blankedAnswer;
m_correctBlank = result.correctAnswer;
}
return m_answerBlank;
}
QString KWQQuizModel::answer()
{
int row = m_list.at(m_currentQuestion);
......
Supports Markdown
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