Rework a test, eliminating some needless conversion via strings

The name toSecsSinceEpoch() gave no hint to the fact that the test
was, in fact, *also* testing round-tripping of the ISODateFormat.
Since there are other tests for string conversion, make this a simple
test of toSecsSinceEpoch().

It did the round-tripping via two methods with overly-terse names that
might just as well be local lambdas - now redundant, so removed.

Change-Id: I1e4fb1cc90224312c995596a8f3fe2bc5d9dfa15
Reviewed-by: Mårten Nordheim <marten.nordheim@qt.io>
Reviewed-by: Andrei Golubev <andrei.golubev@qt.io>
This commit is contained in:
Edward Welbourne 2020-09-25 16:06:34 +02:00
parent 63bbbdc4b8
commit b1cf188e83

View File

@ -43,8 +43,6 @@ class tst_QDateTime : public QObject
public: public:
tst_QDateTime(); tst_QDateTime();
static QString str( int y, int month, int d, int h, int min, int s );
static QDateTime dt( const QString& str );
public Q_SLOTS: public Q_SLOTS:
void initTestCase(); void initTestCase();
private Q_SLOTS: private Q_SLOTS:
@ -273,20 +271,6 @@ void tst_QDateTime::initTestCase()
<< "the Central European timezone"; << "the Central European timezone";
} }
QString tst_QDateTime::str( int y, int month, int d, int h, int min, int s )
{
return QDateTime( QDate(y, month, d), QTime(h, min, s) ).toString( Qt::ISODate );
}
QDateTime tst_QDateTime::dt(const QString &text)
{
#if QT_CONFIG(datestring)
if (text != "INVALID")
return QDateTime::fromString(text, Qt::ISODate);
#endif
return QDateTime();
}
void tst_QDateTime::ctor() void tst_QDateTime::ctor()
{ {
QDateTime dt1(QDate(2004, 1, 2), QTime(1, 2, 3)); QDateTime dt1(QDate(2004, 1, 2), QTime(1, 2, 3));
@ -1721,31 +1705,27 @@ void tst_QDateTime::currentDateTimeUtc2()
void tst_QDateTime::toSecsSinceEpoch_data() void tst_QDateTime::toSecsSinceEpoch_data()
{ {
QTest::addColumn<QString>("dateTimeStr"); QTest::addColumn<QDate>("date");
QTest::addColumn<bool>("valid");
QTest::newRow( "data1" ) << str( 1800, 1, 1, 12, 0, 0 ) << true; QTest::newRow("start-1800") << QDate(1800, 1, 1);
QTest::newRow( "data2" ) << str( 1969, 1, 1, 12, 0, 0 ) << true; QTest::newRow("start-1969") << QDate(1969, 1, 1);
QTest::newRow( "data3" ) << str( 2002, 1, 1, 12, 0, 0 ) << true; QTest::newRow("start-2002") << QDate(2002, 1, 1);
QTest::newRow( "data4" ) << str( 2002, 6, 1, 12, 0, 0 ) << true; QTest::newRow("mid-2002") << QDate(2002, 6, 1);
QTest::newRow( "data5" ) << QString("INVALID") << false; QTest::newRow("start-2038") << QDate(2038, 1, 1);
QTest::newRow( "data6" ) << str( 2038, 1, 1, 12, 0, 0 ) << true; QTest::newRow("star-trek-1st-contact") << QDate(2063, 4, 5);
QTest::newRow( "data7" ) << str( 2063, 4, 5, 12, 0, 0 ) << true; // the day of First Contact QTest::newRow("start-2107") << QDate(2107, 1, 1);
QTest::newRow( "data8" ) << str( 2107, 1, 1, 12, 0, 0 ) << true;
} }
void tst_QDateTime::toSecsSinceEpoch() void tst_QDateTime::toSecsSinceEpoch()
{ {
QFETCH(const QString, dateTimeStr); const QTime noon(12, 0);
const QDateTime datetime = dt(dateTimeStr); QFETCH(const QDate, date);
QFETCH(const bool, valid); const QDateTime dateTime(date, noon);
QCOMPARE(datetime.isValid(), valid); QVERIFY(dateTime.isValid());
if (valid) { const qint64 asSecsSinceEpoch = dateTime.toSecsSinceEpoch();
const qint64 asSecsSinceEpoch = datetime.toSecsSinceEpoch(); QCOMPARE(asSecsSinceEpoch, dateTime.toMSecsSinceEpoch() / 1000);
QCOMPARE(asSecsSinceEpoch, datetime.toMSecsSinceEpoch() / 1000); QCOMPARE(QDateTime::fromSecsSinceEpoch(asSecsSinceEpoch), dateTime);
QCOMPARE(QDateTime::fromSecsSinceEpoch(asSecsSinceEpoch), datetime);
}
} }
void tst_QDateTime::daylightSavingsTimeChange_data() void tst_QDateTime::daylightSavingsTimeChange_data()