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>
bb10
Andreas Buhr 2020-10-07 13:25:57 +02:00
parent cc0be95ac7
commit b0d4d95a29
2 changed files with 80 additions and 0 deletions

View File

@ -240,6 +240,8 @@ private:
return potentialValue(QStringView(str), min, max, index, currentValue, insert);
}
friend class QDTPUnitTestParser;
protected: // for the benefit of QDateTimeEditPrivate
int sectionSize(int index) const;
int sectionMaxSize(int index) const;

View File

@ -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 &section)
{
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");