From 3ca274c015bc99aaa40f6337d493ea172309f294 Mon Sep 17 00:00:00 2001 From: Benjamin Paassen Date: Tue, 9 Dec 2014 18:34:59 +0100 Subject: First draft of ResourceLocator. The stream method is not working yet. Thus I did comment out the compiler commands for the new classes. --- test/core/ResourceLocatorTest.cpp | 48 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 test/core/ResourceLocatorTest.cpp (limited to 'test/core/ResourceLocatorTest.cpp') diff --git a/test/core/ResourceLocatorTest.cpp b/test/core/ResourceLocatorTest.cpp new file mode 100644 index 0000000..2e663f5 --- /dev/null +++ b/test/core/ResourceLocatorTest.cpp @@ -0,0 +1,48 @@ +/* + Ousía + Copyright (C) 2014, 2015 Benjamin Paaßen, Andreas Stöckel + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +#include + +#include + +#include + +namespace ousia { + +//TODO: This does not work yet! + +class TestResourceLocator : public ResourceLocator { + ResourceLocation locate(const std::string &path, + const std::string &relativeTo, + const ResourceType type) const override + { + // trivial test implementation. + return ResourceLocation(true, *this, type, path); + } + + std::istream stream(const std::string &location, std::istream& input) const override { + //trivial test implementation. + input << "test"; + } + +}; + +TEST(ResourceLocator, locate) +{ +} +} -- cgit v1.2.3 From 9a131feeb507988b9bde7ff1baccf4e13fe903c4 Mon Sep 17 00:00:00 2001 From: Benjamin Paassen Date: Tue, 9 Dec 2014 20:38:09 +0100 Subject: another try to get the stream method to work. Still compiler errors. --- src/core/ResourceLocator.hpp | 7 +++---- test/core/ResourceLocatorTest.cpp | 7 +++++-- 2 files changed, 8 insertions(+), 6 deletions(-) (limited to 'test/core/ResourceLocatorTest.cpp') diff --git a/src/core/ResourceLocator.hpp b/src/core/ResourceLocator.hpp index 5ca3ef3..2706aea 100644 --- a/src/core/ResourceLocator.hpp +++ b/src/core/ResourceLocator.hpp @@ -74,9 +74,9 @@ public: * @param stream is an inputstream that gets the data of the Resource at * this location. */ - void stream(std::istream &input) const + std::istream stream() const { - return locator.stream(location, input); + return std::move(locator.stream(location)); } }; @@ -105,8 +105,7 @@ public: * @param stream is an inputstream that gets the data of the Resource at * this location. */ - virtual void stream(const std::string &location, - std::istream &input) const; + virtual std::istream stream(const std::string &location) const; }; } diff --git a/test/core/ResourceLocatorTest.cpp b/test/core/ResourceLocatorTest.cpp index 2e663f5..08259b6 100644 --- a/test/core/ResourceLocatorTest.cpp +++ b/test/core/ResourceLocatorTest.cpp @@ -35,14 +35,17 @@ class TestResourceLocator : public ResourceLocator { return ResourceLocation(true, *this, type, path); } - std::istream stream(const std::string &location, std::istream& input) const override { + std::istream stream(const std::string &location) const override { //trivial test implementation. - input << "test"; + std::stringstream ss; + ss << "test"; + return std::move(ss); } }; TEST(ResourceLocator, locate) { + } } -- cgit v1.2.3 From c379a9e5f031dfafa0dc0e132061610b706a7b28 Mon Sep 17 00:00:00 2001 From: Benjamin Paassen Date: Tue, 9 Dec 2014 23:30:51 +0100 Subject: got a trivial version of the ResourceLocator to work. --- CMakeLists.txt | 4 ++-- src/core/ResourceLocator.hpp | 41 +++++++++++++++++++++++---------------- test/core/ResourceLocatorTest.cpp | 29 +++++++++++++++------------ 3 files changed, 43 insertions(+), 31 deletions(-) (limited to 'test/core/ResourceLocatorTest.cpp') diff --git a/CMakeLists.txt b/CMakeLists.txt index 4f081c9..804276d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -104,7 +104,7 @@ ADD_LIBRARY(ousia_core src/core/Logger src/core/Managed src/core/Node -# src/core/ResourceLocator + src/core/ResourceLocator src/core/Tokenizer # src/core/Typesystem src/core/Utils @@ -162,7 +162,7 @@ IF(TEST) test/core/ManagedContainersTest test/core/NodeTest test/core/RangeSetTest -# test/core/ResourceLocatorTest + test/core/ResourceLocatorTest test/core/TokenizerTest test/core/UtilsTest test/core/parser/ParserStackTest diff --git a/src/core/ResourceLocator.hpp b/src/core/ResourceLocator.hpp index 2706aea..aaea8a5 100644 --- a/src/core/ResourceLocator.hpp +++ b/src/core/ResourceLocator.hpp @@ -20,6 +20,7 @@ #define _OUSIA_RESOURCE_LOCATOR_HPP_ #include +#include namespace ousia { @@ -36,7 +37,7 @@ public: /** * This enum contains all possible types of includable resources in Ousía. */ - enum class ResourceType { + enum class Type { // A Domain description DOMAIN, // An ECMA/JavaScript @@ -50,18 +51,18 @@ public: }; /** - * A ResourceLocation contains the location of a Resource, e.g. a file path + * A Location contains the location of a Resource, e.g. a file path * on a hard drive. Note that the 'found' flag might be set to false * indicating that a resource was not found. */ - struct ResourceLocation { + struct Location { const bool found; const ResourceLocator &locator; - const ResourceType type; + const Type type; const std::string location; - ResourceLocation(const bool found, const ResourceLocator &locator, - const ResourceType type, const std::string location) + Location(const bool found, const ResourceLocator &locator, + const Type type, const std::string location) : found(found), locator(locator), type(type), location(location) { } @@ -71,10 +72,12 @@ public: * found this location and returns a stream containing the data of the * Resource at this location. * - * @param stream is an inputstream that gets the data of the Resource at - * this location. + * @return a stream containing the data of the Resource at this + * location. This has to be a unique_pointer because the current + * C++11 compiler does not yet support move semantics for + * streams. */ - std::istream stream() const + std::unique_ptr stream() const { return std::move(locator.stream(location)); } @@ -82,30 +85,34 @@ public: /** * The locate function uses this ResourceLocator to search for a given - * Resource name (path parameter). It returns a ResourceLocation with the + * Resource name (path parameter). It returns a Location with the * 'found' flag set accordingly. * * @param path is the resource name. * @param relativeTo TODO: What is the meaning of this parameter? * @param type is the type of this resource. * - * @return A ResourceLocation containing either the found location of the + * @return A Location containing either the found location of the * Resource and the found flag set to 'true' or an empty location * and the found flag set to 'false'. */ - virtual ResourceLocation locate(const std::string &path, + virtual Location locate(const std::string &path, const std::string &relativeTo, - const ResourceType type) const; + const Type type) const = 0; /** * This method returns a strem containing the data of the resource at the * given location. * - * @param location is a found location, most likely from a ResourceLocation. - * @param stream is an inputstream that gets the data of the Resource at - * this location. + * @param location is a found location, most likely from a Location. + * + * @return a stream containing the data of the Resource at this + * location. This has to be a unique_pointer because the current + * C++11 compiler does not yet support move semantics for + * streams. */ - virtual std::istream stream(const std::string &location) const; + virtual std::unique_ptr stream( + const std::string &location) const = 0; }; } diff --git a/test/core/ResourceLocatorTest.cpp b/test/core/ResourceLocatorTest.cpp index 08259b6..a3860c5 100644 --- a/test/core/ResourceLocatorTest.cpp +++ b/test/core/ResourceLocatorTest.cpp @@ -24,28 +24,33 @@ namespace ousia { -//TODO: This does not work yet! - class TestResourceLocator : public ResourceLocator { - ResourceLocation locate(const std::string &path, - const std::string &relativeTo, - const ResourceType type) const override +public: + ResourceLocator::Location locate( + const std::string &path, const std::string &relativeTo, + const ResourceLocator::Type type) const override { // trivial test implementation. - return ResourceLocation(true, *this, type, path); + return ResourceLocator::Location(true, *this, type, path); } - std::istream stream(const std::string &location) const override { - //trivial test implementation. - std::stringstream ss; - ss << "test"; + std::unique_ptr stream( + const std::string &location) const override + { + // trivial test implementation. + std::unique_ptr ss(new std::stringstream()); + (*ss) << "test"; return std::move(ss); } - }; TEST(ResourceLocator, locate) { - + TestResourceLocator instance; + ResourceLocator::Location location = + instance.locate("path", "", ResourceLocator::Type::DOMAIN); + ASSERT_TRUE(location.found); + ASSERT_EQ(ResourceLocator::Type::DOMAIN, location.type); + ASSERT_EQ("path", location.location); } } -- cgit v1.2.3 From 325d78169620094178513303d65c71d933182ae4 Mon Sep 17 00:00:00 2001 From: Benjamin Paassen Date: Tue, 9 Dec 2014 23:35:00 +0100 Subject: Added a ResourceLocator stream test. --- test/core/ResourceLocatorTest.cpp | 13 +++++++++++++ 1 file changed, 13 insertions(+) (limited to 'test/core/ResourceLocatorTest.cpp') diff --git a/test/core/ResourceLocatorTest.cpp b/test/core/ResourceLocatorTest.cpp index a3860c5..38cfc3e 100644 --- a/test/core/ResourceLocatorTest.cpp +++ b/test/core/ResourceLocatorTest.cpp @@ -53,4 +53,17 @@ TEST(ResourceLocator, locate) ASSERT_EQ(ResourceLocator::Type::DOMAIN, location.type); ASSERT_EQ("path", location.location); } + +TEST(ResourceLocator, stream) +{ + TestResourceLocator instance; + ResourceLocator::Location location = + instance.locate("path", "", ResourceLocator::Type::DOMAIN); + std::unique_ptr is = location.stream(); + + std::string str; + *is >> str; + + ASSERT_EQ("test", str); +} } -- cgit v1.2.3