diff options
author | Andreas Stöckel <astoecke@techfak.uni-bielefeld.de> | 2015-01-20 00:53:49 +0100 |
---|---|---|
committer | Andreas Stöckel <astoecke@techfak.uni-bielefeld.de> | 2015-01-20 00:53:49 +0100 |
commit | 47311cc8b211a7fef033d744d9eba9f308726ea8 (patch) | |
tree | 348726aef17297729233b93b6d7eef86f25c7a78 /test | |
parent | d836d70ea2352dcf277c6fce91ba1ded3f074b44 (diff) |
Refactored stuff surrounding the ResourceLocator class, implemented StaticResourceLocator which can be used for registering static resources (mainly for testing or if certain resources need to be available from the executable)
Diffstat (limited to 'test')
-rw-r--r-- | test/core/RegistryTest.cpp | 48 | ||||
-rw-r--r-- | test/core/parser/StandaloneParserContext.hpp | 6 | ||||
-rw-r--r-- | test/core/resource/ResourceLocatorTest.cpp | 53 | ||||
-rw-r--r-- | test/plugins/boost/FileLocatorTest.cpp | 89 |
4 files changed, 83 insertions, 113 deletions
diff --git a/test/core/RegistryTest.cpp b/test/core/RegistryTest.cpp index 8280188..45e09d3 100644 --- a/test/core/RegistryTest.cpp +++ b/test/core/RegistryTest.cpp @@ -18,46 +18,26 @@ #include <gtest/gtest.h> -#include <core/Registry.hpp> - #include <sstream> -#include <core/common/Logger.hpp> +#include <core/resource/ResourceLocator.hpp> +#include <core/Registry.hpp> namespace ousia { -class TestResourceLocator : public ResourceLocator { -public: - ResourceLocator::Location locate( - const std::string &path, const std::string &relativeTo, - const ResourceLocator::Type type) const override - { - // trivial test implementation. - return ResourceLocator::Location(true, *this, type, path); - } - - std::unique_ptr<std::istream> stream( - const std::string &location) const override - { - // trivial test implementation. - std::unique_ptr<std::stringstream> ss(new std::stringstream()); - (*ss) << "test"; - return std::move(ss); - } -}; - TEST(Registry, locateResource) { - TestResourceLocator locator; - Logger logger; - Registry instance {logger}; - instance.registerResourceLocator(&locator); - - ResourceLocator::Location location = - instance.locateResource("path", "", ResourceLocator::Type::DOMAIN_DESC); - ASSERT_TRUE(location.found); - ASSERT_EQ(ResourceLocator::Type::DOMAIN_DESC, location.type); - ASSERT_EQ("path", location.location); + StaticResourceLocator locator; + locator.store("path", "test"); + + Registry registry; + registry.registerResourceLocator(locator); + + Resource res; + ASSERT_TRUE( + registry.locateResource(res, "path", ResourceType::DOMAIN_DESC)); + ASSERT_TRUE(res.isValid()); + ASSERT_EQ(ResourceType::DOMAIN_DESC, res.getType()); + ASSERT_EQ("path", res.getLocation()); } - } diff --git a/test/core/parser/StandaloneParserContext.hpp b/test/core/parser/StandaloneParserContext.hpp index aca056e..347d34f 100644 --- a/test/core/parser/StandaloneParserContext.hpp +++ b/test/core/parser/StandaloneParserContext.hpp @@ -37,15 +37,13 @@ public: ParserContext context; StandaloneParserContext() - : registry(logger), - project(new model::Project(manager)), + : project(new model::Project(manager)), context(scope, registry, logger, manager, project) { } StandaloneParserContext(Logger &externalLogger) - : registry(externalLogger), - project(new model::Project(manager)), + : project(new model::Project(manager)), context(scope, registry, externalLogger, manager, project){}; }; } diff --git a/test/core/resource/ResourceLocatorTest.cpp b/test/core/resource/ResourceLocatorTest.cpp index ebb164d..d009883 100644 --- a/test/core/resource/ResourceLocatorTest.cpp +++ b/test/core/resource/ResourceLocatorTest.cpp @@ -18,48 +18,31 @@ #include <gtest/gtest.h> -#include <core/ResourceLocator.hpp> - -#include <sstream> +#include <core/resource/ResourceLocator.hpp> namespace ousia { -class TestResourceLocator : public ResourceLocator { -public: - ResourceLocator::Location locate( - const std::string &path, const std::string &relativeTo, - const ResourceLocator::Type type) const override - { - // trivial test implementation. - return ResourceLocator::Location(true, *this, type, path); - } - - std::unique_ptr<std::istream> stream( - const std::string &location) const override - { - // trivial test implementation. - std::unique_ptr<std::stringstream> ss(new std::stringstream()); - (*ss) << "test"; - return std::move(ss); - } -}; - -TEST(ResourceLocator, locate) +TEST(StaticResourceLocator, locate) { - TestResourceLocator instance; - ResourceLocator::Location location = - instance.locate("path", "", ResourceLocator::Type::DOMAIN_DESC); - ASSERT_TRUE(location.found); - ASSERT_EQ(ResourceLocator::Type::DOMAIN_DESC, location.type); - ASSERT_EQ("path", location.location); + StaticResourceLocator locator; + locator.store("path", "test"); + + Resource res; + ASSERT_TRUE(locator.locate(res, "path")); + ASSERT_TRUE(res.isValid()); + ASSERT_EQ(ResourceType::UNKNOWN, res.getType()); + ASSERT_EQ("path", res.getLocation()); } -TEST(ResourceLocator, stream) +TEST(StaticResourceLocator, stream) { - TestResourceLocator instance; - ResourceLocator::Location location = - instance.locate("path", "", ResourceLocator::Type::DOMAIN_DESC); - std::unique_ptr<std::istream> is = location.stream(); + StaticResourceLocator locator; + locator.store("path", "test"); + + Resource res; + ASSERT_TRUE(locator.locate(res, "path")); + + auto is = res.stream(); std::string str; *is >> str; diff --git a/test/plugins/boost/FileLocatorTest.cpp b/test/plugins/boost/FileLocatorTest.cpp index 2057a9f..473b15e 100644 --- a/test/plugins/boost/FileLocatorTest.cpp +++ b/test/plugins/boost/FileLocatorTest.cpp @@ -20,67 +20,75 @@ #include <plugins/boost/FileLocator.hpp> +#include <boost/filesystem.hpp> + namespace ousia { TEST(FileLocator, testAddSearchPath) { FileLocator instance; - ASSERT_EQ(0, instance.getSearchPaths().size()); + ASSERT_EQ(0U, instance.getSearchPaths().size()); + + // Read the canonical path of "." + std::string canonicalPath = + boost::filesystem::canonical(".").generic_string(); // Add one path for three types. - instance.addSearchPath( - ".", {ResourceLocator::Type::DOMAIN_DESC, ResourceLocator::Type::SCRIPT, - ResourceLocator::Type::TYPESYS}); + instance.addSearchPath(".", + {ResourceType::DOMAIN_DESC, ResourceType::SCRIPT, + ResourceType::TYPESYSTEM}); - ASSERT_EQ(3, instance.getSearchPaths().size()); + ASSERT_EQ(3U, instance.getSearchPaths().size()); - auto it = - instance.getSearchPaths().find(ResourceLocator::Type::DOMAIN_DESC); + auto it = instance.getSearchPaths().find(ResourceType::DOMAIN_DESC); - ASSERT_EQ(1, it->second.size()); - ASSERT_EQ(".", it->second[0].generic_string()); + ASSERT_EQ(1U, it->second.size()); + ASSERT_EQ(canonicalPath, it->second[0]); - it = instance.getSearchPaths().find(ResourceLocator::Type::SCRIPT); + it = instance.getSearchPaths().find(ResourceType::SCRIPT); - ASSERT_EQ(1, it->second.size()); - ASSERT_EQ(".", it->second[0].generic_string()); + ASSERT_EQ(1U, it->second.size()); + ASSERT_EQ(canonicalPath, it->second[0]); - it = instance.getSearchPaths().find(ResourceLocator::Type::TYPESYS); + it = instance.getSearchPaths().find(ResourceType::TYPESYSTEM); - ASSERT_EQ(1, it->second.size()); - ASSERT_EQ(".", it->second[0].generic_string()); + ASSERT_EQ(1U, it->second.size()); + ASSERT_EQ(canonicalPath, it->second[0]); // Add another path for only one of those types. - instance.addSearchPath("..", {ResourceLocator::Type::DOMAIN_DESC}); + std::string canonicalPath2 = + boost::filesystem::canonical("..").generic_string(); + + instance.addSearchPath("..", {ResourceType::DOMAIN_DESC}); - ASSERT_EQ(3, instance.getSearchPaths().size()); + ASSERT_EQ(3U, instance.getSearchPaths().size()); - it = instance.getSearchPaths().find(ResourceLocator::Type::DOMAIN_DESC); + it = instance.getSearchPaths().find(ResourceType::DOMAIN_DESC); - ASSERT_EQ(2, it->second.size()); - ASSERT_EQ(".", it->second[0].generic_string()); - ASSERT_EQ("..", it->second[1].generic_string()); + ASSERT_EQ(2U, it->second.size()); + ASSERT_EQ(canonicalPath, it->second[0]); + ASSERT_EQ(canonicalPath2, it->second[1]); } -void assert_located( - const FileLocator &instance, const std::string &path, - const std::string &relativeTo, - ResourceLocator::Type type = ResourceLocator::Type::DOMAIN_DESC) +void assert_located(const FileLocator &instance, const std::string &path, + const std::string &relativeTo, + ResourceType type = ResourceType::DOMAIN_DESC) { - ResourceLocator::Location loc = instance.locate(path, relativeTo, type); - ASSERT_TRUE(loc.found); - boost::filesystem::path p(loc.location); + Resource res; + ASSERT_TRUE(instance.locate(res, path, type, relativeTo)); + ASSERT_TRUE(res.isValid()); + boost::filesystem::path p(res.getLocation()); ASSERT_TRUE(boost::filesystem::exists(p)); ASSERT_EQ(path, p.filename()); } -void assert_not_located( - const FileLocator &instance, const std::string &path, - const std::string &relativeTo, - ResourceLocator::Type type = ResourceLocator::Type::DOMAIN_DESC) +void assert_not_located(const FileLocator &instance, const std::string &path, + const std::string &relativeTo, + ResourceType type = ResourceType::DOMAIN_DESC) { - ResourceLocator::Location loc = instance.locate(path, relativeTo, type); - ASSERT_FALSE(loc.found); + Resource res; + ASSERT_FALSE(instance.locate(res, path, type, relativeTo)); + ASSERT_FALSE(res.isValid()); } TEST(FileLocator, testLocate) @@ -106,14 +114,14 @@ TEST(FileLocator, testLocate) assert_not_located(instance, "FileLocator.hpp", relativeTo); // Add the respective search path. - instance.addSearchPath(start / "src/plugins/boost", - {ResourceLocator::Type::DOMAIN_DESC}); + instance.addSearchPath((start / "src/plugins/boost").generic_string(), + {ResourceType::DOMAIN_DESC}); // Now we should be able to find both. assert_located(instance, "CMakeLists.txt", relativeTo); assert_located(instance, "FileLocator.hpp", relativeTo); // but only with the correct type. assert_not_located(instance, "FileLocator.hpp", relativeTo, - ResourceLocator::Type::SCRIPT); + ResourceType::SCRIPT); } TEST(FileLocator, testStream) @@ -133,10 +141,11 @@ TEST(FileLocator, testStream) } FileLocator instance; // Locate the CMakeLists.txt - ResourceLocator::Location loc = instance.locate( - "CMakeLists.txt", relativeTo, ResourceLocator::Type::DOMAIN_DESC); + Resource res; + instance.locate(res, "CMakeLists.txt", ResourceType::DOMAIN_DESC, + relativeTo); // Stream the content. - auto is_ptr = loc.stream(); + auto is_ptr = res.stream(); // get the beginning. char buf[256]; is_ptr->getline(buf, 256); |