TextEditor: Move test setup closer to tested code

Change-Id: Ib6edee42b20830a322d47fc1ba071d3cdf2d7538
Reviewed-by: David Schulz <david.schulz@qt.io>
This commit is contained in:
hjk
2024-01-17 08:45:47 +01:00
parent e93948228a
commit f662009bca
9 changed files with 127 additions and 44 deletions

View File

@@ -119,7 +119,10 @@ add_qtc_plugin(TextEditor
extend_qtc_plugin(TextEditor
CONDITION WITH_TESTS
SOURCES
codeassist/codeassist_test.cpp codeassist/codeassist_test.h
highlighter_test.cpp highlighter_test.h
codeassist/codeassist_test.cpp
codeassist/codeassist_test.h
highlighter_test.cpp
highlighter_test.h
texteditor_test.cpp
texteditor_test.h
)

View File

@@ -329,12 +329,21 @@ void formatEditorAsync(TextEditorWidget *editor, const Command &command, int sta
} // namespace TextEditor
#ifdef WITH_TESTS
#include "texteditorplugin.h"
#include <QTest>
namespace TextEditor::Internal {
void TextEditorPlugin::testFormatting_data()
class FormatTextTest final : public QObject
{
Q_OBJECT
private slots:
void testFormatting_data();
void testFormatting();
};
void FormatTextTest::testFormatting_data()
{
QTest::addColumn<QString>("code");
QTest::addColumn<QString>("result");
@@ -360,7 +369,7 @@ void TextEditorPlugin::testFormatting_data()
}
}
void TextEditorPlugin::testFormatting()
void FormatTextTest::testFormatting()
{
QFETCH(QString, code);
QFETCH(QString, result);
@@ -377,6 +386,13 @@ void TextEditorPlugin::testFormatting()
QCOMPARE(editor->toPlainText(), result);
}
QObject *createFormatTextTest()
{
return new FormatTextTest;
}
} // namespace TextEditor::Internal
#endif
#include "formattexteditor.moc"
#endif // WITH_TESTS

View File

@@ -272,13 +272,10 @@ SnippetParseResult Snippet::parse(const QString &snippet)
} // Texteditor
using namespace TextEditor;
#ifdef WITH_TESTS
# include <QTest>
# include "../texteditorplugin.h"
const char NOMANGLER_ID[] = "TextEditor::NoMangler";
struct SnippetPart
@@ -297,9 +294,20 @@ struct SnippetPart
};
Q_DECLARE_METATYPE(SnippetPart);
namespace TextEditor::Internal {
using Parts = QList<SnippetPart>;
void Internal::TextEditorPlugin::testSnippetParsing_data()
class SnippetParserTest final : public QObject
{
Q_OBJECT
public slots:
void testSnippetParsing_data();
void testSnippetParsing();
};
void SnippetParserTest::testSnippetParsing_data()
{
QTest::addColumn<QString>("input");
QTest::addColumn<bool>("success");
@@ -384,7 +392,7 @@ void Internal::TextEditorPlugin::testSnippetParsing_data()
};
}
void Internal::TextEditorPlugin::testSnippetParsing()
void SnippetParserTest::testSnippetParsing()
{
QFETCH(QString, input);
QFETCH(bool, success);
@@ -407,4 +415,14 @@ void Internal::TextEditorPlugin::testSnippetParsing()
for (int i = 0; i < parts.size(); ++i)
rangesCompare(snippet.parts.at(i), parts.at(i));
}
#endif
QObject *createSnippetParserTest()
{
return new SnippetParserTest;
}
} // TextEditor::Internal
#include "snippet.moc"
#endif // WITH_TESTS

View File

@@ -1,19 +1,15 @@
// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
#include "textdocumentlayout.h"
#include "fontsettings.h"
#include "textdocument.h"
#include "texteditorplugin.h"
#include "textdocumentlayout.h"
#include "texteditorsettings.h"
#include <utils/qtcassert.h>
#include <utils/temporarydirectory.h>
#include <QDebug>
#ifdef WITH_TESTS
#include <QTest>
#endif
namespace TextEditor {
@@ -871,9 +867,24 @@ TextSuggestion::TextSuggestion()
TextSuggestion::~TextSuggestion() = default;
} // TextEditor
#ifdef WITH_TESTS
void Internal::TextEditorPlugin::testDeletingMarkOnReload()
#include <QTest>
namespace TextEditor::Internal {
class TextDocumentLayoutTest final : public QObject
{
Q_OBJECT
private slots:
void testDeletingMarkOnReload();
};
void TextDocumentLayoutTest::testDeletingMarkOnReload()
{
auto doc = new TextDocument();
doc->setFilePath(Utils::TemporaryDirectory::masterDirectoryFilePath() / "TestMarkDoc.txt");
@@ -888,6 +899,13 @@ void Internal::TextEditorPlugin::testDeletingMarkOnReload()
QVERIFY(!doc->marks().contains(mark));
}
#endif
QObject *createTextDocumentTest()
{
return new TextDocumentLayoutTest;
}
} // namespace TextEditor
} // TextEditor::Internal
#include "textdocumentlayout.moc"
#endif // WITH_TESTS

