diff options
author | Andreas Stöckel <astoecke@techfak.uni-bielefeld.de> | 2014-12-05 16:08:34 +0100 |
---|---|---|
committer | Andreas Stöckel <andreas@somweyr.de> | 2014-12-05 16:08:34 +0100 |
commit | e06e7ae19851acf5e397f579d6c8459e87086d30 (patch) | |
tree | 2140f3d79239d6f0ebd5c08f1fb48b327586249e /test | |
parent | bf59bc2edbb1f3f4d12bfbd8ed2663fbbb1900c0 (diff) |
added string reading functions of the Reader class
Diffstat (limited to 'test')
-rw-r--r-- | test/core/variant/ReaderTest.cpp | 135 |
1 files changed, 135 insertions, 0 deletions
diff --git a/test/core/variant/ReaderTest.cpp b/test/core/variant/ReaderTest.cpp new file mode 100644 index 0000000..760760b --- /dev/null +++ b/test/core/variant/ReaderTest.cpp @@ -0,0 +1,135 @@ +/* + 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/Reader.hpp> + +namespace ousia { +namespace variant { + +TEST(Reader, readString) +{ + TerminalLogger logger(std::cerr, true); + + // Simple, double quoted string + { + BufferedCharReader reader("\"hello world\""); + auto res = Reader::parseString(reader, logger); + ASSERT_TRUE(res.first); + ASSERT_EQ("hello world", res.second); + } + + // Simple, double quoted string with whitespace + { + BufferedCharReader reader(" \"hello world\" "); + auto res = Reader::parseString(reader, logger); + ASSERT_TRUE(res.first); + ASSERT_EQ("hello world", res.second); + } + + // Simple, single quoted string + { + BufferedCharReader reader("'hello world'"); + auto res = Reader::parseString(reader, logger); + ASSERT_TRUE(res.first); + ASSERT_EQ("hello world", res.second); + } + + // Escape characters + { + BufferedCharReader reader("'\\'\\\"\\b\\f\\n\\r\\t\\v'"); + auto res = Reader::parseString(reader, logger); + ASSERT_TRUE(res.first); + ASSERT_EQ("'\"\b\f\n\r\t\v", res.second); + } +} + +TEST(Reader, parseUnescapedString) +{ + TerminalLogger logger(std::cerr, true); + + // Simple case + { + BufferedCharReader reader("hello world;"); + auto res = Reader::parseUnescapedString(reader, logger, {';'}); + ASSERT_TRUE(res.first); + ASSERT_EQ("hello world", res.second); + } + + // Simple case with whitespace + { + BufferedCharReader reader(" hello world ; "); + auto res = Reader::parseUnescapedString(reader, logger, {';'}); + ASSERT_TRUE(res.first); + ASSERT_EQ("hello world", res.second); + } + + // Linebreaks + { + BufferedCharReader reader(" hello\nworld ; "); + auto res = Reader::parseUnescapedString(reader, logger, {';'}); + ASSERT_TRUE(res.first); + ASSERT_EQ("hello\nworld", res.second); + } + + // End of stream + { + BufferedCharReader reader(" hello world "); + auto res = Reader::parseUnescapedString(reader, logger, {';'}); + ASSERT_TRUE(res.first); + ASSERT_EQ("hello world", res.second); + } +} + +TEST(Reader, parseGeneric) +{ + TerminalLogger logger(std::cerr, true); + + // Simple case, unescaped string + { + BufferedCharReader reader("hello world"); + auto res = Reader::parseGeneric(reader, logger, {';'}); + ASSERT_TRUE(res.first); + ASSERT_TRUE(res.second.isString()); + ASSERT_EQ("hello world", res.second.asString()); + } + + // Simple case, double quoted string + { + BufferedCharReader reader(" \"hello world\" "); + auto res = Reader::parseGeneric(reader, logger, {';'}); + ASSERT_TRUE(res.first); + ASSERT_TRUE(res.second.isString()); + ASSERT_EQ("hello world", res.second.asString()); + } + + // Simple case, single quoted string + { + BufferedCharReader reader(" 'hello world' "); + auto res = Reader::parseGeneric(reader, logger, {';'}); + ASSERT_TRUE(res.first); + ASSERT_TRUE(res.second.isString()); + ASSERT_EQ("hello world", res.second.asString()); + } +} + +} +} + |