1 //===- unittest/Support/YAMLParserTest ------------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "llvm/ADT/SmallString.h"
11 #include "llvm/ADT/Twine.h"
12 #include "llvm/Support/Casting.h"
13 #include "llvm/Support/SourceMgr.h"
14 #include "llvm/Support/YAMLParser.h"
15 #include "gtest/gtest.h"
19 // Checks that the given input gives a parse error. Makes sure that an error
20 // text is available and the parse fails.
21 static void ExpectParseError(StringRef Message, StringRef Input) {
23 yaml::Stream Stream(Input, SM);
24 EXPECT_FALSE(Stream.validate()) << Message << ": " << Input;
25 EXPECT_TRUE(Stream.failed()) << Message << ": " << Input;
28 // Checks that the given input can be parsed without error.
29 static void ExpectParseSuccess(StringRef Message, StringRef Input) {
31 yaml::Stream Stream(Input, SM);
32 EXPECT_TRUE(Stream.validate()) << Message << ": " << Input;
35 TEST(YAMLParser, ParsesEmptyArray) {
36 ExpectParseSuccess("Empty array", "[]");
39 TEST(YAMLParser, FailsIfNotClosingArray) {
40 ExpectParseError("Not closing array", "[");
41 ExpectParseError("Not closing array", " [ ");
42 ExpectParseError("Not closing array", " [x");
45 TEST(YAMLParser, ParsesEmptyArrayWithWhitespace) {
46 ExpectParseSuccess("Array with spaces", " [ ] ");
47 ExpectParseSuccess("All whitespaces", "\t\r\n[\t\n \t\r ]\t\r \n\n");
50 TEST(YAMLParser, ParsesEmptyObject) {
51 ExpectParseSuccess("Empty object", "[{}]");
54 TEST(YAMLParser, ParsesObject) {
55 ExpectParseSuccess("Object with an entry", "[{\"a\":\"/b\"}]");
58 TEST(YAMLParser, ParsesMultipleKeyValuePairsInObject) {
59 ExpectParseSuccess("Multiple key, value pairs",
60 "[{\"a\":\"/b\",\"c\":\"d\",\"e\":\"f\"}]");
63 TEST(YAMLParser, FailsIfNotClosingObject) {
64 ExpectParseError("Missing close on empty", "[{]");
65 ExpectParseError("Missing close after pair", "[{\"a\":\"b\"]");
68 TEST(YAMLParser, FailsIfMissingColon) {
69 ExpectParseError("Missing colon between key and value", "[{\"a\"\"/b\"}]");
70 ExpectParseError("Missing colon between key and value", "[{\"a\" \"b\"}]");
73 TEST(YAMLParser, FailsOnMissingQuote) {
74 ExpectParseError("Missing open quote", "[{a\":\"b\"}]");
75 ExpectParseError("Missing closing quote", "[{\"a\":\"b}]");
78 TEST(YAMLParser, ParsesEscapedQuotes) {
79 ExpectParseSuccess("Parses escaped string in key and value",
80 "[{\"a\":\"\\\"b\\\" \\\" \\\"\"}]");
83 TEST(YAMLParser, ParsesEmptyString) {
84 ExpectParseSuccess("Parses empty string in value", "[{\"a\":\"\"}]");
87 TEST(YAMLParser, ParsesMultipleObjects) {
89 "Multiple objects in array",
97 TEST(YAMLParser, FailsOnMissingComma) {
106 TEST(YAMLParser, ParsesSpacesInBetweenTokens) {
108 "Various whitespace between tokens",
109 " \t \n\n \r [ \t \n\n \r"
110 " \t \n\n \r { \t \n\n \r\"a\"\t \n\n \r :"
111 " \t \n\n \r \"b\"\t \n\n \r } \t \n\n \r,\t \n\n \r"
112 " \t \n\n \r { \t \n\n \r\"a\"\t \n\n \r :"
113 " \t \n\n \r \"b\"\t \n\n \r } \t \n\n \r]\t \n\n \r");
116 TEST(YAMLParser, ParsesArrayOfArrays) {
117 ExpectParseSuccess("Array of arrays", "[[]]");
120 TEST(YAMLParser, HandlesEndOfFileGracefully) {
121 ExpectParseError("In string starting with EOF", "[\"");
122 ExpectParseError("In string hitting EOF", "[\" ");
123 ExpectParseError("In string escaping EOF", "[\" \\");
124 ExpectParseError("In array starting with EOF", "[");
125 ExpectParseError("In array element starting with EOF", "[[], ");
126 ExpectParseError("In array hitting EOF", "[[] ");
127 ExpectParseError("In array hitting EOF", "[[]");
128 ExpectParseError("In object hitting EOF", "{\"\"");
131 // Checks that the given string can be parsed into an identical string inside
133 static void ExpectCanParseString(StringRef String) {
134 std::string StringInArray = (llvm::Twine("[\"") + String + "\"]").str();
136 yaml::Stream Stream(StringInArray, SM);
137 yaml::SequenceNode *ParsedSequence
138 = dyn_cast<yaml::SequenceNode>(Stream.begin()->getRoot());
139 StringRef ParsedString
140 = dyn_cast<yaml::ScalarNode>(
141 static_cast<yaml::Node*>(ParsedSequence->begin()))->getRawValue();
142 ParsedString = ParsedString.substr(1, ParsedString.size() - 2);
143 EXPECT_EQ(String, ParsedString.str());
146 // Checks that parsing the given string inside an array fails.
147 static void ExpectCannotParseString(StringRef String) {
148 std::string StringInArray = (llvm::Twine("[\"") + String + "\"]").str();
149 ExpectParseError((Twine("When parsing string \"") + String + "\"").str(),
153 TEST(YAMLParser, ParsesStrings) {
154 ExpectCanParseString("");
155 ExpectCannotParseString("\\");
156 ExpectCannotParseString("\"");
157 ExpectCanParseString(" ");
158 ExpectCanParseString("\\ ");
159 ExpectCanParseString("\\\"");
160 ExpectCannotParseString("\"\\");
161 ExpectCannotParseString(" \\");
162 ExpectCanParseString("\\\\");
163 ExpectCannotParseString("\\\\\\");
164 ExpectCanParseString("\\\\\\\\");
165 ExpectCanParseString("\\\" ");
166 ExpectCannotParseString("\\\\\" ");
167 ExpectCanParseString("\\\\\\\" ");
168 ExpectCanParseString(" \\\\ \\\" \\\\\\\" ");
171 TEST(YAMLParser, WorksWithIteratorAlgorithms) {
173 yaml::Stream Stream("[\"1\", \"2\", \"3\", \"4\", \"5\", \"6\"]", SM);
174 yaml::SequenceNode *Array
175 = dyn_cast<yaml::SequenceNode>(Stream.begin()->getRoot());
176 EXPECT_EQ(6, std::distance(Array->begin(), Array->end()));
179 } // end namespace llvm