File indexing completed on 2024-04-14 04:35:56

0001 /*
0002     SPDX-FileCopyrightText: 2023 Volker Krause <vkrause@kde.org>
0003     SPDX-License-Identifier: LGPL-2.0-or-later
0004 */
0005 
0006 #include <osm/datatypes.h>
0007 #include <osm/element.h>
0008 #include <osm/io.h>
0009 #include <osm/languages.h>
0010 
0011 #include <QFile>
0012 #include <QLocale>
0013 #include <QTest>
0014 #include <QtPlugin>
0015 
0016 QDebug operator<<(QDebug debug, const OSM::Languages &langs)
0017 {
0018     QStringList l;
0019     std::transform(langs.languages.begin(), langs.languages.end(), std::back_inserter(l), [](const std::string &s) { return QString::fromStdString(s); });
0020     debug << l;
0021     return debug;
0022 }
0023 
0024 Q_IMPORT_PLUGIN(OSM_XmlIOPlugin)
0025 
0026 class LocalizedTagTest : public QObject
0027 {
0028     Q_OBJECT
0029 private Q_SLOTS:
0030     void testLocalizedLookup()
0031     {
0032         QFile f(QStringLiteral(SOURCE_DIR "/data/localizedtags.xml"));
0033         QVERIFY(f.open(QFile::ReadOnly));
0034 
0035         OSM::DataSet dataSet;
0036         auto p = OSM::IO::readerForFileName(f.fileName(), &dataSet);
0037         p->read(&f);
0038         QCOMPARE(dataSet.nodes.size(), 1);
0039 
0040         QLocale l(QLocale::English, QLocale::LatinScript, QLocale::UnitedStates);
0041         auto langs = OSM::Languages::fromQLocale(l);
0042 
0043         const OSM::Element e(&dataSet.nodes[0]);
0044         QCOMPARE(e.tagValue(langs, "name"), "english");
0045         QCOMPARE(e.tagValue(langs, "name:disused"), "subtag english");
0046         QCOMPARE(e.tagValue(langs, "name:local"), "no local english");
0047 
0048         l = QLocale(QLocale::Korean, QLocale::HanScript, QLocale::SouthKorea);
0049         langs = OSM::Languages::fromQLocale(l);
0050         QCOMPARE(e.tagValue(langs, "name"), "korean");
0051         QCOMPARE(e.tagValue(langs, "name:disused"), "subtag local");
0052         QVERIFY(e.tagValue(langs, "name:local").startsWith("no local "));
0053 
0054         l = QLocale(QStringLiteral("sr-Latn-RS"));
0055         langs = OSM::Languages::fromQLocale(l);
0056         qDebug() << l.uiLanguages() << langs;
0057         QCOMPARE(e.tagValue(langs, "name"), "romanized serbian");
0058         QCOMPARE(e.tagValue(langs, "name:disused"), "subtag serbian");
0059 
0060         l = QLocale(QStringLiteral("ja-JP"));
0061         langs = OSM::Languages::fromQLocale(l);
0062         QCOMPARE(e.tagValue(langs, "name"), "local");
0063         QCOMPARE(e.tagValue(langs, "name:disused"), "subtag local");
0064     }
0065 };
0066 
0067 QTEST_GUILESS_MAIN(LocalizedTagTest)
0068 
0069 #include "localizedtagtest.moc"