From 37fc54402744b84b65ba87178387d7f6009d50df Mon Sep 17 00:00:00 2001 From: Benjamin Paassen Date: Tue, 9 Dec 2014 14:40:54 +0100 Subject: added comparison operators for Variant. --- test/core/variant/VariantTest.cpp | 15 +++++++++++++++ 1 file changed, 15 insertions(+) (limited to 'test') diff --git a/test/core/variant/VariantTest.cpp b/test/core/variant/VariantTest.cpp index 270c350..e51cf36 100644 --- a/test/core/variant/VariantTest.cpp +++ b/test/core/variant/VariantTest.cpp @@ -121,6 +121,21 @@ TEST(Variant, mapValue) ASSERT_EQ(2, v2.asMap().find("key1")->second.asArray()[1].asInt()); } +TEST(Variant, relationalOperators){ + Variant a{4}; + Variant b{4}; + + ASSERT_EQ(a,b); + + b.setInt(5); + ASSERT_TRUE(a < b); + + b.setDouble(4); + ASSERT_FALSE(a == b); + + a.setDouble(4); + ASSERT_EQ(a,b); +} } -- cgit v1.2.3 From 98c4f90d3e34bd4d536ee6c84b4d79b71cde3998 Mon Sep 17 00:00:00 2001 From: Benjamin Paassen Date: Tue, 9 Dec 2014 14:41:28 +0100 Subject: made CSSParser full compatible (including pseudo selector arguments) with type system parsing. --- src/core/CSS.hpp | 12 ++++++------ src/plugins/css/CSSParser.cpp | 29 +++++++++++++++-------------- src/plugins/css/CSSParser.hpp | 2 +- test/plugins/css/CSSParserTest.cpp | 2 +- 4 files changed, 23 insertions(+), 22 deletions(-) (limited to 'test') diff --git a/src/core/CSS.hpp b/src/core/CSS.hpp index aa701b5..1510f3a 100644 --- a/src/core/CSS.hpp +++ b/src/core/CSS.hpp @@ -70,7 +70,7 @@ struct Specificity { */ class RuleSet : public Managed { private: - std::map rules; + std::map rules; public: /** @@ -78,9 +78,9 @@ public: */ RuleSet(Manager &mgr) : Managed(mgr), rules() {} - std::map &getRules() { return rules; } + std::map &getRules() { return rules; } - const std::map &getRules() const + const std::map &getRules() const { return rules; } @@ -127,11 +127,11 @@ public: class PseudoSelector { private: const std::string name; - const std::vector args; + const Variant::arrayType args; const bool generative; public: - PseudoSelector(std::string name, std::vector args, + PseudoSelector(std::string name, Variant::arrayType args, bool generative) : name(std::move(name)), args(std::move(args)), generative(generative) { @@ -144,7 +144,7 @@ public: const std::string &getName() const { return name; } - const std::vector &getArgs() const { return args; } + const Variant::arrayType &getArgs() const { return args; } const bool &isGenerative() const { return generative; } }; diff --git a/src/plugins/css/CSSParser.cpp b/src/plugins/css/CSSParser.cpp index 51b2fd2..4bbcc18 100644 --- a/src/plugins/css/CSSParser.cpp +++ b/src/plugins/css/CSSParser.cpp @@ -122,8 +122,7 @@ void CSSParser::parseSelectors(Rooted root, { auto tuple = parseSelector(tokenizer, ctx); // append the SelectorPath to the root node. - std::vector> unmergedLeafs = - root->append(tuple.first); + std::vector> unmergedLeafs = root->append(tuple.first); // append the leaf to the leafList. switch (unmergedLeafs.size()) { case 0: @@ -167,8 +166,8 @@ std::pair, Rooted> CSSParser::parseSelector( // so we parse the rest of the subsequent SelectorPath auto tuple = parseSelector(tokenizer, ctx); // then we establish the DESCENDANT relationship - s->getEdges().push_back(new SelectorNode::SelectorEdge( - ctx.manager, tuple.first)); + s->getEdges().push_back( + new SelectorNode::SelectorEdge(ctx.manager, tuple.first)); // and we return this node as well as the leaf. return std::make_pair(s, tuple.second); } @@ -226,14 +225,16 @@ Rooted CSSParser::parsePrimitiveSelector(CodeTokenizer &tokenizer, return n; } // parse the argument list. - std::vector args; + Variant::arrayType args; // we require at least one argument, if parantheses are used - expect(TOKEN_TEXT, tokenizer, t, true, ctx); - args.push_back(t.content); + args.push_back(variant::Reader::parseGeneric(tokenizer.getInput(), + ctx.logger, + {',', ')'}).second); while (expect(COMMA, tokenizer, t, false, ctx)) { // as long as we find commas we expect new arguments. - expect(TOKEN_TEXT, tokenizer, t, true, ctx); - args.push_back(t.content); + args.push_back( + variant::Reader::parseGeneric( + tokenizer.getInput(), ctx.logger, {',', ')'}).second); } expect(PAREN_CLOSE, tokenizer, t, true, ctx); // and we return with the finished Selector. @@ -247,7 +248,7 @@ Rooted CSSParser::parsePrimitiveSelector(CodeTokenizer &tokenizer, // so we expect an ID now. Token t; expect(TOKEN_TEXT, tokenizer, t, true, ctx); - std::vector args{t.content}; + Variant::arrayType args{Variant(t.content.c_str())}; // and we return the finished Selector Rooted n{ new SelectorNode(ctx.manager, name, {"has_id", args, false})}; @@ -262,7 +263,7 @@ Rooted CSSParser::parsePrimitiveSelector(CodeTokenizer &tokenizer, // in both cases the attribute name comes first. Token t; expect(TOKEN_TEXT, tokenizer, t, true, ctx); - std::vector args{t.content}; + Variant::arrayType args{Variant(t.content.c_str())}; if (!expect(EQUALS, tokenizer, t, false, ctx)) { // if no equals sign follows we have a has_attribute // PseudoSelector @@ -276,7 +277,7 @@ Rooted CSSParser::parsePrimitiveSelector(CodeTokenizer &tokenizer, // with an equals sign we have a has_value PseudoSelector and // expect the value next. expect(STRING, tokenizer, t, true, ctx); - args.push_back(t.content); + args.push_back(Variant(t.content.c_str())); // then we expect a closing bracket. expect(BRACKET_CLOSE, tokenizer, t, true, ctx); // and then we can return the result. @@ -313,14 +314,14 @@ void CSSParser::parseRules(CodeTokenizer &tokenizer, Rooted ruleSet, ParserContext &ctx) { std::string key; - variant::Variant value; + Variant value; while (parseRule(tokenizer, ctx, key, value)) { ruleSet->getRules().insert({key, value}); } } bool CSSParser::parseRule(CodeTokenizer &tokenizer, ParserContext &ctx, - std::string &key, variant::Variant &value) + std::string &key, Variant &value) { Token t; if (!expect(TOKEN_TEXT, tokenizer, t, false, ctx)) { diff --git a/src/plugins/css/CSSParser.hpp b/src/plugins/css/CSSParser.hpp index a4d8cdc..82f0cd1 100644 --- a/src/plugins/css/CSSParser.hpp +++ b/src/plugins/css/CSSParser.hpp @@ -112,7 +112,7 @@ private: * @return true if a rule was found. */ bool parseRule(CodeTokenizer &tokenizer, ParserContext &ctx, - std::string &key, variant::Variant &value); + std::string &key, Variant &value); /** * A convenience function to wrap around the tokenizer peek() function that diff --git a/test/plugins/css/CSSParserTest.cpp b/test/plugins/css/CSSParserTest.cpp index 8873673..6499375 100644 --- a/test/plugins/css/CSSParserTest.cpp +++ b/test/plugins/css/CSSParserTest.cpp @@ -120,7 +120,7 @@ TEST(CSSParser, testParseSelectors) Rooted Ag = children[1]; ASSERT_EQ("A", Ag->getName()); { - PseudoSelector select{"g", {"4", "2", "3"}, true}; + PseudoSelector select{"g", {Variant(4), Variant(2), Variant(3)}, true}; ASSERT_EQ(select, Ag->getPseudoSelector()); } ASSERT_EQ(0, Ag->getEdges().size()); -- cgit v1.2.3 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. --- CMakeLists.txt | 2 + src/core/ResourceLocator.hpp | 114 ++++++++++++++++++++++++++++++++++++++ test/core/ResourceLocatorTest.cpp | 48 ++++++++++++++++ 3 files changed, 164 insertions(+) create mode 100644 src/core/ResourceLocator.hpp create mode 100644 test/core/ResourceLocatorTest.cpp (limited to 'test') diff --git a/CMakeLists.txt b/CMakeLists.txt index efcac0b..4f081c9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -104,6 +104,7 @@ ADD_LIBRARY(ousia_core src/core/Logger src/core/Managed src/core/Node +# src/core/ResourceLocator src/core/Tokenizer # src/core/Typesystem src/core/Utils @@ -161,6 +162,7 @@ IF(TEST) test/core/ManagedContainersTest test/core/NodeTest test/core/RangeSetTest +# 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 new file mode 100644 index 0000000..5ca3ef3 --- /dev/null +++ b/src/core/ResourceLocator.hpp @@ -0,0 +1,114 @@ +/* + 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 . +*/ + +#ifndef _OUSIA_RESOURCE_LOCATOR_HPP_ +#define _OUSIA_RESOURCE_LOCATOR_HPP_ + +#include + +namespace ousia { + +/** + * A ResourceLocator is a class able to locate resources in some way, usually + * on the hard drive. + * + * We specify this as an abstract superclass to have an interface layer between + * the program core and possible future extensions in terms of resource + * locations (e.g. online resources, .zip files, etc.). + */ +class ResourceLocator { +public: + /** + * This enum contains all possible types of includable resources in Ousía. + */ + enum class ResourceType { + // A Domain description + DOMAIN, + // An ECMA/JavaScript + SCRIPT, + // A Type System + TYPESYSTEM, + // TODO: Aren't documents and attribute descriptors missing? + // TODO: What is the purpose of these two? + GENERIC_MODULE, + GENERIC_INCLUDE + }; + + /** + * A ResourceLocation 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 { + const bool found; + const ResourceLocator &locator; + const ResourceType type; + const std::string location; + + ResourceLocation(const bool found, const ResourceLocator &locator, + const ResourceType type, const std::string location) + : found(found), locator(locator), type(type), location(location) + { + } + + /** + * This calls the 'stream' method of the underlying ResourceLocator that + * 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. + */ + void stream(std::istream &input) const + { + return locator.stream(location, input); + } + }; + + /** + * The locate function uses this ResourceLocator to search for a given + * Resource name (path parameter). It returns a ResourceLocation 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 + * 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, + const std::string &relativeTo, + const ResourceType type) const; + + /** + * 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. + */ + virtual void stream(const std::string &location, + std::istream &input) const; +}; +} + +#endif /* _RESOURCE_LOCATOR_HPP_ */ + 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') 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') 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') 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