aboutsummaryrefslogtreecommitdiffstats
path: root/tests/unit/unittest/modulescanner-test.cpp
blob: 92d7959916a0384407914fc9040188c110135e65 (plain)
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
// Copyright (C) 2023 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0

#include "googletest.h"

#include <projectstorage/modulescanner.h>

#include <QDebug>

namespace {

template<typename Matcher>
auto UrlProperty(const Matcher &matcher)
{
    return Property(&QmlDesigner::Import::url, matcher);
}

template<typename Matcher>
auto VersionProperty(const Matcher &matcher)
{
    return Property(&QmlDesigner::Import::version, matcher);
}

MATCHER(HasDuplicates, std::string(negation ? "hasn't duplicates" : "has dublicates"))
{
    auto values = arg;
    std::sort(values.begin(), values.begin());
    auto found = std::adjacent_find(values.begin(), values.end());

    return found != values.end();
}

class ModuleScanner : public testing::Test
{
protected:
    QmlDesigner::ModuleScanner scanner{[](QStringView moduleName) {
                                           return moduleName.endsWith(u"impl");
                                       },
                                       QmlDesigner::VersionScanning::No};
};

TEST_F(ModuleScanner, ReturnEmptyOptionalForWrongPath)
{
    scanner.scan(QStringList{""});

    ASSERT_THAT(scanner.modules(), IsEmpty());
}

TEST_F(ModuleScanner, GetQtQuick)
{
    scanner.scan(QStringList{QT6_INSTALL_PREFIX});

    ASSERT_THAT(scanner.modules(), Contains(UrlProperty("QtQuick")));
}

TEST_F(ModuleScanner, SkipEmptyModules)
{
    scanner.scan(QStringList{QT6_INSTALL_PREFIX});

    ASSERT_THAT(scanner.modules(), Not(Contains(UrlProperty(IsEmpty()))));
}

TEST_F(ModuleScanner, UseSkipFunction)
{
    scanner.scan(QStringList{QT6_INSTALL_PREFIX});

    ASSERT_THAT(scanner.modules(), Not(Contains(UrlProperty(EndsWith(QStringView{u"impl"})))));
}

TEST_F(ModuleScanner, Version)
{
    QmlDesigner::ModuleScanner scanner{[](QStringView moduleName) {
                                           return moduleName.endsWith(u"impl");
                                       },
                                       QmlDesigner::VersionScanning::Yes};

    scanner.scan(QStringList{TESTDATA_DIR "/modulescanner"});

    ASSERT_THAT(scanner.modules(), ElementsAre(AllOf(UrlProperty("Example"), VersionProperty("1.3"))));
}

TEST_F(ModuleScanner, Duplicates)
{
    scanner.scan(QStringList{QT6_INSTALL_PREFIX});

    ASSERT_THAT(scanner.modules(), Not(HasDuplicates()));
}

TEST_F(ModuleScanner, DontAddModulesAgain)
{
    scanner.scan(QStringList{QT6_INSTALL_PREFIX});

    scanner.scan(QStringList{QT6_INSTALL_PREFIX});

    ASSERT_THAT(scanner.modules(), Not(HasDuplicates()));
}

} // namespace