File indexing completed on 2024-04-28 07:47:31

0001 /*
0002  *   SPDX-FileCopyrightText: 2009 Petri Damstén <damu@iki.fi>
0003  *
0004  *   SPDX-License-Identifier: LGPL-2.0-or-later
0005  */
0006 
0007 #include "valuetest.h"
0008 
0009 #include <QStandardPaths>
0010 
0011 using namespace KUnitConversion;
0012 
0013 void ValueTest::initTestCase()
0014 {
0015     qputenv("KF5UNITCONVERT_NO_DOWNLOAD", "1");
0016     QStandardPaths::setTestModeEnabled(true);
0017     QLocale::setDefault(QLocale::c());
0018 
0019     v1 = Value(3.1415, Kilometer);
0020     v2 = Value(6.1415, QStringLiteral("m"));
0021     v3 = Value(9.1415, v1.unit());
0022 }
0023 
0024 void ValueTest::testStrings()
0025 {
0026     QCOMPARE(v1.unit().symbol(), QStringLiteral("km"));
0027     QCOMPARE(v2.toSymbolString(), QStringLiteral("6.1415 m"));
0028     QCOMPARE(v3.toString(), QStringLiteral("9.1415 kilometers"));
0029 }
0030 
0031 void ValueTest::testRound()
0032 {
0033     v1.round(2);
0034     QCOMPARE(v1.number(), 3.14);
0035 }
0036 
0037 void ValueTest::testConvert()
0038 {
0039     v1 = v1.convertTo(Meter);
0040     QCOMPARE(v1.number(), 3140.0);
0041     v1 = v1.convertTo(QStringLiteral("cm"));
0042     QCOMPARE(v1.number(), 314000.0);
0043     v1 = v1.convertTo(v2.unit());
0044     QCOMPARE(v1.number(), 3140.0);
0045 }
0046 
0047 void ValueTest::testInvalid()
0048 {
0049     v1 = v1.convertTo(UnitId(99999));
0050     QCOMPARE(v1.number(), 0.0);
0051     QCOMPARE(v1.toSymbolString(), QLatin1String(""));
0052     v2 = v2.convertTo(QStringLiteral("don't exist"));
0053     QCOMPARE(v2.number(), 0.0);
0054     QCOMPARE(v2.toSymbolString(), QLatin1String(""));
0055 }
0056 
0057 void ValueTest::testCurrencyNotDownloaded()
0058 {
0059     // ensure that no local conversion table is available
0060     const QString cache = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + QStringLiteral("/libkunitconversion/currency.xml");
0061     if (QFile::exists(cache)) {
0062         QFile::remove(cache);
0063     }
0064 
0065     auto pounds = Value(100, Gbp);
0066     auto eur = pounds.convertTo(Eur);
0067     QVERIFY(!eur.isValid());
0068 }
0069 
0070 QTEST_MAIN(ValueTest)
0071 
0072 #include "moc_valuetest.cpp"