From e8f04fc4abb6950b82cbe7de0c04f93d8f45b0da Mon Sep 17 00:00:00 2001 From: Lieven Hey Date: Mon, 15 Apr 2024 12:05:44 +0200 Subject: [PATCH] add test for multiline highlighting --- tests/modeltests/tst_formatting.cpp | 46 +++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/tests/modeltests/tst_formatting.cpp b/tests/modeltests/tst_formatting.cpp index cb2dba31..6eb7b13c 100644 --- a/tests/modeltests/tst_formatting.cpp +++ b/tests/modeltests/tst_formatting.cpp @@ -13,6 +13,11 @@ #include "../testutils.h" +#if KFSyntaxHighlighting_FOUND +#include +#include +#endif + Q_DECLARE_METATYPE(QVector) class TestFormatting : public QObject @@ -83,6 +88,47 @@ private slots: } } } + + void testMultilineHighlighting() + { +#if KFSyntaxHighlighting_FOUND + const auto testfunc = + QStringList({QStringLiteral("int test() {"), QStringLiteral("/* A"), QStringLiteral(" * very"), + QStringLiteral(" * long"), QStringLiteral(" * comment */"), QStringLiteral("return 0;"), + QStringLiteral("}")}); + + auto repository = std::make_unique(); + + HighlightedText text(repository.get()); + text.setText(testfunc); + text.setDefinition(repository->definitionForFileName(QStringLiteral("test.cpp"))); + + // get formatting for line 2 (first commented line) + const auto formats = text.layoutForLine(1)->formats(); + Q_ASSERT(!formats.empty()); + const auto commentFormat = formats[0].format; + + // ensure all other lines have the same format + for (int line = 2; line < 5; line++) { + const auto formats = text.layoutForLine(line)->formats(); + + for (const auto& format : formats) { + QCOMPARE(format.format, commentFormat); + } + } + + { + // ensure that the last line (return 0;) is not formatted in the comment style + const auto formats = text.layoutForLine(5)->formats(); + + for (const auto& format : formats) { + QVERIFY(format.format != commentFormat); + } + } +#else + QSKIP("Test requires KSyntaxHighlighting"); +#endif // KFSyntaxHighlighting_FOUND + } }; HOTSPOT_GUITEST_MAIN(TestFormatting)