diff --git a/autotests/CMakeLists.txt b/autotests/CMakeLists.txt index f7b7bb3..b02c751 100644 --- a/autotests/CMakeLists.txt +++ b/autotests/CMakeLists.txt @@ -1,25 +1,29 @@ configure_file(test-config.h.in ${CMAKE_CURRENT_BINARY_DIR}/test-config.h) add_executable(wildcardmatcher_test wildcardmatcher_test.cpp ${CMAKE_SOURCE_DIR}/src/lib/wildcardmatcher.cpp) add_test(NAME wildcardmatcher_test COMMAND wildcardmatcher_test) target_link_libraries(wildcardmatcher_test KF5SyntaxHighlighting Qt5::Test) add_executable(syntaxrepository_test syntaxrepository_test.cpp) add_test(NAME syntaxrepository_test COMMAND syntaxrepository_test) target_link_libraries(syntaxrepository_test KF5SyntaxHighlighting Qt5::Test) +add_executable(theme_test theme_test.cpp) +add_test(NAME theme_test COMMAND theme_test) +target_link_libraries(theme_test KF5SyntaxHighlighting Qt5::Test) + add_executable(testhighlighter_test testhighlighter.cpp) add_test(NAME testhighlighter_test COMMAND testhighlighter_test) target_link_libraries(testhighlighter_test KF5SyntaxHighlighting Qt5::Test) add_executable(htmlhighlighter_test htmlhighlighter_test.cpp) add_test(NAME htmlhighlighter_test COMMAND htmlhighlighter_test) target_link_libraries(htmlhighlighter_test KF5SyntaxHighlighting Qt5::Test) add_executable(highlighter_benchmark highlighter_benchmark.cpp) add_test(NAME highlighter_benchmark COMMAND highlighter_benchmark) target_link_libraries(highlighter_benchmark KF5SyntaxHighlighting Qt5::Test) add_executable(repository_benchmark repository_benchmark.cpp) add_test(NAME repository_benchmark COMMAND repository_benchmark) target_link_libraries(repository_benchmark KF5SyntaxHighlighting Qt5::Test) diff --git a/autotests/syntaxrepository_test.cpp b/autotests/syntaxrepository_test.cpp index 8cf9b96..0011f7d 100644 --- a/autotests/syntaxrepository_test.cpp +++ b/autotests/syntaxrepository_test.cpp @@ -1,170 +1,157 @@ /* Copyright (C) 2016 Volker Krause This program is free software; you can redistribute it and/or modify it under the terms of the GNU Library 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 Library General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . */ #include #include #include #include -#include #include #include #include #include namespace SyntaxHighlighting { class NullHighlighter : public AbstractHighlighter { public: using AbstractHighlighter::highlightLine; void setFormat(int offset, int length, const Format &format) Q_DECL_OVERRIDE { Q_UNUSED(offset); Q_UNUSED(length); Q_UNUSED(format); } }; class RepositoryTest : public QObject { Q_OBJECT private: Repository m_repo; private Q_SLOTS: void initTestCase() { QStandardPaths::enableTestMode(true); } void testDefinitionByExtension_data() { QTest::addColumn("fileName"); QTest::addColumn("defName"); QTest::newRow("empty") << QString() << QString(); QTest::newRow("qml") << QStringLiteral("/bla/foo.qml") << QStringLiteral("QML"); QTest::newRow("glsl") << QStringLiteral("flat.frag") << QStringLiteral("GLSL"); // the following ones are defined in multiple syntax definitions QTest::newRow("c") << QStringLiteral("test.c") << QStringLiteral("C"); QTest::newRow("c++") << QStringLiteral("test.cpp") << QStringLiteral("C++"); QTest::newRow("markdown") << QStringLiteral("test.md") << QStringLiteral("Markdown"); QTest::newRow("Makefile 1") << QStringLiteral("Makefile") << QStringLiteral("Makefile"); QTest::newRow("Makefile 2") << QStringLiteral("/some/path/to/Makefile") << QStringLiteral("Makefile"); QTest::newRow("Makefile 3") << QStringLiteral("Makefile.am") << QStringLiteral("Makefile"); } void testDefinitionByExtension() { QFETCH(QString, fileName); QFETCH(QString, defName); auto def = m_repo.definitionForFileName(fileName); if (defName.isEmpty()) { QVERIFY(!def.isValid()); } else { QVERIFY(def.isValid()); QCOMPARE(def.name(), defName); } } void testLoadAll() { foreach (auto def, m_repo.definitions()) { QVERIFY(!def.name().isEmpty()); QVERIFY(!def.translatedName().isEmpty()); QVERIFY(!def.section().isEmpty()); QVERIFY(!def.translatedSection().isEmpty()); // indirectly trigger loading, as we can't reach that from public API // if the loading fails the highlighter will produce empty states NullHighlighter hl; State initialState; hl.setDefinition(def); const auto state = hl.highlightLine(QLatin1String("This should not crash } ] ) !"), initialState); QVERIFY(state != initialState); } } void testMetaData() { auto def = m_repo.definitionForName(QLatin1String("Alerts")); QVERIFY(def.isValid()); QVERIFY(def.extensions().isEmpty()); QVERIFY(def.mimeTypes().isEmpty()); QVERIFY(def.version() >= 1.11f); QVERIFY(def.isHidden()); QCOMPARE(def.section(), QLatin1String("Other")); QCOMPARE(def.license(), QLatin1String("LGPL")); QVERIFY(def.author().contains(QLatin1String("Dominik"))); QFileInfo fi(def.filePath()); QVERIFY(fi.isAbsolute()); QVERIFY(def.filePath().endsWith(QLatin1String("alert.xml"))); def = m_repo.definitionForName(QLatin1String("C++")); QVERIFY(def.isValid()); QCOMPARE(def.section(), QLatin1String("Sources")); QCOMPARE(def.indenter(), QLatin1String("cstyle")); QCOMPARE(def.style(), QLatin1String("C++")); QVERIFY(def.mimeTypes().contains(QLatin1String("text/x-c++hdr"))); QVERIFY(def.extensions().contains(QLatin1String("*.h"))); QCOMPARE(def.priority(), 9); def = m_repo.definitionForName(QLatin1String("Apache Configuration")); QVERIFY(def.isValid()); QVERIFY(def.extensions().contains(QLatin1String("httpd.conf"))); QVERIFY(def.extensions().contains(QLatin1String(".htaccess*"))); } void testReload() { auto def = m_repo.definitionForName(QLatin1String("QML")); QVERIFY(!m_repo.definitions().isEmpty()); QVERIFY(def.isValid()); NullHighlighter hl; hl.setDefinition(def); auto oldState = hl.highlightLine(QLatin1String("/* TODO this should not crash */"), State()); m_repo.reload(); QVERIFY(!m_repo.definitions().isEmpty()); QVERIFY(!def.isValid()); hl.highlightLine(QLatin1String("/* TODO this should not crash */"), State()); hl.highlightLine(QLatin1String("/* FIXME neither should this crash */"), oldState); QVERIFY(hl.definition().isValid()); QCOMPARE(hl.definition().name(), QLatin1String("QML")); } - - void testThemes() - { - QVERIFY(!m_repo.themes().isEmpty()); - Q_FOREACH (const auto theme, m_repo.themes()) { - QVERIFY(theme.isValid()); - QVERIFY(!theme.name().isEmpty()); - QVERIFY(!theme.filePath().isEmpty()); - QVERIFY(QFileInfo::exists(theme.filePath())); - QVERIFY(m_repo.theme(theme.name()).isValid()); - } - } }; } QTEST_MAIN(SyntaxHighlighting::RepositoryTest) #include "syntaxrepository_test.moc" diff --git a/autotests/theme_test.cpp b/autotests/theme_test.cpp new file mode 100644 index 0000000..e343cf9 --- /dev/null +++ b/autotests/theme_test.cpp @@ -0,0 +1,120 @@ +/* + Copyright (C) 2016 Volker Krause + + This program is free software; you can redistribute it and/or modify it + under the terms of the GNU Library 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 Library General Public + License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include + +namespace SyntaxHighlighting { + +class FormatCollector : public AbstractHighlighter +{ +public: + using AbstractHighlighter::highlightLine; + void setFormat(int offset, int length, const Format &format) Q_DECL_OVERRIDE + { + Q_UNUSED(offset); + Q_UNUSED(length); + formatMap.insert(format.name(), format); + } + QHash formatMap; +}; + +class ThemeTest : public QObject +{ + Q_OBJECT +private: + Repository m_repo; + +private Q_SLOTS: + void initTestCase() + { + QStandardPaths::enableTestMode(true); + } + + void testThemes() + { + QVERIFY(!m_repo.themes().isEmpty()); + Q_FOREACH (const auto theme, m_repo.themes()) { + QVERIFY(theme.isValid()); + QVERIFY(!theme.name().isEmpty()); + QVERIFY(!theme.filePath().isEmpty()); + QVERIFY(QFileInfo::exists(theme.filePath())); + QVERIFY(m_repo.theme(theme.name()).isValid()); + } + } + + void testFormat_data() + { + QTest::addColumn("themeName"); + QTest::newRow("default") << "Default"; + // TODO this breaks isNormal below +// QTest::newRow("dark") << "Breeze Dark"; + } + + void testFormat() + { + QFETCH(QString, themeName); + + // somewhat complicated way to get proper Format objects + FormatCollector collector; + collector.setDefinition(m_repo.definitionForName(QLatin1String("QML"))); + const auto t = m_repo.theme(themeName); + QVERIFY(t.isValid()); + collector.setTheme(t); + collector.highlightLine(QLatin1String("normal + property"), State()); + + QVERIFY(collector.formatMap.size() >= 3); + qDebug() << collector.formatMap.keys(); + + // normal text + auto f = collector.formatMap.value(QLatin1String("Normal Text")); + QVERIFY(f.isValid()); + + QVERIFY(f.isNormal(t)); // ### is this correct? breaks for dark themes + QVERIFY(!f.hasTextColor(t)); // ### same here + + // visually identical to normal text + f = collector.formatMap.value(QLatin1String("Symbol")); + QVERIFY(f.isValid()); + + QVERIFY(f.isNormal(t)); + QVERIFY(!f.hasTextColor(t)); + + // visually different to normal text + f = collector.formatMap.value(QLatin1String("Keywords")); + QVERIFY(f.isValid()); + + QVERIFY(!f.isNormal(t)); + QVERIFY(!f.hasTextColor(t)); + QVERIFY(f.isBold(t)); + } +}; +} + +QTEST_MAIN(SyntaxHighlighting::ThemeTest) + +#include "theme_test.moc" +