summaryrefslogtreecommitdiffstats
path: root/llvm/unittests
diff options
context:
space:
mode:
authorManuel Klimek <klimek@google.com>2011-12-21 18:16:39 +0000
committerManuel Klimek <klimek@google.com>2011-12-21 18:16:39 +0000
commit25eb0ac4183ff31a1886719a52ccda979472ab5b (patch)
treea9261824411b3d04292a6473aae69cb4cdf3f454 /llvm/unittests
parent9b5eaa07e9a3bb780acc48844e7e977d62efa5ea (diff)
downloadbcm5719-llvm-25eb0ac4183ff31a1886719a52ccda979472ab5b.tar.gz
bcm5719-llvm-25eb0ac4183ff31a1886719a52ccda979472ab5b.zip
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. llvm-svn: 147063
Diffstat (limited to 'llvm/unittests')
-rw-r--r--llvm/unittests/Support/JSONParserTest.cpp55
1 files changed, 14 insertions, 41 deletions
diff --git a/llvm/unittests/Support/JSONParserTest.cpp b/llvm/unittests/Support/JSONParserTest.cpp
index 1cd987daf1e..e9efb817c29 100644
--- a/llvm/unittests/Support/JSONParserTest.cpp
+++ b/llvm/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()));
}
OpenPOWER on IntegriCloud