blob: 2a911d10635b2bd96ebbe5b0ecd632f9b476bcb7 [file] [log] [blame]
Manuel Klimek9a05fa92011-04-27 16:39:14 +00001//===- unittest/Tooling/JsonCompileCommandLineDatabaseTest ----------------===//
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 "../../lib/Tooling/JsonCompileCommandLineDatabase.h"
11#include "gtest/gtest.h"
12
13namespace clang {
14namespace tooling {
15
16TEST(UnescapeJsonCommandLine, ReturnsEmptyArrayOnEmptyString) {
17 std::vector<std::string> Result = UnescapeJsonCommandLine("");
18 EXPECT_TRUE(Result.empty());
19}
20
21TEST(UnescapeJsonCommandLine, SplitsOnSpaces) {
22 std::vector<std::string> Result = UnescapeJsonCommandLine("a b c");
23 ASSERT_EQ(3ul, Result.size());
24 EXPECT_EQ("a", Result[0]);
25 EXPECT_EQ("b", Result[1]);
26 EXPECT_EQ("c", Result[2]);
27}
28
29TEST(UnescapeJsonCommandLine, MungesMultipleSpaces) {
30 std::vector<std::string> Result = UnescapeJsonCommandLine(" a b ");
31 ASSERT_EQ(2ul, Result.size());
32 EXPECT_EQ("a", Result[0]);
33 EXPECT_EQ("b", Result[1]);
34}
35
36TEST(UnescapeJsonCommandLine, UnescapesBackslashCharacters) {
37 std::vector<std::string> Backslash = UnescapeJsonCommandLine("a\\\\\\\\");
38 ASSERT_EQ(1ul, Backslash.size());
39 EXPECT_EQ("a\\", Backslash[0]);
40 std::vector<std::string> Quote = UnescapeJsonCommandLine("a\\\\\\\"");
41 ASSERT_EQ(1ul, Quote.size());
42 EXPECT_EQ("a\"", Quote[0]);
43}
44
45TEST(UnescapeJsonCommandLine, DoesNotMungeSpacesBetweenQuotes) {
46 std::vector<std::string> Result = UnescapeJsonCommandLine("\\\" a b \\\"");
47 ASSERT_EQ(1ul, Result.size());
48 EXPECT_EQ(" a b ", Result[0]);
49}
50
51TEST(UnescapeJsonCommandLine, AllowsMultipleQuotedArguments) {
52 std::vector<std::string> Result = UnescapeJsonCommandLine(
53 " \\\" a \\\" \\\" b \\\" ");
54 ASSERT_EQ(2ul, Result.size());
55 EXPECT_EQ(" a ", Result[0]);
56 EXPECT_EQ(" b ", Result[1]);
57}
58
59TEST(UnescapeJsonCommandLine, AllowsEmptyArgumentsInQuotes) {
60 std::vector<std::string> Result = UnescapeJsonCommandLine(
61 "\\\"\\\"\\\"\\\"");
62 ASSERT_EQ(1ul, Result.size());
63 EXPECT_TRUE(Result[0].empty()) << Result[0];
64}
65
66TEST(UnescapeJsonCommandLine, ParsesEscapedQuotesInQuotedStrings) {
67 std::vector<std::string> Result = UnescapeJsonCommandLine(
68 "\\\"\\\\\\\"\\\"");
69 ASSERT_EQ(1ul, Result.size());
70 EXPECT_EQ("\"", Result[0]);
71}
72
73TEST(UnescapeJsonCommandLine, ParsesMultipleArgumentsWithEscapedCharacters) {
74 std::vector<std::string> Result = UnescapeJsonCommandLine(
75 " \\\\\\\" \\\"a \\\\\\\" b \\\" \\\"and\\\\\\\\c\\\" \\\\\\\"");
76 ASSERT_EQ(4ul, Result.size());
77 EXPECT_EQ("\"", Result[0]);
78 EXPECT_EQ("a \" b ", Result[1]);
79 EXPECT_EQ("and\\c", Result[2]);
80 EXPECT_EQ("\"", Result[3]);
81}
82
83TEST(UnescapeJsonCommandLine, ParsesStringsWithoutSpacesIntoSingleArgument) {
84 std::vector<std::string> QuotedNoSpaces = UnescapeJsonCommandLine(
85 "\\\"a\\\"\\\"b\\\"");
86 ASSERT_EQ(1ul, QuotedNoSpaces.size());
87 EXPECT_EQ("ab", QuotedNoSpaces[0]);
88
89 std::vector<std::string> MixedNoSpaces = UnescapeJsonCommandLine(
90 "\\\"a\\\"bcd\\\"ef\\\"\\\"\\\"\\\"g\\\"");
91 ASSERT_EQ(1ul, MixedNoSpaces.size());
92 EXPECT_EQ("abcdefg", MixedNoSpaces[0]);
93}
94
Manuel Klimek850dd802011-05-02 18:27:26 +000095TEST(UnescapeJsonCommandLine, ParsesQuotedStringWithoutClosingQuote) {
96 std::vector<std::string> Unclosed = UnescapeJsonCommandLine("\"abc");
97 ASSERT_EQ(1ul, Unclosed.size());
98 EXPECT_EQ("abc", Unclosed[0]);
99
100 std::vector<std::string> EndsInBackslash = UnescapeJsonCommandLine("\"a\\");
101 ASSERT_EQ(1ul, EndsInBackslash.size());
102 EXPECT_EQ("a", EndsInBackslash[0]);
103
104 std::vector<std::string> Empty = UnescapeJsonCommandLine("\"");
105 ASSERT_EQ(1ul, Empty.size());
106 EXPECT_EQ("", Empty[0]);
107}
108
Manuel Klimek9a05fa92011-04-27 16:39:14 +0000109TEST(JsonCompileCommandLineParser, FailsOnEmptyString) {
110 JsonCompileCommandLineParser Parser("", NULL);
111 EXPECT_FALSE(Parser.Parse()) << Parser.GetErrorMessage();
112}
113
114TEST(JsonCompileCommandLineParser, DoesNotReadAfterInput) {
115 JsonCompileCommandLineParser Parser(llvm::StringRef(NULL, 0), NULL);
116 EXPECT_FALSE(Parser.Parse()) << Parser.GetErrorMessage();
117}
118
119TEST(JsonCompileCommandLineParser, ParsesEmptyArray) {
120 JsonCompileCommandLineParser Parser("[]", NULL);
121 EXPECT_TRUE(Parser.Parse()) << Parser.GetErrorMessage();
122}
123
124TEST(JsonCompileCommandLineParser, FailsIfNotClosingArray) {
125 JsonCompileCommandLineParser JustOpening("[", NULL);
126 EXPECT_FALSE(JustOpening.Parse()) << JustOpening.GetErrorMessage();
127 JsonCompileCommandLineParser WithSpaces(" [ ", NULL);
128 EXPECT_FALSE(WithSpaces.Parse()) << WithSpaces.GetErrorMessage();
129 JsonCompileCommandLineParser WithGarbage(" [x", NULL);
130 EXPECT_FALSE(WithGarbage.Parse()) << WithGarbage.GetErrorMessage();
131}
132
133TEST(JsonCompileCommandLineParser, ParsesEmptyArrayWithWhitespace) {
134 JsonCompileCommandLineParser Spaces(" [ ] ", NULL);
135 EXPECT_TRUE(Spaces.Parse()) << Spaces.GetErrorMessage();
136 JsonCompileCommandLineParser AllWhites("\t\r\n[\t\n \t\r ]\t\r \n\n", NULL);
137 EXPECT_TRUE(AllWhites.Parse()) << AllWhites.GetErrorMessage();
138}
139
140TEST(JsonCompileCommandLineParser, FailsIfNotStartingArray) {
141 JsonCompileCommandLineParser ObjectStart("{", NULL);
142 EXPECT_FALSE(ObjectStart.Parse()) << ObjectStart.GetErrorMessage();
143 // We don't implement a full JSON parser, and thus parse only a subset
144 // of valid JSON.
145 JsonCompileCommandLineParser Object("{}", NULL);
146 EXPECT_FALSE(Object.Parse()) << Object.GetErrorMessage();
147 JsonCompileCommandLineParser Character("x", NULL);
148 EXPECT_FALSE(Character.Parse()) << Character.GetErrorMessage();
149}
150
151TEST(JsonCompileCommandLineParser, ParsesEmptyObject) {
152 JsonCompileCommandLineParser Parser("[{}]", NULL);
153 EXPECT_TRUE(Parser.Parse()) << Parser.GetErrorMessage();
154}
155
156TEST(JsonCompileCommandLineParser, ParsesObject) {
157 JsonCompileCommandLineParser Parser("[{\"a\":\"/b\"}]", NULL);
158 EXPECT_TRUE(Parser.Parse()) << Parser.GetErrorMessage();
159}
160
161TEST(JsonCompileCommandLineParser, ParsesMultipleKeyValuePairsInObject) {
162 JsonCompileCommandLineParser Parser(
163 "[{\"a\":\"/b\",\"c\":\"d\",\"e\":\"f\"}]", NULL);
164 EXPECT_TRUE(Parser.Parse()) << Parser.GetErrorMessage();
165}
166
167TEST(JsonCompileCommandLineParser, FailsIfNotClosingObject) {
168 JsonCompileCommandLineParser MissingCloseOnEmpty("[{]", NULL);
169 EXPECT_FALSE(MissingCloseOnEmpty.Parse())
170 << MissingCloseOnEmpty.GetErrorMessage();
171 JsonCompileCommandLineParser MissingCloseAfterPair("[{\"a\":\"b\"]", NULL);
172 EXPECT_FALSE(MissingCloseAfterPair.Parse())
173 << MissingCloseAfterPair.GetErrorMessage();
174}
175
176TEST(JsonCompileCommandLineParser, FailsIfMissingColon) {
177 JsonCompileCommandLineParser StringString("[{\"a\"\"/b\"}]", NULL);
178 EXPECT_FALSE(StringString.Parse()) << StringString.GetErrorMessage();
179 JsonCompileCommandLineParser StringSpaceString("[{\"a\" \"b\"}]", NULL);
180 EXPECT_FALSE(StringSpaceString.Parse())
181 << StringSpaceString.GetErrorMessage();
182}
183
184TEST(JsonCompileCommandLineParser, FailsOnMissingQuote) {
185 JsonCompileCommandLineParser OpenQuote("[{a\":\"b\"}]", NULL);
186 EXPECT_FALSE(OpenQuote.Parse()) << OpenQuote.GetErrorMessage();
187 JsonCompileCommandLineParser CloseQuote("[{\"a\":\"b}]", NULL);
188 EXPECT_FALSE(CloseQuote.Parse()) << CloseQuote.GetErrorMessage();
189}
190
191TEST(JsonCompileCommandLineParser, ParsesEscapedQuotes) {
192 JsonCompileCommandLineParser Parser(
193 "[{\"a\":\"\\\"b\\\" \\\" \\\"\"}]", NULL);
194 EXPECT_TRUE(Parser.Parse()) << Parser.GetErrorMessage();
195}
196
197TEST(JsonCompileCommandLineParser, ParsesEmptyString) {
198 JsonCompileCommandLineParser Parser("[{\"a\":\"\"}]", NULL);
199 EXPECT_TRUE(Parser.Parse()) << Parser.GetErrorMessage();
200}
201
202TEST(JsonCompileCommandLineParser, FailsOnMissingString) {
203 JsonCompileCommandLineParser MissingValue("[{\"a\":}]", NULL);
204 EXPECT_FALSE(MissingValue.Parse()) << MissingValue.GetErrorMessage();
205 JsonCompileCommandLineParser MissingKey("[{:\"b\"}]", NULL);
206 EXPECT_FALSE(MissingKey.Parse()) << MissingKey.GetErrorMessage();
207}
208
209TEST(JsonCompileCommandLineParser, ParsesMultipleObjects) {
210 JsonCompileCommandLineParser Parser(
211 "["
212 " { \"a\" : \"b\" },"
213 " { \"a\" : \"b\" },"
214 " { \"a\" : \"b\" }"
215 "]", NULL);
216 EXPECT_TRUE(Parser.Parse()) << Parser.GetErrorMessage();
217}
218
219TEST(JsonCompileCommandLineParser, FailsOnMissingComma) {
220 JsonCompileCommandLineParser Parser(
221 "["
222 " { \"a\" : \"b\" }"
223 " { \"a\" : \"b\" }"
224 "]", NULL);
225 EXPECT_FALSE(Parser.Parse()) << Parser.GetErrorMessage();
226}
227
228TEST(JsonCompileCommandLineParser, FailsOnSuperfluousComma) {
229 JsonCompileCommandLineParser Parser(
230 "[ { \"a\" : \"b\" }, ]", NULL);
231 EXPECT_FALSE(Parser.Parse()) << Parser.GetErrorMessage();
232}
233
234TEST(JsonCompileCommandLineParser, ParsesSpacesInBetweenTokens) {
235 JsonCompileCommandLineParser Parser(
236 " \t \n\n \r [ \t \n\n \r"
237 " \t \n\n \r { \t \n\n \r\"a\"\t \n\n \r :"
238 " \t \n\n \r \"b\"\t \n\n \r } \t \n\n \r,\t \n\n \r"
239 " \t \n\n \r { \t \n\n \r\"a\"\t \n\n \r :"
240 " \t \n\n \r \"b\"\t \n\n \r } \t \n\n \r]\t \n\n \r",
241 NULL);
242 EXPECT_TRUE(Parser.Parse()) << Parser.GetErrorMessage();
243}
244
245} // end namespace tooling
246} // end namespace clang