diff options
Diffstat (limited to 'test/core')
| -rw-r--r-- | test/core/BufferedCharReaderTest.cpp | 4 | ||||
| -rw-r--r-- | test/core/LoggerTest.cpp | 10 | ||||
| -rw-r--r-- | test/core/UtilsTest.cpp | 8 | ||||
| -rw-r--r-- | test/core/parser/ParserStackTest.cpp | 162 | ||||
| -rw-r--r-- | test/core/parser/XmlParserTest.cpp | 37 | ||||
| -rw-r--r-- | test/core/variant/VariantTest.cpp | 126 | 
6 files changed, 306 insertions, 41 deletions
diff --git a/test/core/BufferedCharReaderTest.cpp b/test/core/BufferedCharReaderTest.cpp index b0955c2..b3498f7 100644 --- a/test/core/BufferedCharReaderTest.cpp +++ b/test/core/BufferedCharReaderTest.cpp @@ -1,6 +1,6 @@  /* -    SCAENEA IDL Compiler (scidlc) -    Copyright (C) 2014  Andreas Stöckel +    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 diff --git a/test/core/LoggerTest.cpp b/test/core/LoggerTest.cpp index 4badc28..7031dc7 100644 --- a/test/core/LoggerTest.cpp +++ b/test/core/LoggerTest.cpp @@ -26,17 +26,23 @@ namespace ousia {  TEST(TerminalLogger, log)  { +	// Test for manual visual expection only -- no assertions  	TerminalLogger logger{std::cerr, true}; -	logger.pushFilename("/homes/mmuster/ousia/test.odp"); +	logger.pushFilename("test.odp");  	logger.debug("This is a test debug message", 10, 20); +	logger.debug("This is a test debug message with no column", 10); +	logger.debug("This is a test debug message with no line"); +	logger.debug("This is a test debug message with no file", ""); +	logger.debug("This is a test debug message with no file but a line", "", 10); +	logger.debug("This is a test debug message with no file but a line and a column", "", 10, 20);  	logger.note("This is a test note", 10, 20);  	logger.warning("This is a test warning", 10, 20);  	logger.error("This is a test error", 10, 20);  	logger.fatalError("This is a test fatal error!", 10, 20);  	try { -		throw LoggableException{"A fatal exception", true}; +		throw LoggableException{"A fatal exception"};  	} catch (const LoggableException &ex) {  		logger.log(ex);  	} diff --git a/test/core/UtilsTest.cpp b/test/core/UtilsTest.cpp index 4f754a5..0a7d2a3 100644 --- a/test/core/UtilsTest.cpp +++ b/test/core/UtilsTest.cpp @@ -31,5 +31,13 @@ TEST(Utils, isIdentifier)  	ASSERT_FALSE(Utils::isIdentifier("0t-_EST"));  } +TEST(Utils, trim) +{ +	ASSERT_EQ("hello world", Utils::trim("\t hello world   \n\r\t")); +	ASSERT_EQ("hello world", Utils::trim("hello world   \n\r\t")); +	ASSERT_EQ("hello world", Utils::trim("   hello world")); +	ASSERT_EQ("hello world", Utils::trim("hello world")); +} +  } diff --git a/test/core/parser/ParserStackTest.cpp b/test/core/parser/ParserStackTest.cpp new file mode 100644 index 0000000..5dab979 --- /dev/null +++ b/test/core/parser/ParserStackTest.cpp @@ -0,0 +1,162 @@ +/* +    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 <http://www.gnu.org/licenses/>. +*/ + +#include <iostream> + +#include <gtest/gtest.h> + +#include <core/parser/ParserStack.hpp> + +namespace ousia { +namespace parser { + +static const State STATE_DOCUMENT = 0; +static const State STATE_BODY = 1; +static const State STATE_EMPTY = 2; + +static int startCount = 0; +static int endCount = 0; +static int dataCount = 0; +static int childCount = 0; + +class TestHandler : public Handler { + +public: +	using Handler::Handler; + +	void start(const Variant &args) override +	{ +		startCount++; +	} + +	void end() override +	{ +		endCount++; +	} + +	void data(const std::string &data, int field) override  +	{ +		dataCount++; +	} + +	void child(std::shared_ptr<Handler> handler) override +	{ +		childCount++; +	} + +}; + +static Handler* createTestHandler(const ParserContext &ctx, +                                        std::string name, State state, +                                        State parentState, bool isChild) +{ +	return new TestHandler(ctx, name, state, parentState, isChild); +} + +static const std::multimap<std::string, HandlerDescriptor> TEST_HANDLERS{ +	{"document", {{STATE_NONE}, createTestHandler, STATE_DOCUMENT}}, +	{"body", {{STATE_DOCUMENT}, createTestHandler, STATE_BODY, true}}, +	{"empty", {{STATE_DOCUMENT}, createTestHandler, STATE_EMPTY}}, +	{"special", {{STATE_ALL}, createTestHandler, STATE_EMPTY}}, +}; + +TEST(ParserStack, simpleTest) +{ +	StandaloneParserContext ctx; +	ParserStack s{ctx, TEST_HANDLERS}; + +	startCount = 0; +	endCount = 0; +	dataCount = 0; +	childCount = 0; + +	ASSERT_EQ("", s.currentName()); +	ASSERT_EQ(STATE_NONE, s.currentState()); + +	s.start("document", nullptr); +	s.data("test1"); + +	ASSERT_EQ("document", s.currentName()); +	ASSERT_EQ(STATE_DOCUMENT, s.currentState()); +	ASSERT_EQ(1, startCount); +	ASSERT_EQ(1, dataCount); + +	s.start("body", nullptr); +	s.data("test2"); +	ASSERT_EQ("body", s.currentName()); +	ASSERT_EQ(STATE_BODY, s.currentState()); +	ASSERT_EQ(2, startCount); +	ASSERT_EQ(2, dataCount); + +	s.start("inner", nullptr); +	ASSERT_EQ("inner", s.currentName()); +	ASSERT_EQ(STATE_BODY, s.currentState()); +	s.end(); +	ASSERT_EQ(3, startCount); +	ASSERT_EQ(1, childCount); +	ASSERT_EQ(1, endCount); + +	s.end(); +	ASSERT_EQ(2, childCount); +	ASSERT_EQ(2, endCount); + +	ASSERT_EQ("document", s.currentName()); +	ASSERT_EQ(STATE_DOCUMENT, s.currentState()); + +	s.start("body", nullptr); +	s.data("test3"); +	ASSERT_EQ("body", s.currentName()); +	ASSERT_EQ(STATE_BODY, s.currentState()); +	s.end(); +	ASSERT_EQ(4, startCount); +	ASSERT_EQ(3, dataCount); +	ASSERT_EQ(3, childCount); +	ASSERT_EQ(3, endCount); + +	ASSERT_EQ("document", s.currentName()); +	ASSERT_EQ(STATE_DOCUMENT, s.currentState()); + +	s.end(); +	ASSERT_EQ(4, endCount); + +	ASSERT_EQ("", s.currentName()); +	ASSERT_EQ(STATE_NONE, s.currentState()); +} + +TEST(ParserStack, errorHandling) +{ +	StandaloneParserContext ctx; +	ParserStack s{ctx, TEST_HANDLERS}; + +	ASSERT_THROW(s.start("body", nullptr), OusiaException); +	s.start("document", nullptr); +	ASSERT_THROW(s.start("document", nullptr), OusiaException); +	s.start("empty", nullptr); +	ASSERT_THROW(s.start("body", nullptr), OusiaException); +	s.start("special", nullptr); +	s.end(); +	s.end(); +	s.end(); +	ASSERT_EQ(STATE_NONE, s.currentState()); +	ASSERT_THROW(s.end(), OusiaException); +	ASSERT_THROW(s.data("test", 1), OusiaException); +} + +} +} + diff --git a/test/core/parser/XmlParserTest.cpp b/test/core/parser/XmlParserTest.cpp deleted file mode 100644 index d2c4410..0000000 --- a/test/core/parser/XmlParserTest.cpp +++ /dev/null @@ -1,37 +0,0 @@ -/* -    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 <http://www.gnu.org/licenses/>. -*/ - -#include <iostream> - -#include <gtest/gtest.h> - -#include <core/parser/XmlParser.hpp> - -namespace ousia { - -TEST(XmlParser, logging) -{ -	TerminalLogger log(std::cerr, true); -	XmlParser p; - -	log.pushFilename("test.xml"); -	p.parse("<test></btest>", nullptr, log); -} - -} - diff --git a/test/core/variant/VariantTest.cpp b/test/core/variant/VariantTest.cpp new file mode 100644 index 0000000..270c350 --- /dev/null +++ b/test/core/variant/VariantTest.cpp @@ -0,0 +1,126 @@ +/* +    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 <http://www.gnu.org/licenses/>. +*/ + +#include <iostream> + +#include <gtest/gtest.h> + +#include <core/variant/Variant.hpp> + +namespace ousia { + +TEST(Variant, nullValue) +{ +	Variant v; +	ASSERT_TRUE(v.isNull()); + +	v = 1; +	ASSERT_FALSE(v.isNull()); + +	v = nullptr; +	ASSERT_TRUE(v.isNull()); + +	Variant v2{nullptr}; +	ASSERT_TRUE(v.isNull()); +} + +TEST(Variant, booleanValue) +{ +	Variant v{true}; +	ASSERT_TRUE(v.isBool()); +	ASSERT_TRUE(v.asBool()); + +	v = false; +	ASSERT_TRUE(v.isBool()); +	ASSERT_FALSE(v.asBool()); + +	v.setBool(true); +	ASSERT_TRUE(v.isBool()); +	ASSERT_TRUE(v.asBool()); + +	v = nullptr; +	ASSERT_FALSE(v.isBool()); +} + +TEST(Variant, intValue) +{ +	Variant v{42}; +	ASSERT_TRUE(v.isInt()); +	ASSERT_EQ(42, v.asInt()); + +	v = 43; +	ASSERT_TRUE(v.isInt()); +	ASSERT_EQ(43, v.asInt()); + +	v = false; +	ASSERT_FALSE(v.isInt()); +} + +TEST(Variant, doubleValue) +{ +	Variant v{42.5}; +	ASSERT_TRUE(v.isDouble()); +	ASSERT_EQ(42.5, v.asDouble()); + +	v = 42; +	ASSERT_FALSE(v.isDouble()); + +	v = 43.5; +	ASSERT_TRUE(v.isDouble()); +	ASSERT_EQ(43.5, v.asDouble()); +} + +TEST(Variant, stringValue) +{ +	Variant v{"Hello World"}; +	ASSERT_TRUE(v.isString()); +	ASSERT_EQ("Hello World", v.asString()); + +	v = "Goodbye World"; +	ASSERT_TRUE(v.isString()); +	ASSERT_EQ("Goodbye World", v.asString()); + +	v = 42; +	ASSERT_FALSE(v.isString()); +} + +TEST(Variant, arrayValue) +{ +	const Variant v{{"test1", 42}}; +	ASSERT_EQ(2, v.asArray().size()); +	ASSERT_EQ("test1", v.asArray()[0].asString()); +	ASSERT_EQ(42, v.asArray()[1].asInt()); +} + +TEST(Variant, mapValue) +{ +	const Variant v{{{"key1", "entry1"}, {"key2", "entry2"}}}; + +	auto map = v.asMap(); +	ASSERT_EQ(2, map.size()); + +	ASSERT_EQ("entry1", map.find("key1")->second.asString()); +	ASSERT_EQ("entry2", map.find("key2")->second.asString()); + +	const Variant v2{{{"key1", Variant::arrayType{1, 2}}, {"key2", "entry2"}}}; +	ASSERT_EQ(2, v2.asMap().find("key1")->second.asArray()[1].asInt()); +} + + +} +  | 
