Manuel Klimek | 9a05fa9 | 2011-04-27 16:39:14 +0000 | [diff] [blame] | 1 | //===- 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 | |
| 13 | namespace clang { |
| 14 | namespace tooling { |
| 15 | |
| 16 | TEST(UnescapeJsonCommandLine, ReturnsEmptyArrayOnEmptyString) { |
| 17 | std::vector<std::string> Result = UnescapeJsonCommandLine(""); |
| 18 | EXPECT_TRUE(Result.empty()); |
| 19 | } |
| 20 | |
| 21 | TEST(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 | |
| 29 | TEST(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 | |
| 36 | TEST(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 | |
| 45 | TEST(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 | |
| 51 | TEST(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 | |
| 59 | TEST(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 | |
| 66 | TEST(UnescapeJsonCommandLine, ParsesEscapedQuotesInQuotedStrings) { |
| 67 | std::vector<std::string> Result = UnescapeJsonCommandLine( |
| 68 | "\\\"\\\\\\\"\\\""); |
| 69 | ASSERT_EQ(1ul, Result.size()); |
| 70 | EXPECT_EQ("\"", Result[0]); |
| 71 | } |
| 72 | |
| 73 | TEST(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 | |
| 83 | TEST(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 | |
| 95 | TEST(JsonCompileCommandLineParser, FailsOnEmptyString) { |
| 96 | JsonCompileCommandLineParser Parser("", NULL); |
| 97 | EXPECT_FALSE(Parser.Parse()) << Parser.GetErrorMessage(); |
| 98 | } |
| 99 | |
| 100 | TEST(JsonCompileCommandLineParser, DoesNotReadAfterInput) { |
| 101 | JsonCompileCommandLineParser Parser(llvm::StringRef(NULL, 0), NULL); |
| 102 | EXPECT_FALSE(Parser.Parse()) << Parser.GetErrorMessage(); |
| 103 | } |
| 104 | |
| 105 | TEST(JsonCompileCommandLineParser, ParsesEmptyArray) { |
| 106 | JsonCompileCommandLineParser Parser("[]", NULL); |
| 107 | EXPECT_TRUE(Parser.Parse()) << Parser.GetErrorMessage(); |
| 108 | } |
| 109 | |
| 110 | TEST(JsonCompileCommandLineParser, FailsIfNotClosingArray) { |
| 111 | JsonCompileCommandLineParser JustOpening("[", NULL); |
| 112 | EXPECT_FALSE(JustOpening.Parse()) << JustOpening.GetErrorMessage(); |
| 113 | JsonCompileCommandLineParser WithSpaces(" [ ", NULL); |
| 114 | EXPECT_FALSE(WithSpaces.Parse()) << WithSpaces.GetErrorMessage(); |
| 115 | JsonCompileCommandLineParser WithGarbage(" [x", NULL); |
| 116 | EXPECT_FALSE(WithGarbage.Parse()) << WithGarbage.GetErrorMessage(); |
| 117 | } |
| 118 | |
| 119 | TEST(JsonCompileCommandLineParser, ParsesEmptyArrayWithWhitespace) { |
| 120 | JsonCompileCommandLineParser Spaces(" [ ] ", NULL); |
| 121 | EXPECT_TRUE(Spaces.Parse()) << Spaces.GetErrorMessage(); |
| 122 | JsonCompileCommandLineParser AllWhites("\t\r\n[\t\n \t\r ]\t\r \n\n", NULL); |
| 123 | EXPECT_TRUE(AllWhites.Parse()) << AllWhites.GetErrorMessage(); |
| 124 | } |
| 125 | |
| 126 | TEST(JsonCompileCommandLineParser, FailsIfNotStartingArray) { |
| 127 | JsonCompileCommandLineParser ObjectStart("{", NULL); |
| 128 | EXPECT_FALSE(ObjectStart.Parse()) << ObjectStart.GetErrorMessage(); |
| 129 | // We don't implement a full JSON parser, and thus parse only a subset |
| 130 | // of valid JSON. |
| 131 | JsonCompileCommandLineParser Object("{}", NULL); |
| 132 | EXPECT_FALSE(Object.Parse()) << Object.GetErrorMessage(); |
| 133 | JsonCompileCommandLineParser Character("x", NULL); |
| 134 | EXPECT_FALSE(Character.Parse()) << Character.GetErrorMessage(); |
| 135 | } |
| 136 | |
| 137 | TEST(JsonCompileCommandLineParser, ParsesEmptyObject) { |
| 138 | JsonCompileCommandLineParser Parser("[{}]", NULL); |
| 139 | EXPECT_TRUE(Parser.Parse()) << Parser.GetErrorMessage(); |
| 140 | } |
| 141 | |
| 142 | TEST(JsonCompileCommandLineParser, ParsesObject) { |
| 143 | JsonCompileCommandLineParser Parser("[{\"a\":\"/b\"}]", NULL); |
| 144 | EXPECT_TRUE(Parser.Parse()) << Parser.GetErrorMessage(); |
| 145 | } |
| 146 | |
| 147 | TEST(JsonCompileCommandLineParser, ParsesMultipleKeyValuePairsInObject) { |
| 148 | JsonCompileCommandLineParser Parser( |
| 149 | "[{\"a\":\"/b\",\"c\":\"d\",\"e\":\"f\"}]", NULL); |
| 150 | EXPECT_TRUE(Parser.Parse()) << Parser.GetErrorMessage(); |
| 151 | } |
| 152 | |
| 153 | TEST(JsonCompileCommandLineParser, FailsIfNotClosingObject) { |
| 154 | JsonCompileCommandLineParser MissingCloseOnEmpty("[{]", NULL); |
| 155 | EXPECT_FALSE(MissingCloseOnEmpty.Parse()) |
| 156 | << MissingCloseOnEmpty.GetErrorMessage(); |
| 157 | JsonCompileCommandLineParser MissingCloseAfterPair("[{\"a\":\"b\"]", NULL); |
| 158 | EXPECT_FALSE(MissingCloseAfterPair.Parse()) |
| 159 | << MissingCloseAfterPair.GetErrorMessage(); |
| 160 | } |
| 161 | |
| 162 | TEST(JsonCompileCommandLineParser, FailsIfMissingColon) { |
| 163 | JsonCompileCommandLineParser StringString("[{\"a\"\"/b\"}]", NULL); |
| 164 | EXPECT_FALSE(StringString.Parse()) << StringString.GetErrorMessage(); |
| 165 | JsonCompileCommandLineParser StringSpaceString("[{\"a\" \"b\"}]", NULL); |
| 166 | EXPECT_FALSE(StringSpaceString.Parse()) |
| 167 | << StringSpaceString.GetErrorMessage(); |
| 168 | } |
| 169 | |
| 170 | TEST(JsonCompileCommandLineParser, FailsOnMissingQuote) { |
| 171 | JsonCompileCommandLineParser OpenQuote("[{a\":\"b\"}]", NULL); |
| 172 | EXPECT_FALSE(OpenQuote.Parse()) << OpenQuote.GetErrorMessage(); |
| 173 | JsonCompileCommandLineParser CloseQuote("[{\"a\":\"b}]", NULL); |
| 174 | EXPECT_FALSE(CloseQuote.Parse()) << CloseQuote.GetErrorMessage(); |
| 175 | } |
| 176 | |
| 177 | TEST(JsonCompileCommandLineParser, ParsesEscapedQuotes) { |
| 178 | JsonCompileCommandLineParser Parser( |
| 179 | "[{\"a\":\"\\\"b\\\" \\\" \\\"\"}]", NULL); |
| 180 | EXPECT_TRUE(Parser.Parse()) << Parser.GetErrorMessage(); |
| 181 | } |
| 182 | |
| 183 | TEST(JsonCompileCommandLineParser, ParsesEmptyString) { |
| 184 | JsonCompileCommandLineParser Parser("[{\"a\":\"\"}]", NULL); |
| 185 | EXPECT_TRUE(Parser.Parse()) << Parser.GetErrorMessage(); |
| 186 | } |
| 187 | |
| 188 | TEST(JsonCompileCommandLineParser, FailsOnMissingString) { |
| 189 | JsonCompileCommandLineParser MissingValue("[{\"a\":}]", NULL); |
| 190 | EXPECT_FALSE(MissingValue.Parse()) << MissingValue.GetErrorMessage(); |
| 191 | JsonCompileCommandLineParser MissingKey("[{:\"b\"}]", NULL); |
| 192 | EXPECT_FALSE(MissingKey.Parse()) << MissingKey.GetErrorMessage(); |
| 193 | } |
| 194 | |
| 195 | TEST(JsonCompileCommandLineParser, ParsesMultipleObjects) { |
| 196 | JsonCompileCommandLineParser Parser( |
| 197 | "[" |
| 198 | " { \"a\" : \"b\" }," |
| 199 | " { \"a\" : \"b\" }," |
| 200 | " { \"a\" : \"b\" }" |
| 201 | "]", NULL); |
| 202 | EXPECT_TRUE(Parser.Parse()) << Parser.GetErrorMessage(); |
| 203 | } |
| 204 | |
| 205 | TEST(JsonCompileCommandLineParser, FailsOnMissingComma) { |
| 206 | JsonCompileCommandLineParser Parser( |
| 207 | "[" |
| 208 | " { \"a\" : \"b\" }" |
| 209 | " { \"a\" : \"b\" }" |
| 210 | "]", NULL); |
| 211 | EXPECT_FALSE(Parser.Parse()) << Parser.GetErrorMessage(); |
| 212 | } |
| 213 | |
| 214 | TEST(JsonCompileCommandLineParser, FailsOnSuperfluousComma) { |
| 215 | JsonCompileCommandLineParser Parser( |
| 216 | "[ { \"a\" : \"b\" }, ]", NULL); |
| 217 | EXPECT_FALSE(Parser.Parse()) << Parser.GetErrorMessage(); |
| 218 | } |
| 219 | |
| 220 | TEST(JsonCompileCommandLineParser, ParsesSpacesInBetweenTokens) { |
| 221 | JsonCompileCommandLineParser Parser( |
| 222 | " \t \n\n \r [ \t \n\n \r" |
| 223 | " \t \n\n \r { \t \n\n \r\"a\"\t \n\n \r :" |
| 224 | " \t \n\n \r \"b\"\t \n\n \r } \t \n\n \r,\t \n\n \r" |
| 225 | " \t \n\n \r { \t \n\n \r\"a\"\t \n\n \r :" |
| 226 | " \t \n\n \r \"b\"\t \n\n \r } \t \n\n \r]\t \n\n \r", |
| 227 | NULL); |
| 228 | EXPECT_TRUE(Parser.Parse()) << Parser.GetErrorMessage(); |
| 229 | } |
| 230 | |
| 231 | } // end namespace tooling |
| 232 | } // end namespace clang |