diff options
author | Andreas Stöckel <astoecke@techfak.uni-bielefeld.de> | 2014-12-07 01:14:26 +0100 |
---|---|---|
committer | Andreas Stöckel <astoecke@techfak.uni-bielefeld.de> | 2014-12-07 01:14:26 +0100 |
commit | a2fafc917f96b879ad023b117978da0de124d12b (patch) | |
tree | e51df9fcabbd685f4549cd03a37122d994a97d4e /test | |
parent | 52cb69b8611a4376b27d55078d16855a16f2c88c (diff) |
implemented parseArray
Diffstat (limited to 'test')
-rw-r--r-- | test/core/variant/ReaderTest.cpp | 68 |
1 files changed, 67 insertions, 1 deletions
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 |