Skip to content

Commit

Permalink
Add ResourceLoader::resource_exists
Browse files Browse the repository at this point in the history
  • Loading branch information
adamkewley committed Jan 17, 2025
1 parent 360d255 commit 924a759
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 0 deletions.
6 changes: 6 additions & 0 deletions liboscar/Platform/FilesystemResourceLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ namespace
}
}

bool osc::FilesystemResourceLoader::impl_resource_exists(const ResourcePath& resource_path)
{
const std::filesystem::path full_path = calc_full_path(root_directory_, resource_path);
return std::filesystem::exists(full_path);
}

ResourceStream osc::FilesystemResourceLoader::impl_open(const ResourcePath& resource_path)
{
if (log_level() <= LogLevel::debug) {
Expand Down
1 change: 1 addition & 0 deletions liboscar/Platform/FilesystemResourceLoader.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ namespace osc
{}

private:
bool impl_resource_exists(const ResourcePath&) final;
ResourceStream impl_open(const ResourcePath&) final;
std::function<std::optional<ResourceDirectoryEntry>()> impl_iterate_directory(const ResourcePath&) final;

Expand Down
2 changes: 2 additions & 0 deletions liboscar/Platform/IResourceLoader.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ namespace osc
public:
virtual ~IResourceLoader() noexcept = default;

bool resource_exists(const ResourcePath& resource_path) { return impl_resource_exists(resource_path); }
ResourceStream open(const ResourcePath& resource_path) { return impl_open(resource_path); }
std::string slurp(const ResourcePath&);

Expand All @@ -30,6 +31,7 @@ namespace osc
}

private:
virtual bool impl_resource_exists(const ResourcePath&) = 0;
virtual ResourceStream impl_open(const ResourcePath&) = 0;
virtual std::function<std::optional<ResourceDirectoryEntry>()> impl_iterate_directory(const ResourcePath&)
{
Expand Down
5 changes: 5 additions & 0 deletions liboscar/Platform/ResourceLoader.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ namespace osc
operator IResourceLoader& () { return *impl_; }
operator const IResourceLoader& () const { return *impl_; }

bool resource_exists(const ResourcePath& resource_path)
{
return impl_->resource_exists(resource_path);
}

ResourceStream open(const ResourcePath& resource_path)
{
return impl_->open(prefix_ / resource_path);
Expand Down
15 changes: 15 additions & 0 deletions liboscar/Platform/ResourceLoader.tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ namespace
{
struct MockState final {
std::optional<ResourcePath> last_open_call_path;
std::optional<ResourcePath> last_existence_check_path;
};

class MockResourceLoader : public IResourceLoader {
Expand All @@ -26,6 +27,12 @@ namespace
state_{std::move(state_)}
{}
private:
bool impl_resource_exists(const ResourcePath& resource_path) final
{
state_->last_existence_check_path = resource_path;
return true;
}

ResourceStream impl_open(const ResourcePath& resource_path) override
{
state_->last_open_call_path = resource_path;
Expand Down Expand Up @@ -64,3 +71,11 @@ TEST(ResourceLoader, WithPrefixCausesIResourceLoaderToBeCalledWithPrefixedPath)
prefixed_loader.open(ResourcePath{"path"});
ASSERT_EQ(mock_state->last_open_call_path, ResourcePath{"prefix/path"}) << "with_prefix should return a loader the prefixes each open call";
}

TEST(ResourceLoader, resource_exists_calls_underlying_impl_resource_exists)
{
const auto mock_state = std::make_shared<MockState>();
ResourceLoader resource_loader = make_resource_loader<MockResourceLoader>(mock_state);
ASSERT_TRUE(resource_loader.resource_exists("should/exist"));
ASSERT_EQ(mock_state->last_existence_check_path, ResourcePath{"should/exist"});
}

0 comments on commit 924a759

Please sign in to comment.