tests/auto/linguist/lrelease/tst_lrelease.cpp
changeset 0 1918ee327afb
child 3 41300fa6a67c
equal deleted inserted replaced
-1:000000000000 0:1918ee327afb
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the Qt Linguist of the Qt Toolkit.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 
       
    42 #include <QtCore/QDir>
       
    43 #include <QtCore/QDebug>
       
    44 #include <QtCore/QFile>
       
    45 #include <QtCore/QByteArray>
       
    46 
       
    47 #include <QtTest/QtTest>
       
    48 
       
    49 class tst_lrelease : public QObject
       
    50 {
       
    51     Q_OBJECT
       
    52 
       
    53 public:
       
    54     tst_lrelease() : binDir(QLibraryInfo::location(QLibraryInfo::BinariesPath)) {}
       
    55 
       
    56 private:
       
    57 
       
    58 private slots:
       
    59     void translate();
       
    60     void mixedcodecs();
       
    61     void compressed();
       
    62     void idbased();
       
    63     void dupes();
       
    64 
       
    65 private:
       
    66     void doCompare(const QStringList &actual, const QString &expectedFn);
       
    67 
       
    68     QString binDir;
       
    69 };
       
    70 
       
    71 void tst_lrelease::doCompare(const QStringList &actual, const QString &expectedFn)
       
    72 {
       
    73     QFile file(expectedFn);
       
    74     QVERIFY(file.open(QIODevice::ReadOnly));
       
    75     QStringList expected = QString(file.readAll()).trimmed().remove('\r').split('\n');
       
    76 
       
    77     int i = 0, ei = expected.size(), gi = actual.size();
       
    78     for (; ; i++) {
       
    79         if (i == gi) {
       
    80             if (i == ei)
       
    81                 return;
       
    82             gi = 0;
       
    83             break;
       
    84         } else if (i == ei) {
       
    85             ei = 0;
       
    86             break;
       
    87         } else if (!QRegExp(expected.at(i)).exactMatch(actual.at(i))) {
       
    88             while ((ei - 1) >= i && (gi - 1) >= i &&
       
    89                      (QRegExp(expected.at(ei - 1)).exactMatch(actual.at(gi - 1))))
       
    90                 ei--, gi--;
       
    91             break;
       
    92         }
       
    93     }
       
    94     QByteArray diff;
       
    95     for (int j = qMax(0, i - 3); j < i; j++)
       
    96         diff += expected.at(j) + '\n';
       
    97     diff += "<<<<<<< got\n";
       
    98     for (int j = i; j < gi; j++) {
       
    99         diff += actual.at(j) + '\n';
       
   100         if (j >= i + 5) {
       
   101             diff += "...\n";
       
   102             break;
       
   103         }
       
   104     }
       
   105     diff += "=========\n";
       
   106     for (int j = i; j < ei; j++) {
       
   107         diff += expected.at(j) + '\n';
       
   108         if (j >= i + 5) {
       
   109             diff += "...\n";
       
   110             break;
       
   111         }
       
   112     }
       
   113     diff += ">>>>>>> expected\n";
       
   114     for (int j = ei; j < qMin(ei + 3, expected.size()); j++)
       
   115         diff += expected.at(j) + '\n';
       
   116     QFAIL(qPrintable("Output for " + expectedFn + " does not meet expectations:\n" + diff));
       
   117 }
       
   118 
       
   119 void tst_lrelease::translate()
       
   120 {
       
   121     QVERIFY(!QProcess::execute(binDir + "/lrelease testdata/translate.ts"));
       
   122 
       
   123     QTranslator translator;
       
   124     QVERIFY(translator.load("testdata/translate.qm"));
       
   125     qApp->installTranslator(&translator);
       
   126 
       
   127     QCOMPARE(QObject::tr("\nnewline at the start"), QString("\nNEWLINE AT THE START"));
       
   128     QCOMPARE(QObject::tr("newline at the end\n"),   QString("NEWLINE AT THE END\n"));
       
   129     QCOMPARE(QObject::tr("newline and space at the end\n "),   QString("NEWLINE AND SPACE AT THE END\n "));
       
   130     QCOMPARE(QObject::tr("space and newline at the end \n"),   QString("SPACE AND NEWLINE AT THE END \n"));
       
   131     QCOMPARE(QObject::tr("\ttab at the start and newline at the end\n"),   QString("\tTAB AT THE START AND NEWLINE AT THE END\n"));
       
   132     QCOMPARE(QObject::tr("\n\tnewline and tab at the start"), QString("\n\tNEWLINE AND TAB AT THE START"));
       
   133     QCOMPARE(QObject::tr(" \tspace and tab at the start"), QString(" \tSPACE AND TAB AT THE START"));
       
   134     QCOMPARE(QObject::tr(" string that does not exist"), QString(" string that does not exist"));
       
   135 
       
   136     QCOMPARE(QCoreApplication::translate("CubeForm", "Test"), QString::fromAscii("BBBB"));
       
   137     QCOMPARE(QCoreApplication::translate("", "Test", "Empty context"), QString("AAAA"));
       
   138 
       
   139     // Test plurals
       
   140     QString txed = QCoreApplication::translate("Plurals", "There are %n houses", 0, QCoreApplication::UnicodeUTF8, 0);
       
   141     QCOMPARE(QString::fromAscii("[%1]").arg(txed), QString("[There are 0 houses]"));
       
   142     QCOMPARE(QCoreApplication::translate("Plurals", "There are %n houses", 0, QCoreApplication::UnicodeUTF8, 1), QString("There is 1 house"));
       
   143     QCOMPARE(QCoreApplication::translate("Plurals", "There are %n houses", 0, QCoreApplication::UnicodeUTF8, 2), QString("There are 2 houses"));
       
   144     QCOMPARE(QCoreApplication::translate("Plurals", "There are %n houses", 0, QCoreApplication::UnicodeUTF8, 3), QString("There are 3 houses"));
       
   145 
       
   146 
       
   147     // More plurals
       
   148     QCOMPARE(tr("There are %n cars", "More Plurals", 0) , QString("There are 0 cars"));
       
   149     QCOMPARE(tr("There are %n cars", "More Plurals", 1) , QString("There is 1 car"));
       
   150     QCOMPARE(tr("There are %n cars", "More Plurals", 2) , QString("There are 2 cars"));
       
   151     QCOMPARE(tr("There are %n cars", "More Plurals", 3) , QString("There are 3 cars"));
       
   152 
       
   153 
       
   154     QCOMPARE(QCoreApplication::translate("no_en", "Kj\370r K\345re, kj\346re"), QString::fromAscii("Drive K\345re, dear"));
       
   155     QCOMPARE(QCoreApplication::translate("en_no", "Drive K\345re, dear"), QString::fromAscii("Kj\370r K\345re, kj\346re"));
       
   156     QCOMPARE(QCoreApplication::translate("en_ch", "Chinese symbol:"), QString::fromAscii("Chinese symbol:%1").arg(QChar(0x7c1f)));
       
   157 
       
   158 //    printf("halo\r\nhallo");
       
   159   //  QCOMPARE(tr("This\r\nwill fail"), QString("THIS\nWILL FAIL"));    // \r\n =  0d 0a
       
   160 
       
   161     QCOMPARE(tr("Completely random string"),
       
   162              QString::fromLatin1("Super-lange Uebersetzung mit Schikanen\x9c"
       
   163                                  "Mittlere Uebersetung\x9c"
       
   164                                  "Kurze Uebers."));
       
   165 
       
   166     qApp->removeTranslator(&translator);
       
   167 }
       
   168 
       
   169 void tst_lrelease::mixedcodecs()
       
   170 {
       
   171     QVERIFY(!QProcess::execute(binDir + "/lrelease testdata/mixedcodecs-ts11.ts"));
       
   172     QVERIFY(!QProcess::execute(binDir + "/lrelease testdata/mixedcodecs-ts20.ts"));
       
   173     QVERIFY(!QProcess::execute("cmp testdata/mixedcodecs-ts11.qm testdata/mixedcodecs-ts20.qm"));
       
   174     QTranslator translator;
       
   175     QVERIFY(translator.load("testdata/mixedcodecs-ts11.qm"));
       
   176     qApp->installTranslator(&translator);
       
   177 
       
   178     QCOMPARE(QCoreApplication::translate("FooBar", "this contains an umlaut \xfc &uuml;"),
       
   179              QString::fromAscii("random stuff with umlaut"));
       
   180     QCOMPARE(QCoreApplication::translate("FooBar", "umlaut \xc3\xbc &uuml; in utf8"),
       
   181              QString::fromAscii("more random stuff with umlaut"));
       
   182 }
       
   183 
       
   184 void tst_lrelease::compressed()
       
   185 {
       
   186     QVERIFY(!QProcess::execute(binDir + "/lrelease -compress testdata/compressed.ts"));
       
   187 
       
   188     QTranslator translator;
       
   189     QVERIFY(translator.load("testdata/compressed.qm"));
       
   190     qApp->installTranslator(&translator);
       
   191 
       
   192     QCOMPARE(QCoreApplication::translate("Context1", "Foo"), QString::fromAscii("in first context"));
       
   193     QCOMPARE(QCoreApplication::translate("Context2", "Bar"), QString::fromAscii("in second context"));
       
   194 
       
   195     QCOMPARE(QCoreApplication::translate("Action1", "Component Name"), QString::fromAscii("translation in first context"));
       
   196     QCOMPARE(QCoreApplication::translate("Action2", "Component Name"), QString::fromAscii("translation in second context"));
       
   197     QCOMPARE(QCoreApplication::translate("Action3", "Component Name"), QString::fromAscii("translation in third context"));
       
   198 
       
   199 }
       
   200 
       
   201 void tst_lrelease::idbased()
       
   202 {
       
   203     QVERIFY(!QProcess::execute(binDir + "/lrelease -idbased testdata/idbased.ts"));
       
   204 
       
   205     QTranslator translator;
       
   206     QVERIFY(translator.load("testdata/idbased.qm"));
       
   207     qApp->installTranslator(&translator);
       
   208 
       
   209     QCOMPARE(qtTrId("test_id"), QString::fromAscii("This is a test string."));
       
   210     QCOMPARE(qtTrId("untranslated_id"), QString::fromAscii("This has no translation."));
       
   211 }
       
   212 
       
   213 void tst_lrelease::dupes()
       
   214 {
       
   215     QProcess proc;
       
   216     proc.start(binDir + "/lrelease testdata/dupes.ts");
       
   217     QVERIFY(proc.waitForFinished());
       
   218     QVERIFY(proc.exitStatus() == QProcess::NormalExit);
       
   219     doCompare(QString(proc.readAllStandardError()).trimmed().remove('\r').split('\n'), "testdata/dupes.errors");
       
   220 }
       
   221 
       
   222 QTEST_MAIN(tst_lrelease)
       
   223 #include "tst_lrelease.moc"