diff options
author | Ivan Solovev <ivan.solovev@qt.io> | 2022-05-30 12:07:17 +0200 |
---|---|---|
committer | Ivan Solovev <ivan.solovev@qt.io> | 2022-06-03 22:37:50 +0200 |
commit | 9327d1aaf79d4214db78ec86b8b6df7738bd1875 (patch) | |
tree | 6c61b2dccece96a3a9164006b17e83747d6768b1 /src/testlib/qtaptestlogger.cpp | |
parent | 3dcf1779be43373f820fb924c3fed21d05d5c5d2 (diff) |
Testlib: teach TAP test logger to support new QCOMPARE_* operators
The TAP test logger will now correctly print comparison types such
as QCOMPARE_NE or QCOMPARE_LT, and also provide a proper expected/wanted
value (by adding proper arithmetical operators in front of the value).
Sample output:
type: QCOMPARE_GE
message: Left value is expected to be greater than or equal to right value, but is not
wanted: >= 1 (rhs)
found: 0 (lhs)
expected: >= 1 (rhs)
actual: 0 (lhs)
at: tst_ExtendedCompare::compareUnregistereEnum() (tst_extendedcompare.cpp:232)
file: tst_extendedcompare.cpp
line: 232
As a drive-by: make some variables const.
Task-number: QTBUG-98873
Change-Id: Idb54eaabcb937b42d3fc844f30041aab82d73f69
Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
Diffstat (limited to 'src/testlib/qtaptestlogger.cpp')
-rw-r--r-- | src/testlib/qtaptestlogger.cpp | 77 |
1 files changed, 67 insertions, 10 deletions
diff --git a/src/testlib/qtaptestlogger.cpp b/src/testlib/qtaptestlogger.cpp index b6f8c87144..b6fa4295bc 100644 --- a/src/testlib/qtaptestlogger.cpp +++ b/src/testlib/qtaptestlogger.cpp @@ -281,25 +281,53 @@ void QTapTestLogger::addIncident(IncidentTypes type, const char *description, const char *indent = isExpectedFail ? YAML_INDENT YAML_INDENT YAML_INDENT : YAML_INDENT; if (!ok) { #if QT_CONFIG(regularexpression) + enum class OperationType { + Unknown, + Compare, /* Plain old QCOMPARE */ + Verify, /* QVERIFY */ + CompareOp, /* QCOMPARE_OP */ + }; + // This is fragile, but unfortunately testlib doesn't plumb // the expected and actual values to the loggers (yet). - static QRegularExpression verifyRegex(u"^'(?<actualexpression>.*)' returned " - "(?<actual>\\w+).+\\((?<message>.*)\\)$"_s); + static const QRegularExpression verifyRegex( + u"^'(?<actualexpression>.*)' returned " + "(?<actual>\\w+).+\\((?<message>.*)\\)$"_s); - static QRegularExpression compareRegex( + static const QRegularExpression compareRegex( u"^(?<message>.*)\n" "\\s*Actual\\s+\\((?<actualexpression>.*)\\)\\s*: (?<actual>.*)\n" "\\s*Expected\\s+\\((?<expectedexpresssion>.*)\\)\\s*: " "(?<expected>.*)$"_s); - QString descriptionString = QString::fromUtf8(description); + static const QRegularExpression compareOpRegex( + u"^(?<message>.*)\n" + "\\s*Left\\s+\\((?<actualexpression>.*)\\)\\s*: (?<actual>.*)\n" + "\\s*Right\\s+\\((?<expectedexpresssion>.*)\\)\\s*: " + "(?<expected>.*)$"_s); + + const QString descriptionString = QString::fromUtf8(description); QRegularExpressionMatch match = verifyRegex.match(descriptionString); - const bool isVerify = match.hasMatch(); - if (!isVerify) + + OperationType opType = OperationType::Unknown; + if (match.hasMatch()) + opType = OperationType::Verify; + + if (opType == OperationType::Unknown) { match = compareRegex.match(descriptionString); + if (match.hasMatch()) + opType = OperationType::Compare; + } + + if (opType == OperationType::Unknown) { + match = compareOpRegex.match(descriptionString); + if (match.hasMatch()) + opType = OperationType::CompareOp; + } - if (match.hasMatch()) { + if (opType != OperationType::Unknown) { QString message = match.captured(u"message"); + QLatin1StringView comparisonType; QString expected; QString actual; const auto parenthesize = [&match](QLatin1StringView key) -> QString { @@ -307,16 +335,45 @@ void QTapTestLogger::addIncident(IncidentTypes type, const char *description, }; const QString actualExpression = parenthesize("actualexpression"_L1); - if (isVerify) { + if (opType == OperationType::Verify) { + comparisonType = "QVERIFY"_L1; actual = match.captured(u"actual").toLower() % actualExpression; expected = (actual.startsWith("true "_L1) ? "false"_L1 : "true"_L1) % actualExpression; if (message.isEmpty()) message = u"Verification failed"_s; - } else { + } else if (opType == OperationType::Compare) { + comparisonType = "QCOMPARE"_L1; expected = match.captured(u"expected") % parenthesize("expectedexpresssion"_L1); actual = match.captured(u"actual") % actualExpression; + } else { + struct ComparisonInfo { + const char *comparisonType; + const char *comparisonStringOp; + }; + // get a proper comparison type based on the error message + const auto info = [](const QString &err) -> ComparisonInfo { + if (err.contains("different"_L1)) + return { "QCOMPARE_NE", "!= " }; + else if (err.contains("less than or equal to"_L1)) + return { "QCOMPARE_LE", "<= " }; + else if (err.contains("greater than or equal to"_L1)) + return { "QCOMPARE_GE", ">= " }; + else if (err.contains("less than"_L1)) + return { "QCOMPARE_LT", "< " }; + else if (err.contains("greater than"_L1)) + return { "QCOMPARE_GT", "> " }; + else if (err.contains("to be equal to"_L1)) + return { "QCOMPARE_EQ", "== " }; + else + return { "Unknown", "" }; + }(message); + comparisonType = QLatin1StringView(info.comparisonType); + expected = QLatin1StringView(info.comparisonStringOp) + % match.captured(u"expected") + % parenthesize("expectedexpresssion"_L1); + actual = match.captured(u"actual") % actualExpression; } QTestCharBuffer diagnosticsYamlish; @@ -329,7 +386,7 @@ void QTapTestLogger::addIncident(IncidentTypes type, const char *description, "%sfound: %s\n" "%sexpected: %s\n" "%sactual: %s\n", - indent, isVerify ? "QVERIFY" : "QCOMPARE", + indent, comparisonType.latin1(), indent, qPrintable(message), indent, qPrintable(expected), indent, qPrintable(actual), indent, qPrintable(expected), indent, qPrintable(actual) |