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/Support/YAMLParser.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 "gtest/gtest.h"
19 static void SuppressDiagnosticsOutput(const SMDiagnostic
&, void *) {
20 // Prevent SourceMgr from writing errors to stderr
21 // to reduce noise in unit test runs.
24 // Assumes Ctx is an SMDiagnostic where Diag can be stored.
25 static void CollectDiagnosticsOutput(const SMDiagnostic
&Diag
, void *Ctx
) {
26 SMDiagnostic
* DiagOut
= static_cast<SMDiagnostic
*>(Ctx
);
30 // Checks that the given input gives a parse error. Makes sure that an error
31 // text is available and the parse fails.
32 static void ExpectParseError(StringRef Message
, StringRef Input
) {
34 yaml::Stream
Stream(Input
, SM
);
35 SM
.setDiagHandler(SuppressDiagnosticsOutput
);
36 EXPECT_FALSE(Stream
.validate()) << Message
<< ": " << Input
;
37 EXPECT_TRUE(Stream
.failed()) << Message
<< ": " << Input
;
40 // Checks that the given input can be parsed without error.
41 static void ExpectParseSuccess(StringRef Message
, StringRef Input
) {
43 yaml::Stream
Stream(Input
, SM
);
44 EXPECT_TRUE(Stream
.validate()) << Message
<< ": " << Input
;
47 TEST(YAMLParser
, ParsesEmptyArray
) {
48 ExpectParseSuccess("Empty array", "[]");
51 TEST(YAMLParser
, FailsIfNotClosingArray
) {
52 ExpectParseError("Not closing array", "[");
53 ExpectParseError("Not closing array", " [ ");
54 ExpectParseError("Not closing array", " [x");
57 TEST(YAMLParser
, ParsesEmptyArrayWithWhitespace
) {
58 ExpectParseSuccess("Array with spaces", " [ ] ");
59 ExpectParseSuccess("All whitespaces", "\t\r\n[\t\n \t\r ]\t\r \n\n");
62 TEST(YAMLParser
, ParsesEmptyObject
) {
63 ExpectParseSuccess("Empty object", "[{}]");
66 TEST(YAMLParser
, ParsesObject
) {
67 ExpectParseSuccess("Object with an entry", "[{\"a\":\"/b\"}]");
70 TEST(YAMLParser
, ParsesMultipleKeyValuePairsInObject
) {
71 ExpectParseSuccess("Multiple key, value pairs",
72 "[{\"a\":\"/b\",\"c\":\"d\",\"e\":\"f\"}]");
75 TEST(YAMLParser
, FailsIfNotClosingObject
) {
76 ExpectParseError("Missing close on empty", "[{]");
77 ExpectParseError("Missing close after pair", "[{\"a\":\"b\"]");
80 TEST(YAMLParser
, FailsIfMissingColon
) {
81 ExpectParseError("Missing colon between key and value", "[{\"a\"\"/b\"}]");
82 ExpectParseError("Missing colon between key and value", "[{\"a\" \"b\"}]");
85 TEST(YAMLParser
, FailsOnMissingQuote
) {
86 ExpectParseError("Missing open quote", "[{a\":\"b\"}]");
87 ExpectParseError("Missing closing quote", "[{\"a\":\"b}]");
90 TEST(YAMLParser
, ParsesEscapedQuotes
) {
91 ExpectParseSuccess("Parses escaped string in key and value",
92 "[{\"a\":\"\\\"b\\\" \\\" \\\"\"}]");
95 TEST(YAMLParser
, ParsesEmptyString
) {
96 ExpectParseSuccess("Parses empty string in value", "[{\"a\":\"\"}]");
99 TEST(YAMLParser
, ParsesMultipleObjects
) {
101 "Multiple objects in array",
103 " { \"a\" : \"b\" },"
104 " { \"a\" : \"b\" },"
109 TEST(YAMLParser
, FailsOnMissingComma
) {
118 TEST(YAMLParser
, ParsesSpacesInBetweenTokens
) {
120 "Various whitespace between tokens",
121 " \t \n\n \r [ \t \n\n \r"
122 " \t \n\n \r { \t \n\n \r\"a\"\t \n\n \r :"
123 " \t \n\n \r \"b\"\t \n\n \r } \t \n\n \r,\t \n\n \r"
124 " \t \n\n \r { \t \n\n \r\"a\"\t \n\n \r :"
125 " \t \n\n \r \"b\"\t \n\n \r } \t \n\n \r]\t \n\n \r");
128 TEST(YAMLParser
, ParsesArrayOfArrays
) {
129 ExpectParseSuccess("Array of arrays", "[[]]");
132 TEST(YAMLParser
, ParsesBlockLiteralScalars
) {
133 ExpectParseSuccess("Block literal scalar", "test: |\n Hello\n World\n");
134 ExpectParseSuccess("Block literal scalar EOF", "test: |\n Hello\n World");
135 ExpectParseSuccess("Empty block literal scalar header EOF", "test: | ");
136 ExpectParseSuccess("Empty block literal scalar", "test: |\ntest2: 20");
137 ExpectParseSuccess("Empty block literal scalar 2", "- | \n \n\n \n- 42");
138 ExpectParseSuccess("Block literal scalar in sequence",
139 "- |\n Testing\n Out\n\n- 22");
140 ExpectParseSuccess("Block literal scalar in document",
141 "--- |\n Document\n...");
142 ExpectParseSuccess("Empty non indented lines still count",
143 "- |\n First line\n \n\n Another line\n\n- 2");
144 ExpectParseSuccess("Comment in block literal scalar header",
145 "test: | # Comment \n No Comment\ntest 2: | # Void");
146 ExpectParseSuccess("Chomping indicators in block literal scalar header",
147 "test: |- \n Hello\n\ntest 2: |+ \n\n World\n\n\n");
148 ExpectParseSuccess("Indent indicators in block literal scalar header",
149 "test: |1 \n \n Hello \n World\n");
150 ExpectParseSuccess("Chomping and indent indicators in block literals",
151 "test: |-1\n Hello\ntest 2: |9+\n World");
152 ExpectParseSuccess("Trailing comments in block literals",
153 "test: |\n Content\n # Trailing\n #Comment\ntest 2: 3");
154 ExpectParseError("Invalid block scalar header", "test: | failure");
155 ExpectParseError("Invalid line indentation", "test: |\n First line\n Error");
156 ExpectParseError("Long leading space line", "test: |\n \n Test\n");
159 TEST(YAMLParser
, NullTerminatedBlockScalars
) {
161 yaml::Stream
Stream("test: |\n Hello\n World\n", SM
);
162 yaml::Document
&Doc
= *Stream
.begin();
163 yaml::MappingNode
*Map
= cast
<yaml::MappingNode
>(Doc
.getRoot());
165 cast
<yaml::BlockScalarNode
>(Map
->begin()->getValue())->getValue();
167 EXPECT_EQ(Value
, "Hello\nWorld\n");
168 EXPECT_EQ(Value
.data()[Value
.size()], '\0');
171 TEST(YAMLParser
, HandlesEndOfFileGracefully
) {
172 ExpectParseError("In string starting with EOF", "[\"");
173 ExpectParseError("In string hitting EOF", "[\" ");
174 ExpectParseError("In string escaping EOF", "[\" \\");
175 ExpectParseError("In array starting with EOF", "[");
176 ExpectParseError("In array element starting with EOF", "[[], ");
177 ExpectParseError("In array hitting EOF", "[[] ");
178 ExpectParseError("In array hitting EOF", "[[]");
179 ExpectParseError("In object hitting EOF", "{\"\"");
182 TEST(YAMLParser
, HandlesNullValuesInKeyValueNodesGracefully
) {
183 ExpectParseError("KeyValueNode with null key", "? \"\n:");
184 ExpectParseError("KeyValueNode with null value", "test: '");
187 // Checks that the given string can be parsed into an identical string inside
189 static void ExpectCanParseString(StringRef String
) {
190 std::string StringInArray
= (llvm::Twine("[\"") + String
+ "\"]").str();
192 yaml::Stream
Stream(StringInArray
, SM
);
193 yaml::SequenceNode
*ParsedSequence
194 = dyn_cast
<yaml::SequenceNode
>(Stream
.begin()->getRoot());
195 StringRef ParsedString
196 = dyn_cast
<yaml::ScalarNode
>(
197 static_cast<yaml::Node
*>(ParsedSequence
->begin()))->getRawValue();
198 ParsedString
= ParsedString
.substr(1, ParsedString
.size() - 2);
199 EXPECT_EQ(String
, ParsedString
.str());
202 // Checks that parsing the given string inside an array fails.
203 static void ExpectCannotParseString(StringRef String
) {
204 std::string StringInArray
= (llvm::Twine("[\"") + String
+ "\"]").str();
205 ExpectParseError((Twine("When parsing string \"") + String
+ "\"").str(),
209 TEST(YAMLParser
, ParsesStrings
) {
210 ExpectCanParseString("");
211 ExpectCannotParseString("\\");
212 ExpectCannotParseString("\"");
213 ExpectCanParseString(" ");
214 ExpectCanParseString("\\ ");
215 ExpectCanParseString("\\\"");
216 ExpectCannotParseString("\"\\");
217 ExpectCannotParseString(" \\");
218 ExpectCanParseString("\\\\");
219 ExpectCannotParseString("\\\\\\");
220 ExpectCanParseString("\\\\\\\\");
221 ExpectCanParseString("\\\" ");
222 ExpectCannotParseString("\\\\\" ");
223 ExpectCanParseString("\\\\\\\" ");
224 ExpectCanParseString(" \\\\ \\\" \\\\\\\" ");
227 TEST(YAMLParser
, WorksWithIteratorAlgorithms
) {
229 yaml::Stream
Stream("[\"1\", \"2\", \"3\", \"4\", \"5\", \"6\"]", SM
);
230 yaml::SequenceNode
*Array
231 = dyn_cast
<yaml::SequenceNode
>(Stream
.begin()->getRoot());
232 EXPECT_EQ(6, std::distance(Array
->begin(), Array
->end()));
235 TEST(YAMLParser
, DefaultDiagnosticFilename
) {
238 SMDiagnostic GeneratedDiag
;
239 SM
.setDiagHandler(CollectDiagnosticsOutput
, &GeneratedDiag
);
241 // When we construct a YAML stream over an unnamed string,
242 // the filename is hard-coded as "YAML".
243 yaml::Stream
UnnamedStream("[]", SM
);
244 UnnamedStream
.printError(UnnamedStream
.begin()->getRoot(), "Hello, World!");
245 EXPECT_EQ("YAML", GeneratedDiag
.getFilename());
248 TEST(YAMLParser
, DiagnosticFilenameFromBufferID
) {
251 SMDiagnostic GeneratedDiag
;
252 SM
.setDiagHandler(CollectDiagnosticsOutput
, &GeneratedDiag
);
254 // When we construct a YAML stream over a named buffer,
255 // we get its ID as filename in diagnostics.
256 std::unique_ptr
<MemoryBuffer
> Buffer
=
257 MemoryBuffer::getMemBuffer("[]", "buffername.yaml");
258 yaml::Stream
Stream(Buffer
->getMemBufferRef(), SM
);
259 Stream
.printError(Stream
.begin()->getRoot(), "Hello, World!");
260 EXPECT_EQ("buffername.yaml", GeneratedDiag
.getFilename());
263 TEST(YAMLParser
, SameNodeIteratorOperatorNotEquals
) {
265 yaml::Stream
Stream("[\"1\", \"2\"]", SM
);
267 yaml::SequenceNode
*Node
= dyn_cast
<yaml::SequenceNode
>(
268 Stream
.begin()->getRoot());
270 auto Begin
= Node
->begin();
271 auto End
= Node
->end();
273 EXPECT_TRUE(Begin
!= End
);
274 EXPECT_FALSE(Begin
!= Begin
);
275 EXPECT_FALSE(End
!= End
);
278 TEST(YAMLParser
, SameNodeIteratorOperatorEquals
) {
280 yaml::Stream
Stream("[\"1\", \"2\"]", SM
);
282 yaml::SequenceNode
*Node
= dyn_cast
<yaml::SequenceNode
>(
283 Stream
.begin()->getRoot());
285 auto Begin
= Node
->begin();
286 auto End
= Node
->end();
288 EXPECT_FALSE(Begin
== End
);
289 EXPECT_TRUE(Begin
== Begin
);
290 EXPECT_TRUE(End
== End
);
293 TEST(YAMLParser
, DifferentNodesIteratorOperatorNotEquals
) {
295 yaml::Stream
Stream("[\"1\", \"2\"]", SM
);
296 yaml::Stream
AnotherStream("[\"1\", \"2\"]", SM
);
298 yaml::SequenceNode
*Node
= dyn_cast
<yaml::SequenceNode
>(
299 Stream
.begin()->getRoot());
300 yaml::SequenceNode
*AnotherNode
= dyn_cast
<yaml::SequenceNode
>(
301 AnotherStream
.begin()->getRoot());
303 auto Begin
= Node
->begin();
304 auto End
= Node
->end();
306 auto AnotherBegin
= AnotherNode
->begin();
307 auto AnotherEnd
= AnotherNode
->end();
309 EXPECT_TRUE(Begin
!= AnotherBegin
);
310 EXPECT_TRUE(Begin
!= AnotherEnd
);
311 EXPECT_FALSE(End
!= AnotherEnd
);
314 TEST(YAMLParser
, DifferentNodesIteratorOperatorEquals
) {
316 yaml::Stream
Stream("[\"1\", \"2\"]", SM
);
317 yaml::Stream
AnotherStream("[\"1\", \"2\"]", SM
);
319 yaml::SequenceNode
*Node
= dyn_cast
<yaml::SequenceNode
>(
320 Stream
.begin()->getRoot());
321 yaml::SequenceNode
*AnotherNode
= dyn_cast
<yaml::SequenceNode
>(
322 AnotherStream
.begin()->getRoot());
324 auto Begin
= Node
->begin();
325 auto End
= Node
->end();
327 auto AnotherBegin
= AnotherNode
->begin();
328 auto AnotherEnd
= AnotherNode
->end();
330 EXPECT_FALSE(Begin
== AnotherBegin
);
331 EXPECT_FALSE(Begin
== AnotherEnd
);
332 EXPECT_TRUE(End
== AnotherEnd
);
335 } // end namespace llvm