diff options
author | Manuel Klimek <klimek@google.com> | 2011-12-21 18:16:39 +0000 |
---|---|---|
committer | Manuel Klimek <klimek@google.com> | 2011-12-21 18:16:39 +0000 |
commit | 84cbb6f00de84fa04012462a28a3636e13834401 (patch) | |
tree | ca8fca2df705cb701ef77cea6028b1b3dcb14403 /unittests | |
parent | edae8e1e4d5bd9b59f18ecef04a248be95d8ca46 (diff) |
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
Diffstat (limited to 'unittests')
-rw-r--r-- | unittests/Support/JSONParserTest.cpp | 55 |
1 files changed, 14 insertions, 41 deletions
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<char> CutTrailingZero(StringRef String) { - std::vector<char> 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<char> &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<char> &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<JSONArray>(Parser.parseRoot()); StringRef ParsedString = dyn_cast<JSONString>(*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<JSONArray>(Parser.parseRoot()); EXPECT_EQ(6, std::distance(Array->begin(), Array->end())); } |