diff options
author | Edward Welbourne <edward.welbourne@qt.io> | 2021-05-05 16:56:32 +0200 |
---|---|---|
committer | Edward Welbourne <edward.welbourne@qt.io> | 2021-05-11 16:20:55 +0200 |
commit | 0e7dcfb64100ed3b78c2748bbd665ecace8c2a45 (patch) | |
tree | 2fa82e21b3ec595a3e44b5800cdf5374f5c44090 /tests | |
parent | e8ea7a12623b2189c6186965ffec4d7c17798e82 (diff) |
Make a UTC time test more thorough and coherent
It was previously mixing in testing of QString -> JS String and JS's
Number() and its string's split(), none of which was relevant,
illuminating or simplifying. It also had a MyDateClass instance in
the test that wasn't used, and only failed or passed, without giving
any clue to what went wrong.
Add console.log()s to report each error, pass a UTC QDateTime as
parameter instead of a QString, use the known values for the date and
time fields instead of extracting them from a string, and verify that
the passed QDateTime has all expected UTC properties. Test all ways of
constructing a UTC time, rather than only one of them.
Change-Id: I3f5828fc994e38d567e06edf96071188154de8bc
Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
Reviewed-by: Ulf Hermann <ulf.hermann@qt.io>
Diffstat (limited to 'tests')
-rw-r--r-- | tests/auto/qml/qqmlecmascript/data/utcdate.qml | 84 | ||||
-rw-r--r-- | tests/auto/qml/qqmlecmascript/tst_qqmlecmascript.cpp | 5 |
2 files changed, 61 insertions, 28 deletions
diff --git a/tests/auto/qml/qqmlecmascript/data/utcdate.qml b/tests/auto/qml/qqmlecmascript/data/utcdate.qml index 7f66ee3f23..92b5e2e843 100644 --- a/tests/auto/qml/qqmlecmascript/data/utcdate.qml +++ b/tests/auto/qml/qqmlecmascript/data/utcdate.qml @@ -2,33 +2,65 @@ import QtQuick 2.0 import Qt.test 1.0 Item { - MyDateClass { - id: mdc + function check_value(date, tag, qdt) { + var result = true; + if (date.getUTCFullYear() != 2014) { + console.warn("Wrong year (" + tag + "):", date.getUTCFullYear(), "!= 2014") + result = false; + } + // July; JS's months are Jan 0 to 11 Dec, vs. Qt's 1 to 12. + if (date.getUTCMonth() != 6) { + console.warn("Wrong month (" + tag + "):", date.getUTCMonth(), "!= 6"); + result = false; + } + if (date.getUTCDate() != 16) { + console.warn("Wrong day (" + tag + "):", date.getUTCDate(), "!= 16"); + result = false; + } + if (date.getUTCHours() != 23) { + console.warn("Wrong hour (" + tag + "):", date.getUTCHours(), "!= 23"); + result = false; + } + if (date.getUTCMinutes() != 30) { + console.warn("Wrong minute (" + tag + "):", date.getUTCMinutes(), "!= 30"); + result = false; + } + if (date.getUTCSeconds() != 31) { + console.warn("Wrong second (" + tag + "):", date.getUTCSecondss(), "!= 31"); + result = false; + } + + if (qdt != undefined) { + if (date.toISOString() != qdt.toISOString()) { + console.warn("Different ISO strings (" + tag + "):", + date.toISOString(), "!=", qdt.toISOString()); + result = false; + } + if (date.getTime() != qdt.getTime()) { + console.warn("Different epoch times (" + tag + "):", + date.getTime(), "!=", qdt.getTime()); + } + } + return result; } - function check_utc(utcstr) { - var datetimeutc = utcstr.split('T') - var dateutc = datetimeutc[0].split('-') - var timeutc = datetimeutc[1].split(':') - var utcDate = new Date(0) - utcDate.setUTCFullYear(Number(dateutc[0])) - utcDate.setUTCMonth(Number(dateutc[1])-1) - utcDate.setUTCDate(Number(dateutc[2])) - utcDate.setUTCHours(Number(timeutc[0])) - utcDate.setUTCMinutes(Number(timeutc[1])) - utcDate.setUTCSeconds(Number(timeutc[2])) - if (utcDate.getUTCFullYear() != Number(dateutc[0])) - return false; - if (utcDate.getUTCMonth() != Number(dateutc[1])-1) - return false; - if (utcDate.getUTCDate() != Number(dateutc[2])) - return false; - if (utcDate.getUTCHours() != Number(timeutc[0])) - return false; - if (utcDate.getUTCMinutes() != Number(timeutc[1])) - return false; - if (utcDate.getUTCSeconds() != Number(timeutc[2])) - return false; - return true; + function check_utc(qdt) { + var result = check_value(qdt, "raw"); + if (!check_value(new Date(Date.UTC(2014, 6, 16, 23, 30, 31)), "by args", qdt)) + result = false; + if (!check_value(new Date(Date.parse("2014-07-16T23:30:31Z")), "parsed", qdt)) + result = false; + + var utcDate = new Date(0); + utcDate.setUTCFullYear(2014); + utcDate.setUTCMonth(6); + utcDate.setUTCDate(16); + utcDate.setUTCHours(23); + utcDate.setUTCMinutes(30); + utcDate.setUTCSeconds(31); + if (!check_value(utcDate, "by field", qdt)) + result = false; + + return result; } } diff --git a/tests/auto/qml/qqmlecmascript/tst_qqmlecmascript.cpp b/tests/auto/qml/qqmlecmascript/tst_qqmlecmascript.cpp index db878bc325..fd84691e3d 100644 --- a/tests/auto/qml/qqmlecmascript/tst_qqmlecmascript.cpp +++ b/tests/auto/qml/qqmlecmascript/tst_qqmlecmascript.cpp @@ -7959,8 +7959,9 @@ void tst_qqmlecmascript::utcDate() QVERIFY2(object, qPrintable(component.errorString())); QVariant q; - QVariant val = QString::fromLatin1("2014-07-16T23:30:31"); - QMetaObject::invokeMethod(object.get(), "check_utc", Q_RETURN_ARG(QVariant, q), Q_ARG(QVariant, val)); + QDateTime val(QDate(2014, 7, 16), QTime(23, 30, 31), Qt::UTC); + QMetaObject::invokeMethod(object.get(), "check_utc", + Q_RETURN_ARG(QVariant, q), Q_ARG(QVariant, val)); QVERIFY(q.toBool()); } |