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/MemoryBuffer.h"
14 #include "llvm/Support/SourceMgr.h"
15 #include "llvm/Support/YAMLParser.h"
16 #include "gtest/gtest.h"
20 static void SuppressDiagnosticsOutput(const SMDiagnostic &, void *) {
21 // Prevent SourceMgr from writing errors to stderr
22 // to reduce noise in unit test runs.
25 // Assumes Ctx is an SMDiagnostic where Diag can be stored.
26 static void CollectDiagnosticsOutput(const SMDiagnostic &Diag, void *Ctx) {
27 SMDiagnostic* DiagOut = static_cast<SMDiagnostic*>(Ctx);
31 // Checks that the given input gives a parse error. Makes sure that an error
32 // text is available and the parse fails.
33 static void ExpectParseError(StringRef Message, StringRef Input) {
35 yaml::Stream Stream(Input, SM);
36 SM.setDiagHandler(SuppressDiagnosticsOutput);
37 EXPECT_FALSE(Stream.validate()) << Message << ": " << Input;
38 EXPECT_TRUE(Stream.failed()) << Message << ": " << Input;
41 // Checks that the given input can be parsed without error.
42 static void ExpectParseSuccess(StringRef Message, StringRef Input) {
44 yaml::Stream Stream(Input, SM);
45 EXPECT_TRUE(Stream.validate()) << Message << ": " << Input;
48 TEST(YAMLParser, ParsesEmptyArray) {
49 ExpectParseSuccess("Empty array", "[]");
52 TEST(YAMLParser, FailsIfNotClosingArray) {
53 ExpectParseError("Not closing array", "[");
54 ExpectParseError("Not closing array", " [ ");
55 ExpectParseError("Not closing array", " [x");
58 TEST(YAMLParser, ParsesEmptyArrayWithWhitespace) {
59 ExpectParseSuccess("Array with spaces", " [ ] ");
60 ExpectParseSuccess("All whitespaces", "\t\r\n[\t\n \t\r ]\t\r \n\n");
63 TEST(YAMLParser, ParsesEmptyObject) {
64 ExpectParseSuccess("Empty object", "[{}]");
67 TEST(YAMLParser, ParsesObject) {
68 ExpectParseSuccess("Object with an entry", "[{\"a\":\"/b\"}]");
71 TEST(YAMLParser, ParsesMultipleKeyValuePairsInObject) {
72 ExpectParseSuccess("Multiple key, value pairs",
73 "[{\"a\":\"/b\",\"c\":\"d\",\"e\":\"f\"}]");
76 TEST(YAMLParser, FailsIfNotClosingObject) {
77 ExpectParseError("Missing close on empty", "[{]");
78 ExpectParseError("Missing close after pair", "[{\"a\":\"b\"]");
81 TEST(YAMLParser, FailsIfMissingColon) {
82 ExpectParseError("Missing colon between key and value", "[{\"a\"\"/b\"}]");
83 ExpectParseError("Missing colon between key and value", "[{\"a\" \"b\"}]");
86 TEST(YAMLParser, FailsOnMissingQuote) {
87 ExpectParseError("Missing open quote", "[{a\":\"b\"}]");
88 ExpectParseError("Missing closing quote", "[{\"a\":\"b}]");
91 TEST(YAMLParser, ParsesEscapedQuotes) {
92 ExpectParseSuccess("Parses escaped string in key and value",
93 "[{\"a\":\"\\\"b\\\" \\\" \\\"\"}]");
96 TEST(YAMLParser, ParsesEmptyString) {
97 ExpectParseSuccess("Parses empty string in value", "[{\"a\":\"\"}]");
100 TEST(YAMLParser, ParsesMultipleObjects) {
102 "Multiple objects in array",
104 " { \"a\" : \"b\" },"
105 " { \"a\" : \"b\" },"
110 TEST(YAMLParser, FailsOnMissingComma) {
119 TEST(YAMLParser, ParsesSpacesInBetweenTokens) {
121 "Various whitespace between tokens",
122 " \t \n\n \r [ \t \n\n \r"
123 " \t \n\n \r { \t \n\n \r\"a\"\t \n\n \r :"
124 " \t \n\n \r \"b\"\t \n\n \r } \t \n\n \r,\t \n\n \r"
125 " \t \n\n \r { \t \n\n \r\"a\"\t \n\n \r :"
126 " \t \n\n \r \"b\"\t \n\n \r } \t \n\n \r]\t \n\n \r");
129 TEST(YAMLParser, ParsesArrayOfArrays) {
130 ExpectParseSuccess("Array of arrays", "[[]]");
133 TEST(YAMLParser, ParsesBlockLiteralScalars) {
134 ExpectParseSuccess("Block literal scalar", "test: |\n Hello\n World\n");
135 ExpectParseSuccess("Block literal scalar EOF", "test: |\n Hello\n World");
136 ExpectParseSuccess("Empty block literal scalar header EOF", "test: | ");
137 ExpectParseSuccess("Empty block literal scalar", "test: |\ntest2: 20");
138 ExpectParseSuccess("Empty block literal scalar 2", "- | \n \n\n \n- 42");
139 ExpectParseSuccess("Block literal scalar in sequence",
140 "- |\n Testing\n Out\n\n- 22");
141 ExpectParseSuccess("Block literal scalar in document",
142 "--- |\n Document\n...");
143 ExpectParseSuccess("Empty non indented lines still count",
144 "- |\n First line\n \n\n Another line\n\n- 2");
145 ExpectParseSuccess("Comment in block literal scalar header",
146 "test: | # Comment \n No Comment\ntest 2: | # Void");
147 ExpectParseSuccess("Chomping indicators in block literal scalar header",
148 "test: |- \n Hello\n\ntest 2: |+ \n\n World\n\n\n");
149 ExpectParseSuccess("Indent indicators in block literal scalar header",
150 "test: |1 \n \n Hello \n World\n");
151 ExpectParseSuccess("Chomping and indent indicators in block literals",
152 "test: |-1\n Hello\ntest 2: |9+\n World");
153 ExpectParseSuccess("Trailing comments in block literals",
154 "test: |\n Content\n # Trailing\n #Comment\ntest 2: 3");
155 ExpectParseError("Invalid block scalar header", "test: | failure");
156 ExpectParseError("Invalid line indentation", "test: |\n First line\n Error");
157 ExpectParseError("Long leading space line", "test: |\n \n Test\n");
160 TEST(YAMLParser, HandlesEndOfFileGracefully) {
161 ExpectParseError("In string starting with EOF", "[\"");
162 ExpectParseError("In string hitting EOF", "[\" ");
163 ExpectParseError("In string escaping EOF", "[\" \\");
164 ExpectParseError("In array starting with EOF", "[");
165 ExpectParseError("In array element starting with EOF", "[[], ");
166 ExpectParseError("In array hitting EOF", "[[] ");
167 ExpectParseError("In array hitting EOF", "[[]");
168 ExpectParseError("In object hitting EOF", "{\"\"");
171 TEST(YAMLParser, HandlesNullValuesInKeyValueNodesGracefully) {
172 ExpectParseError("KeyValueNode with null value", "test: '");
175 // Checks that the given string can be parsed into an identical string inside
177 static void ExpectCanParseString(StringRef String) {
178 std::string StringInArray = (llvm::Twine("[\"") + String + "\"]").str();
180 yaml::Stream Stream(StringInArray, SM);
181 yaml::SequenceNode *ParsedSequence
182 = dyn_cast<yaml::SequenceNode>(Stream.begin()->getRoot());
183 StringRef ParsedString
184 = dyn_cast<yaml::ScalarNode>(
185 static_cast<yaml::Node*>(ParsedSequence->begin()))->getRawValue();
186 ParsedString = ParsedString.substr(1, ParsedString.size() - 2);
187 EXPECT_EQ(String, ParsedString.str());
190 // Checks that parsing the given string inside an array fails.
191 static void ExpectCannotParseString(StringRef String) {
192 std::string StringInArray = (llvm::Twine("[\"") + String + "\"]").str();
193 ExpectParseError((Twine("When parsing string \"") + String + "\"").str(),
197 TEST(YAMLParser, ParsesStrings) {
198 ExpectCanParseString("");
199 ExpectCannotParseString("\\");
200 ExpectCannotParseString("\"");
201 ExpectCanParseString(" ");
202 ExpectCanParseString("\\ ");
203 ExpectCanParseString("\\\"");
204 ExpectCannotParseString("\"\\");
205 ExpectCannotParseString(" \\");
206 ExpectCanParseString("\\\\");
207 ExpectCannotParseString("\\\\\\");
208 ExpectCanParseString("\\\\\\\\");
209 ExpectCanParseString("\\\" ");
210 ExpectCannotParseString("\\\\\" ");
211 ExpectCanParseString("\\\\\\\" ");
212 ExpectCanParseString(" \\\\ \\\" \\\\\\\" ");
215 TEST(YAMLParser, WorksWithIteratorAlgorithms) {
217 yaml::Stream Stream("[\"1\", \"2\", \"3\", \"4\", \"5\", \"6\"]", SM);
218 yaml::SequenceNode *Array
219 = dyn_cast<yaml::SequenceNode>(Stream.begin()->getRoot());
220 EXPECT_EQ(6, std::distance(Array->begin(), Array->end()));
223 TEST(YAMLParser, DefaultDiagnosticFilename) {
226 SMDiagnostic GeneratedDiag;
227 SM.setDiagHandler(CollectDiagnosticsOutput, &GeneratedDiag);
229 // When we construct a YAML stream over an unnamed string,
230 // the filename is hard-coded as "YAML".
231 yaml::Stream UnnamedStream("[]", SM);
232 UnnamedStream.printError(UnnamedStream.begin()->getRoot(), "Hello, World!");
233 EXPECT_EQ("YAML", GeneratedDiag.getFilename());
236 TEST(YAMLParser, DiagnosticFilenameFromBufferID) {
239 SMDiagnostic GeneratedDiag;
240 SM.setDiagHandler(CollectDiagnosticsOutput, &GeneratedDiag);
242 // When we construct a YAML stream over a named buffer,
243 // we get its ID as filename in diagnostics.
244 std::unique_ptr<MemoryBuffer> Buffer =
245 MemoryBuffer::getMemBuffer("[]", "buffername.yaml");
246 yaml::Stream Stream(Buffer->getMemBufferRef(), SM);
247 Stream.printError(Stream.begin()->getRoot(), "Hello, World!");
248 EXPECT_EQ("buffername.yaml", GeneratedDiag.getFilename());
251 } // end namespace llvm