View File

@@ -238,6 +238,7 @@ QtcPlugin {
"highlighter_test.cpp",
"highlighter_test.h",
"texteditor_test.cpp",
"texteditor_test.h",
]
}
}

View File

@@ -4,14 +4,13 @@
#ifdef WITH_TESTS
#include "tabsettings.h"
#include "texteditorplugin.h"
#include <QTextDocument>
#include <QtTest/QtTest>
namespace TextEditor {
namespace TextEditor::Internal {
QString tabPolicyToString(TabSettings::TabPolicy policy)
static QString tabPolicyToString(TabSettings::TabPolicy policy)
{
switch (policy) {
case TabSettings::SpacesOnlyTabPolicy:
@@ -24,7 +23,7 @@ QString tabPolicyToString(TabSettings::TabPolicy policy)
return QString();
}
QString continuationAlignBehaviorToString(TabSettings::ContinuationAlignBehavior behavior)
static QString continuationAlignBehaviorToString(TabSettings::ContinuationAlignBehavior behavior)
{
switch (behavior) {
case TabSettings::NoContinuationAlign:
@@ -37,13 +36,15 @@ QString continuationAlignBehaviorToString(TabSettings::ContinuationAlignBehavior
return QString();
}
struct TabSettingsFlags{
struct TabSettingsFlags
{
TabSettings::TabPolicy policy;
TabSettings::ContinuationAlignBehavior behavior;
};
using IsClean = std::function<bool (TabSettingsFlags)>;
void generateTestRows(const QLatin1String &name, const QString &text, IsClean isClean)
static void generateTestRows(const QLatin1String &name, const QString &text, IsClean isClean)
{
const QVector<TabSettings::TabPolicy> allPolicies = {
TabSettings::SpacesOnlyTabPolicy,
@@ -74,7 +75,16 @@ void generateTestRows(const QLatin1String &name, const QString &text, IsClean is
}
}
void Internal::TextEditorPlugin::testIndentationClean_data()
class TextEditorTest final : public QObject
{
Q_OBJECT
private slots:
void testIndentationClean_data();
void testIndentationClean();
};
void TextEditorTest::testIndentationClean_data()
{
QTest::addColumn<TabSettings::TabPolicy>("policy");
QTest::addColumn<TabSettings::ContinuationAlignBehavior>("behavior");
@@ -126,7 +136,7 @@ void Internal::TextEditorPlugin::testIndentationClean_data()
});
}
void Internal::TextEditorPlugin::testIndentationClean()
void TextEditorTest::testIndentationClean()
{
// fetch test data
QFETCH(TabSettings::TabPolicy, policy);
@@ -142,6 +152,13 @@ void Internal::TextEditorPlugin::testIndentationClean()
QCOMPARE(settings.isIndentationClean(block, indentSize), clean);
}
} // namespace TextEditor
QObject *createTextEditorTest()
{
return new TextEditorTest;
}
#endif // ifdef WITH_TESTS
} // TextEditor::Internal
#include "texteditor_test.moc"
#endif // WITH_TESTS

View File

@@ -0,0 +1,15 @@
// Copyright (C) 2024 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
#pragma once
#include <QObject>
namespace TextEditor::Internal {
QObject *createTextEditorTest();
QObject *createTextDocumentTest();
QObject *createSnippetParserTest();
QObject *createFormatTextTest();
} // TextEditor::Internal

View File

@@ -16,10 +16,12 @@
#include "markdowneditor.h"
#include "outlinefactory.h"
#include "plaintexteditorfactory.h"
#include "snippets/snippet.h"
#include "snippets/snippetprovider.h"
#include "tabsettings.h"
#include "textdocument.h"
#include "texteditor.h"
#include "texteditor_test.h"
#include "texteditorconstants.h"
#include "texteditorsettings.h"
#include "texteditortr.h"
@@ -283,6 +285,13 @@ TextEditorPlugin *TextEditorPlugin::instance()
void TextEditorPlugin::initialize()
{
#ifdef WITH_TESTS
addTestCreator(createFormatTextTest);
addTestCreator(createTextDocumentTest);
addTestCreator(createTextEditorTest);
addTestCreator(createSnippetParserTest);
#endif
setupOutlineFactory();
d = new TextEditorPluginPrivate;

View File

@@ -29,20 +29,6 @@ private:
void extensionsInitialized() final;
class TextEditorPluginPrivate *d = nullptr;
#ifdef WITH_TESTS
private slots:
void testSnippetParsing_data();
void testSnippetParsing();
void testIndentationClean_data();
void testIndentationClean();
void testFormatting_data();
void testFormatting();
void testDeletingMarkOnReload();
#endif
};
} // namespace Internal