2012-04-03 23:09:22 +00:00
|
|
|
//===- unittest/Support/YAMLParserTest ------------------------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/ADT/SmallString.h"
|
|
|
|
#include "llvm/ADT/Twine.h"
|
|
|
|
#include "llvm/Support/Casting.h"
|
2012-11-19 23:21:47 +00:00
|
|
|
#include "llvm/Support/MemoryBuffer.h"
|
2012-04-03 23:09:22 +00:00
|
|
|
#include "llvm/Support/SourceMgr.h"
|
|
|
|
#include "llvm/Support/YAMLParser.h"
|
|
|
|
#include "gtest/gtest.h"
|
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
|
2012-07-31 22:18:15 +00:00
|
|
|
static void SuppressDiagnosticsOutput(const SMDiagnostic &, void *) {
|
2014-08-13 16:11:50 +00:00
|
|
|
// Prevent SourceMgr from writing errors to stderr
|
2012-07-31 22:18:15 +00:00
|
|
|
// to reduce noise in unit test runs.
|
|
|
|
}
|
|
|
|
|
2012-11-19 23:21:47 +00:00
|
|
|
// Assumes Ctx is an SMDiagnostic where Diag can be stored.
|
|
|
|
static void CollectDiagnosticsOutput(const SMDiagnostic &Diag, void *Ctx) {
|
|
|
|
SMDiagnostic* DiagOut = static_cast<SMDiagnostic*>(Ctx);
|
|
|
|
*DiagOut = Diag;
|
|
|
|
}
|
|
|
|
|
2012-04-03 23:09:22 +00:00
|
|
|
// 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, StringRef Input) {
|
|
|
|
SourceMgr SM;
|
|
|
|
yaml::Stream Stream(Input, SM);
|
2012-07-31 22:18:15 +00:00
|
|
|
SM.setDiagHandler(SuppressDiagnosticsOutput);
|
2012-04-03 23:09:22 +00:00
|
|
|
EXPECT_FALSE(Stream.validate()) << Message << ": " << Input;
|
|
|
|
EXPECT_TRUE(Stream.failed()) << Message << ": " << Input;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Checks that the given input can be parsed without error.
|
|
|
|
static void ExpectParseSuccess(StringRef Message, StringRef Input) {
|
|
|
|
SourceMgr SM;
|
|
|
|
yaml::Stream Stream(Input, SM);
|
|
|
|
EXPECT_TRUE(Stream.validate()) << Message << ": " << Input;
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(YAMLParser, ParsesEmptyArray) {
|
|
|
|
ExpectParseSuccess("Empty array", "[]");
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(YAMLParser, FailsIfNotClosingArray) {
|
|
|
|
ExpectParseError("Not closing array", "[");
|
|
|
|
ExpectParseError("Not closing array", " [ ");
|
|
|
|
ExpectParseError("Not closing array", " [x");
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(YAMLParser, ParsesEmptyArrayWithWhitespace) {
|
|
|
|
ExpectParseSuccess("Array with spaces", " [ ] ");
|
|
|
|
ExpectParseSuccess("All whitespaces", "\t\r\n[\t\n \t\r ]\t\r \n\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(YAMLParser, ParsesEmptyObject) {
|
|
|
|
ExpectParseSuccess("Empty object", "[{}]");
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(YAMLParser, ParsesObject) {
|
|
|
|
ExpectParseSuccess("Object with an entry", "[{\"a\":\"/b\"}]");
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(YAMLParser, ParsesMultipleKeyValuePairsInObject) {
|
|
|
|
ExpectParseSuccess("Multiple key, value pairs",
|
|
|
|
"[{\"a\":\"/b\",\"c\":\"d\",\"e\":\"f\"}]");
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(YAMLParser, FailsIfNotClosingObject) {
|
|
|
|
ExpectParseError("Missing close on empty", "[{]");
|
|
|
|
ExpectParseError("Missing close after pair", "[{\"a\":\"b\"]");
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(YAMLParser, FailsIfMissingColon) {
|
|
|
|
ExpectParseError("Missing colon between key and value", "[{\"a\"\"/b\"}]");
|
|
|
|
ExpectParseError("Missing colon between key and value", "[{\"a\" \"b\"}]");
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(YAMLParser, FailsOnMissingQuote) {
|
|
|
|
ExpectParseError("Missing open quote", "[{a\":\"b\"}]");
|
|
|
|
ExpectParseError("Missing closing quote", "[{\"a\":\"b}]");
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(YAMLParser, ParsesEscapedQuotes) {
|
|
|
|
ExpectParseSuccess("Parses escaped string in key and value",
|
|
|
|
"[{\"a\":\"\\\"b\\\" \\\" \\\"\"}]");
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(YAMLParser, ParsesEmptyString) {
|
|
|
|
ExpectParseSuccess("Parses empty string in value", "[{\"a\":\"\"}]");
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(YAMLParser, ParsesMultipleObjects) {
|
|
|
|
ExpectParseSuccess(
|
|
|
|
"Multiple objects in array",
|
|
|
|
"["
|
|
|
|
" { \"a\" : \"b\" },"
|
|
|
|
" { \"a\" : \"b\" },"
|
|
|
|
" { \"a\" : \"b\" }"
|
|
|
|
"]");
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(YAMLParser, FailsOnMissingComma) {
|
|
|
|
ExpectParseError(
|
|
|
|
"Missing comma",
|
|
|
|
"["
|
|
|
|
" { \"a\" : \"b\" }"
|
|
|
|
" { \"a\" : \"b\" }"
|
|
|
|
"]");
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(YAMLParser, ParsesSpacesInBetweenTokens) {
|
|
|
|
ExpectParseSuccess(
|
|
|
|
"Various whitespace between tokens",
|
|
|
|
" \t \n\n \r [ \t \n\n \r"
|
|
|
|
" \t \n\n \r { \t \n\n \r\"a\"\t \n\n \r :"
|
|
|
|
" \t \n\n \r \"b\"\t \n\n \r } \t \n\n \r,\t \n\n \r"
|
|
|
|
" \t \n\n \r { \t \n\n \r\"a\"\t \n\n \r :"
|
|
|
|
" \t \n\n \r \"b\"\t \n\n \r } \t \n\n \r]\t \n\n \r");
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(YAMLParser, ParsesArrayOfArrays) {
|
|
|
|
ExpectParseSuccess("Array of arrays", "[[]]");
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(YAMLParser, HandlesEndOfFileGracefully) {
|
|
|
|
ExpectParseError("In string starting with EOF", "[\"");
|
|
|
|
ExpectParseError("In string hitting EOF", "[\" ");
|
|
|
|
ExpectParseError("In string escaping EOF", "[\" \\");
|
|
|
|
ExpectParseError("In array starting with EOF", "[");
|
|
|
|
ExpectParseError("In array element starting with EOF", "[[], ");
|
|
|
|
ExpectParseError("In array hitting EOF", "[[] ");
|
|
|
|
ExpectParseError("In array hitting EOF", "[[]");
|
|
|
|
ExpectParseError("In object hitting EOF", "{\"\"");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Checks that the given string can be parsed into an identical string inside
|
|
|
|
// of an array.
|
|
|
|
static void ExpectCanParseString(StringRef String) {
|
|
|
|
std::string StringInArray = (llvm::Twine("[\"") + String + "\"]").str();
|
|
|
|
SourceMgr SM;
|
|
|
|
yaml::Stream Stream(StringInArray, SM);
|
|
|
|
yaml::SequenceNode *ParsedSequence
|
|
|
|
= dyn_cast<yaml::SequenceNode>(Stream.begin()->getRoot());
|
|
|
|
StringRef ParsedString
|
|
|
|
= dyn_cast<yaml::ScalarNode>(
|
|
|
|
static_cast<yaml::Node*>(ParsedSequence->begin()))->getRawValue();
|
|
|
|
ParsedString = ParsedString.substr(1, ParsedString.size() - 2);
|
|
|
|
EXPECT_EQ(String, ParsedString.str());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Checks that parsing the given string inside an array fails.
|
|
|
|
static void ExpectCannotParseString(StringRef String) {
|
|
|
|
std::string StringInArray = (llvm::Twine("[\"") + String + "\"]").str();
|
|
|
|
ExpectParseError((Twine("When parsing string \"") + String + "\"").str(),
|
|
|
|
StringInArray);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(YAMLParser, ParsesStrings) {
|
|
|
|
ExpectCanParseString("");
|
|
|
|
ExpectCannotParseString("\\");
|
|
|
|
ExpectCannotParseString("\"");
|
|
|
|
ExpectCanParseString(" ");
|
|
|
|
ExpectCanParseString("\\ ");
|
|
|
|
ExpectCanParseString("\\\"");
|
|
|
|
ExpectCannotParseString("\"\\");
|
|
|
|
ExpectCannotParseString(" \\");
|
|
|
|
ExpectCanParseString("\\\\");
|
|
|
|
ExpectCannotParseString("\\\\\\");
|
|
|
|
ExpectCanParseString("\\\\\\\\");
|
|
|
|
ExpectCanParseString("\\\" ");
|
|
|
|
ExpectCannotParseString("\\\\\" ");
|
|
|
|
ExpectCanParseString("\\\\\\\" ");
|
|
|
|
ExpectCanParseString(" \\\\ \\\" \\\\\\\" ");
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(YAMLParser, WorksWithIteratorAlgorithms) {
|
|
|
|
SourceMgr SM;
|
|
|
|
yaml::Stream Stream("[\"1\", \"2\", \"3\", \"4\", \"5\", \"6\"]", SM);
|
|
|
|
yaml::SequenceNode *Array
|
|
|
|
= dyn_cast<yaml::SequenceNode>(Stream.begin()->getRoot());
|
|
|
|
EXPECT_EQ(6, std::distance(Array->begin(), Array->end()));
|
|
|
|
}
|
|
|
|
|
2012-11-19 23:21:47 +00:00
|
|
|
TEST(YAMLParser, DefaultDiagnosticFilename) {
|
|
|
|
SourceMgr SM;
|
|
|
|
|
|
|
|
SMDiagnostic GeneratedDiag;
|
|
|
|
SM.setDiagHandler(CollectDiagnosticsOutput, &GeneratedDiag);
|
|
|
|
|
|
|
|
// When we construct a YAML stream over an unnamed string,
|
|
|
|
// the filename is hard-coded as "YAML".
|
|
|
|
yaml::Stream UnnamedStream("[]", SM);
|
|
|
|
UnnamedStream.printError(UnnamedStream.begin()->getRoot(), "Hello, World!");
|
|
|
|
EXPECT_EQ("YAML", GeneratedDiag.getFilename());
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(YAMLParser, DiagnosticFilenameFromBufferID) {
|
|
|
|
SourceMgr SM;
|
|
|
|
|
|
|
|
SMDiagnostic GeneratedDiag;
|
|
|
|
SM.setDiagHandler(CollectDiagnosticsOutput, &GeneratedDiag);
|
|
|
|
|
|
|
|
// When we construct a YAML stream over a named buffer,
|
|
|
|
// we get its ID as filename in diagnostics.
|
2014-08-17 22:20:33 +00:00
|
|
|
std::unique_ptr<MemoryBuffer> Buffer(
|
|
|
|
MemoryBuffer::getMemBuffer("[]", "buffername.yaml"));
|
2014-08-27 19:03:22 +00:00
|
|
|
yaml::Stream Stream(Buffer->getMemBufferRef(), SM);
|
2012-11-19 23:21:47 +00:00
|
|
|
Stream.printError(Stream.begin()->getRoot(), "Hello, World!");
|
|
|
|
EXPECT_EQ("buffername.yaml", GeneratedDiag.getFilename());
|
|
|
|
}
|
|
|
|
|
2012-04-03 23:09:22 +00:00
|
|
|
} // end namespace llvm
|