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, HandlesEndOfFileGracefully) {
134 ExpectParseError("In string starting with EOF", "[\"");
135 ExpectParseError("In string hitting EOF", "[\" ");
136 ExpectParseError("In string escaping EOF", "[\" \\");
137 ExpectParseError("In array starting with EOF", "[");
138 ExpectParseError("In array element starting with EOF", "[[], ");
139 ExpectParseError("In array hitting EOF", "[[] ");
140 ExpectParseError("In array hitting EOF", "[[]");
141 ExpectParseError("In object hitting EOF", "{\"\"");
144 // Checks that the given string can be parsed into an identical string inside
146 static void ExpectCanParseString(StringRef String) {
147 std::string StringInArray = (llvm::Twine("[\"") + String + "\"]").str();
149 yaml::Stream Stream(StringInArray, SM);
150 yaml::SequenceNode *ParsedSequence
151 = dyn_cast<yaml::SequenceNode>(Stream.begin()->getRoot());
152 StringRef ParsedString
153 = dyn_cast<yaml::ScalarNode>(
154 static_cast<yaml::Node*>(ParsedSequence->begin()))->getRawValue();
155 ParsedString = ParsedString.substr(1, ParsedString.size() - 2);
156 EXPECT_EQ(String, ParsedString.str());
159 // Checks that parsing the given string inside an array fails.
160 static void ExpectCannotParseString(StringRef String) {
161 std::string StringInArray = (llvm::Twine("[\"") + String + "\"]").str();
162 ExpectParseError((Twine("When parsing string \"") + String + "\"").str(),
166 TEST(YAMLParser, ParsesStrings) {
167 ExpectCanParseString("");
168 ExpectCannotParseString("\\");
169 ExpectCannotParseString("\"");
170 ExpectCanParseString(" ");
171 ExpectCanParseString("\\ ");
172 ExpectCanParseString("\\\"");
173 ExpectCannotParseString("\"\\");
174 ExpectCannotParseString(" \\");
175 ExpectCanParseString("\\\\");
176 ExpectCannotParseString("\\\\\\");
177 ExpectCanParseString("\\\\\\\\");
178 ExpectCanParseString("\\\" ");
179 ExpectCannotParseString("\\\\\" ");
180 ExpectCanParseString("\\\\\\\" ");
181 ExpectCanParseString(" \\\\ \\\" \\\\\\\" ");
184 TEST(YAMLParser, WorksWithIteratorAlgorithms) {
186 yaml::Stream Stream("[\"1\", \"2\", \"3\", \"4\", \"5\", \"6\"]", SM);
187 yaml::SequenceNode *Array
188 = dyn_cast<yaml::SequenceNode>(Stream.begin()->getRoot());
189 EXPECT_EQ(6, std::distance(Array->begin(), Array->end()));
192 TEST(YAMLParser, DefaultDiagnosticFilename) {
195 SMDiagnostic GeneratedDiag;
196 SM.setDiagHandler(CollectDiagnosticsOutput, &GeneratedDiag);
198 // When we construct a YAML stream over an unnamed string,
199 // the filename is hard-coded as "YAML".
200 yaml::Stream UnnamedStream("[]", SM);
201 UnnamedStream.printError(UnnamedStream.begin()->getRoot(), "Hello, World!");
202 EXPECT_EQ("YAML", GeneratedDiag.getFilename());
205 TEST(YAMLParser, DiagnosticFilenameFromBufferID) {
208 SMDiagnostic GeneratedDiag;
209 SM.setDiagHandler(CollectDiagnosticsOutput, &GeneratedDiag);
211 // When we construct a YAML stream over a named buffer,
212 // we get its ID as filename in diagnostics.
213 MemoryBuffer* Buffer = MemoryBuffer::getMemBuffer("[]", "buffername.yaml");
214 yaml::Stream Stream(Buffer, SM);
215 Stream.printError(Stream.begin()->getRoot(), "Hello, World!");
216 EXPECT_EQ("buffername.yaml", GeneratedDiag.getFilename());
219 } // end namespace llvm