YAML: Implement block scalar parsing.
[oota-llvm.git] / unittests / Support / YAMLParserTest.cpp
1 //===- 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"
13 #include "llvm/Support/MemoryBuffer.h"
14 #include "llvm/Support/SourceMgr.h"
15 #include "llvm/Support/YAMLParser.h"
16 #include "gtest/gtest.h"
17
18 namespace llvm {
19
20 static void SuppressDiagnosticsOutput(const SMDiagnostic &, void *) {
21   // Prevent SourceMgr from writing errors to stderr
22   // to reduce noise in unit test runs.
23 }
24
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);
28   *DiagOut = Diag;
29 }
30
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) {
34   SourceMgr SM;
35   yaml::Stream Stream(Input, SM);
36   SM.setDiagHandler(SuppressDiagnosticsOutput);
37   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.
42 static void ExpectParseSuccess(StringRef Message, StringRef Input) {
43   SourceMgr SM;
44   yaml::Stream Stream(Input, SM);
45   EXPECT_TRUE(Stream.validate()) << Message << ": " << Input;
46 }
47
48 TEST(YAMLParser, ParsesEmptyArray) {
49   ExpectParseSuccess("Empty array", "[]");
50 }
51
52 TEST(YAMLParser, FailsIfNotClosingArray) {
53   ExpectParseError("Not closing array", "[");
54   ExpectParseError("Not closing array", "  [  ");
55   ExpectParseError("Not closing array", "  [x");
56 }
57
58 TEST(YAMLParser, ParsesEmptyArrayWithWhitespace) {
59   ExpectParseSuccess("Array with spaces", "  [  ]  ");
60   ExpectParseSuccess("All whitespaces", "\t\r\n[\t\n \t\r ]\t\r \n\n");
61 }
62
63 TEST(YAMLParser, ParsesEmptyObject) {
64   ExpectParseSuccess("Empty object", "[{}]");
65 }
66
67 TEST(YAMLParser, ParsesObject) {
68   ExpectParseSuccess("Object with an entry", "[{\"a\":\"/b\"}]");
69 }
70
71 TEST(YAMLParser, ParsesMultipleKeyValuePairsInObject) {
72   ExpectParseSuccess("Multiple key, value pairs",
73                      "[{\"a\":\"/b\",\"c\":\"d\",\"e\":\"f\"}]");
74 }
75
76 TEST(YAMLParser, FailsIfNotClosingObject) {
77   ExpectParseError("Missing close on empty", "[{]");
78   ExpectParseError("Missing close after pair", "[{\"a\":\"b\"]");
79 }
80
81 TEST(YAMLParser, FailsIfMissingColon) {
82   ExpectParseError("Missing colon between key and value", "[{\"a\"\"/b\"}]");
83   ExpectParseError("Missing colon between key and value", "[{\"a\" \"b\"}]");
84 }
85
86 TEST(YAMLParser, FailsOnMissingQuote) {
87   ExpectParseError("Missing open quote", "[{a\":\"b\"}]");
88   ExpectParseError("Missing closing quote", "[{\"a\":\"b}]");
89 }
90
91 TEST(YAMLParser, ParsesEscapedQuotes) {
92   ExpectParseSuccess("Parses escaped string in key and value",
93                      "[{\"a\":\"\\\"b\\\"  \\\" \\\"\"}]");
94 }
95
96 TEST(YAMLParser, ParsesEmptyString) {
97   ExpectParseSuccess("Parses empty string in value", "[{\"a\":\"\"}]");
98 }
99
100 TEST(YAMLParser, ParsesMultipleObjects) {
101   ExpectParseSuccess(
102       "Multiple objects in array",
103       "["
104       " { \"a\" : \"b\" },"
105       " { \"a\" : \"b\" },"
106       " { \"a\" : \"b\" }"
107       "]");
108 }
109
110 TEST(YAMLParser, FailsOnMissingComma) {
111   ExpectParseError(
112       "Missing comma",
113       "["
114       " { \"a\" : \"b\" }"
115       " { \"a\" : \"b\" }"
116       "]");
117 }
118
119 TEST(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
129 TEST(YAMLParser, ParsesArrayOfArrays) {
130   ExpectParseSuccess("Array of arrays", "[[]]");
131 }
132
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");
158 }
159
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", "{\"\"");
169 }
170
171 TEST(YAMLParser, HandlesNullValuesInKeyValueNodesGracefully) {
172   ExpectParseError("KeyValueNode with null value", "test: '");
173 }
174
175 // Checks that the given string can be parsed into an identical string inside
176 // of an array.
177 static void ExpectCanParseString(StringRef String) {
178   std::string StringInArray = (llvm::Twine("[\"") + String + "\"]").str();
179   SourceMgr SM;
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());
188 }
189
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(),
194                    StringInArray);
195 }
196
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("    \\\\  \\\"  \\\\\\\"   ");
213 }
214
215 TEST(YAMLParser, WorksWithIteratorAlgorithms) {
216   SourceMgr SM;
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()));
221 }
222
223 TEST(YAMLParser, DefaultDiagnosticFilename) {
224   SourceMgr SM;
225
226   SMDiagnostic GeneratedDiag;
227   SM.setDiagHandler(CollectDiagnosticsOutput, &GeneratedDiag);
228
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());
234 }
235
236 TEST(YAMLParser, DiagnosticFilenameFromBufferID) {
237   SourceMgr SM;
238
239   SMDiagnostic GeneratedDiag;
240   SM.setDiagHandler(CollectDiagnosticsOutput, &GeneratedDiag);
241
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());
249 }
250
251 } // end namespace llvm