From 610a110341c18ed55ca783c50829d3cb70b5c82c Mon Sep 17 00:00:00 2001 From: hjk Date: Fri, 19 Jan 2024 17:48:18 +0100 Subject: [PATCH] TextEditor: Move test creation closer to tested code Change-Id: I0f132dddd405bf17161e0e032f9496a90cdc5ab9 Reviewed-by: David Schulz --- .../texteditor/codeassist/codeassist_test.cpp | 51 +++++++++++++++---- .../texteditor/codeassist/codeassist_test.h | 27 ++-------- src/plugins/texteditor/highlighter_test.cpp | 23 +++++++++ src/plugins/texteditor/highlighter_test.h | 19 +------ src/plugins/texteditor/texteditorplugin.cpp | 4 +- 5 files changed, 70 insertions(+), 54 deletions(-) diff --git a/src/plugins/texteditor/codeassist/codeassist_test.cpp b/src/plugins/texteditor/codeassist/codeassist_test.cpp index 8b724dbe166..adb32dd7589 100644 --- a/src/plugins/texteditor/codeassist/codeassist_test.cpp +++ b/src/plugins/texteditor/codeassist/codeassist_test.cpp @@ -43,10 +43,10 @@ public: quint64 hash() const override { return 0; } // used to remove duplicates }; -class OpenEditorItem : public TestProposalItem +class OpenEditorItem final : public TestProposalItem { public: - void apply(TextDocumentManipulatorInterface &, int) const override + void apply(TextDocumentManipulatorInterface &, int) const final { m_openedEditor = Core::EditorManager::openEditor(m_filePath, Core::Constants::K_DEFAULT_TEXT_EDITOR_ID); @@ -56,10 +56,10 @@ public: Utils::FilePath m_filePath; }; -class TestProposalWidget : public GenericProposalWidget +class TestProposalWidget final : public GenericProposalWidget { public: - void showProposal(const QString &prefix) override + void showProposal(const QString &prefix) final { GenericProposalModelPtr proposalModel = model(); if (proposalModel && proposalModel->size() == 1) { @@ -71,30 +71,34 @@ public: } }; -class TestProposal : public GenericProposal +class TestProposal final : public GenericProposal { public: TestProposal(int pos, const QList &items) : GenericProposal(pos, items) {} - IAssistProposalWidget *createWidget() const override { return new TestProposalWidget; } + IAssistProposalWidget *createWidget() const final { return new TestProposalWidget; } }; -class TestProcessor : public AsyncProcessor +class TestProcessor final : public AsyncProcessor { public: TestProcessor(const QList &items) : m_items(items) {} - IAssistProposal *performAsync() override - { return new TestProposal(interface()->position(), m_items); } + + IAssistProposal *performAsync() final + { + return new TestProposal(interface()->position(), m_items); + } + QList m_items; }; -class TestProvider : public CompletionAssistProvider +class TestProvider final : public CompletionAssistProvider { public: - IAssistProcessor *createProcessor(const AssistInterface *assistInterface) const override + IAssistProcessor *createProcessor(const AssistInterface *assistInterface) const final { Q_UNUSED(assistInterface); return new TestProcessor(m_items); @@ -102,6 +106,24 @@ public: QList m_items; }; + +class CodeAssistTests final : public QObject +{ + Q_OBJECT + +private slots: + void initTestCase(); + + void testFollowSymbolBigFile(); + + void cleanupTestCase(); + +private: + TextEditor::BaseTextEditor *m_editor = nullptr; + QList m_editorsToClose; + TestProvider *m_testProvider = nullptr; +}; + void CodeAssistTests::initTestCase() { Core::IEditor *editor = Core::EditorManager::openEditorWithContents( @@ -147,6 +169,13 @@ void CodeAssistTests::cleanupTestCase() QVERIFY(Core::EditorManager::currentEditor() == nullptr); } +QObject *createCodeAssistTests() +{ + return new CodeAssistTests; +} + } // namespace TextEditor::Internal +#include "codeassist_test.moc" + #endif // ifdef WITH_TESTS diff --git a/src/plugins/texteditor/codeassist/codeassist_test.h b/src/plugins/texteditor/codeassist/codeassist_test.h index 3bf734aadfb..16895d539ba 100644 --- a/src/plugins/texteditor/codeassist/codeassist_test.h +++ b/src/plugins/texteditor/codeassist/codeassist_test.h @@ -7,31 +7,10 @@ #include -namespace Core { class IEditor; } -namespace TextEditor { class BaseTextEditor; } - namespace TextEditor::Internal { -class TestProvider; +QObject *createCodeAssistTests(); -class CodeAssistTests : public QObject -{ - Q_OBJECT -public: +} // TextEditor::Internal -private slots: - void initTestCase(); - - void testFollowSymbolBigFile(); - - void cleanupTestCase(); - -private: - TextEditor::BaseTextEditor *m_editor = nullptr; - QList m_editorsToClose; - TestProvider *m_testProvider = nullptr; -}; - -} // namespace TextEditor::Internal - -#endif +#endif // WITH_TESTS diff --git a/src/plugins/texteditor/highlighter_test.cpp b/src/plugins/texteditor/highlighter_test.cpp index 64cf2c2f3c9..d50c560e23a 100644 --- a/src/plugins/texteditor/highlighter_test.cpp +++ b/src/plugins/texteditor/highlighter_test.cpp @@ -34,6 +34,22 @@ constexpr auto json = R"( } )"; +class GenerigHighlighterTests final : public QObject +{ + Q_OBJECT + +private slots: + void initTestCase(); + void testHighlight_data(); + void testHighlight(); + void testChange(); + void testPreeditText(); + void cleanupTestCase(); + +private: + BaseTextEditor *m_editor = nullptr; +}; + void GenerigHighlighterTests::initTestCase() { QString title = "test.json"; @@ -221,4 +237,11 @@ void GenerigHighlighterTests::cleanupTestCase() QVERIFY(Core::EditorManager::currentEditor() == nullptr); } +QObject *createGenericHighlighterTests() +{ + return new GenerigHighlighterTests; +} + } // namespace TextEditor::Internal + +#include "highlighter_test.moc" diff --git a/src/plugins/texteditor/highlighter_test.h b/src/plugins/texteditor/highlighter_test.h index 3e638783d54..a3ca59415fe 100644 --- a/src/plugins/texteditor/highlighter_test.h +++ b/src/plugins/texteditor/highlighter_test.h @@ -5,23 +5,8 @@ #include -namespace TextEditor { class BaseTextEditor; } - namespace TextEditor::Internal { -class GenerigHighlighterTests : public QObject -{ - Q_OBJECT -private slots: - void initTestCase(); - void testHighlight_data(); - void testHighlight(); - void testChange(); - void testPreeditText(); - void cleanupTestCase(); +QObject *createGenericHighlighterTests(); -private: - TextEditor::BaseTextEditor *m_editor = nullptr; -}; - -} // namespace TextEditor::Internal +} // TextEditor::Internal diff --git a/src/plugins/texteditor/texteditorplugin.cpp b/src/plugins/texteditor/texteditorplugin.cpp index 85c0833cdca..533b2ffb24f 100644 --- a/src/plugins/texteditor/texteditorplugin.cpp +++ b/src/plugins/texteditor/texteditorplugin.cpp @@ -177,8 +177,8 @@ void TextEditorPlugin::initialize() d->createStandardContextMenu(); #ifdef WITH_TESTS - addTest(); - addTest(); + addTestCreator(createCodeAssistTests); + addTestCreator(createGenericHighlighterTests); #endif }