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

Add autotest for computing 'Your answer'

Extract the code to make it testable
parent dbd1a590
ecm_add_test(blanktest.cpp ../src/blankanswer.cpp TEST_NAME blankTest LINK_LIBRARIES Qt::Test)
ecm_add_test(youranswertest.cpp ../src/blankanswer.cpp TEST_NAME yourAnswerTest 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 YourAnswerTest : public QObject
{
Q_OBJECT
private Q_SLOTS:
void testYourAnswer_data();
void testYourAnswer();
};
void YourAnswerTest::testYourAnswer_data()
{
QTest::addColumn<QString>("givenAnswer");
QTest::addColumn<QString>("blankedAnswer");
QTest::addColumn<QString>("expectedYourAnswer");
QTest::addRow("no_blank") << "Hallo"
<< ""
<< "Hallo";
QTest::addRow("one_blank_empty") << ""
<< "One blank .........."
<< "<qt>One blank <u></u></qt>";
QTest::addRow("one_blank_not_empty") << "filled"
<< "One blank .........."
<< "<qt>One blank <u>filled</u></qt>";
QTest::addRow("two_blanks_filled") << "blanks;filled"
<< "Two .......... .........."
<< "<qt>Two <u>blanks</u> <u>filled</u></qt>";
}
void YourAnswerTest::testYourAnswer()
{
QFETCH(QString, givenAnswer);
QFETCH(QString, blankedAnswer);
QFETCH(QString, expectedYourAnswer);
const QString result = BlankAnswer::yourAnswerResult(givenAnswer, blankedAnswer);
QCOMPARE(result, expectedYourAnswer);
}
QTEST_MAIN(YourAnswerTest)
#include "youranswertest.moc"
......@@ -6,6 +6,7 @@
#include "blankanswer.h"
#include <QRegExp>
#include <QStringList>
namespace BlankAnswer
{
......@@ -42,4 +43,35 @@ BlankResult blankAnswer(const QString &input)
return result;
}
QString yourAnswerResult(const QString &givenAnswer, const QString &blankedAnswer)
{
QString result = givenAnswer;
if (!blankedAnswer.isEmpty()) {
result.clear();
QStringList ls;
if (givenAnswer.indexOf(';') > 0)
ls = givenAnswer.split(';');
else
ls.append(givenAnswer);
result = QString(blankedAnswer).replace(QLatin1String(".........."), QLatin1String("<u></u>"));
int offset = 0, counter = 0;
while (offset >= 0) {
offset = result.indexOf(QStringLiteral("<u>"), offset);
if (offset >= 0) {
result.insert(offset + 3, ls[counter]);
offset++;
counter++;
}
}
result.append("</qt>");
result.prepend("<qt>");
}
return result;
}
}; // namespace BlankAnswer
......@@ -17,6 +17,9 @@ struct BlankResult {
};
BlankResult blankAnswer(const QString &input);
QString yourAnswerResult(const QString &givenAnswer, const QString &blankedAnswer);
}; // namespace BlankAnswer
#endif
......@@ -258,36 +258,9 @@ QString KWQQuizModel::quizIcon(QuizIcon ico)
return s;
}
QString KWQQuizModel::yourAnswer(const QString & s)
QString KWQQuizModel::yourAnswer(const QString &givenAnswer) const
{
QString result = s;
if (!m_answerBlank.isEmpty()) {
result.clear();
QStringList ls;
if (s.indexOf(';') > 0)
ls = s.split(';');
else
ls.append(s);
result = m_answerBlank.replace(QLatin1String(".........."), QLatin1String("<u></u>"));
int offset = 0, counter = 0;
while (offset >= 0) {
offset = result.indexOf(QStringLiteral("<u>"), offset);
if (offset >= 0) {
result.insert(offset + 3, ls[counter]);
offset++;
counter++;
}
}
result.append("</qt>");
result.prepend("<qt>");
}
return result;
return BlankAnswer::yourAnswerResult(givenAnswer, m_answerBlank);
}
......
......@@ -41,7 +41,7 @@ public:
bool hasErrors();
QStringList multiOptions();
QString quizIcon(QuizIcon ico);
QString yourAnswer(const QString & );
QString yourAnswer(const QString &) const;
QString hint();
Prefs::EnumStartSession::type quizType() const {return m_quizType;}
......
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