SilverSearcher: Use data driven testing for output parser

Change-Id: Ia5a312a3c55eb753820d45ffcd72763137856933
Reviewed-by: Orgad Shaneh <orgads@gmail.com>
Reviewed-by: Przemyslaw Gorszkowski <pgorszkowski@gmail.com>
This commit is contained in:
Eike Ziller
2021-08-09 11:22:51 +02:00
parent d22505c41f
commit 6f69ee8b27
4 changed files with 49 additions and 116 deletions

View File

@@ -180,8 +180,16 @@ public:
matchStart(matchStart),
matchLength(matchLength),
regexpCapturedTexts(regexpCapturedTexts)
{}
bool operator==(const FileSearchResult &o) const
{
return fileName == o.fileName && lineNumber == o.lineNumber
&& matchingLine == o.matchingLine && matchStart == o.matchStart
&& matchLength == o.matchLength && regexpCapturedTexts == o.regexpCapturedTexts;
}
bool operator!=(const FileSearchResult &o) const { return !(*this == o); }
QString fileName;
int lineNumber;
QString matchingLine;

View File

@@ -28,113 +28,53 @@
#include <QtTest>
using namespace Utils;
namespace SilverSearcher {
namespace Internal {
void OutputParserTest::testNoResults()
void OutputParserTest::test_data()
{
const char parserOutput[] = "\n";
const QByteArray output(parserOutput);
SilverSearcher::SilverSearcherOutputParser ssop(output);
const QList<Utils::FileSearchResult> items = ssop.parse();
QCOMPARE(items.size(), 0);
QTest::addColumn<QByteArray>("parserOutput");
QTest::addColumn<FileSearchResultList>("results");
QTest::addRow("nothing") << QByteArray("\n") << FileSearchResultList();
QTest::addRow("oneFileOneMatch")
<< QByteArray(":/file/path/to/filename.h\n"
"1;1 5:match\n")
<< FileSearchResultList({{"/file/path/to/filename.h", 1, "match", 1, 5, {}}});
QTest::addRow("multipleFilesWithOneMatch")
<< QByteArray(":/file/path/to/filename1.h\n"
"1;1 5:match\n"
"\n"
":/file/path/to/filename2.h\n"
"2;2 5: match\n")
<< FileSearchResultList({{"/file/path/to/filename1.h", 1, "match", 1, 5, {}},
{"/file/path/to/filename2.h", 2, " match", 2, 5, {}}});
QTest::addRow("oneFileMultipleMatches")
<< QByteArray(":/file/path/to/filename.h\n"
"1;1 5,7 5:match match\n")
<< FileSearchResultList({{"/file/path/to/filename.h", 1, "match match", 1, 5, {}},
{"/file/path/to/filename.h", 1, "match match", 7, 5, {}}});
QTest::addRow("multipleFilesWithMultipleMatches")
<< QByteArray(":/file/path/to/filename1.h\n"
"1;1 5,7 5:match match\n"
"\n"
":/file/path/to/filename2.h\n"
"2;2 5,8 5: match match\n")
<< FileSearchResultList({{"/file/path/to/filename1.h", 1, "match match", 1, 5, {}},
{"/file/path/to/filename1.h", 1, "match match", 7, 5, {}},
{"/file/path/to/filename2.h", 2, " match match", 2, 5, {}},
{"/file/path/to/filename2.h", 2, " match match", 8, 5, {}}});
}
void OutputParserTest::testOneFileWithOneMatch()
void OutputParserTest::test()
{
const char parserOutput[] = ":/file/path/to/filename.h\n"
"1;1 5:match\n";
const QByteArray output(parserOutput);
SilverSearcher::SilverSearcherOutputParser ssop(output);
QFETCH(QByteArray, parserOutput);
QFETCH(FileSearchResultList, results);
SilverSearcher::SilverSearcherOutputParser ssop(parserOutput);
const QList<Utils::FileSearchResult> items = ssop.parse();
QCOMPARE(items.size(), 1);
QCOMPARE(items[0].fileName, QStringLiteral("/file/path/to/filename.h"));
QCOMPARE(items[0].lineNumber, 1);
QCOMPARE(items[0].matchingLine, QStringLiteral("match"));
QCOMPARE(items[0].matchStart, 1);
QCOMPARE(items[0].matchLength, 5);
}
void OutputParserTest::testMultipleFilesWithOneMatch()
{
const char parserOutput[] = ":/file/path/to/filename1.h\n"
"1;1 5:match\n"
"\n"
":/file/path/to/filename2.h\n"
"2;2 5: match\n"
;
const QByteArray output(parserOutput);
SilverSearcher::SilverSearcherOutputParser ssop(output);
const QList<Utils::FileSearchResult> items = ssop.parse();
QCOMPARE(items.size(), 2);
QCOMPARE(items[0].fileName, QStringLiteral("/file/path/to/filename1.h"));
QCOMPARE(items[0].lineNumber, 1);
QCOMPARE(items[0].matchingLine, QStringLiteral("match"));
QCOMPARE(items[0].matchStart, 1);
QCOMPARE(items[0].matchLength, 5);
QCOMPARE(items[1].fileName, QStringLiteral("/file/path/to/filename2.h"));
QCOMPARE(items[1].lineNumber, 2);
QCOMPARE(items[1].matchingLine, QStringLiteral(" match"));
QCOMPARE(items[1].matchStart, 2);
QCOMPARE(items[1].matchLength, 5);
}
void OutputParserTest::testOneFileWithMultipleMatches()
{
const char parserOutput[] = ":/file/path/to/filename.h\n"
"1;1 5,7 5:match match\n";
const QByteArray output(parserOutput);
SilverSearcher::SilverSearcherOutputParser ssop(output);
const QList<Utils::FileSearchResult> items = ssop.parse();
QCOMPARE(items.size(), 2);
QCOMPARE(items[0].fileName, QStringLiteral("/file/path/to/filename.h"));
QCOMPARE(items[0].lineNumber, 1);
QCOMPARE(items[0].matchingLine, QStringLiteral("match match"));
QCOMPARE(items[0].matchStart, 1);
QCOMPARE(items[0].matchLength, 5);
QCOMPARE(items[1].fileName, QStringLiteral("/file/path/to/filename.h"));
QCOMPARE(items[1].lineNumber, 1);
QCOMPARE(items[1].matchingLine, QStringLiteral("match match"));
QCOMPARE(items[1].matchStart, 7);
QCOMPARE(items[1].matchLength, 5);
}
void OutputParserTest::testMultipleFilesWithMultipleMatches()
{
const char parserOutput[] = ":/file/path/to/filename1.h\n"
"1;1 5,7 5:match match\n"
"\n"
":/file/path/to/filename2.h\n"
"2;2 5,8 5: match match\n";
const QByteArray output(parserOutput);
SilverSearcher::SilverSearcherOutputParser ssop(output);
const QList<Utils::FileSearchResult> items = ssop.parse();
QCOMPARE(items.size(), 4);
QCOMPARE(items[0].fileName, QStringLiteral("/file/path/to/filename1.h"));
QCOMPARE(items[0].lineNumber, 1);
QCOMPARE(items[0].matchingLine, QStringLiteral("match match"));
QCOMPARE(items[0].matchStart, 1);
QCOMPARE(items[0].matchLength, 5);
QCOMPARE(items[1].fileName, QStringLiteral("/file/path/to/filename1.h"));
QCOMPARE(items[1].lineNumber, 1);
QCOMPARE(items[1].matchingLine, QStringLiteral("match match"));
QCOMPARE(items[1].matchStart, 7);
QCOMPARE(items[1].matchLength, 5);
QCOMPARE(items[2].fileName, QStringLiteral("/file/path/to/filename2.h"));
QCOMPARE(items[2].lineNumber, 2);
QCOMPARE(items[2].matchingLine, QStringLiteral(" match match"));
QCOMPARE(items[2].matchStart, 2);
QCOMPARE(items[2].matchLength, 5);
QCOMPARE(items[3].fileName, QStringLiteral("/file/path/to/filename2.h"));
QCOMPARE(items[3].lineNumber, 2);
QCOMPARE(items[3].matchingLine, QStringLiteral(" match match"));
QCOMPARE(items[3].matchStart, 8);
QCOMPARE(items[3].matchLength, 5);
QCOMPARE(items, results);
}
} // namespace Internal

View File

@@ -33,11 +33,8 @@ class OutputParserTest : public QObject
Q_OBJECT
private slots:
void testNoResults();
void testOneFileWithOneMatch();
void testMultipleFilesWithOneMatch();
void testOneFileWithMultipleMatches();
void testMultipleFilesWithMultipleMatches();
void test_data();
void test();
};
} // namespace Internal

View File

@@ -28,18 +28,6 @@
#include <QTextCodec>
#include <QtTest>
namespace Utils {
bool operator==(const Utils::FileSearchResult &r1, const Utils::FileSearchResult &r2)
{
return r1.fileName == r2.fileName
&& r1.lineNumber == r2.lineNumber
&& r1.matchingLine == r2.matchingLine
&& r1.matchStart == r2.matchStart
&& r1.matchLength == r2.matchLength
&& r1.regexpCapturedTexts == r2.regexpCapturedTexts;
}
} // Utils
class tst_FileSearch : public QObject
{
Q_OBJECT