diff options
author | Shawn Rutledge <shawn.rutledge@qt.io> | 2017-04-10 12:44:20 +0200 |
---|---|---|
committer | Edward Welbourne <edward.welbourne@qt.io> | 2017-06-20 09:53:14 +0000 |
commit | 9d23aebb271ea534a66cb3aceb2e63d9a1c870d6 (patch) | |
tree | a78a9b2739aabc594e04cb73cae3a81a24c54dd1 /tests/auto/corelib/tools/qlocale/tst_qlocale.cpp | |
parent | ccca8c94359e7f07333b710119bfdf00b25b78fd (diff) |
Add QLocale::formattedDataSize and consolidate use cases
It should be easier to translate sizes in bytes to human-readable
strings consistently rather than having to repeat this code (and the
string translations) in various places. The FileDialog in QtQuick.Controls
has a use for this, too.
[ChangeLog][QtCore][QLocale] Added QLocale::formattedDataSize() for
formatting quantities of bytes as kB, MB, GB etc.
Done-with: Edward Welbourne <edward.welbourne@qt.io>
Change-Id: I27bca146c3eba90fa7a5d52ef6626ce85723e3f0
Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Diffstat (limited to 'tests/auto/corelib/tools/qlocale/tst_qlocale.cpp')
-rw-r--r-- | tests/auto/corelib/tools/qlocale/tst_qlocale.cpp | 78 |
1 files changed, 78 insertions, 0 deletions
diff --git a/tests/auto/corelib/tools/qlocale/tst_qlocale.cpp b/tests/auto/corelib/tools/qlocale/tst_qlocale.cpp index 2c342dcfe6..c48f0e24eb 100644 --- a/tests/auto/corelib/tools/qlocale/tst_qlocale.cpp +++ b/tests/auto/corelib/tools/qlocale/tst_qlocale.cpp @@ -137,6 +137,9 @@ private slots: void textDirection_data(); void textDirection(); + void formattedDataSize_data(); + void formattedDataSize(); + private: QString m_decimal, m_thousand, m_sdate, m_ldate, m_time; QString m_sysapp; @@ -2473,5 +2476,80 @@ void tst_QLocale::textDirection() QCOMPARE(locale.textDirection() == Qt::RightToLeft, rightToLeft); } +void tst_QLocale::formattedDataSize_data() +{ + QTest::addColumn<QLocale::Language>("language"); + QTest::addColumn<int>("decimalPlaces"); + QTest::addColumn<QLocale::DataSizeFormats>("units"); + QTest::addColumn<int>("bytes"); + QTest::addColumn<QString>("output"); + + struct { + const char *name; + QLocale::Language lang; + const char *bytes; + const char abbrev; + const char sep; // decimal separator + } data[] = { + { "English", QLocale::English, "bytes", 'B', '.' }, + { "French", QLocale::French, "octets", 'o', ',' }, + { "C", QLocale::C, "bytes", 'B', '.' } + }; + + for (const auto row : data) { +#define ROWB(id, deci, num, text) \ + QTest::addRow("%s-%s", row.name, id) \ + << row.lang << deci << format \ + << num << (QString(text) + QChar(' ') + QString(row.bytes)) +#define ROWQ(id, deci, num, head, tail) \ + QTest::addRow("%s-%s", row.name, id) \ + << row.lang << deci << format \ + << num << (QString(head) + QChar(row.sep) + QString(tail) + QChar(row.abbrev)) + + // Metatype system fails to handle raw enum members as format; needs variable + { + const QLocale::DataSizeFormats format = QLocale::DataSizeIecFormat; + ROWB("IEC-0", 2, 0, "0"); + ROWB("IEC-10", 2, 10, "10"); + ROWQ("IEC-12Ki", 2, 12345, "12", "06 Ki"); + ROWQ("IEC-16Ki", 2, 16384, "16", "00 Ki"); + ROWQ("IEC-1235k", 2, 1234567, "1", "18 Mi"); + ROWQ("IEC-1374k", 2, 1374744, "1", "31 Mi"); + ROWQ("IEC-1234M", 2, 1234567890, "1", "15 Gi"); + } + { + const QLocale::DataSizeFormats format = QLocale::DataSizeTraditionalFormat; + ROWB("Trad-0", 2, 0, "0"); + ROWB("Trad-10", 2, 10, "10"); + ROWQ("Trad-12Ki", 2, 12345, "12", "06 k"); + ROWQ("Trad-16Ki", 2, 16384, "16", "00 k"); + ROWQ("Trad-1235k", 2, 1234567, "1", "18 M"); + ROWQ("Trad-1374k", 2, 1374744, "1", "31 M"); + ROWQ("Trad-1234M", 2, 1234567890, "1", "15 G"); + } + { + const QLocale::DataSizeFormats format = QLocale::DataSizeSIFormat; + ROWB("Decimal-0", 2, 0, "0"); + ROWB("Decimal-10", 2, 10, "10"); + ROWQ("Decimal-16Ki", 2, 16384, "16", "38 k"); + ROWQ("Decimal-1234k", 2, 1234567, "1", "23 M"); + ROWQ("Decimal-1374k", 2, 1374744, "1", "37 M"); + ROWQ("Decimal-1234M", 2, 1234567890, "1", "23 G"); + } +#undef ROWQ +#undef ROWB + } +} + +void tst_QLocale::formattedDataSize() +{ + QFETCH(QLocale::Language, language); + QFETCH(int, decimalPlaces); + QFETCH(QLocale::DataSizeFormats, units); + QFETCH(int, bytes); + QFETCH(QString, output); + QCOMPARE(QLocale(language).formattedDataSize(bytes, decimalPlaces, units), output); +} + QTEST_MAIN(tst_QLocale) #include "tst_qlocale.moc" |