TextEditor: Move test creation closer to tested code

Change-Id: I0f132dddd405bf17161e0e032f9496a90cdc5ab9
Reviewed-by: David Schulz <david.schulz@qt.io>
This commit is contained in:
hjk
2024-01-19 17:48:18 +01:00
parent df9b802842
commit 610a110341
5 changed files with 70 additions and 54 deletions

View File

@@ -43,10 +43,10 @@ public:
quint64 hash() const override { return 0; } // used to remove duplicates quint64 hash() const override { return 0; } // used to remove duplicates
}; };
class OpenEditorItem : public TestProposalItem class OpenEditorItem final : public TestProposalItem
{ {
public: public:
void apply(TextDocumentManipulatorInterface &, int) const override void apply(TextDocumentManipulatorInterface &, int) const final
{ {
m_openedEditor = Core::EditorManager::openEditor(m_filePath, m_openedEditor = Core::EditorManager::openEditor(m_filePath,
Core::Constants::K_DEFAULT_TEXT_EDITOR_ID); Core::Constants::K_DEFAULT_TEXT_EDITOR_ID);
@@ -56,10 +56,10 @@ public:
Utils::FilePath m_filePath; Utils::FilePath m_filePath;
}; };
class TestProposalWidget : public GenericProposalWidget class TestProposalWidget final : public GenericProposalWidget
{ {
public: public:
void showProposal(const QString &prefix) override void showProposal(const QString &prefix) final
{ {
GenericProposalModelPtr proposalModel = model(); GenericProposalModelPtr proposalModel = model();
if (proposalModel && proposalModel->size() == 1) { if (proposalModel && proposalModel->size() == 1) {
@@ -71,30 +71,34 @@ public:
} }
}; };
class TestProposal : public GenericProposal class TestProposal final : public GenericProposal
{ {
public: public:
TestProposal(int pos, const QList<AssistProposalItemInterface *> &items) TestProposal(int pos, const QList<AssistProposalItemInterface *> &items)
: GenericProposal(pos, 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: public:
TestProcessor(const QList<AssistProposalItemInterface *> &items) TestProcessor(const QList<AssistProposalItemInterface *> &items)
: m_items(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<AssistProposalItemInterface *> m_items; QList<AssistProposalItemInterface *> m_items;
}; };
class TestProvider : public CompletionAssistProvider class TestProvider final : public CompletionAssistProvider
{ {
public: public:
IAssistProcessor *createProcessor(const AssistInterface *assistInterface) const override IAssistProcessor *createProcessor(const AssistInterface *assistInterface) const final
{ {
Q_UNUSED(assistInterface); Q_UNUSED(assistInterface);
return new TestProcessor(m_items); return new TestProcessor(m_items);
@@ -102,6 +106,24 @@ public:
QList<AssistProposalItemInterface *> m_items; QList<AssistProposalItemInterface *> m_items;
}; };
class CodeAssistTests final : public QObject
{
Q_OBJECT
private slots:
void initTestCase();
void testFollowSymbolBigFile();
void cleanupTestCase();
private:
TextEditor::BaseTextEditor *m_editor = nullptr;
QList<Core::IEditor *> m_editorsToClose;
TestProvider *m_testProvider = nullptr;
};
void CodeAssistTests::initTestCase() void CodeAssistTests::initTestCase()
{ {
Core::IEditor *editor = Core::EditorManager::openEditorWithContents( Core::IEditor *editor = Core::EditorManager::openEditorWithContents(
@@ -147,6 +169,13 @@ void CodeAssistTests::cleanupTestCase()
QVERIFY(Core::EditorManager::currentEditor() == nullptr); QVERIFY(Core::EditorManager::currentEditor() == nullptr);
} }
QObject *createCodeAssistTests()
{
return new CodeAssistTests;
}
} // namespace TextEditor::Internal } // namespace TextEditor::Internal
#include "codeassist_test.moc"
#endif // ifdef WITH_TESTS #endif // ifdef WITH_TESTS

View File

@@ -7,31 +7,10 @@
#include <QObject> #include <QObject>
namespace Core { class IEditor; }
namespace TextEditor { class BaseTextEditor; }
namespace TextEditor::Internal { namespace TextEditor::Internal {
class TestProvider; QObject *createCodeAssistTests();
class CodeAssistTests : public QObject } // TextEditor::Internal
{
Q_OBJECT
public:
private slots: #endif // WITH_TESTS
void initTestCase();
void testFollowSymbolBigFile();
void cleanupTestCase();
private:
TextEditor::BaseTextEditor *m_editor = nullptr;
QList<Core::IEditor *> m_editorsToClose;
TestProvider *m_testProvider = nullptr;
};
} // namespace TextEditor::Internal
#endif

View File

@@ -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() void GenerigHighlighterTests::initTestCase()
{ {
QString title = "test.json"; QString title = "test.json";
@@ -221,4 +237,11 @@ void GenerigHighlighterTests::cleanupTestCase()
QVERIFY(Core::EditorManager::currentEditor() == nullptr); QVERIFY(Core::EditorManager::currentEditor() == nullptr);
} }
QObject *createGenericHighlighterTests()
{
return new GenerigHighlighterTests;
}
} // namespace TextEditor::Internal } // namespace TextEditor::Internal
#include "highlighter_test.moc"

View File

@@ -5,23 +5,8 @@
#include <QObject> #include <QObject>
namespace TextEditor { class BaseTextEditor; }
namespace TextEditor::Internal { namespace TextEditor::Internal {
class GenerigHighlighterTests : public QObject QObject *createGenericHighlighterTests();
{
Q_OBJECT
private slots:
void initTestCase();
void testHighlight_data();
void testHighlight();
void testChange();
void testPreeditText();
void cleanupTestCase();
private: } // TextEditor::Internal
TextEditor::BaseTextEditor *m_editor = nullptr;
};
} // namespace TextEditor::Internal

View File

@@ -177,8 +177,8 @@ void TextEditorPlugin::initialize()
d->createStandardContextMenu(); d->createStandardContextMenu();
#ifdef WITH_TESTS #ifdef WITH_TESTS
addTest<CodeAssistTests>(); addTestCreator(createCodeAssistTests);
addTest<GenerigHighlighterTests>(); addTestCreator(createGenericHighlighterTests);
#endif #endif
} }