diff options
author | Benjamin Paassen <bpaassen@techfak.uni-bielefeld.de> | 2014-12-03 00:57:57 +0100 |
---|---|---|
committer | Benjamin Paassen <bpaassen@techfak.uni-bielefeld.de> | 2014-12-03 00:57:57 +0100 |
commit | 41366eb61e5b85524b8ee07ae183df4f9f8a1f6d (patch) | |
tree | ac9468e4adc6cfcb63b4adc324770dc07de0e5aa /test/core/parser | |
parent | 314e97ac5307f5053fc0c31ec23c39ba9c9a0aac (diff) | |
parent | ed79df8f263dcd973c8ceb016b516644d87d8aa8 (diff) |
Merge branch 'master' of somweyr.de:ousia
Diffstat (limited to 'test/core/parser')
-rw-r--r-- | test/core/parser/ParserStackTest.cpp | 162 | ||||
-rw-r--r-- | test/core/parser/XmlParserTest.cpp | 37 |
2 files changed, 162 insertions, 37 deletions
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); -} - -} - |