diff options
author | Andreas Buhr <andreas.buhr@qt.io> | 2020-10-07 13:25:57 +0200 |
---|---|---|
committer | Andreas Buhr <andreas.buhr@qt.io> | 2020-10-30 10:15:29 +0100 |
commit | b0d4d95a2986595fda4a2cc8a163e44c9b4162a6 (patch) | |
tree | acf8fe711f3e34e5eb672a671f6ad90821d3cb9e /tests/auto | |
parent | cc0be95ac782c9cf0bf994d0af6e2966cf29f8fa (diff) |
Add unit tests for QDateTimeParser internals
So far, the internals of QDateTimeParser and especially the handling
of 'Intermediate' values were only tested implicitly by
tst_qdatetimeedit. 'Intermediate' values are values which
are not valid according to the specified format, but could
become valid by adding more characters.
This patch adds unit tests which tests parsing of
these intermediate values directly.
These tests will help implement handling of negative
year numbers, where additional complications arise
because of possible ambiguities between the minus sign '-'
and the separator '-'.
Task-number: QTBUG-84334
Change-Id: Ia6ba08df198288b8b11d3b2d2052c194f04fe8a1
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
Reviewed-by: Andrei Golubev <andrei.golubev@qt.io>
Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
Diffstat (limited to 'tests/auto')
-rw-r--r-- | tests/auto/corelib/time/qdatetimeparser/tst_qdatetimeparser.cpp | 78 |
1 files changed, 78 insertions, 0 deletions
diff --git a/tests/auto/corelib/time/qdatetimeparser/tst_qdatetimeparser.cpp b/tests/auto/corelib/time/qdatetimeparser/tst_qdatetimeparser.cpp index c6490c6158..0d1b082831 100644 --- a/tests/auto/corelib/time/qdatetimeparser/tst_qdatetimeparser.cpp +++ b/tests/auto/corelib/time/qdatetimeparser/tst_qdatetimeparser.cpp @@ -29,14 +29,92 @@ #include <QtTest/QtTest> #include <private/qdatetimeparser_p.h> +QT_BEGIN_NAMESPACE + +// access to needed members in QDateTimeParser +class QDTPUnitTestParser : public QDateTimeParser +{ +public: + QDTPUnitTestParser() : QDateTimeParser(QMetaType::QDateTime, QDateTimeParser::DateTimeEdit) { } + + // forward data structures + using QDateTimeParser::ParsedSection; + using QDateTimeParser::State; + + // function to manipulate private internals + void setText(QString text) { m_text = text; } + + // forwarding of methods + using QDateTimeParser::parseSection; +}; + +bool operator==(const QDTPUnitTestParser::ParsedSection &a, + const QDTPUnitTestParser::ParsedSection &b) +{ + return a.value == b.value && a.used == b.used && a.zeroes == b.zeroes && a.state == b.state; +} + +// pretty printing for ParsedSection +char *toString(const QDTPUnitTestParser::ParsedSection §ion) +{ + using QTest::toString; + return toString(QByteArray("ParsedSection(") + "state=" + QByteArray::number(section.state) + + ", value=" + QByteArray::number(section.value) + + ", used=" + QByteArray::number(section.used) + + ", zeros=" + QByteArray::number(section.zeroes) + ")"); +} + +QT_END_NAMESPACE + class tst_QDateTimeParser : public QObject { Q_OBJECT + private Q_SLOTS: + void parseSection_data(); + void parseSection(); + void intermediateYear_data(); void intermediateYear(); }; +void tst_QDateTimeParser::parseSection_data() +{ + QTest::addColumn<QString>("format"); + QTest::addColumn<QString>("input"); + QTest::addColumn<int>("sectionIndex"); + QTest::addColumn<int>("offset"); + QTest::addColumn<QDTPUnitTestParser::ParsedSection>("expected"); + + using ParsedSection = QDTPUnitTestParser::ParsedSection; + using State = QDTPUnitTestParser::State; + QTest::newRow("short-year-begin") + << "yyyy_MM_dd" << "200_12_15" << 0 << 0 + << ParsedSection(State::Intermediate ,200, 3, 0); + + QTest::newRow("short-year-middle") + << "MM-yyyy-dd" << "12-200-15" << 1 << 3 + << ParsedSection(State::Intermediate, 200, 3, 0); +} + +void tst_QDateTimeParser::parseSection() +{ + QFETCH(QString, format); + QFETCH(QString, input); + QFETCH(int, sectionIndex); + QFETCH(int, offset); + QFETCH(QDTPUnitTestParser::ParsedSection, expected); + + QDTPUnitTestParser testParser; + + QVERIFY(testParser.parseFormat(format)); + QDateTime val(QDate(1900, 1, 1).startOfDay()); + + testParser.setText(input); + auto result = testParser.parseSection(val, sectionIndex, offset); + QCOMPARE(result, expected); +} + void tst_QDateTimeParser::intermediateYear_data() { QTest::addColumn<QString>("format"); |