File indexing completed on 2022-09-20 12:43:21

0001 /*
0002     SPDX-FileCopyrightText: 2021 Volker Krause <vkrause@kde.org>
0003     SPDX-License-Identifier: LGPL-2.0-or-later
0004 */
0005 
0006 #include <KLazyLocalizedString>
0007 #include <KLocalizedString>
0008 
0009 #include <QTest>
0010 
0011 #include <cstring>
0012 
0013 class KLazyLocalizedStringTest : public QObject
0014 {
0015     Q_OBJECT
0016 private Q_SLOTS:
0017     void testImplicitConversionToLocalizedString()
0018     {
0019         // this has to compile
0020         KLocalizedString s = kli18n("message");
0021         s = kli18nc("context", "message");
0022         s = kli18np("singular", "plural");
0023         s = kli18ncp("context", "singular", "plural");
0024         s = klxi18n("message");
0025         s = klxi18nc("context", "message");
0026         s = klxi18np("singular", "plural");
0027         s = klxi18ncp("context", "singular", "plural");
0028 
0029         // this should not compile
0030         // s = kli18n(QStringLiteral("message").toUtf8().constData());
0031         // auto s2 = new KLazyLocalizedString("bla", "blub", "foo", false);
0032     }
0033 
0034     void testEmpty()
0035     {
0036         KLazyLocalizedString ls;
0037         QVERIFY(ls.isEmpty());
0038 
0039         KLocalizedString s = ls;
0040         QVERIFY(s.isEmpty());
0041     }
0042 
0043     void testStaticMessageTable()
0044     {
0045         struct {
0046             int someProperty;
0047             KLazyLocalizedString msg;
0048         } static constexpr const msg_table[] = {
0049             {0, kli18n("message")},
0050             {1, kli18nc("context", "message")},
0051             {2, kli18np("singular", "plural")},
0052             {3, kli18ncp("context", "singular", "plural")},
0053             {4, klxi18n("message")},
0054             {5, klxi18nc("context", "message")},
0055             {6, klxi18np("singular", "plural")},
0056             {7, klxi18ncp("context", "singular", "plural")},
0057         };
0058 
0059         // direct access
0060         for (const auto &entry : msg_table) {
0061             QVERIFY(!entry.msg.toString().isEmpty());
0062         }
0063 
0064         // storing in a local variable
0065         int max = 0;
0066         KLazyLocalizedString ls;
0067         for (const auto &entry : msg_table) {
0068             if (entry.someProperty > max) {
0069                 max = entry.someProperty;
0070                 ls = entry.msg;
0071             }
0072         }
0073         QVERIFY(!ls.toString().isEmpty());
0074         QCOMPARE(std::strcmp(ls.untranslatedText(), "singular"), 0);
0075     }
0076 };
0077 
0078 QTEST_GUILESS_MAIN(KLazyLocalizedStringTest)
0079 
0080 #include "klazylocalizedstringtest.moc"