File indexing completed on 2022-09-27 12:38:59

0001 /*
0002     SPDX-FileCopyrightText: 2021 Andreas Cord-Landwehr <cordlandwehr@kde.org>
0003     SPDX-License-Identifier: LGPL-2.1-or-later
0004 */
0005 
0006 #include "translateshelltest.h"
0007 #include "translateshelladapter.h"
0008 #include <QProcess>
0009 #include <QSignalSpy>
0010 
0011 // LC_ALL=C trans -l en -s en -t de homework -show-alternatives=n -show-original=n -show-languages=n -show-original-dictionary=n -no-warn
0012 static const char *s_homework_en_de = R"""(Hausaufgaben
0013 
0014 Definitions of homework
0015 
0016 noun
0017     (die) Hausaufgaben
0018         homework
0019     (die) Heimarbeit
0020         homework, outwork
0021     (die) Hausaufgabe
0022         homework, homework assignment, assignment
0023     (die) Hausarbeit
0024         housework, homework, homework assignment, assignment
0025     (die) Aufgabe
0026         task, duty, mission, job, function, homework
0027 )""";
0028 
0029 // translation request of untranslatable string
0030 // LC_ALL=C trans -l en -s en -t de FOOBAA -show-alternatives=n -show-original=n -show-languages=n -show-original-dictionary=n -no-warn
0031 static const char *s_foobaa_en_de = R"""(FOOBAA
0032 
0033 Translations of FOOBAA
0034 )""";
0035 
0036 // translation request of string with with special char, where warning "Did you mean: homework" is skipped with argument
0037 // LC_ALL=C trans -l en -s en -t de FOOBAA -show-alternatives=n -show-original=n -show-languages=n -show-original-dictionary=n -no-warn
0038 static const char *s_homework_special_char_en_de = R"""(_Hausaufgaben
0039 
0040 Definitions of _homework
0041 
0042 noun
0043     (die) Hausaufgaben
0044         homework
0045     (die) Heimarbeit
0046         homework, outwork
0047     (die) Hausaufgabe
0048         homework, homework assignment, assignment
0049     (die) Hausarbeit
0050         housework, homework, homework assignment, assignment
0051     (die) Aufgabe
0052         task, duty, mission, job, function, homework
0053 )""";
0054 
0055 // LC_ALL=C trans -l en -s en -t de run -show-alternatives=n -show-original=n -show-languages=n -show-original-dictionary=n -no-warn
0056 static const char *s_run_en_de = R"""(Lauf
0057 
0058 Definitions of run
0059 
0060 noun
0061     (der) Run
0062         run
0063     (der) Lauf
0064         running, run, course, barrel, race, operation
0065     (die) Auflage
0066         edition, circulation, rest, run, overlay, condition
0067     (die) Fahrt
0068         ride, trip, journey, tour, run, voyage
0069     (die) Laufzeit
0070         term, period of validity, operational time, running time, run-time
0071     (die) Strecke
0072         route, distance, track, line, stretch, run
0073     (die) Serie
0074         series, serial, set, run, string, succession
0075     (der) Ansturm
0076         rush, stampede, onslaught, run, onrush, crowd
0077     (die) Folge
0078         episode, result, sequence, consequence, order, succession
0079     (die) Reihe
0080         series, row, number, range, set, line
0081     (die) Sequenz
0082         sequence, progression, run, flush
0083     (das) Gehege
0084         enclosure, reserve, pen, preserve, compound, run
0085     (der) Hühnerhof
0086         run
0087     (die) Masche
0088         mesh, stitch, ploy, trick, pitch, run
0089     (die) Laufmasche
0090         run, ladder
0091     (die) Spielzeit
0092         season, playing time, period, run, inning
0093     (der) Ausflug
0094         tour, trip, excursion, outing, flight, hike
0095     (die) Tendenz
0096         trend, tendency, bias, direction, drift, run
0097     (der) Flug
0098         flight
0099 
0100 verb
0101     laufen
0102         run, walk, go, operate, work, race
0103     verlaufen
0104         run, proceed, go off
0105     starten
0106         start, launch, take off, set off, power up, blast off
0107     führen
0108         lead, run, carry, guide, conduct, keep
0109     ablaufen
0110         run, expire, drain off, run out, pass, go off
0111     fahren
0112         drive, ride, run, move, pass, motor
0113     rennen
0114         race, run, sprint
0115     durchführen
0116         lead through, take through, run through, carry out, implement, run
0117     leiten
0118         guide, conduct, lead, direct, manage, run
0119     verkehren
0120         operate, run, associate, have intercourse, consort, haunt
0121     auslaufen
0122         leak, run out, run, drain, leak out, sail
0123     fließen
0124         flow, pass, run, move, stream, circulate
0125     unterhalten
0126         support, maintain, keep, operate, entertain, amuse
0127     verwalten
0128         manage, administer, maintain, govern, hold, conduct
0129     ausgehen
0130         go out, go, start, assume, emanate, run out
0131     rinnen
0132         run, stream, pour
0133     halten
0134         keep, hold, maintain, stop, stick, stay
0135     steuern
0136         control, manage, steer, drive, navigate, run
0137     abwickeln
0138         unwind, complete, carry out, transact, unroll, uncoil
0139     schmeißen
0140         throw, chuck, fling, sling, slam, bung
0141     zerfließen
0142         melt away, run
0143     färben
0144         color, dye, colour
0145     verfließen
0146         pass, run, go by, become blurred
0147     tropfen
0148         drop, drip, leak
0149     einlassen
0150         admit, let in, embed, set, run, bed in
0151     tröpfeln
0152         dribble, trickle, drip, run
0153     triefen
0154         drip, ooze, run, water, be dripping wet
0155     plagen
0156         plague, afflict, torment, infest, harass, run
0157 )""";
0158 
0159 // LC_ALL=C trans -l en -s en -t de nice -show-alternatives=n -show-original=n -show-languages=n -show-original-dictionary=n -no-warn
0160 static const char *s_nice_en_de = R"""(nett
0161 
0162 Definitions of nice
0163 
0164 adjective
0165     schön
0166         beautiful, nice, lovely, good, pretty, fine
0167     nett
0168         nice, cute, kind, lovely, pleasant, neat
0169     gut
0170         good, fine, nice, solid, beneficial, sharp
0171     hübsch
0172         pretty, nice, lovely, fine, neat, comely
0173     sauber
0174         clean, cleanly, neat, tidy, nice, fresh
0175     fein
0176         fine, delicate, nice, subtle, sensitive, elegant
0177     lieb
0178         dear, sweet, kind, nice, good, lovely
0179     sympathisch
0180         nice, pleasant, likable, simpatico
0181     lecker
0182         delicious, yummy, nice, scrumptious, lovely, savory
0183     genau
0184         exact, accurate, precise, close, specific, detailed
0185     heikel
0186         delicate, tricky, difficult, awkward, fussy, ticklish
0187     pingelig
0188         picky, fussy, finicky, niggling, over-particular, nit-picking
0189     anspruchsvoll
0190         demanding, exacting, fastidious, ambitious, discriminating, challenging
0191 )""";
0192 
0193 // C_ALL=C trans -l en -s en -t de "this is a dog" -show-alternatives=y -show-original=n -show-languages=n -show-original-dictionary=n -no-warn
0194 static const char *s_sentence_dog_en_de = R"""(das ist ein Hund
0195 
0196 Translations of this is a dog
0197 
0198 this is a dog
0199     das ist ein Hund, dies ist ein Hund
0200 )""";
0201 
0202 void TranslateShellTest::translationShellOutputParserTest()
0203 {
0204     {
0205         TranslateShellAdapter::Translation result = TranslateShellAdapter::parseTranslateShellResult(QString(s_homework_en_de));
0206         QCOMPARE(result.m_suggestions, QStringList{"Hausaufgaben"});
0207     }
0208     {
0209         TranslateShellAdapter::Translation result = TranslateShellAdapter::parseTranslateShellResult(QString(s_foobaa_en_de));
0210         QCOMPARE(result.m_suggestions, QStringList{"FOOBAA"});
0211     }
0212     {
0213         TranslateShellAdapter::Translation result = TranslateShellAdapter::parseTranslateShellResult(QString(s_homework_special_char_en_de));
0214         QCOMPARE(result.m_suggestions, QStringList{"_Hausaufgaben"});
0215     }
0216     {
0217         TranslateShellAdapter::Translation result = TranslateShellAdapter::parseTranslateShellResult(QString(s_run_en_de));
0218         QCOMPARE(result.m_suggestions, QStringList{"Lauf"});
0219     }
0220     {
0221         TranslateShellAdapter::Translation result = TranslateShellAdapter::parseTranslateShellResult(QString(s_nice_en_de));
0222         QCOMPARE(result.m_suggestions, QStringList{"nett"});
0223     }
0224     {
0225         TranslateShellAdapter::Translation result = TranslateShellAdapter::parseTranslateShellResult(QString(s_sentence_dog_en_de));
0226         QCOMPARE(result.m_suggestions, QStringList{"das ist ein Hund"});
0227     }
0228 }
0229 
0230 void TranslateShellTest::translateShellProcessInteractionTest()
0231 {
0232     // important: we cannot rely on correct answers from the webservice, which may stop answering our requests
0233     // after a surprisingly small number of API interactions
0234     QFuture<TranslateShellAdapter::Translation> translation = TranslateShellAdapter::translateAsync("haus", "de", "en");
0235     translation.waitForFinished();
0236     if (translation.result().m_suggestions.size() == 0) {
0237         QWARN("did not receive any translation results");
0238         return;
0239     }
0240     if (translation.result().m_suggestions.first() != "House") { // translation "haus" -> "House" is expected to be stable
0241         QWARN("translation result differes from expectation");
0242     }
0243 }
0244 
0245 QTEST_GUILESS_MAIN(TranslateShellTest);