From 84cbb6f00de84fa04012462a28a3636e13834401 Mon Sep 17 00:00:00 2001 From: Manuel Klimek Date: Wed, 21 Dec 2011 18:16:39 +0000 Subject: Changes the JSON parser to use the SourceMgr. Diagnostics are now emitted via the SourceMgr and we use MemoryBuffer for buffer management. Switched the code to make use of the trailing '0' that MemoryBuffer guarantees where it makes sense. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@147063 91177308-0d34-0410-b5e6-96231b3b80d8 --- unittests/Support/JSONParserTest.cpp | 55 +++++++++--------------------------- 1 file changed, 14 insertions(+), 41 deletions(-) (limited to 'unittests/Support/JSONParserTest.cpp') diff --git a/unittests/Support/JSONParserTest.cpp b/unittests/Support/JSONParserTest.cpp index 1cd987daf1..e9efb817c2 100644 --- a/unittests/Support/JSONParserTest.cpp +++ b/unittests/Support/JSONParserTest.cpp @@ -14,57 +14,28 @@ namespace llvm { -// Returns a buffer that contains the content of the given string without -// the trailing zero, in order to get valgrind to catch out-of-bound reads. -static std::vector CutTrailingZero(StringRef String) { - std::vector InputWithoutZero(String.size()); - memcpy(&InputWithoutZero[0], String.data(), String.size()); - return InputWithoutZero; -} - // Checks that the given input gives a parse error. Makes sure that an error // text is available and the parse fails. -static void ExpectParseError(StringRef Message, - const std::vector &InputWithoutZero) { - StringRef Input = StringRef(&InputWithoutZero[0], InputWithoutZero.size()); - JSONParser Parser(Input); +static void ExpectParseError(StringRef Message, StringRef Input) { + SourceMgr SM; + JSONParser Parser(Input, &SM); EXPECT_FALSE(Parser.validate()) << Message << ": " << Input; EXPECT_TRUE(Parser.failed()) << Message << ": " << Input; - EXPECT_FALSE(Parser.getErrorMessage().empty()) << Message << ": " << Input; -} - -// Overloads the above to allow using const char * as Input. -static void ExpectParseError(StringRef Message, StringRef Input) { - return ExpectParseError(Message, CutTrailingZero(Input)); } // Checks that the given input can be parsed without error. -static void ExpectParseSuccess(StringRef Message, - const std::vector &InputWithoutZero) { - StringRef Input = StringRef(&InputWithoutZero[0], InputWithoutZero.size()); - JSONParser Parser(Input); - EXPECT_TRUE(Parser.validate()) - << Message << ": " << Input << " - " << Parser.getErrorMessage(); -} - -// Overloads the above to allow using const char * as Input. static void ExpectParseSuccess(StringRef Message, StringRef Input) { - return ExpectParseSuccess(Message, CutTrailingZero(Input)); + SourceMgr SM; + JSONParser Parser(Input, &SM); + EXPECT_TRUE(Parser.validate()) << Message << ": " << Input; } TEST(JSONParser, FailsOnEmptyString) { - JSONParser Parser(""); - EXPECT_EQ(NULL, Parser.parseRoot()); + ExpectParseError("Empty JSON text", ""); } - -TEST(JSONParser, DoesNotReadAfterInput) { - JSONParser Parser(llvm::StringRef(NULL, 0)); - EXPECT_EQ(NULL, Parser.parseRoot()); -} - + TEST(JSONParser, FailsIfStartsWithString) { - JSONParser Character("\"x\""); - EXPECT_EQ(NULL, Character.parseRoot()); + ExpectParseError("Top-level string", "\"x\""); } TEST(JSONParser, ParsesEmptyArray) { @@ -177,11 +148,12 @@ TEST(JSONParser, HandlesEndOfFileGracefully) { // of an array. static void ExpectCanParseString(StringRef String) { std::string StringInArray = (llvm::Twine("[\"") + String + "\"]").str(); - JSONParser Parser(StringInArray); + SourceMgr SM; + JSONParser Parser(StringInArray, &SM); const JSONArray *ParsedArray = dyn_cast(Parser.parseRoot()); StringRef ParsedString = dyn_cast(*ParsedArray->begin())->getRawText(); - EXPECT_EQ(String, ParsedString.str()) << Parser.getErrorMessage(); + EXPECT_EQ(String, ParsedString.str()); } // Checks that parsing the given string inside an array fails. @@ -210,7 +182,8 @@ TEST(JSONParser, ParsesStrings) { } TEST(JSONParser, WorksWithIteratorAlgorithms) { - JSONParser Parser("[\"1\", \"2\", \"3\", \"4\", \"5\", \"6\"]"); + SourceMgr SM; + JSONParser Parser("[\"1\", \"2\", \"3\", \"4\", \"5\", \"6\"]", &SM); const JSONArray *Array = dyn_cast(Parser.parseRoot()); EXPECT_EQ(6, std::distance(Array->begin(), Array->end())); } -- cgit v1.2.3-18-g5258