From a2fafc917f96b879ad023b117978da0de124d12b Mon Sep 17 00:00:00 2001 From: Andreas Stöckel Date: Sun, 7 Dec 2014 01:14:26 +0100 Subject: implemented parseArray --- test/core/variant/ReaderTest.cpp | 68 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 67 insertions(+), 1 deletion(-) (limited to 'test/core/variant/ReaderTest.cpp') diff --git a/test/core/variant/ReaderTest.cpp b/test/core/variant/ReaderTest.cpp index 73e6bf8..bfa523d 100644 --- a/test/core/variant/ReaderTest.cpp +++ b/test/core/variant/ReaderTest.cpp @@ -24,7 +24,7 @@ namespace ousia { namespace variant { -Logger logger; +static TerminalLogger logger{std::cerr, true}; TEST(Reader, readString) { @@ -240,6 +240,72 @@ TEST(Reader, parseDouble) } } +TEST(Reader, parseArray) +{ + // Simple case (only primitive data types) + { + BufferedCharReader reader("[\"Hello, World\", unescaped\n string ,\n" + "1234, 0.56, true, false, null]"); + auto res = Reader::parseArray(reader, logger); + ASSERT_TRUE(res.first); + + // Make sure array has the correct size + ASSERT_EQ(7, res.second.size()); + + // Check the types + ASSERT_TRUE(res.second[0].isString()); + ASSERT_TRUE(res.second[1].isString()); + ASSERT_TRUE(res.second[2].isInt()); + ASSERT_TRUE(res.second[3].isDouble()); + ASSERT_TRUE(res.second[4].isBool()); + ASSERT_TRUE(res.second[5].isBool()); + ASSERT_TRUE(res.second[6].isNull()); + + // Check the values + ASSERT_EQ("Hello, World", res.second[0].asString()); + ASSERT_EQ("unescaped\n string", res.second[1].asString()); + ASSERT_EQ(1234, res.second[2].asInt()); + ASSERT_EQ(0.56, res.second[3].asDouble()); + ASSERT_TRUE(res.second[4].asBool()); + ASSERT_FALSE(res.second[5].asBool()); + } + + // Ending with comma + { + BufferedCharReader reader("[ 'test' ,]"); + auto res = Reader::parseArray(reader, logger); + ASSERT_TRUE(res.first); + + // Make sure the array has the correct size + ASSERT_EQ(1, res.second.size()); + + // Check the types + ASSERT_TRUE(res.second[0].isString()); + + // Check the values + ASSERT_EQ("test", res.second[0].asString()); + } + + // Recovery from invalid values + // TODO: Actually parseGeneric should fall back to returning a simple string + // if parsing of a special (non-string) type failed + { + BufferedCharReader reader("[ 0invalidNumber, str, 1invalid]"); + auto res = Reader::parseArray(reader, logger); + ASSERT_FALSE(res.first); + + // Make sure the array has the correct size + ASSERT_EQ(3, res.second.size()); + + // Check the types (only for the valid entries, the other types are + // undefined) + ASSERT_TRUE(res.second[1].isString()); + + // Check the values + ASSERT_EQ("str", res.second[1].asString()); + } +} + TEST(Reader, parseGeneric) { // Simple case, unescaped string -- cgit v1.2.3