Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(174)

Side by Side Diff: unittests/Support/YAMLParserTest.cpp

Issue 5604054: YAML Parser
Patch Set: Update Created 12 years, 10 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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/SourceMgr.h"
13 #include "llvm/Support/YAMLParser.h"
14 #include "gtest/gtest.h"
15
16 namespace llvm {
17
18 // Checks that the given input gives a parse error. Makes sure that an error
19 // text is available and the parse fails.
20 static void ExpectParseError(StringRef Message, StringRef Input) {
21 SourceMgr SM;
22 yaml::Stream Stream(Input, SM);
23 EXPECT_FALSE(Stream.validate()) << Message << ": " << Input;
24 EXPECT_TRUE(Stream.failed()) << Message << ": " << Input;
25 }
26
27 // Checks that the given input can be parsed without error.
28 static void ExpectParseSuccess(StringRef Message, StringRef Input) {
29 SourceMgr SM;
30 yaml::Stream Stream(Input, SM);
31 EXPECT_TRUE(Stream.validate()) << Message << ": " << Input;
32 }
33
34 TEST(YAMLParser, ParsesEmptyArray) {
35 ExpectParseSuccess("Empty array", "[]");
36 }
37
38 TEST(YAMLParser, FailsIfNotClosingArray) {
39 ExpectParseError("Not closing array", "[");
40 ExpectParseError("Not closing array", " [ ");
41 ExpectParseError("Not closing array", " [x");
42 }
43
44 TEST(YAMLParser, ParsesEmptyArrayWithWhitespace) {
45 ExpectParseSuccess("Array with spaces", " [ ] ");
46 ExpectParseSuccess("All whitespaces", "\t\r\n[\t\n \t\r ]\t\r \n\n");
47 }
48
49 TEST(YAMLParser, ParsesEmptyObject) {
50 ExpectParseSuccess("Empty object", "[{}]");
51 }
52
53 TEST(YAMLParser, ParsesObject) {
54 ExpectParseSuccess("Object with an entry", "[{\"a\":\"/b\"}]");
55 }
56
57 TEST(YAMLParser, ParsesMultipleKeyValuePairsInObject) {
58 ExpectParseSuccess("Multiple key, value pairs",
59 "[{\"a\":\"/b\",\"c\":\"d\",\"e\":\"f\"}]");
60 }
61
62 TEST(YAMLParser, FailsIfNotClosingObject) {
63 ExpectParseError("Missing close on empty", "[{]");
64 ExpectParseError("Missing close after pair", "[{\"a\":\"b\"]");
65 }
66
67 TEST(YAMLParser, FailsIfMissingColon) {
68 ExpectParseError("Missing colon between key and value", "[{\"a\"\"/b\"}]");
69 ExpectParseError("Missing colon between key and value", "[{\"a\" \"b\"}]");
70 }
71
72 TEST(YAMLParser, FailsOnMissingQuote) {
73 ExpectParseError("Missing open quote", "[{a\":\"b\"}]");
74 ExpectParseError("Missing closing quote", "[{\"a\":\"b}]");
75 }
76
77 TEST(YAMLParser, ParsesEscapedQuotes) {
78 ExpectParseSuccess("Parses escaped string in key and value",
79 "[{\"a\":\"\\\"b\\\" \\\" \\\"\"}]");
80 }
81
82 TEST(YAMLParser, ParsesEmptyString) {
83 ExpectParseSuccess("Parses empty string in value", "[{\"a\":\"\"}]");
84 }
85
86 TEST(YAMLParser, ParsesMultipleObjects) {
87 ExpectParseSuccess(
88 "Multiple objects in array",
89 "["
90 " { \"a\" : \"b\" },"
91 " { \"a\" : \"b\" },"
92 " { \"a\" : \"b\" }"
93 "]");
94 }
95
96 TEST(YAMLParser, FailsOnMissingComma) {
97 ExpectParseError(
98 "Missing comma",
99 "["
100 " { \"a\" : \"b\" }"
101 " { \"a\" : \"b\" }"
102 "]");
103 }
104
105 TEST(YAMLParser, ParsesSpacesInBetweenTokens) {
106 ExpectParseSuccess(
107 "Various whitespace between tokens",
108 " \t \n\n \r [ \t \n\n \r"
109 " \t \n\n \r { \t \n\n \r\"a\"\t \n\n \r :"
110 " \t \n\n \r \"b\"\t \n\n \r } \t \n\n \r,\t \n\n \r"
111 " \t \n\n \r { \t \n\n \r\"a\"\t \n\n \r :"
112 " \t \n\n \r \"b\"\t \n\n \r } \t \n\n \r]\t \n\n \r");
113 }
114
115 TEST(YAMLParser, ParsesArrayOfArrays) {
116 ExpectParseSuccess("Array of arrays", "[[]]");
117 }
118
119 TEST(YAMLParser, HandlesEndOfFileGracefully) {
120 ExpectParseError("In string starting with EOF", "[\"");
121 ExpectParseError("In string hitting EOF", "[\" ");
122 ExpectParseError("In string escaping EOF", "[\" \\");
123 ExpectParseError("In array starting with EOF", "[");
124 ExpectParseError("In array element starting with EOF", "[[], ");
125 ExpectParseError("In array hitting EOF", "[[] ");
126 ExpectParseError("In array hitting EOF", "[[]");
127 ExpectParseError("In object hitting EOF", "{\"\"");
128 }
129
130 // Checks that the given string can be parsed into an identical string inside
131 // of an array.
132 static void ExpectCanParseString(StringRef String) {
133 std::string StringInArray = (llvm::Twine("[\"") + String + "\"]").str();
134 SourceMgr SM;
135 yaml::Stream Stream(StringInArray, SM);
136 yaml::SequenceNode *ParsedSequence
137 = dyn_cast<yaml::SequenceNode>(Stream.begin()->getRoot());
138 StringRef ParsedString
139 = dyn_cast<yaml::ScalarNode>(
140 static_cast<yaml::Node*>(ParsedSequence->begin()))->getRawValue();
141 ParsedString = ParsedString.substr(1, ParsedString.size() - 2);
142 EXPECT_EQ(String, ParsedString.str());
143 }
144
145 // Checks that parsing the given string inside an array fails.
146 static void ExpectCannotParseString(StringRef String) {
147 std::string StringInArray = (llvm::Twine("[\"") + String + "\"]").str();
148 ExpectParseError((Twine("When parsing string \"") + String + "\"").str(),
149 StringInArray);
150 }
151
152 TEST(YAMLParser, ParsesStrings) {
153 ExpectCanParseString("");
154 ExpectCannotParseString("\\");
155 ExpectCannotParseString("\"");
156 ExpectCanParseString(" ");
157 ExpectCanParseString("\\ ");
158 ExpectCanParseString("\\\"");
159 ExpectCannotParseString("\"\\");
160 ExpectCannotParseString(" \\");
161 ExpectCanParseString("\\\\");
162 ExpectCannotParseString("\\\\\\");
163 ExpectCanParseString("\\\\\\\\");
164 ExpectCanParseString("\\\" ");
165 ExpectCannotParseString("\\\\\" ");
166 ExpectCanParseString("\\\\\\\" ");
167 ExpectCanParseString(" \\\\ \\\" \\\\\\\" ");
168 }
169
170 TEST(YAMLParser, WorksWithIteratorAlgorithms) {
171 SourceMgr SM;
172 yaml::Stream Stream("[\"1\", \"2\", \"3\", \"4\", \"5\", \"6\"]", SM);
173 yaml::SequenceNode *Array
174 = dyn_cast<yaml::SequenceNode>(Stream.begin()->getRoot());
175 EXPECT_EQ(6, std::distance(Array->begin(), Array->end()));
176 }
177
178 } // end namespace llvm
OLDNEW

Powered by Google App Engine
RSS Feeds Recent Issues | This issue
This is Rietveld f62528b