Skip to content

Commit

Permalink
feat(resource_resolver): add class and unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
kionz committed Aug 16, 2017
1 parent 5a7f906 commit 03ee8b4
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/rime/resource_resolver.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//
// Copyright RIME Developers
// Distributed under the BSD License
//

#include <rime/resource_resolver.h>

namespace rime {

boost::filesystem::path ResourceResolver::ResolvePath(const string& resource_id) {
return boost::filesystem::absolute(
boost::filesystem::path(type_.prefix + resource_id + type_.suffix),
root_path_);
}

} // namespace rime
40 changes: 40 additions & 0 deletions src/rime/resource_resolver.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
//
// Copyright RIME Developers
// Distributed under the BSD License
//

#ifndef RIME_RESOURCE_RESOLVER_H_
#define RIME_RESOURCE_RESOLVER_H_

#include <boost/filesystem.hpp>
#include <rime/common.h>

namespace rime {

class ResourceResolver;

struct ResourceType {
string name;
string prefix;
string suffix;
};

class ResourceResolver {
public:
explicit ResourceResolver(const ResourceType type) : type_(type) {
}
boost::filesystem::path ResolvePath(const string& resource_id);
void set_root_path(const boost::filesystem::path& root_path) {
root_path_ = root_path;
}
boost::filesystem::path root_path() const {
return root_path_;
}
private:
const ResourceType type_;
boost::filesystem::path root_path_;
};

} // namespace rime

#endif // RIME_RESOURCE_RESOLVER_H_
17 changes: 17 additions & 0 deletions test/resource_resolver_test.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include <gtest/gtest.h>
#include <rime/resource_resolver.h>

using namespace rime;

TEST(RimeResourceResolverTest, ResolvePath) {
const auto type = ResourceType{
"minerals",
"not_",
".minerals",
};
the<ResourceResolver> rr(new ResourceResolver(type));
rr->set_root_path("/starcraft");
auto actual = rr->ResolvePath("enough");
boost::filesystem::path expected = "/starcraft/not_enough.minerals";
EXPECT_TRUE(expected == actual);
}

0 comments on commit 03ee8b4

Please sign in to comment.