1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386
|
/*
Copyright 2016 Anton Anikin <anton.anikin@htower.ru>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "test_kdevformatsource.h"
#include "../kdevformatfile.h"
#include "../filesystemhelpers.h"
#include <QTest>
#include <QByteArray>
#include <QByteArrayList>
#include <QDebug>
#include <QDir>
#include <QFile>
#include <QFileInfo>
#include <QString>
#include <QStringList>
#include <QTemporaryDir>
#include <QTextStream>
#include <vector>
QTEST_MAIN(KDevelop::TestKdevFormatSource)
using namespace KDevelop;
namespace {
QString applyFormatting(const QString& path, bool expectedFormattingResult)
{
KDevFormatFile formatFile(path, path);
if (!formatFile.find()) {
return "found no format_sources file for " + path;
}
if (!formatFile.read()) {
return "reading format_sources file failed for " + path;
}
if (formatFile.apply() != expectedFormattingResult) {
if (expectedFormattingResult) {
return "formatting was expected to succeed but actually failed for " + path;
} else {
return "formatting was expected to fail but actually succeeded for " + path;
}
}
return QString{};
}
}
TestKdevFormatSource::TestKdevFormatSource()
{
}
TestKdevFormatSource::~TestKdevFormatSource()
{
}
void TestKdevFormatSource::testNotFound_data()
{
static const QStringList formatFileData = {};
QCOMPARE(initTest(formatFileData), true);
for (const Source& source : qAsConst(m_sources)) {
QTest::newRow(source.path.toUtf8()) << source.path << false << false << false << source.lines;
}
}
void TestKdevFormatSource::testNotFound()
{
runTest();
}
void TestKdevFormatSource::testNoCommands_data()
{
static const QStringList formatFileData = {QStringLiteral("# some comment")};
QCOMPARE(initTest(formatFileData), true);
for (const Source& source : qAsConst(m_sources)) {
QTest::newRow(source.path.toUtf8()) << source.path << true << false << false << source.lines;
}
}
void TestKdevFormatSource::testNoCommands()
{
runTest();
}
void TestKdevFormatSource::testNotMatch_data()
{
static const QStringList formatFileData = {QStringLiteral("notmatched.cpp : unused_command")};
QCOMPARE(initTest(formatFileData), true);
for (const Source& source : qAsConst(m_sources)) {
QTest::newRow(source.path.toUtf8()) << source.path << true << true << false << source.lines;
}
}
void TestKdevFormatSource::testNotMatch()
{
runTest();
}
void TestKdevFormatSource::testMatch1_data()
{
static const QStringList formatFileData({
QStringLiteral("src1/source_1.cpp : cat $ORIGFILE | sed 's/foo/FOO/' > tmp && mv tmp $ORIGFILE"),
QStringLiteral("src2/source_2.cpp : cat $ORIGFILE | sed 's/sqrt/std::sqrt/' > tmp && mv tmp $ORIGFILE"),
QStringLiteral("*.cpp : cat $ORIGFILE | sed 's/z/Z/' > tmp && mv tmp $ORIGFILE"),
QStringLiteral("notmatched.cpp : unused_command"),
});
QCOMPARE(initTest(formatFileData), true);
m_sources[0].lines.replaceInStrings(QStringLiteral("foo"), QStringLiteral("FOO"));
m_sources[1].lines.replaceInStrings(QStringLiteral("sqrt"), QStringLiteral("std::sqrt"));
m_sources[2].lines.replaceInStrings(QStringLiteral("z"), QStringLiteral("Z"));
for (const Source& source : qAsConst(m_sources)) {
QTest::newRow(source.path.toUtf8()) << source.path << true << true << true << source.lines;
}
}
void TestKdevFormatSource::testMatch1()
{
runTest();
}
void TestKdevFormatSource::testMatch2_data()
{
static const QStringList formatFileData({QStringLiteral("cat $ORIGFILE | sed 's/;/;;/' > tmp && mv tmp $ORIGFILE")});
QCOMPARE(initTest(formatFileData), true);
for (Source& source : m_sources) {
source.lines.replaceInStrings(QStringLiteral(";"), QStringLiteral(";;"));
QTest::newRow(source.path.toUtf8()) << source.path << true << true << true << source.lines;
}
}
void TestKdevFormatSource::testMatch2()
{
runTest();
}
void TestKdevFormatSource::testWildcardPathMatching_data()
{
struct FormatInfo{ const char* dir; const char* contents; };
struct Row{
const char* dataTag;
std::vector<FormatInfo> formatInfos;
std::vector<const char*> unmatchedPaths;
std::vector<const char*> matchedPaths;
};
const std::vector<Row> dataRows{
Row{"format_sources without wildcards (simple syntax)",
{FormatInfo{"", "true"}},
{},
{"x", "a/b", "exclude", "x.c", "p q\tr", "v/l/p/a/t/h.x"}
}, Row{"Single root format_sources with a single command",
{FormatInfo{"", "rd/* *include* *.h : true"}},
{"x", "r", "r.d", "includ", "includh", "rdh", "rd.h/x", "a/b.hh", "rc/x.h/y"},
{"x.h", "rd/x", "rd/x.h", "aincludeb", "include", "include.h", "rd/a/b/c", "a/b/c.h", "a/include"}
}, Row{"Single root format_sources with different commands",
{FormatInfo{"", "*inc/*:\n q/* *x?z:true \n dd/*: \n *.c:false \n *ab*:true"}},
{"q", "a.b", "xz", "xyzc", "c", "ac", "inc", "inc-/x", "ayz", "xy", "add/s", "incc", "a./c", "x/yz", "a-b", "minc"},
{"xyz", "x.c", "incxyz", "ainc/b.c", "a/b/.c", "a/.c", "x/z", "a/x-z", "p/x.z", "asinc/v", "a/b/cab/d/e", "dd/d", "dd/.c"}
}, Row{"Multiple format_sources files",
{FormatInfo{"a/b/", "q/* *x?z : false"}, FormatInfo{"", "*.c *cab* : true"}},
{"a/q", "a/xyz", "q/x", "xz", "a/b/qu", "a/bu/xyz", "ab/q/x", "a/b/qt/x", "a/bxyz", "a/x/z", "a/b/xz", "a/b/.c", "a/b/x-z.c"},
{"a/b/xyz", "x.c", "a/b/cdxyz", "a/b/cd/xyz", "a/b/q/x", "a/.c", "a/b/x/z", "exclude.c", "a/bcab/d/e"}
}, Row{"Case sensitivity",
{FormatInfo{"", "pQ* *RS* : true"}},
{"a/b/CDE", "cdpq", "a/b/.e", "a/b/cDe", "prcpQ.Eqs"},
{"a/b/pQrs", "a/b/c/d/pq/rs", "a/b/RSPQ", "pq", "uvrs", "PQa/b"}
}};
QTest::addColumn<QStringList>("formatDirs");
QTest::addColumn<QByteArrayList>("formatContents");
QTest::addColumn<QStringList>("unmatchedPaths");
QTest::addColumn<QStringList>("matchedPaths");
for (const Row& row : dataRows) {
QStringList formatDirs;
QByteArrayList formatContents;
for (const FormatInfo& info : row.formatInfos) {
formatDirs.push_back(info.dir);
formatContents.push_back(info.contents);
}
#if QT_VERSION >= QT_VERSION_CHECK(5, 14, 0)
const QStringList unmatchedPaths(row.unmatchedPaths.cbegin(), row.unmatchedPaths.cend());
const QStringList matchedPaths(row.matchedPaths.cbegin(), row.matchedPaths.cend());
#else
const auto vectorToList = [](const std::vector<const char*>& vec) {
QStringList result;
for (const char* s : vec)
result.push_back(s);
return result;
};
const QStringList unmatchedPaths = vectorToList(row.unmatchedPaths);
const QStringList matchedPaths = vectorToList(row.matchedPaths);
#endif
QTest::newRow(row.dataTag) << formatDirs << formatContents << unmatchedPaths << matchedPaths;
}
}
void TestKdevFormatSource::testWildcardPathMatching()
{
QFETCH(QStringList, formatDirs);
QFETCH(QByteArrayList, formatContents);
QFETCH(QStringList, unmatchedPaths);
QFETCH(QStringList, matchedPaths);
QTemporaryDir tmpDir;
QVERIFY2(tmpDir.isValid(), qPrintable("couldn't create temporary directory: " + tmpDir.errorString()));
using FilesystemHelpers::makeAbsoluteCreateAndWrite;
for (auto& dir : formatDirs) {
dir = QFileInfo{QDir{dir}, "format_sources"}.filePath();
}
QString errorPath = makeAbsoluteCreateAndWrite(tmpDir.path(), formatDirs, formatContents);
QVERIFY2(errorPath.isEmpty(), qPrintable("couldn't create or write to temporary file or directory " + errorPath));
errorPath = makeAbsoluteCreateAndWrite(tmpDir.path(), unmatchedPaths);
if (errorPath.isEmpty()) {
errorPath = makeAbsoluteCreateAndWrite(tmpDir.path(), matchedPaths);
}
QVERIFY2(errorPath.isEmpty(), qPrintable("couldn't create temporary file or directory " + errorPath));
bool expectedFormattingResult = false; // for unmatchedPaths
for (const auto& paths : { unmatchedPaths, matchedPaths }) {
for (const auto& path : paths) {
QVERIFY2(QFileInfo{path}.isFile(), qPrintable(path + ": file was not created or was deleted"));
const QString error = applyFormatting(path, expectedFormattingResult);
QVERIFY2(error.isEmpty(), qPrintable(error));
}
expectedFormattingResult = true; // for matchedPaths
}
}
bool TestKdevFormatSource::initTest(const QStringList& formatFileData)
{
QTest::addColumn<QString>("path");
QTest::addColumn<bool>("isFound");
QTest::addColumn<bool>("isRead");
QTest::addColumn<bool>("isApplied");
QTest::addColumn<QStringList>("lines");
m_temporaryDir.reset(new QTemporaryDir);
const QString workPath = m_temporaryDir->path();
qDebug() << "Using temporary dir:" << workPath;
if (!mkPath(workPath + "/src1"))
return false;
if (!mkPath(workPath + "/src2"))
return false;
if (!QDir::setCurrent(workPath)) {
qDebug() << "unable to set current directory to" << workPath;
return false;
}
m_sources.resize(3);
m_sources[0].path = workPath + "/src1/source_1.cpp";
m_sources[0].lines = QStringList({
QStringLiteral("void foo(int x) {"),
QStringLiteral(" printf(\"squared x = %d\\n\", x * x);"),
QStringLiteral("}")
});
m_sources[1].path = workPath + "/src2/source_2.cpp";
m_sources[1].lines = QStringList({
QStringLiteral("void bar(double x) {"),
QStringLiteral(" x = sqrt(x);"),
QStringLiteral(" printf(\"sqrt(x) = %e\\n\", x);"),
QStringLiteral("}")
});
m_sources[2].path = workPath + "/source_3.cpp";
m_sources[2].lines = QStringList({
QStringLiteral("void baz(double x, double y) {"),
QStringLiteral(" double z = pow(x, y);"),
QStringLiteral(" printf(\"x^y = %e\\n\", z);"),
QStringLiteral("}")
});
for (const Source& source : qAsConst(m_sources)) {
if (!writeLines(source.path, source.lines))
return false;
}
if (!formatFileData.isEmpty() && !writeLines(QStringLiteral("format_sources"), formatFileData))
return false;
return true;
}
void TestKdevFormatSource::runTest() const
{
QFETCH(QString, path);
QFETCH(bool, isFound);
QFETCH(bool, isRead);
QFETCH(bool, isApplied);
QFETCH(QStringList, lines);
KDevFormatFile formatFile(path, path);
QCOMPARE(formatFile.find(), isFound);
if (isFound)
QCOMPARE(formatFile.read(), isRead);
if (isRead)
QCOMPARE(formatFile.apply(), isApplied);
QStringList processedLines;
QCOMPARE(readLines(path, processedLines), true);
QCOMPARE(processedLines, lines);
}
bool TestKdevFormatSource::mkPath(const QString& path) const
{
if (!QDir().exists(path) && !QDir().mkpath(path)) {
qDebug() << "unable to create directory" << path;
return false;
}
return true;
}
bool TestKdevFormatSource::writeLines(const QString& path, const QStringList& lines) const
{
QFile outFile(path);
if (!outFile.open(QIODevice::WriteOnly)) {
qDebug() << "unable to open file" << path << "for writing";
return false;
}
QTextStream outStream(&outFile);
for (const QString& line : lines) {
outStream << line << "\n";
}
outStream.flush();
outFile.close();
return true;
}
bool TestKdevFormatSource::readLines(const QString& path, QStringList& lines) const
{
QFile inFile(path);
if (!inFile.open(QIODevice::ReadOnly)) {
qDebug() << "unable to open file" << path << "for reading";
return false;
}
lines.clear();
QTextStream inStream(&inFile);
while (!inStream.atEnd()) {
lines += inStream.readLine();
}
inFile.close();
return true;
}
|