diff --git a/autotests/foldingtest.cpp b/autotests/foldingtest.cpp --- a/autotests/foldingtest.cpp +++ b/autotests/foldingtest.cpp @@ -178,16 +178,10 @@ highlighter.setDefinition(def); highlighter.highlightFile(inFile, outFile); - const auto diffExecutable = QStandardPaths::findExecutable(QStringLiteral("diff")); - if (!diffExecutable.isEmpty()) { - QProcess proc; - proc.setProcessChannelMode(QProcess::ForwardedChannels); - proc.start(diffExecutable, {QStringLiteral("-u"), refFile, outFile}); - QVERIFY(proc.waitForFinished()); - QCOMPARE(proc.exitCode(), 0); - } else { - qDebug() << "Skipping part of the test since the 'diff' executable is not in PATH"; - } + /** + * compare results + */ + compareFiles(refFile, outFile); } }; diff --git a/autotests/htmlhighlighter_test.cpp b/autotests/htmlhighlighter_test.cpp --- a/autotests/htmlhighlighter_test.cpp +++ b/autotests/htmlhighlighter_test.cpp @@ -107,16 +107,10 @@ highlighter.setOutputFile(outFile); highlighter.highlightFile(inFile); - const auto diffExecutable = QStandardPaths::findExecutable(QStringLiteral("diff")); - if (!diffExecutable.isEmpty()) { - QProcess proc; - proc.setProcessChannelMode(QProcess::ForwardedChannels); - proc.start(diffExecutable, {QStringLiteral("-u"), refFile, outFile}); - QVERIFY(proc.waitForFinished()); - QCOMPARE(proc.exitCode(), 0); - } else { - qDebug() << "Skipping part of the test since the 'diff' executable is not in PATH"; - } + /** + * compare results + */ + compareFiles(refFile, outFile); } }; diff --git a/autotests/test-config.h.in b/autotests/test-config.h.in --- a/autotests/test-config.h.in +++ b/autotests/test-config.h.in @@ -21,7 +21,11 @@ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +#include +#include +#include #include +#include #include @@ -34,12 +38,51 @@ * * @param repository repository to init search paths */ -static void initRepositorySearchPaths(KSyntaxHighlighting::Repository &repository) +void initRepositorySearchPaths(KSyntaxHighlighting::Repository &repository) { #ifdef HAS_SYNTAX_RESOURCE Q_UNUSED(repository) #else repository.addCustomSearchPath(QStringLiteral("@CMAKE_SOURCE_DIR@/data")); repository.addCustomSearchPath(QStringLiteral("@CMAKE_BINARY_DIR@/data")); #endif } + +/** + * helper to compare files + * @param refFile reference file + * @param outFile output file + */ +void compareFiles(const QString &refFile, const QString &outFile) +{ + /** + * quick compare, all fine, if no diffs! + */ + QFile ref(refFile); + QFile out(outFile); + QVERIFY(ref.open(QIODevice::ReadOnly)); + QVERIFY(out.open(QIODevice::ReadOnly)); + const bool equalResults = ref.readAll() == out.readAll(); + if (equalResults) { + return; + } + + /** + * elaborate diff output, if possible + */ + const auto diffExecutable = QStandardPaths::findExecutable(QStringLiteral("diff")); + if (!diffExecutable.isEmpty()) { + QProcess proc; + proc.setProcessChannelMode(QProcess::ForwardedChannels); + proc.start(diffExecutable, {QStringLiteral("-u"), refFile, outFile}); + QVERIFY(proc.waitForFinished()); + QCOMPARE(proc.exitCode(), 0); + } else { + qDebug() << "Skipping differences output as the 'diff' executable is not in the PATH"; + } + + /** + * if we arrive here, all lost! + */ + QVERIFY(equalResults); +} diff --git a/autotests/testhighlighter.cpp b/autotests/testhighlighter.cpp --- a/autotests/testhighlighter.cpp +++ b/autotests/testhighlighter.cpp @@ -33,8 +33,6 @@ #include #include #include -#include -#include #include #include @@ -176,16 +174,10 @@ highlighter.setDefinition(def); highlighter.highlightFile(inFile, outFile); - const auto diffExecutable = QStandardPaths::findExecutable(QStringLiteral("diff")); - if (!diffExecutable.isEmpty()) { - QProcess proc; - proc.setProcessChannelMode(QProcess::ForwardedChannels); - proc.start(diffExecutable, {QStringLiteral("-u"), refFile, outFile}); - QVERIFY(proc.waitForFinished()); - QCOMPARE(proc.exitCode(), 0); - } else { - qDebug() << "Skipping part of the test since the 'diff' executable is not in PATH"; - } + /** + * compare results + */ + compareFiles(refFile, outFile); } }; diff --git a/src/lib/abstracthighlighter.cpp b/src/lib/abstracthighlighter.cpp --- a/src/lib/abstracthighlighter.cpp +++ b/src/lib/abstracthighlighter.cpp @@ -127,12 +127,13 @@ auto defData = DefinitionData::get(d->m_definition); auto newState = state; auto stateData = StateData::get(newState); - if (stateData->initialContext() && DefinitionData::get(stateData->initialContext()->definition()) != defData) { + if (stateData->m_defData && defData != stateData->m_defData) { qCDebug(Log) << "Got invalid state, resetting."; stateData->clear(); } if (stateData->isEmpty()) { stateData->push(defData->initialContext(), QStringList()); + stateData->m_defData = defData; } // process empty lines diff --git a/src/lib/state.cpp b/src/lib/state.cpp --- a/src/lib/state.cpp +++ b/src/lib/state.cpp @@ -63,11 +63,6 @@ m_contextStack.pop_back(); } -Context* StateData::initialContext() const -{ - return isEmpty() ? nullptr : m_contextStack.first().first; -} - Context* StateData::topContext() const { Q_ASSERT(!isEmpty()); @@ -102,7 +97,7 @@ bool State::operator==(const State &other) const { - return d->m_contextStack == other.d->m_contextStack; + return d->m_contextStack == other.d->m_contextStack && d->m_defData == other.d->m_defData; } bool State::operator!=(const State &other) const diff --git a/src/lib/state_p.h b/src/lib/state_p.h --- a/src/lib/state_p.h +++ b/src/lib/state_p.h @@ -51,11 +51,18 @@ int size() const; void push(Context *context, const QStringList &captures); void pop(); - Context* initialContext() const; Context* topContext() const; const QStringList &topCaptures() const; + /** + * definition pointer to check for invalid states + * FIXME: this is a hack, one could get the same pointer + * later for other object + */ + DefinitionData *m_defData = nullptr; + private: + /** * the context stack combines the active context + valid captures */