QmlDesigner: Remove duplicates

Change-Id: I5b2d8433c0aca286ec530f237750ba660c409a48
Reviewed-by: Aleksei German <aleksei.german@qt.io>
Reviewed-by: Marco Bubke <marco.bubke@qt.io>
This commit is contained in:
Marco Bubke
2023-04-21 15:47:50 +02:00
parent 79fee70ed2
commit eafe43399d
2 changed files with 28 additions and 6 deletions

View File

@@ -51,8 +51,6 @@ void ModuleScanner::scan([[maybe_unused]] std::string_view modulePath)
#ifdef QDS_HAS_QMLPRIVATE
QDirIterator dirIterator{QString::fromUtf8(modulePath), QDir::Dirs, QDirIterator::Subdirectories};
QMap<QString, bool> moduleNames;
while (dirIterator.hasNext()) {
auto directoryPath = dirIterator.next();
QString qmldirPath = directoryPath + "/qmldir";
@@ -72,14 +70,13 @@ void ModuleScanner::scan([[maybe_unused]] std::string_view modulePath)
if (moduleName.isEmpty() || m_skip(moduleName))
continue;
if (moduleNames.contains(moduleName))
continue;
moduleNames.insert(moduleName, true);
m_modules.push_back(
Import::createLibraryImport(moduleName, createVersion(parser.components())));
}
}
std::sort(m_modules.begin(), m_modules.end());
m_modules.erase(std::unique(m_modules.begin(), m_modules.end()), m_modules.end());
#endif
}

View File

@@ -21,6 +21,15 @@ 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:
@@ -70,4 +79,20 @@ TEST_F(ModuleScanner, Version)
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