File: test_document.cpp

package info (click to toggle)
kdevelop 4%3A24.12.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 71,888 kB
  • sloc: cpp: 290,869; python: 3,626; javascript: 3,518; sh: 1,316; ansic: 703; xml: 401; php: 95; lisp: 66; makefile: 31; sed: 12
file content (118 lines) | stat: -rw-r--r-- 2,895 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/*
    SPDX-FileCopyrightText: 2007 Alexander Dymo <adymo@kdevelop.org>

    SPDX-License-Identifier: LGPL-2.0-or-later
*/

#include "test_document.h"

#include <sublime/controller.h>
#include <sublime/tooldocument.h>
#include <sublime/urldocument.h>
#include <sublime/view.h>
#include <tests/testhelpermacros.h>

#include <QIcon>
#include <QMimeDatabase>
#include <QMimeType>
#include <QStandardPaths>
#include <QTest>
#include <QTextEdit>
#include <QUrl>

using namespace Sublime;

namespace {
QUrl makeUrl(const QString& fileName)
{
    constexpr auto nonexistentDirPath =
#ifdef Q_OS_WIN
        "C:"
#endif
        "/non/existent/path/to/a-dir/";

    return QUrl::fromLocalFile(nonexistentDirPath + fileName);
}

void testUrlDocument(const UrlDocument& doc, const QUrl& url)
{
    QCOMPARE(doc.url(), url);
    QCOMPARE(doc.title(Document::TitleType::Normal), url.fileName());

    QCOMPARE(doc.statusIcon(), QIcon{});
    QCOMPARE(doc.icon(), doc.defaultIcon());

    QCOMPARE(doc.mimeType(), QMimeDatabase().mimeTypeForUrl(url));
    QCOMPARE(doc.defaultIcon(), QIcon::fromTheme(doc.mimeType().iconName()));
}

class EditableUrlDocument : public UrlDocument
{
    Q_OBJECT
public:
    using UrlDocument::setUrl;
    using UrlDocument::UrlDocument;
};
} // unnamed namespace

void TestDocument::initTestCase()
{
    QStandardPaths::setTestModeEnabled(true);
}

void TestDocument::testUrlDocument_data()
{
    QTest::addColumn<QString>("fileName");

    QTest::newRow("c") << "source.c";
    QTest::newRow("cpp") << "test.cpp";
    QTest::newRow("python") << "a.py";
    QTest::newRow("php") << "d.php";
    QTest::newRow("Objective-C") << "obj.m";
    QTest::newRow("CMakeLists") << "CMakeLists.txt";
    QTest::newRow("cmake") << "module.cmake";
    QTest::newRow("diff") << "my.diff";
    QTest::newRow("htm") << "page.htm";
    QTest::newRow("java") << "to.java";
    QTest::newRow("txt") << "bar.txt";
    QTest::newRow("no-extension") << "x";
}

void TestDocument::testUrlDocument()
{
    QFETCH(const QString, fileName);
    const auto url = makeUrl(fileName);

    Controller controller;
    EditableUrlDocument doc(&controller, url);

    ::testUrlDocument(doc, url);
    RETURN_IF_TEST_FAILED();

    const auto plainTextUrl = makeUrl("x.txt");
    doc.setUrl(plainTextUrl);
    ::testUrlDocument(doc, plainTextUrl);
    RETURN_IF_TEST_FAILED();

    doc.setUrl(url);
    ::testUrlDocument(doc, url);
    RETURN_IF_TEST_FAILED();
}

void TestDocument::viewDeletion()
{
    Controller controller;
    Document *doc = new ToolDocument(QStringLiteral("tool"), &controller, new SimpleToolWidgetFactory<QTextEdit>(QStringLiteral("tool")));

    View *view = doc->createView();
    view->widget();
    QCOMPARE(doc->views().count(), 1);

    delete view;
    QCOMPARE(doc->views().count(), 0);
}

QTEST_MAIN(TestDocument)

#include "test_document.moc"
#include "moc_test_document.cpp"