diff options
-rw-r--r-- | src/core/variant/Reader.cpp | 16 | ||||
-rw-r--r-- | test/core/variant/ReaderTest.cpp | 17 |
2 files changed, 20 insertions, 13 deletions
diff --git a/src/core/variant/Reader.cpp b/src/core/variant/Reader.cpp index 3f1934e..5c167cd 100644 --- a/src/core/variant/Reader.cpp +++ b/src/core/variant/Reader.cpp @@ -577,11 +577,19 @@ std::pair<bool, Variant> Reader::parseGeneric( // TODO: Parse struct descriptor } - // Try to parse a number if a character in [0-9-] is reached + // Try to parse everything that looks like a number as number if (Utils::isNumeric(c) || c == '-') { - reader.resetPeek(); Number n; - if (n.parse(reader, logger, delims)) { + + // Fork the reader + utils::CharReaderFork fork = reader.fork(); + + // TODO: Fork logger + + // Try to parse the number + if (n.parse(fork, logger, delims)) { + // Parsing was successful, advance the reader + fork.commit(); if (n.isInt()) { return std::make_pair( true, @@ -589,8 +597,6 @@ std::pair<bool, Variant> Reader::parseGeneric( } else { return std::make_pair(true, n.doubleValue()); } - } else { - return std::make_pair(false, n.doubleValue()); } } diff --git a/test/core/variant/ReaderTest.cpp b/test/core/variant/ReaderTest.cpp index 7349856..43e85a5 100644 --- a/test/core/variant/ReaderTest.cpp +++ b/test/core/variant/ReaderTest.cpp @@ -253,7 +253,7 @@ TEST(Reader, parseArray) ASSERT_TRUE(res.first); // Make sure array has the correct size - ASSERT_EQ(7, res.second.size()); + ASSERT_EQ(7U, res.second.size()); // Check the types ASSERT_TRUE(res.second[0].isString()); @@ -280,7 +280,7 @@ TEST(Reader, parseArray) ASSERT_TRUE(res.first); // Make sure the array has the correct size - ASSERT_EQ(1, res.second.size()); + ASSERT_EQ(1U, res.second.size()); // Check the types ASSERT_TRUE(res.second[0].isString()); @@ -290,22 +290,23 @@ TEST(Reader, parseArray) } // Recovery from invalid values - // TODO: Actually parseGeneric should fall back to returning a simple string - // if parsing of a special (non-string) type failed { CharReader reader("[ 0invalidNumber, str, 1invalid]"); auto res = Reader::parseArray(reader, logger); - ASSERT_FALSE(res.first); + ASSERT_TRUE(res.first); // Make sure the array has the correct size - ASSERT_EQ(3, res.second.size()); + ASSERT_EQ(3U, res.second.size()); - // Check the types (only for the valid entries, the other types are - // undefined) + // Check the types (all must be strings since the numbers are invalid) + ASSERT_TRUE(res.second[0].isString()); ASSERT_TRUE(res.second[1].isString()); + ASSERT_TRUE(res.second[2].isString()); // Check the values + ASSERT_EQ("0invalidNumber", res.second[0].asString()); ASSERT_EQ("str", res.second[1].asString()); + ASSERT_EQ("1invalid", res.second[2].asString()); } } |