forked from qt-creator/qt-creator
QmlDesigner: Add source cache name id getter
To test if a source name exists we add a getter to the id. Change-Id: If5d430402189474b85897dbe0b47f3558916d096 Reviewed-by: Burak Hancerli <burak.hancerli@qt.io> Reviewed-by: Thomas Hartmann <thomas.hartmann@qt.io>
This commit is contained in:
committed by
Thomas Hartmann
parent
72faa129c3
commit
ee05b2a65a
@@ -43,9 +43,9 @@ public:
|
|||||||
|
|
||||||
void populateIfEmpty() override
|
void populateIfEmpty() override
|
||||||
{
|
{
|
||||||
if (m_sourcePathCache.isEmpty()) {
|
if (m_sourceNameCache.isEmpty()) {
|
||||||
m_sourceContextPathCache.populate();
|
m_sourceContextPathCache.populate();
|
||||||
m_sourcePathCache.populate();
|
m_sourceNameCache.populate();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -57,15 +57,19 @@ public:
|
|||||||
|
|
||||||
Utils::SmallStringView sourceName = sourcePath.name();
|
Utils::SmallStringView sourceName = sourcePath.name();
|
||||||
|
|
||||||
auto sourceId = m_sourcePathCache.id(sourceName);
|
auto sourceNameId = m_sourceNameCache.id(sourceName);
|
||||||
|
|
||||||
return SourceId::create(sourceId, sourceContextId);
|
return SourceId::create(sourceNameId, sourceContextId);
|
||||||
}
|
}
|
||||||
|
|
||||||
SourceId sourceId(SourceContextId sourceContextId,
|
SourceNameId sourceNameId(Utils::SmallStringView sourceName) const override
|
||||||
Utils::SmallStringView sourceName) const override
|
|
||||||
{
|
{
|
||||||
SourceNameId sourceNameId = m_sourcePathCache.id(sourceName);
|
return m_sourceNameCache.id(sourceName);
|
||||||
|
}
|
||||||
|
|
||||||
|
SourceId sourceId(SourceContextId sourceContextId, Utils::SmallStringView sourceName) const override
|
||||||
|
{
|
||||||
|
SourceNameId sourceNameId = m_sourceNameCache.id(sourceName);
|
||||||
|
|
||||||
return SourceId::create(sourceNameId, sourceContextId);
|
return SourceId::create(sourceNameId, sourceContextId);
|
||||||
}
|
}
|
||||||
@@ -81,10 +85,10 @@ public:
|
|||||||
|
|
||||||
SourcePath sourcePath(SourceId sourceId) const override
|
SourcePath sourcePath(SourceId sourceId) const override
|
||||||
{
|
{
|
||||||
if (Q_UNLIKELY(!sourceId.isValid()))
|
if (!sourceId) [[unlikely]]
|
||||||
throw NoSourcePathForInvalidSourceId();
|
throw NoSourcePathForInvalidSourceId();
|
||||||
|
|
||||||
auto sourceName = m_sourcePathCache.value(sourceId.mainId());
|
auto sourceName = m_sourceNameCache.value(sourceId.mainId());
|
||||||
|
|
||||||
Utils::PathString sourceContextPath = m_sourceContextPathCache.value(sourceId.contextId());
|
Utils::PathString sourceContextPath = m_sourceContextPathCache.value(sourceId.contextId());
|
||||||
|
|
||||||
@@ -93,12 +97,20 @@ public:
|
|||||||
|
|
||||||
Utils::PathString sourceContextPath(SourceContextId sourceContextId) const override
|
Utils::PathString sourceContextPath(SourceContextId sourceContextId) const override
|
||||||
{
|
{
|
||||||
if (Q_UNLIKELY(!sourceContextId.isValid()))
|
if (!sourceContextId) [[unlikely]]
|
||||||
throw NoSourceContextPathForInvalidSourceContextId();
|
throw NoSourceContextPathForInvalidSourceContextId();
|
||||||
|
|
||||||
return m_sourceContextPathCache.value(sourceContextId);
|
return m_sourceContextPathCache.value(sourceContextId);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Utils::SmallString sourceName(SourceNameId sourceNameId) const override
|
||||||
|
{
|
||||||
|
if (!sourceNameId) [[unlikely]]
|
||||||
|
throw NoSourceNameForInvalidSourceNameId();
|
||||||
|
|
||||||
|
return m_sourceNameCache.value(sourceNameId);
|
||||||
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
class SourceContextStorageAdapter
|
class SourceContextStorageAdapter
|
||||||
{
|
{
|
||||||
@@ -157,7 +169,7 @@ private:
|
|||||||
SourceContextStorageAdapter m_sourceContextStorageAdapter;
|
SourceContextStorageAdapter m_sourceContextStorageAdapter;
|
||||||
SourceNameStorageAdapter m_sourceNameStorageAdapter;
|
SourceNameStorageAdapter m_sourceNameStorageAdapter;
|
||||||
mutable SourceContextPathCache m_sourceContextPathCache{m_sourceContextStorageAdapter};
|
mutable SourceContextPathCache m_sourceContextPathCache{m_sourceContextStorageAdapter};
|
||||||
mutable SourceNameCache m_sourcePathCache{m_sourceNameStorageAdapter};
|
mutable SourceNameCache m_sourceNameCache{m_sourceNameStorageAdapter};
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace QmlDesigner
|
} // namespace QmlDesigner
|
||||||
|
@@ -28,12 +28,16 @@ public:
|
|||||||
Utils::SmallStringView sourceName) const
|
Utils::SmallStringView sourceName) const
|
||||||
= 0;
|
= 0;
|
||||||
|
|
||||||
|
virtual SourceNameId sourceNameId(Utils::SmallStringView sourceName) const = 0;
|
||||||
|
|
||||||
virtual SourceContextId sourceContextId(Utils::SmallStringView sourceContextPath) const = 0;
|
virtual SourceContextId sourceContextId(Utils::SmallStringView sourceContextPath) const = 0;
|
||||||
|
|
||||||
virtual SourcePath sourcePath(SourceId sourceId) const = 0;
|
virtual SourcePath sourcePath(SourceId sourceId) const = 0;
|
||||||
|
|
||||||
virtual Utils::PathString sourceContextPath(SourceContextId sourceContextId) const = 0;
|
virtual Utils::PathString sourceContextPath(SourceContextId sourceContextId) const = 0;
|
||||||
|
|
||||||
|
virtual Utils::SmallString sourceName(SourceNameId sourceNameId) const = 0;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
~SourcePathCacheInterface() = default;
|
~SourcePathCacheInterface() = default;
|
||||||
};
|
};
|
||||||
|
@@ -56,4 +56,14 @@ const char *SourceNameIdDoesNotExists::what() const noexcept
|
|||||||
return "The source id does not exist in the database!";
|
return "The source id does not exist in the database!";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
NoSourceNameForInvalidSourceNameId::NoSourceNameForInvalidSourceNameId()
|
||||||
|
{
|
||||||
|
category().threadEvent("NoSourceNameForInvalidSourceNameId");
|
||||||
|
}
|
||||||
|
|
||||||
|
const char *NoSourceNameForInvalidSourceNameId::what() const noexcept
|
||||||
|
{
|
||||||
|
return "You cannot get a source name for an invalid source name id!";
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace QmlDesigner
|
} // namespace QmlDesigner
|
||||||
|
@@ -27,6 +27,13 @@ public:
|
|||||||
const char *what() const noexcept override;
|
const char *what() const noexcept override;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
class QMLDESIGNERCORE_EXPORT NoSourceNameForInvalidSourceNameId : public SourcePathError
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
NoSourceNameForInvalidSourceNameId();
|
||||||
|
const char *what() const noexcept override;
|
||||||
|
};
|
||||||
|
|
||||||
class QMLDESIGNERCORE_EXPORT NoSourceContextPathForInvalidSourceContextId : public SourcePathError
|
class QMLDESIGNERCORE_EXPORT NoSourceContextPathForInvalidSourceContextId : public SourcePathError
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
@@ -24,6 +24,10 @@ public:
|
|||||||
sourceId,
|
sourceId,
|
||||||
(QmlDesigner::SourceContextId sourceContextId, Utils::SmallStringView sourceName),
|
(QmlDesigner::SourceContextId sourceContextId, Utils::SmallStringView sourceName),
|
||||||
(const, override));
|
(const, override));
|
||||||
|
MOCK_METHOD(QmlDesigner::SourceNameId,
|
||||||
|
sourceNameId,
|
||||||
|
(Utils::SmallStringView sourceName),
|
||||||
|
(const, override));
|
||||||
MOCK_METHOD(QmlDesigner::SourcePath,
|
MOCK_METHOD(QmlDesigner::SourcePath,
|
||||||
sourcePath,
|
sourcePath,
|
||||||
(QmlDesigner::SourceId sourceId),
|
(QmlDesigner::SourceId sourceId),
|
||||||
@@ -36,6 +40,10 @@ public:
|
|||||||
sourceContextPath,
|
sourceContextPath,
|
||||||
(QmlDesigner::SourceContextId directoryPathId),
|
(QmlDesigner::SourceContextId directoryPathId),
|
||||||
(const, override));
|
(const, override));
|
||||||
|
MOCK_METHOD(Utils::SmallString,
|
||||||
|
sourceName,
|
||||||
|
(QmlDesigner::SourceNameId sourceName),
|
||||||
|
(const, override));
|
||||||
MOCK_METHOD(void, populateIfEmpty, (), (override));
|
MOCK_METHOD(void, populateIfEmpty, (), (override));
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@@ -372,4 +372,78 @@ TEST_F(SourcePathCache, get_file_path_after_populate_if_empty)
|
|||||||
ASSERT_THAT(path, Eq("/path/to/file.cpp"));
|
ASSERT_THAT(path, Eq("/path/to/file.cpp"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST_F(SourcePathCache, source_name_id_calls_fetch_source_name_id)
|
||||||
|
{
|
||||||
|
EXPECT_CALL(storageMock, fetchSourceNameId(Eq("file.cpp")));
|
||||||
|
|
||||||
|
cache.sourceNameId(Utils::SmallString("file.cpp"));
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_F(SourcePathCache, second_source_name_id_calls_not_fetch_source_name_id)
|
||||||
|
{
|
||||||
|
cache.sourceNameId(Utils::SmallString("file.cpp"));
|
||||||
|
|
||||||
|
EXPECT_CALL(storageMock, fetchSourceNameId(Eq("file.cpp"))).Times(0);
|
||||||
|
|
||||||
|
cache.sourceNameId(Utils::SmallString("file.cpp"));
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_F(SourcePathCache, source_name_id)
|
||||||
|
{
|
||||||
|
auto id = cache.sourceNameId(Utils::SmallString("file.cpp"));
|
||||||
|
|
||||||
|
ASSERT_THAT(id, Eq(sourceNameId42));
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_F(SourcePathCache, source_name_id_is_already_in_cache)
|
||||||
|
{
|
||||||
|
auto firstId = cache.sourceNameId(Utils::SmallString("file.cpp"));
|
||||||
|
|
||||||
|
auto secondId = cache.sourceNameId(Utils::SmallString("file.cpp"));
|
||||||
|
|
||||||
|
ASSERT_THAT(secondId, firstId);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_F(SourcePathCache, throw_for_getting_source_name_with_an_invalid_id)
|
||||||
|
{
|
||||||
|
SourceNameId sourceNameId;
|
||||||
|
|
||||||
|
ASSERT_THROW(cache.sourceName(sourceNameId), QmlDesigner::NoSourceNameForInvalidSourceNameId);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_F(SourcePathCache, get_a_source_name)
|
||||||
|
{
|
||||||
|
SourceNameId sourceNameId{sourceNameId42};
|
||||||
|
|
||||||
|
auto sourceName = cache.sourceName(sourceNameId);
|
||||||
|
|
||||||
|
ASSERT_THAT(sourceName, Eq(Utils::SmallStringView{"file.cpp"}));
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_F(SourcePathCache, get_a_source_name_with_cached_source_name_id)
|
||||||
|
{
|
||||||
|
SourceNameId sourceNameId{sourceNameId42};
|
||||||
|
cache.sourceName(sourceNameId);
|
||||||
|
|
||||||
|
auto sourceName = cache.sourceName(sourceNameId);
|
||||||
|
|
||||||
|
ASSERT_THAT(sourceName, Eq(Utils::SmallStringView{"file.cpp"}));
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_F(SourcePathCache, source_name_calls_fetch_source_name)
|
||||||
|
{
|
||||||
|
EXPECT_CALL(storageMock, fetchSourceName(Eq(sourceNameId42)));
|
||||||
|
|
||||||
|
cache.sourceName(sourceNameId42);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_F(SourcePathCache, second_source_name_calls_not_fetch_source_name)
|
||||||
|
{
|
||||||
|
cache.sourceName(sourceNameId42);
|
||||||
|
|
||||||
|
EXPECT_CALL(storageMock, fetchSourceName(_)).Times(0);
|
||||||
|
|
||||||
|
cache.sourceName(sourceNameId42);
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace
|
} // namespace
|
||||||
|
Reference in New Issue
Block a user