diff --git a/autotests/foldingtest.cpp b/autotests/foldingtest.cpp index fceef9d..5d10166 100644 --- a/autotests/foldingtest.cpp +++ b/autotests/foldingtest.cpp @@ -1,191 +1,191 @@ /* Copyright (C) 2016 Volker Krause Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ #include "test-config.h" #include #include #include #include #include #include #include #include #include #include #include #include using namespace KSyntaxHighlighting; class FoldingHighlighter : public AbstractHighlighter { public: void highlightFile(const QString &inFileName, const QString &outFileName) { QFile outFile(outFileName); if (!outFile.open(QFile::WriteOnly | QFile::Truncate)) { qWarning() << "Failed to open output file" << outFileName << ":" << outFile.errorString(); return; } m_out.setDevice(&outFile); m_out.setCodec("UTF-8"); QFile f(inFileName); if (!f.open(QFile::ReadOnly)) { qWarning() << "Failed to open input file" << inFileName << ":" << f.errorString(); return; } QTextStream in(&f); in.setCodec("UTF-8"); State state; bool indentationFoldEnabled = definition().indentationBasedFoldingEnabled(); if (indentationFoldEnabled) m_out << ""; while (!in.atEnd()) { const auto currentLine = in.readLine(); state = highlightLine(currentLine, state); if (indentationFoldEnabled != state.indentationBasedFoldingEnabled()) { indentationFoldEnabled = state.indentationBasedFoldingEnabled(); if (indentationFoldEnabled) m_out << ""; else m_out << ""; } int offset = 0; foreach (const auto &fold, m_folds) { m_out << currentLine.mid(offset, fold.offset - offset); if (fold.region.type() == FoldingRegion::Begin) m_out << ""; else m_out << ""; m_out << currentLine.mid(fold.offset, fold.length); if (fold.region.type() == FoldingRegion::Begin) m_out << ""; else m_out << ""; offset = fold.offset + fold.length; } m_out << currentLine.mid(offset) << '\n'; m_folds.clear(); } m_out.flush(); } protected: void applyFormat(int offset, int length, const Format &format) Q_DECL_OVERRIDE { Q_UNUSED(offset); Q_UNUSED(length); Q_UNUSED(format); } void applyFolding(int offset, int length, FoldingRegion region) Q_DECL_OVERRIDE { Q_ASSERT(region.isValid()); m_folds.push_back({offset, length, region}); } private: QTextStream m_out; struct Fold { int offset; int length; FoldingRegion region; }; QVector m_folds; }; class FoldingTest : public QObject { Q_OBJECT public: explicit FoldingTest(QObject *parent = nullptr) : QObject(parent) {} private: private Q_SLOTS: void initTestCase() { QStandardPaths::enableTestMode(true); } void testFolding_data() { QTest::addColumn("inFile"); QTest::addColumn("outFile"); QTest::addColumn("refFile"); QTest::addColumn("syntax"); - QDir dir(QStringLiteral(TESTSRCDIR "/input")); + const QDir dir(QStringLiteral(TESTSRCDIR "/input")); foreach (const auto &fileName, dir.entryList(QDir::Files | QDir::NoSymLinks | QDir::Readable, QDir::Name)) { const auto inFile = dir.absoluteFilePath(fileName); if (inFile.endsWith(QLatin1String(".syntax"))) continue; QString syntax; QFile syntaxOverride(inFile + QStringLiteral(".syntax")); if (syntaxOverride.exists() && syntaxOverride.open(QFile::ReadOnly)) syntax = QString::fromUtf8(syntaxOverride.readAll()).trimmed(); QTest::newRow(fileName.toUtf8().constData()) << inFile << (QStringLiteral(TESTBUILDDIR "/folding.out/") + fileName + QStringLiteral(".fold")) << (QStringLiteral(TESTSRCDIR "/folding/") + fileName + QStringLiteral(".fold")) << syntax; } QDir().mkpath(QStringLiteral(TESTBUILDDIR "/folding.out/")); } void testFolding() { QFETCH(QString, inFile); QFETCH(QString, outFile); QFETCH(QString, refFile); QFETCH(QString, syntax); Repository m_repo; initRepositorySearchPaths(m_repo); auto def = m_repo.definitionForFileName(inFile); if (!syntax.isEmpty()) def = m_repo.definitionForName(syntax); FoldingHighlighter highlighter; QVERIFY(def.isValid()); highlighter.setDefinition(def); highlighter.highlightFile(inFile, outFile); /** * compare results */ compareFiles(refFile, outFile); } }; QTEST_GUILESS_MAIN(FoldingTest) #include "foldingtest.moc" diff --git a/autotests/highlighter_benchmark.cpp b/autotests/highlighter_benchmark.cpp index 9ff3c2f..315a60d 100644 --- a/autotests/highlighter_benchmark.cpp +++ b/autotests/highlighter_benchmark.cpp @@ -1,129 +1,129 @@ /* Copyright (C) 2016 Volker Krause Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ #include "test-config.h" #include #include #include #include #include -#include +#include #include #include using namespace KSyntaxHighlighting; class NullHighlighter : public AbstractHighlighter { public: void highlightFile(const QString &inFileName) { QFile f(inFileName); if (!f.open(QFile::ReadOnly)) { qWarning() << "Failed to open input file" << inFileName << ":" << f.errorString(); return; } QTextStream in(&f); in.setCodec("UTF-8"); State state; while (!in.atEnd()) state = highlightLine(in.readLine(), state); } protected: void applyFormat(int, int, const Format&) Q_DECL_OVERRIDE {} }; class HighlighterBenchmark : public QObject { Q_OBJECT public: explicit HighlighterBenchmark(QObject *parent = nullptr) : QObject(parent), m_repo(nullptr) {} private: Repository *m_repo = nullptr; private Q_SLOTS: void initTestCase() { m_repo = new Repository; initRepositorySearchPaths(*m_repo); } void cleanupTestCase() { delete m_repo; m_repo = nullptr; } void benchmarkHighlight_data() { QTest::addColumn("inFile"); QTest::addColumn("syntax"); - QDirIterator it(QStringLiteral(TESTSRCDIR "/input"), QDir::Files | QDir::NoSymLinks | QDir::Readable); - while (it.hasNext()) { - const auto inFile = it.next(); + const QDir dir(QStringLiteral(TESTSRCDIR "/input")); + foreach (const auto &fileName, dir.entryList(QDir::Files | QDir::NoSymLinks | QDir::Readable, QDir::Name)) { + const auto inFile = dir.absoluteFilePath(fileName); if (inFile.endsWith(QLatin1String(".syntax"))) continue; QString syntax; QFile syntaxOverride(inFile + QStringLiteral(".syntax")); if (syntaxOverride.exists() && syntaxOverride.open(QFile::ReadOnly)) syntax = QString::fromUtf8(syntaxOverride.readAll()).trimmed(); - QTest::newRow(it.fileName().toUtf8().constData()) << inFile << syntax; + QTest::newRow(fileName.toUtf8().constData()) << inFile << syntax; } } void benchmarkHighlight() { QFETCH(QString, inFile); QFETCH(QString, syntax); QVERIFY(m_repo); NullHighlighter highlighter; auto def = m_repo->definitionForFileName(inFile); if (!syntax.isEmpty()) def = m_repo->definitionForName(syntax); QVERIFY(def.isValid()); highlighter.setDefinition(def); // trigger loading of definition per benchmarking loop QVERIFY(!def.formats().isEmpty()); // benchmark the highlighting QBENCHMARK { highlighter.highlightFile(inFile); } } }; QTEST_GUILESS_MAIN(HighlighterBenchmark) #include "highlighter_benchmark.moc" diff --git a/autotests/htmlhighlighter_test.cpp b/autotests/htmlhighlighter_test.cpp index 4d6037b..f7cc30a 100644 --- a/autotests/htmlhighlighter_test.cpp +++ b/autotests/htmlhighlighter_test.cpp @@ -1,121 +1,121 @@ /* Copyright (C) 2016 Volker Krause Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ #include "test-config.h" #include #include #include #include -#include +#include #include #include #include #include using namespace KSyntaxHighlighting; class HTMLHighlighterTest : public QObject { Q_OBJECT public: explicit HTMLHighlighterTest(QObject *parent = nullptr) : QObject(parent), m_repo(nullptr) {} private: Repository *m_repo; private Q_SLOTS: void initTestCase() { QStandardPaths::enableTestMode(true); m_repo = new Repository; initRepositorySearchPaths(*m_repo); } void cleanupTestCase() { delete m_repo; m_repo = nullptr; } void testHighlight_data() { QTest::addColumn("inFile"); QTest::addColumn("outFile"); QTest::addColumn("refFile"); QTest::addColumn("syntax"); - QDirIterator it(QStringLiteral(TESTSRCDIR "/input"), QDir::Files | QDir::NoSymLinks | QDir::Readable); - while (it.hasNext()) { - const auto inFile = it.next(); + const QDir dir(QStringLiteral(TESTSRCDIR "/input")); + foreach (const auto &fileName, dir.entryList(QDir::Files | QDir::NoSymLinks | QDir::Readable, QDir::Name)) { + const auto inFile = dir.absoluteFilePath(fileName); if (inFile.endsWith(QLatin1String(".syntax"))) continue; QString syntax; QFile syntaxOverride(inFile + QStringLiteral(".syntax")); if (syntaxOverride.exists() && syntaxOverride.open(QFile::ReadOnly)) syntax = QString::fromUtf8(syntaxOverride.readAll()).trimmed(); - QTest::newRow(it.fileName().toUtf8().constData()) << inFile - << (QStringLiteral(TESTBUILDDIR "/html.output/") + it.fileName() + QStringLiteral(".html")) - << (QStringLiteral(TESTSRCDIR "/html/") + it.fileName() + QStringLiteral(".html")) + QTest::newRow(fileName.toUtf8().constData()) << inFile + << (QStringLiteral(TESTBUILDDIR "/html.output/") + fileName + QStringLiteral(".html")) + << (QStringLiteral(TESTSRCDIR "/html/") + fileName + QStringLiteral(".html")) << syntax; } QDir().mkpath(QStringLiteral(TESTBUILDDIR "/html.output/")); } void testHighlight() { QFETCH(QString, inFile); QFETCH(QString, outFile); QFETCH(QString, refFile); QFETCH(QString, syntax); QVERIFY(m_repo); HtmlHighlighter highlighter; highlighter.setTheme(m_repo->defaultTheme()); QVERIFY(highlighter.theme().isValid()); auto def = m_repo->definitionForFileName(inFile); if (!syntax.isEmpty()) def = m_repo->definitionForName(syntax); QVERIFY(def.isValid()); highlighter.setDefinition(def); highlighter.setOutputFile(outFile); highlighter.highlightFile(inFile); /** * compare results */ compareFiles(refFile, outFile); } }; QTEST_GUILESS_MAIN(HTMLHighlighterTest) #include "htmlhighlighter_test.moc" diff --git a/autotests/testhighlighter.cpp b/autotests/testhighlighter.cpp index a61129c..fad0300 100644 --- a/autotests/testhighlighter.cpp +++ b/autotests/testhighlighter.cpp @@ -1,189 +1,189 @@ /* Copyright (C) 2016 Volker Krause Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ #include "test-config.h" #include #include #include #include #include #include -#include +#include #include #include #include #include using namespace KSyntaxHighlighting; class TestHighlighter : public AbstractHighlighter { public: void highlightFile(const QString &inFileName, const QString &outFileName) { QFile outFile(outFileName); if (!outFile.open(QFile::WriteOnly | QFile::Truncate)) { qWarning() << "Failed to open output file" << outFileName << ":" << outFile.errorString(); return; } m_out.setDevice(&outFile); m_out.setCodec("UTF-8"); QFile f(inFileName); if (!f.open(QFile::ReadOnly)) { qWarning() << "Failed to open input file" << inFileName << ":" << f.errorString(); return; } QTextStream in(&f); in.setCodec("UTF-8"); State state; while (!in.atEnd()) { m_currentLine = in.readLine(); state = highlightLine(m_currentLine, state); m_out << "
\n"; } m_out.flush(); } protected: void applyFormat(int offset, int length, const Format &format) Q_DECL_OVERRIDE { if (format.name().isEmpty()) m_out << "" << m_currentLine.midRef(offset, length) << ""; else m_out << "<" << format.name() << ">" << m_currentLine.midRef(offset, length) << ""; } private: QTextStream m_out; QString m_currentLine; }; class TestHighlighterTest : public QObject { Q_OBJECT public: explicit TestHighlighterTest(QObject *parent = nullptr) : QObject(parent), m_repo(nullptr) {} private: Repository *m_repo; QSet m_coveredDefinitions; private Q_SLOTS: void initTestCase() { QStandardPaths::enableTestMode(true); m_repo = new Repository; initRepositorySearchPaths(*m_repo); } void cleanupTestCase() { QFile coveredList(QLatin1String(TESTBUILDDIR "/covered-definitions.txt")); QFile uncoveredList(QLatin1String(TESTBUILDDIR "/uncovered-definition.txt")); QVERIFY(coveredList.open(QFile::WriteOnly)); QVERIFY(uncoveredList.open(QFile::WriteOnly)); int count = 0; foreach (const auto &def, m_repo->definitions()) { if (def.isHidden() || !def.isValid()) continue; ++count; if (m_coveredDefinitions.contains(def.name())) coveredList.write(def.name().toUtf8() + '\n'); else uncoveredList.write(def.name().toUtf8() + '\n'); } qDebug() << "Syntax definitions with test coverage:" << ((float)m_coveredDefinitions.size() * 100.0f / (float)count) << "%"; delete m_repo; m_repo = nullptr; } void testHighlight_data() { QTest::addColumn("inFile"); QTest::addColumn("outFile"); QTest::addColumn("refFile"); QTest::addColumn("syntax"); - QDirIterator it(QStringLiteral(TESTSRCDIR "/input"), QDir::Files | QDir::NoSymLinks | QDir::Readable); - while (it.hasNext()) { - const auto inFile = it.next(); + const QDir dir(QStringLiteral(TESTSRCDIR "/input")); + foreach (const auto &fileName, dir.entryList(QDir::Files | QDir::NoSymLinks | QDir::Readable, QDir::Name)) { + const auto inFile = dir.absoluteFilePath(fileName); if (inFile.endsWith(QLatin1String(".syntax"))) continue; QString syntax; QFile syntaxOverride(inFile + QStringLiteral(".syntax")); if (syntaxOverride.exists() && syntaxOverride.open(QFile::ReadOnly)) syntax = QString::fromUtf8(syntaxOverride.readAll()).trimmed(); - QTest::newRow(it.fileName().toUtf8().constData()) << inFile - << (QStringLiteral(TESTBUILDDIR "/output/") + it.fileName() + QStringLiteral(".ref")) - << (QStringLiteral(TESTSRCDIR "/reference/") + it.fileName() + QStringLiteral(".ref")) + QTest::newRow(fileName.toUtf8().constData()) << inFile + << (QStringLiteral(TESTBUILDDIR "/output/") + fileName + QStringLiteral(".ref")) + << (QStringLiteral(TESTSRCDIR "/reference/") + fileName + QStringLiteral(".ref")) << syntax; } QVERIFY(QDir().mkpath(QStringLiteral(TESTBUILDDIR "/output/"))); } void testHighlight() { QFETCH(QString, inFile); QFETCH(QString, outFile); QFETCH(QString, refFile); QFETCH(QString, syntax); QVERIFY(m_repo); auto def = m_repo->definitionForFileName(inFile); if (!syntax.isEmpty()) def = m_repo->definitionForName(syntax); TestHighlighter highlighter; highlighter.setTheme(m_repo->defaultTheme()); QVERIFY(highlighter.theme().isValid()); QVERIFY(def.isValid()); qDebug() << "Using syntax" << def.name(); m_coveredDefinitions.insert(def.name()); highlighter.setDefinition(def); highlighter.highlightFile(inFile, outFile); /** * compare results */ compareFiles(refFile, outFile); } }; QTEST_GUILESS_MAIN(TestHighlighterTest) #include "testhighlighter.moc"