1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
|
/*
* SPDX-FileCopyrightText: 2009 Petri Damstén <damu@iki.fi>
*
* SPDX-License-Identifier: LGPL-2.0-or-later
*/
#include "valuetest.h"
#include <QStandardPaths>
using namespace KUnitConversion;
void ValueTest::initTestCase()
{
qputenv("KF5UNITCONVERT_NO_DOWNLOAD", "1");
QStandardPaths::setTestModeEnabled(true);
QLocale::setDefault(QLocale::c());
v1 = Value(3.1415, Kilometer);
v2 = Value(6.1415, QStringLiteral("m"));
v3 = Value(9.1415, v1.unit());
}
void ValueTest::testStrings()
{
QCOMPARE(v1.unit().symbol(), QStringLiteral("km"));
QCOMPARE(v2.toSymbolString(), QStringLiteral("6.1415 m"));
QCOMPARE(v3.toString(), QStringLiteral("9.1415 kilometers"));
}
void ValueTest::testRound()
{
v1.round(2);
QCOMPARE(v1.number(), 3.14);
}
void ValueTest::testConvert()
{
v1 = v1.convertTo(Meter);
QCOMPARE(v1.number(), 3140.0);
v1 = v1.convertTo(QStringLiteral("cm"));
QCOMPARE(v1.number(), 314000.0);
v1 = v1.convertTo(v2.unit());
QCOMPARE(v1.number(), 3140.0);
}
void ValueTest::testInvalid()
{
v1 = v1.convertTo(UnitId(99999));
QCOMPARE(v1.number(), 0.0);
QCOMPARE(v1.toSymbolString(), QLatin1String(""));
v2 = v2.convertTo(QStringLiteral("don't exist"));
QCOMPARE(v2.number(), 0.0);
QCOMPARE(v2.toSymbolString(), QLatin1String(""));
}
void ValueTest::testCurrencyNotDownloaded()
{
// ensure that no local conversion table is available
const QString cache = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + QStringLiteral("/libkunitconversion/currency.xml");
if (QFile::exists(cache)) {
QFile::remove(cache);
}
auto pounds = Value(100, Gbp);
auto eur = pounds.convertTo(Eur);
QVERIFY(!eur.isValid());
}
QTEST_MAIN(ValueTest)
#include "moc_valuetest.cpp"
|