Changeset View
Changeset View
Standalone View
Standalone View
autotests/test_highlighter.cpp
- This file was added.
1 | // krazy:excludeall=spelling | ||||
---|---|---|---|---|---|
2 | /** | ||||
3 | * Copyright (C) 2017 David Faure <faure@kde.org> | ||||
4 | * | ||||
5 | * This library is free software; you can redistribute it and/or | ||||
6 | * modify it under the terms of the GNU Lesser General Public | ||||
7 | * License as published by the Free Software Foundation; either | ||||
8 | * version 2.1 of the License, or (at your option) any later version. | ||||
9 | * | ||||
10 | * This library is distributed in the hope that it will be useful, | ||||
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||||
13 | * Lesser General Public License for more details. | ||||
14 | * | ||||
15 | * You should have received a copy of the GNU Lesser General Public | ||||
16 | * License along with this library; if not, write to the Free Software | ||||
17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA | ||||
18 | * 02110-1301 USA | ||||
19 | */ | ||||
20 | | ||||
21 | #include "highlighter.h" | ||||
22 | #include "speller.h" | ||||
23 | | ||||
24 | #include <QPlainTextEdit> | ||||
25 | #include <QStandardPaths> | ||||
26 | #include <QDebug> | ||||
27 | #include <QObject> | ||||
28 | #include <qtest.h> | ||||
29 | | ||||
30 | using namespace Sonnet; | ||||
31 | | ||||
32 | class HighlighterTest : public QObject | ||||
33 | { | ||||
34 | Q_OBJECT | ||||
35 | | ||||
36 | private Q_SLOTS: | ||||
37 | void initTestCase(); | ||||
38 | void testEnglish(); | ||||
39 | void testFrench(); | ||||
40 | void testMultipleLanguages(); | ||||
41 | }; | ||||
42 | | ||||
43 | void HighlighterTest::initTestCase() | ||||
44 | { | ||||
45 | QStandardPaths::setTestModeEnabled(true); | ||||
46 | | ||||
47 | Speller speller(QStringLiteral("en_US")); | ||||
48 | if (!speller.availableBackends().contains(QLatin1String("ASpell"))) { | ||||
49 | QSKIP("ASpell not available"); | ||||
50 | } | ||||
51 | // Doing this here affects all the highlighters created later on, due to the weird hidden "Settings" class saving stuff behind the API's back... | ||||
52 | speller.setDefaultClient(QStringLiteral("ASpell")); | ||||
53 | if (!speller.availableLanguages().contains(QLatin1String("en"))) { | ||||
54 | QSKIP("'en' not available"); | ||||
55 | } | ||||
56 | | ||||
57 | // How weird to have to do this here and not with the Highlighter API.... | ||||
58 | speller.setAttribute(Speller::AutoDetectLanguage, true); | ||||
59 | } | ||||
60 | | ||||
61 | static const char s_englishSentence[] = "Hello helo this is the highlighter test enviroment guvernment"; // words from test_suggest.cpp | ||||
62 | | ||||
63 | void HighlighterTest::testEnglish() | ||||
64 | { | ||||
65 | // GIVEN | ||||
66 | QPlainTextEdit textEdit; | ||||
67 | textEdit.setPlainText(QString::fromLatin1(s_englishSentence)); | ||||
68 | Sonnet::Highlighter highlighter(&textEdit); | ||||
69 | highlighter.setCurrentLanguage(QStringLiteral("en")); | ||||
70 | QVERIFY(highlighter.spellCheckerFound()); | ||||
71 | highlighter.rehighlight(); | ||||
72 | QTextCursor cursor(textEdit.document()); | ||||
73 | | ||||
74 | // WHEN | ||||
75 | cursor.setPosition(6); | ||||
76 | const QStringList suggestionsForHelo = highlighter.suggestionsForWord(QStringLiteral("helo"), cursor); | ||||
77 | cursor.setPosition(40); | ||||
78 | const QStringList suggestionsForEnviroment = highlighter.suggestionsForWord(QStringLiteral("enviroment"), cursor); | ||||
79 | | ||||
80 | // THEN | ||||
81 | QCOMPARE(suggestionsForHelo.count(), 10); | ||||
82 | QVERIFY2(suggestionsForHelo.contains(QStringLiteral("hello")), qPrintable(suggestionsForHelo.join(QLatin1Char(',')))); | ||||
83 | QVERIFY2(suggestionsForEnviroment.contains(QStringLiteral("environment")), qPrintable(suggestionsForEnviroment.join(QLatin1Char(',')))); | ||||
84 | } | ||||
85 | | ||||
86 | static const char s_frenchSentence[] = "Bnjour est un bon mot pour tester le dictionnare."; | ||||
87 | | ||||
88 | void HighlighterTest::testFrench() | ||||
89 | { | ||||
90 | // GIVEN | ||||
91 | QPlainTextEdit textEdit; | ||||
92 | textEdit.setPlainText(QString::fromLatin1(s_frenchSentence)); | ||||
93 | Sonnet::Highlighter highlighter(&textEdit); | ||||
94 | highlighter.setCurrentLanguage(QStringLiteral("fr_FR")); | ||||
95 | QVERIFY(highlighter.spellCheckerFound()); | ||||
96 | highlighter.rehighlight(); | ||||
97 | QTextCursor cursor(textEdit.document()); | ||||
98 | | ||||
99 | // WHEN | ||||
100 | cursor.setPosition(0); | ||||
101 | const QStringList suggestionsForBnjour = highlighter.suggestionsForWord(QStringLiteral("Bnjour"), cursor); | ||||
102 | cursor.setPosition(37); | ||||
103 | const QStringList suggestionsForDict = highlighter.suggestionsForWord(QStringLiteral("dictionnare"), cursor); | ||||
104 | | ||||
105 | // THEN | ||||
106 | QVERIFY2(suggestionsForBnjour.contains(QStringLiteral("Bonjour")), qPrintable(suggestionsForBnjour.join(QLatin1Char(',')))); | ||||
107 | QVERIFY2(suggestionsForDict.contains(QStringLiteral("dictionnaire")), qPrintable(suggestionsForDict.join(QLatin1Char(',')))); | ||||
108 | } | ||||
109 | | ||||
110 | void HighlighterTest::testMultipleLanguages() | ||||
111 | { | ||||
112 | // GIVEN | ||||
113 | QPlainTextEdit textEdit; | ||||
114 | const QString englishSentence = QString::fromLatin1(s_englishSentence) + QLatin1Char('\n'); | ||||
115 | textEdit.setPlainText(englishSentence + QString::fromLatin1(s_frenchSentence)); | ||||
116 | Sonnet::Highlighter highlighter(&textEdit); | ||||
117 | highlighter.rehighlight(); | ||||
118 | QTextCursor cursor(textEdit.document()); | ||||
119 | | ||||
120 | // WHEN | ||||
121 | cursor.setPosition(6); | ||||
122 | const QStringList suggestionsForHelo = highlighter.suggestionsForWord(QStringLiteral("helo"), cursor); | ||||
123 | cursor.setPosition(40); | ||||
124 | const QStringList suggestionsForEnviroment = highlighter.suggestionsForWord(QStringLiteral("enviroment"), cursor); | ||||
125 | cursor.setPosition(englishSentence.size()); | ||||
126 | const QStringList suggestionsForBnjour = highlighter.suggestionsForWord(QStringLiteral("Bnjour"), cursor); | ||||
127 | cursor.setPosition(englishSentence.size() + 37); | ||||
128 | const QStringList suggestionsForDict = highlighter.suggestionsForWord(QStringLiteral("dictionnare"), cursor); | ||||
129 | | ||||
130 | // THEN | ||||
131 | QVERIFY2(suggestionsForHelo.contains(QStringLiteral("hello")), qPrintable(suggestionsForHelo.join(QLatin1Char(',')))); | ||||
132 | QVERIFY2(suggestionsForEnviroment.contains(QStringLiteral("environment")), qPrintable(suggestionsForEnviroment.join(QLatin1Char(',')))); | ||||
133 | QVERIFY2(suggestionsForBnjour.contains(QStringLiteral("Bonjour")), qPrintable(suggestionsForBnjour.join(QLatin1Char(',')))); | ||||
134 | QVERIFY2(suggestionsForDict.contains(QStringLiteral("dictionnaire")), qPrintable(suggestionsForDict.join(QLatin1Char(',')))); | ||||
135 | } | ||||
136 | | ||||
137 | QTEST_MAIN(HighlighterTest) | ||||
138 | | ||||
139 | #include "test_highlighter.moc" |