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