blob: 918c2059ea63d55ddfc7bbeecf0f2032521e2db8 [file] [log] [blame]
Michael J. Spencer22120c42012-04-03 23:09:22 +00001//===- unittest/Support/YAMLParserTest ------------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "llvm/ADT/SmallString.h"
11#include "llvm/ADT/Twine.h"
12#include "llvm/Support/Casting.h"
Sean Silvaaba82702012-11-19 23:21:47 +000013#include "llvm/Support/MemoryBuffer.h"
Michael J. Spencer22120c42012-04-03 23:09:22 +000014#include "llvm/Support/SourceMgr.h"
15#include "llvm/Support/YAMLParser.h"
16#include "gtest/gtest.h"
17
18namespace llvm {
19
Nick Kledzik204720b2012-07-31 22:18:15 +000020static void SuppressDiagnosticsOutput(const SMDiagnostic &, void *) {
Benjamin Fosterbdefab92014-08-13 16:11:50 +000021 // Prevent SourceMgr from writing errors to stderr
Nick Kledzik204720b2012-07-31 22:18:15 +000022 // to reduce noise in unit test runs.
23}
24
Sean Silvaaba82702012-11-19 23:21:47 +000025// Assumes Ctx is an SMDiagnostic where Diag can be stored.
26static void CollectDiagnosticsOutput(const SMDiagnostic &Diag, void *Ctx) {
27 SMDiagnostic* DiagOut = static_cast<SMDiagnostic*>(Ctx);
28 *DiagOut = Diag;
29}
30
Michael J. Spencer22120c42012-04-03 23:09:22 +000031// Checks that the given input gives a parse error. Makes sure that an error
32// text is available and the parse fails.
33static void ExpectParseError(StringRef Message, StringRef Input) {
34 SourceMgr SM;
35 yaml::Stream Stream(Input, SM);
Nick Kledzik204720b2012-07-31 22:18:15 +000036 SM.setDiagHandler(SuppressDiagnosticsOutput);
Michael J. Spencer22120c42012-04-03 23:09:22 +000037 EXPECT_FALSE(Stream.validate()) << Message << ": " << Input;
38 EXPECT_TRUE(Stream.failed()) << Message << ": " << Input;
39}
40
41// Checks that the given input can be parsed without error.
42static void ExpectParseSuccess(StringRef Message, StringRef Input) {
43 SourceMgr SM;
44 yaml::Stream Stream(Input, SM);
45 EXPECT_TRUE(Stream.validate()) << Message << ": " << Input;
46}
47
48TEST(YAMLParser, ParsesEmptyArray) {
49 ExpectParseSuccess("Empty array", "[]");
50}
51
52TEST(YAMLParser, FailsIfNotClosingArray) {
53 ExpectParseError("Not closing array", "[");
54 ExpectParseError("Not closing array", " [ ");
55 ExpectParseError("Not closing array", " [x");
56}
57
58TEST(YAMLParser, ParsesEmptyArrayWithWhitespace) {
59 ExpectParseSuccess("Array with spaces", " [ ] ");
60 ExpectParseSuccess("All whitespaces", "\t\r\n[\t\n \t\r ]\t\r \n\n");
61}
62
63TEST(YAMLParser, ParsesEmptyObject) {
64 ExpectParseSuccess("Empty object", "[{}]");
65}
66
67TEST(YAMLParser, ParsesObject) {
68 ExpectParseSuccess("Object with an entry", "[{\"a\":\"/b\"}]");
69}
70
71TEST(YAMLParser, ParsesMultipleKeyValuePairsInObject) {
72 ExpectParseSuccess("Multiple key, value pairs",
73 "[{\"a\":\"/b\",\"c\":\"d\",\"e\":\"f\"}]");
74}
75
76TEST(YAMLParser, FailsIfNotClosingObject) {
77 ExpectParseError("Missing close on empty", "[{]");
78 ExpectParseError("Missing close after pair", "[{\"a\":\"b\"]");
79}
80
81TEST(YAMLParser, FailsIfMissingColon) {
82 ExpectParseError("Missing colon between key and value", "[{\"a\"\"/b\"}]");
83 ExpectParseError("Missing colon between key and value", "[{\"a\" \"b\"}]");
84}
85
86TEST(YAMLParser, FailsOnMissingQuote) {
87 ExpectParseError("Missing open quote", "[{a\":\"b\"}]");
88 ExpectParseError("Missing closing quote", "[{\"a\":\"b}]");
89}
90
91TEST(YAMLParser, ParsesEscapedQuotes) {
92 ExpectParseSuccess("Parses escaped string in key and value",
93 "[{\"a\":\"\\\"b\\\" \\\" \\\"\"}]");
94}
95
96TEST(YAMLParser, ParsesEmptyString) {
97 ExpectParseSuccess("Parses empty string in value", "[{\"a\":\"\"}]");
98}
99
100TEST(YAMLParser, ParsesMultipleObjects) {
101 ExpectParseSuccess(
102 "Multiple objects in array",
103 "["
104 " { \"a\" : \"b\" },"
105 " { \"a\" : \"b\" },"
106 " { \"a\" : \"b\" }"
107 "]");
108}
109
110TEST(YAMLParser, FailsOnMissingComma) {
111 ExpectParseError(
112 "Missing comma",
113 "["
114 " { \"a\" : \"b\" }"
115 " { \"a\" : \"b\" }"
116 "]");
117}
118
119TEST(YAMLParser, ParsesSpacesInBetweenTokens) {
120 ExpectParseSuccess(
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");
127}
128
129TEST(YAMLParser, ParsesArrayOfArrays) {
130 ExpectParseSuccess("Array of arrays", "[[]]");
131}
132
133TEST(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", "{\"\"");
142}
143
Alex Lorenz74b63eb2015-05-06 23:21:29 +0000144TEST(YAMLParser, HandlesNullValuesInKeyValueNodesGracefully) {
145 ExpectParseError("KeyValueNode with null value", "test: '");
146}
147
Michael J. Spencer22120c42012-04-03 23:09:22 +0000148// Checks that the given string can be parsed into an identical string inside
149// of an array.
150static void ExpectCanParseString(StringRef String) {
151 std::string StringInArray = (llvm::Twine("[\"") + String + "\"]").str();
152 SourceMgr SM;
153 yaml::Stream Stream(StringInArray, SM);
154 yaml::SequenceNode *ParsedSequence
155 = dyn_cast<yaml::SequenceNode>(Stream.begin()->getRoot());
156 StringRef ParsedString
157 = dyn_cast<yaml::ScalarNode>(
158 static_cast<yaml::Node*>(ParsedSequence->begin()))->getRawValue();
159 ParsedString = ParsedString.substr(1, ParsedString.size() - 2);
160 EXPECT_EQ(String, ParsedString.str());
161}
162
163// Checks that parsing the given string inside an array fails.
164static void ExpectCannotParseString(StringRef String) {
165 std::string StringInArray = (llvm::Twine("[\"") + String + "\"]").str();
166 ExpectParseError((Twine("When parsing string \"") + String + "\"").str(),
167 StringInArray);
168}
169
170TEST(YAMLParser, ParsesStrings) {
171 ExpectCanParseString("");
172 ExpectCannotParseString("\\");
173 ExpectCannotParseString("\"");
174 ExpectCanParseString(" ");
175 ExpectCanParseString("\\ ");
176 ExpectCanParseString("\\\"");
177 ExpectCannotParseString("\"\\");
178 ExpectCannotParseString(" \\");
179 ExpectCanParseString("\\\\");
180 ExpectCannotParseString("\\\\\\");
181 ExpectCanParseString("\\\\\\\\");
182 ExpectCanParseString("\\\" ");
183 ExpectCannotParseString("\\\\\" ");
184 ExpectCanParseString("\\\\\\\" ");
185 ExpectCanParseString(" \\\\ \\\" \\\\\\\" ");
186}
187
188TEST(YAMLParser, WorksWithIteratorAlgorithms) {
189 SourceMgr SM;
190 yaml::Stream Stream("[\"1\", \"2\", \"3\", \"4\", \"5\", \"6\"]", SM);
191 yaml::SequenceNode *Array
192 = dyn_cast<yaml::SequenceNode>(Stream.begin()->getRoot());
193 EXPECT_EQ(6, std::distance(Array->begin(), Array->end()));
194}
195
Sean Silvaaba82702012-11-19 23:21:47 +0000196TEST(YAMLParser, DefaultDiagnosticFilename) {
197 SourceMgr SM;
198
199 SMDiagnostic GeneratedDiag;
200 SM.setDiagHandler(CollectDiagnosticsOutput, &GeneratedDiag);
201
202 // When we construct a YAML stream over an unnamed string,
203 // the filename is hard-coded as "YAML".
204 yaml::Stream UnnamedStream("[]", SM);
205 UnnamedStream.printError(UnnamedStream.begin()->getRoot(), "Hello, World!");
206 EXPECT_EQ("YAML", GeneratedDiag.getFilename());
207}
208
209TEST(YAMLParser, DiagnosticFilenameFromBufferID) {
210 SourceMgr SM;
211
212 SMDiagnostic GeneratedDiag;
213 SM.setDiagHandler(CollectDiagnosticsOutput, &GeneratedDiag);
214
215 // When we construct a YAML stream over a named buffer,
216 // we get its ID as filename in diagnostics.
Rafael Espindola3560ff22014-08-27 20:03:13 +0000217 std::unique_ptr<MemoryBuffer> Buffer =
218 MemoryBuffer::getMemBuffer("[]", "buffername.yaml");
Rafael Espindola68669e32014-08-27 19:03:22 +0000219 yaml::Stream Stream(Buffer->getMemBufferRef(), SM);
Sean Silvaaba82702012-11-19 23:21:47 +0000220 Stream.printError(Stream.begin()->getRoot(), "Hello, World!");
221 EXPECT_EQ("buffername.yaml", GeneratedDiag.getFilename());
222}
223
Michael J. Spencer22120c42012-04-03 23:09:22 +0000224} // end namespace llvm