Commit d67d2e24 authored by Laurent Montel's avatar Laurent Montel 😁

Improve++ + start to create parser

parent 51bb0bac
add_definitions( -DGRAMMALECTE_DATA_DIR="${CMAKE_CURRENT_SOURCE_DIR}/data")
set(kmail_grammalecteplugin_debug_SRCS)
macro(add_kmail_grammalecte_unittest _source _additional)
......
{
"data": [
{
"iParagraph": 1,
"lGrammarErrors": [
],
"lSpellingErrors": [
]
}
],
"grammalecte": "1.0",
"lang": "fr"
}
{
"data": [
{
"iParagraph": 1,
"lGrammarErrors": [
],
"lSpellingErrors": [
]
},
{
"iParagraph": 2,
"lGrammarErrors": [
],
"lSpellingErrors": [
]
}
],
"grammalecte": "1.0",
"lang": "fr"
}
......@@ -20,6 +20,7 @@
#include "grammalecteparsertest.h"
#include "grammalecteparser.h"
#include <QJsonDocument>
#include <QTest>
QTEST_MAIN(GrammalecteParserTest)
......@@ -28,3 +29,27 @@ GrammalecteParserTest::GrammalecteParserTest(QObject *parent)
{
}
void GrammalecteParserTest::shouldParseJson_data()
{
QTest::addColumn<QString>("fileName");
QTest::addColumn<int>("numberOfElement");
QTest::newRow("noerror") << QStringLiteral("noerror") << 0;
QTest::newRow("noerror2") << QStringLiteral("noerror2") << 0;
}
void GrammalecteParserTest::shouldParseJson()
{
QFETCH(QString, fileName);
QFETCH(int, numberOfElement);
const QString originalJsonFile = QLatin1String(GRAMMALECTE_DATA_DIR) + QLatin1Char('/') + fileName + QStringLiteral(".json");
QFile f(originalJsonFile);
QVERIFY(f.open(QIODevice::ReadOnly));
const QByteArray content = f.readAll();
f.close();
const QJsonDocument doc = QJsonDocument::fromJson(content);
const QJsonObject fields = doc.object();
GrammalecteParser parser;
QCOMPARE(parser.parseResult(fields).count(), numberOfElement);
}
......@@ -28,6 +28,9 @@ class GrammalecteParserTest : public QObject
public:
explicit GrammalecteParserTest(QObject *parent = nullptr);
~GrammalecteParserTest() = default;
private Q_SLOTS:
void shouldParseJson_data();
void shouldParseJson();
};
#endif // GRAMMALECTEPARSERTEST_H
......@@ -24,6 +24,8 @@
GrammalecteConfigWidget::GrammalecteConfigWidget(QWidget *parent)
: QWidget(parent)
{
QVBoxLayout *mainLayout = new QVBoxLayout(this);
mainLayout->setObjectName(QStringLiteral("mainlayout"));
}
......
......@@ -92,10 +92,23 @@ void GrammalecteInfo::setSuggestions(const QStringList &suggestions)
bool GrammalecteInfo::isValid() const
{
//TODO
if ((mEnd != -1) && (mBegin != -1))
return true;
return false;
}
void GrammalecteInfo::parse()
void GrammalecteInfo::parse(const QJsonObject &obj)
{
//TODO
}
QDebug operator <<(QDebug d, const GrammalecteInfo &t)
{
d << "mError: " << t.error();
d << "Start: " << t.begin();
d << "End: " << t.end();
d << "BlockId: " << t.blockId();
d << "Color: " << t.color().name();
d << "Suggestion: " << t.suggestions();
return d;
}
......@@ -23,6 +23,7 @@
#include <QObject>
#include <QColor>
#include <QString>
#include <QDebug>
#include "libgrammalect_private_export.h"
class LIBGRAMMALECTPRIVATE_TESTS_EXPORT GrammalecteInfo
{
......@@ -49,7 +50,7 @@ public:
Q_REQUIRED_RESULT bool isValid() const;
void parse();
void parse(const QJsonObject &obj);
private:
QStringList mSuggestions;
......@@ -61,5 +62,5 @@ private:
};
Q_DECLARE_TYPEINFO(GrammalecteInfo, Q_MOVABLE_TYPE);
LIBKMAILGRAMMALECT_EXPORT QDebug operator <<(QDebug d, const GrammalecteInfo &t);
#endif // GRAMMALECTEINFO_H
......@@ -18,6 +18,10 @@
*/
#include "grammalecteparser.h"
#include "grammalecteplugin_debug.h"
#include <QJsonArray>
#include <QJsonObject>
#include <QJsonValue>
GrammalecteParser::GrammalecteParser()
{
......@@ -31,5 +35,19 @@ GrammalecteParser::~GrammalecteParser()
QVector<GrammalecteInfo> GrammalecteParser::parseResult(const QJsonObject &obj) const
{
return {};
QVector<GrammalecteInfo> infos;
QJsonArray array = obj.value(QLatin1String("data")).toArray();
for (const QJsonValue &current : array) {
if (current.type() == QJsonValue::Object) {
const QJsonObject grammaObject = current.toObject();
GrammalecteInfo info;
info.parse(grammaObject);
if (info.isValid()) {
infos.append(info);
}
} else {
qCWarning(KMAIL_EDITOR_GRAMMALECTE_PLUGIN_LOG) << "Problem when parsing grammalecte result" << current;
}
}
return infos;
}
......@@ -19,6 +19,7 @@
#include "grammarresultjob.h"
#include "grammalecteplugin_debug.h"
#include <QProcess>
GrammarResultJob::GrammarResultJob(QObject *parent)
: QObject(parent)
......
......@@ -37,7 +37,7 @@ public:
void setText(const QString &text);
Q_SIGNALS:
void finished();
void finished(const QString &result);
void error();
private:
......
......@@ -32,7 +32,10 @@ GrammarResultTextEdit::~GrammarResultTextEdit()
}
void GrammarResultTextEdit::checkGrammar()
void GrammarResultTextEdit::applyGrammarResult(const QVector<GrammalecteInfo> &infos)
{
//TODO
for (const GrammalecteInfo &info : infos) {
//TODO
}
//TOOD
}
......@@ -21,6 +21,7 @@
#define GRAMMARRESULTTEXTEDIT_H
#include <QTextEdit>
#include "grammalecteinfo.h"
#include "libgrammalect_private_export.h"
class LIBGRAMMALECTPRIVATE_TESTS_EXPORT GrammarResultTextEdit : public QTextEdit
{
......@@ -29,7 +30,7 @@ public:
explicit GrammarResultTextEdit(QWidget *parent = nullptr);
~GrammarResultTextEdit();
void checkGrammar();
void applyGrammarResult(const QVector<GrammalecteInfo> &infos);
};
#endif // GRAMMARRESULTTEXTEDIT_H
......@@ -44,7 +44,7 @@ void GrammarResultWidget::setText(const QString &str)
mResult->setText(str);
}
void GrammarResultWidget::checkGrammar()
void GrammarResultWidget::applyGrammarResult(const QVector<GrammalecteInfo> &infos)
{
mResult->checkGrammar();
mResult->applyGrammarResult(infos);
}
......@@ -22,6 +22,7 @@
#include <QWidget>
#include "libgrammalect_private_export.h"
#include "grammalecteinfo.h"
class GrammarResultTextEdit;
class LIBGRAMMALECTPRIVATE_TESTS_EXPORT GrammarResultWidget : public QWidget
{
......@@ -30,7 +31,7 @@ public:
explicit GrammarResultWidget(QWidget *parent = nullptr);
~GrammarResultWidget();
void setText(const QString &str);
void checkGrammar();
void applyGrammarResult(const QVector<GrammalecteInfo> &infos);
private:
GrammarResultTextEdit *mResult = nullptr;
};
......
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