1 /* 2 SPDX-FileCopyrightText: 2020 Arjen Hiemstra <ahiemstra@heimr.nl> 3 4 SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL 5 */ 6 7 #include <QLocale> 8 #include <QTest> 9 10 #include "Formatter.h" 11 #include "Unit.h" 12 13 #define QSL QStringLiteral 14 15 class FormatterTest : public QObject 16 { 17 Q_OBJECT 18 private Q_SLOTS: initTestCase()19 void initTestCase() 20 { 21 // Ensure we use a known locale for the test. 22 QLocale::setDefault(QLocale{QLocale::English, QLocale::UnitedStates}); 23 } 24 testDouble_data()25 void testDouble_data() 26 { 27 QTest::addColumn<double>("input"); 28 QTest::addColumn<KSysGuard::Unit>("unit"); 29 QTest::addColumn<QString>("output"); 30 31 QTest::newRow("1.0, B") << 1.0 << KSysGuard::UnitByte << QSL("1.0 B"); 32 QTest::newRow("1.0, KiB") << 1.0 << KSysGuard::UnitKiloByte << QSL("1.0 KiB"); 33 QTest::newRow("1.0, KiB/s") << 1.0 << KSysGuard::UnitKiloByteRate << QSL("1.0 KiB/s"); 34 QTest::newRow("1.0, %") << 1.0 << KSysGuard::UnitPercent << QSL("1.0%"); 35 36 QTest::newRow("0.213, B") << 0.213 << KSysGuard::UnitByte << QString::number(0.2) + QSL(" B"); 37 QTest::newRow("5.647, KiB") << 5.647 << KSysGuard::UnitKiloByte << QString::number(5.6) + QSL(" KiB"); 38 QTest::newRow("99.99, KiB/s") << 99.99 << KSysGuard::UnitKiloByteRate << QString::number(100.0, 'f', 1) + QSL(" KiB/s"); 39 QTest::newRow("0.2567, %") << 0.2567 << KSysGuard::UnitPercent << QString::number(0.3) + QSL("%"); 40 } 41 testDouble()42 void testDouble() 43 { 44 QFETCH(double, input); 45 QFETCH(KSysGuard::Unit, unit); 46 QFETCH(QString, output); 47 48 auto formatted = KSysGuard::Formatter::formatValue(input, unit); 49 QCOMPARE(formatted, output); 50 } 51 testFormatTime_data()52 void testFormatTime_data() 53 { 54 QTest::addColumn<int>("input"); 55 QTest::addColumn<QString>("output"); 56 QTest::newRow("1 s") << 1 << QSL("0:01"); 57 QTest::newRow("10 s") << 10 << QSL("0:10"); 58 QTest::newRow("1 m") << 60 << QSL("1:00"); 59 QTest::newRow("10m") << 60 * 10 << QSL("10:00"); 60 QTest::newRow("1h") << 60 * 60 << QSL("60:00"); 61 QTest::newRow("1h 1 m 1s") << (60 * 60) + 60 + 1 << QSL("61:01"); 62 } 63 testFormatTime()64 void testFormatTime() 65 { 66 QFETCH(int, input); 67 QFETCH(QString, output); 68 auto formatted = KSysGuard::Formatter::formatValue(input, KSysGuard::UnitTime); 69 QCOMPARE(formatted, output); 70 } 71 }; 72 73 QTEST_MAIN(FormatterTest); 74 75 #include "formattertest.moc" 76