Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1 | //===- unittest/Format/FormatTest.cpp - Formatting unit tests -------------===// |
| 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 | |
Chandler Carruth | 1050e8b | 2012-12-04 09:45:34 +0000 | [diff] [blame] | 10 | #include "clang/Format/Format.h" |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 11 | #include "../Tooling/RewriterTestContext.h" |
| 12 | #include "clang/Lex/Lexer.h" |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 13 | #include "gtest/gtest.h" |
| 14 | |
| 15 | namespace clang { |
| 16 | namespace format { |
| 17 | |
| 18 | class FormatTest : public ::testing::Test { |
| 19 | protected: |
| 20 | std::string format(llvm::StringRef Code, unsigned Offset, unsigned Length, |
| 21 | const FormatStyle &Style) { |
| 22 | RewriterTestContext Context; |
| 23 | FileID ID = Context.createInMemoryFile("input.cc", Code); |
| 24 | SourceLocation Start = |
| 25 | Context.Sources.getLocForStartOfFile(ID).getLocWithOffset(Offset); |
| 26 | std::vector<CharSourceRange> Ranges( |
| 27 | 1, |
| 28 | CharSourceRange::getCharRange(Start, Start.getLocWithOffset(Length))); |
| 29 | LangOptions LangOpts; |
| 30 | LangOpts.CPlusPlus = 1; |
Daniel Jasper | ba3d307 | 2013-01-02 17:21:36 +0000 | [diff] [blame] | 31 | LangOpts.CPlusPlus11 = 1; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 32 | Lexer Lex(ID, Context.Sources.getBuffer(ID), Context.Sources, LangOpts); |
Daniel Jasper | cd16238 | 2013-01-07 13:26:07 +0000 | [diff] [blame] | 33 | tooling::Replacements Replace = reformat(Style, Lex, Context.Sources, |
| 34 | Ranges); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 35 | EXPECT_TRUE(applyAllReplacements(Replace, Context.Rewrite)); |
| 36 | return Context.getRewrittenText(ID); |
| 37 | } |
| 38 | |
| 39 | std::string format(llvm::StringRef Code, |
| 40 | const FormatStyle &Style = getLLVMStyle()) { |
| 41 | return format(Code, 0, Code.size(), Style); |
| 42 | } |
| 43 | |
Daniel Jasper | e35ec2b | 2012-12-18 19:56:56 +0000 | [diff] [blame] | 44 | std::string messUp(llvm::StringRef Code) { |
| 45 | std::string MessedUp(Code.str()); |
| 46 | bool InComment = false; |
| 47 | bool JustReplacedNewline = false; |
| 48 | for (unsigned i = 0, e = MessedUp.size() - 1; i != e; ++i) { |
| 49 | if (MessedUp[i] == '/' && MessedUp[i + 1] == '/') { |
| 50 | if (JustReplacedNewline) |
| 51 | MessedUp[i - 1] = '\n'; |
| 52 | InComment = true; |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 53 | } else if (MessedUp[i] == '\\' && MessedUp[i + 1] == '\n') { |
| 54 | MessedUp[i] = ' '; |
Daniel Jasper | e35ec2b | 2012-12-18 19:56:56 +0000 | [diff] [blame] | 55 | } else if (MessedUp[i] == '\n') { |
| 56 | if (InComment) { |
| 57 | InComment = false; |
| 58 | } else { |
| 59 | JustReplacedNewline = true; |
| 60 | MessedUp[i] = ' '; |
| 61 | } |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 62 | } else if (MessedUp[i] != ' ') { |
| 63 | JustReplacedNewline = false; |
Daniel Jasper | e35ec2b | 2012-12-18 19:56:56 +0000 | [diff] [blame] | 64 | } |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 65 | } |
Daniel Jasper | e35ec2b | 2012-12-18 19:56:56 +0000 | [diff] [blame] | 66 | return MessedUp; |
| 67 | } |
| 68 | |
Manuel Klimek | 060143e | 2013-01-02 18:33:23 +0000 | [diff] [blame] | 69 | FormatStyle getLLVMStyleWithColumns(unsigned ColumnLimit) { |
| 70 | FormatStyle Style = getLLVMStyle(); |
| 71 | Style.ColumnLimit = ColumnLimit; |
| 72 | return Style; |
| 73 | } |
| 74 | |
| 75 | void verifyFormat(llvm::StringRef Code, |
| 76 | const FormatStyle &Style = getLLVMStyle()) { |
| 77 | EXPECT_EQ(Code.str(), format(messUp(Code), Style)); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | void verifyGoogleFormat(llvm::StringRef Code) { |
Manuel Klimek | 060143e | 2013-01-02 18:33:23 +0000 | [diff] [blame] | 81 | verifyFormat(Code, getGoogleStyle()); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 82 | } |
| 83 | }; |
| 84 | |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 85 | //===----------------------------------------------------------------------===// |
| 86 | // Basic function tests. |
| 87 | //===----------------------------------------------------------------------===// |
| 88 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 89 | TEST_F(FormatTest, DoesNotChangeCorrectlyFormatedCode) { |
| 90 | EXPECT_EQ(";", format(";")); |
| 91 | } |
| 92 | |
| 93 | TEST_F(FormatTest, FormatsGlobalStatementsAt0) { |
| 94 | EXPECT_EQ("int i;", format(" int i;")); |
| 95 | EXPECT_EQ("\nint i;", format(" \n\t \r int i;")); |
| 96 | EXPECT_EQ("int i;\nint j;", format(" int i; int j;")); |
| 97 | EXPECT_EQ("int i;\nint j;", format(" int i;\n int j;")); |
| 98 | } |
| 99 | |
| 100 | TEST_F(FormatTest, FormatsUnwrappedLinesAtFirstFormat) { |
| 101 | EXPECT_EQ("int i;", format("int\ni;")); |
| 102 | } |
| 103 | |
| 104 | TEST_F(FormatTest, FormatsNestedBlockStatements) { |
| 105 | EXPECT_EQ("{\n {\n {\n }\n }\n}", format("{{{}}}")); |
| 106 | } |
| 107 | |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 108 | TEST_F(FormatTest, FormatsNestedCall) { |
| 109 | verifyFormat("Method(f1, f2(f3));"); |
| 110 | verifyFormat("Method(f1(f2, f3()));"); |
| 111 | } |
| 112 | |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 113 | //===----------------------------------------------------------------------===// |
| 114 | // Tests for control statements. |
| 115 | //===----------------------------------------------------------------------===// |
| 116 | |
| 117 | TEST_F(FormatTest, FormatIfWithoutCompountStatement) { |
| 118 | verifyFormat("if (true)\n f();\ng();"); |
| 119 | verifyFormat("if (a)\n if (b)\n if (c)\n g();\nh();"); |
| 120 | verifyFormat("if (a)\n if (b) {\n f();\n }\ng();"); |
Daniel Jasper | e35ec2b | 2012-12-18 19:56:56 +0000 | [diff] [blame] | 121 | verifyFormat("if (a)\n" |
| 122 | " // comment\n" |
| 123 | " f();"); |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | TEST_F(FormatTest, ParseIfElse) { |
| 127 | verifyFormat("if (true)\n" |
| 128 | " if (true)\n" |
| 129 | " if (true)\n" |
| 130 | " f();\n" |
| 131 | " else\n" |
| 132 | " g();\n" |
| 133 | " else\n" |
| 134 | " h();\n" |
| 135 | "else\n" |
| 136 | " i();"); |
| 137 | verifyFormat("if (true)\n" |
| 138 | " if (true)\n" |
| 139 | " if (true) {\n" |
| 140 | " if (true)\n" |
| 141 | " f();\n" |
| 142 | " } else {\n" |
| 143 | " g();\n" |
| 144 | " }\n" |
| 145 | " else\n" |
| 146 | " h();\n" |
| 147 | "else {\n" |
| 148 | " i();\n" |
| 149 | "}"); |
| 150 | } |
| 151 | |
| 152 | TEST_F(FormatTest, ElseIf) { |
| 153 | verifyFormat("if (a) {\n" |
| 154 | "} else if (b) {\n" |
| 155 | "}"); |
| 156 | verifyFormat("if (a)\n" |
| 157 | " f();\n" |
| 158 | "else if (b)\n" |
| 159 | " g();\n" |
| 160 | "else\n" |
| 161 | " h();"); |
| 162 | } |
| 163 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 164 | TEST_F(FormatTest, FormatsForLoop) { |
| 165 | verifyFormat( |
| 166 | "for (int VeryVeryLongLoopVariable = 0; VeryVeryLongLoopVariable < 10;\n" |
Alexander Kornienko | 2e97cfc | 2012-12-05 15:06:06 +0000 | [diff] [blame] | 167 | " ++VeryVeryLongLoopVariable)\n" |
| 168 | " ;"); |
| 169 | verifyFormat("for (;;)\n" |
| 170 | " f();"); |
| 171 | verifyFormat("for (;;) {\n" |
| 172 | "}"); |
| 173 | verifyFormat("for (;;) {\n" |
| 174 | " f();\n" |
| 175 | "}"); |
Daniel Jasper | a324a0e | 2012-12-21 14:37:20 +0000 | [diff] [blame] | 176 | |
| 177 | verifyFormat( |
| 178 | "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n" |
| 179 | " E = UnwrappedLines.end();\n" |
| 180 | " I != E; ++I) {\n}"); |
| 181 | |
| 182 | verifyFormat( |
| 183 | "for (MachineFun::iterator IIII = PrevIt, EEEE = F.end(); IIII != EEEE;\n" |
| 184 | " ++IIIII) {\n}"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | TEST_F(FormatTest, FormatsWhileLoop) { |
| 188 | verifyFormat("while (true) {\n}"); |
Alexander Kornienko | 2e97cfc | 2012-12-05 15:06:06 +0000 | [diff] [blame] | 189 | verifyFormat("while (true)\n" |
| 190 | " f();"); |
| 191 | verifyFormat("while () {\n" |
| 192 | "}"); |
| 193 | verifyFormat("while () {\n" |
| 194 | " f();\n" |
| 195 | "}"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 196 | } |
| 197 | |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 198 | TEST_F(FormatTest, FormatsDoWhile) { |
| 199 | verifyFormat("do {\n" |
| 200 | " do_something();\n" |
| 201 | "} while (something());"); |
| 202 | verifyFormat("do\n" |
| 203 | " do_something();\n" |
| 204 | "while (something());"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 205 | } |
| 206 | |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 207 | TEST_F(FormatTest, FormatsSwitchStatement) { |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 208 | verifyFormat("switch (x) {\n" |
| 209 | "case 1:\n" |
| 210 | " f();\n" |
| 211 | " break;\n" |
| 212 | "case kFoo:\n" |
| 213 | "case ns::kBar:\n" |
| 214 | "case kBaz:\n" |
| 215 | " break;\n" |
| 216 | "default:\n" |
| 217 | " g();\n" |
| 218 | " break;\n" |
| 219 | "}"); |
| 220 | verifyFormat("switch (x) {\n" |
| 221 | "case 1: {\n" |
| 222 | " f();\n" |
| 223 | " break;\n" |
| 224 | "}\n" |
| 225 | "}"); |
| 226 | verifyFormat("switch (test)\n" |
| 227 | " ;"); |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 228 | verifyGoogleFormat("switch (x) {\n" |
| 229 | " case 1:\n" |
| 230 | " f();\n" |
| 231 | " break;\n" |
| 232 | " case kFoo:\n" |
| 233 | " case ns::kBar:\n" |
| 234 | " case kBaz:\n" |
| 235 | " break;\n" |
| 236 | " default:\n" |
| 237 | " g();\n" |
| 238 | " break;\n" |
| 239 | "}"); |
| 240 | verifyGoogleFormat("switch (x) {\n" |
| 241 | " case 1: {\n" |
| 242 | " f();\n" |
| 243 | " break;\n" |
| 244 | " }\n" |
| 245 | "}"); |
| 246 | verifyGoogleFormat("switch (test)\n" |
| 247 | " ;"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 248 | } |
| 249 | |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 250 | TEST_F(FormatTest, FormatsLabels) { |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 251 | verifyFormat("void f() {\n" |
| 252 | " some_code();\n" |
| 253 | "test_label:\n" |
| 254 | " some_other_code();\n" |
| 255 | " {\n" |
| 256 | " some_more_code();\n" |
| 257 | " another_label:\n" |
| 258 | " some_more_code();\n" |
| 259 | " }\n" |
| 260 | "}"); |
| 261 | verifyFormat("some_code();\n" |
| 262 | "test_label:\n" |
| 263 | "some_other_code();"); |
| 264 | } |
| 265 | |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 266 | //===----------------------------------------------------------------------===// |
| 267 | // Tests for comments. |
| 268 | //===----------------------------------------------------------------------===// |
| 269 | |
| 270 | TEST_F(FormatTest, UnderstandsSingleLineComments) { |
Daniel Jasper | e35ec2b | 2012-12-18 19:56:56 +0000 | [diff] [blame] | 271 | verifyFormat("// line 1\n" |
| 272 | "// line 2\n" |
| 273 | "void f() {\n}\n"); |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 274 | |
Daniel Jasper | e35ec2b | 2012-12-18 19:56:56 +0000 | [diff] [blame] | 275 | verifyFormat("void f() {\n" |
| 276 | " // Doesn't do anything\n" |
| 277 | "}"); |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 278 | |
Daniel Jasper | 7ad4eff | 2013-01-07 11:09:06 +0000 | [diff] [blame] | 279 | verifyFormat("int i // This is a fancy variable\n" |
Daniel Jasper | e35ec2b | 2012-12-18 19:56:56 +0000 | [diff] [blame] | 280 | " = 5;"); |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 281 | |
Daniel Jasper | e35ec2b | 2012-12-18 19:56:56 +0000 | [diff] [blame] | 282 | verifyFormat("enum E {\n" |
| 283 | " // comment\n" |
Daniel Jasper | 7ad4eff | 2013-01-07 11:09:06 +0000 | [diff] [blame] | 284 | " VAL_A, // comment\n" |
Daniel Jasper | e35ec2b | 2012-12-18 19:56:56 +0000 | [diff] [blame] | 285 | " VAL_B\n" |
| 286 | "};"); |
Daniel Jasper | 05b1ac8 | 2012-12-17 11:29:41 +0000 | [diff] [blame] | 287 | |
| 288 | verifyFormat( |
| 289 | "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" |
Daniel Jasper | 7ad4eff | 2013-01-07 11:09:06 +0000 | [diff] [blame] | 290 | " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; // Trailing comment"); |
Manuel Klimek | 6cf5814 | 2013-01-07 08:54:53 +0000 | [diff] [blame] | 291 | |
Daniel Jasper | 7ad4eff | 2013-01-07 11:09:06 +0000 | [diff] [blame] | 292 | EXPECT_EQ("int i; // single line trailing comment", |
Manuel Klimek | 6cf5814 | 2013-01-07 08:54:53 +0000 | [diff] [blame] | 293 | format("int i;\\\n// single line trailing comment")); |
Daniel Jasper | 7ad4eff | 2013-01-07 11:09:06 +0000 | [diff] [blame] | 294 | |
| 295 | verifyGoogleFormat("int a; // Trailing comment."); |
Daniel Jasper | 05b1ac8 | 2012-12-17 11:29:41 +0000 | [diff] [blame] | 296 | } |
| 297 | |
| 298 | TEST_F(FormatTest, UnderstandsMultiLineComments) { |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 299 | verifyFormat("f(/*test=*/ true);"); |
| 300 | } |
| 301 | |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 302 | //===----------------------------------------------------------------------===// |
| 303 | // Tests for classes, namespaces, etc. |
| 304 | //===----------------------------------------------------------------------===// |
| 305 | |
| 306 | TEST_F(FormatTest, DoesNotBreakSemiAfterClassDecl) { |
| 307 | verifyFormat("class A {\n};"); |
| 308 | } |
| 309 | |
| 310 | TEST_F(FormatTest, UnderstandsAccessSpecifiers) { |
| 311 | verifyFormat("class A {\n" |
| 312 | "public:\n" |
| 313 | "protected:\n" |
| 314 | "private:\n" |
| 315 | " void f() {\n" |
| 316 | " }\n" |
| 317 | "};"); |
| 318 | verifyGoogleFormat("class A {\n" |
| 319 | " public:\n" |
| 320 | " protected:\n" |
| 321 | " private:\n" |
| 322 | " void f() {\n" |
| 323 | " }\n" |
| 324 | "};"); |
| 325 | } |
| 326 | |
| 327 | TEST_F(FormatTest, FormatsDerivedClass) { |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 328 | verifyFormat("class A : public B {\n" |
| 329 | "};"); |
Daniel Jasper | c74e279 | 2012-12-07 09:52:15 +0000 | [diff] [blame] | 330 | verifyFormat("class A : public ::B {\n" |
| 331 | "};"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 332 | } |
| 333 | |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 334 | TEST_F(FormatTest, FormatsEnum) { |
Alexander Kornienko | a166e73 | 2012-12-04 14:46:19 +0000 | [diff] [blame] | 335 | verifyFormat("enum {\n" |
| 336 | " Zero,\n" |
| 337 | " One = 1,\n" |
| 338 | " Two = One + 1,\n" |
| 339 | " Three = (One + Two),\n" |
| 340 | " Four = (Zero && (One ^ Two)) | (One << Two),\n" |
| 341 | " Five = (One, Two, Three, Four, 5)\n" |
| 342 | "};"); |
| 343 | verifyFormat("enum Enum {\n" |
| 344 | "};"); |
| 345 | verifyFormat("enum {\n" |
| 346 | "};"); |
| 347 | } |
| 348 | |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 349 | TEST_F(FormatTest, FormatsNamespaces) { |
| 350 | verifyFormat("namespace some_namespace {\n" |
| 351 | "class A {\n" |
| 352 | "};\n" |
| 353 | "void f() {\n" |
| 354 | " f();\n" |
| 355 | "}\n" |
| 356 | "}"); |
| 357 | verifyFormat("namespace {\n" |
| 358 | "class A {\n" |
| 359 | "};\n" |
| 360 | "void f() {\n" |
| 361 | " f();\n" |
| 362 | "}\n" |
| 363 | "}"); |
Dmitri Gribenko | 1f94f2b | 2012-12-30 21:27:25 +0000 | [diff] [blame] | 364 | verifyFormat("inline namespace X {\n" |
| 365 | "class A {\n" |
| 366 | "};\n" |
| 367 | "void f() {\n" |
| 368 | " f();\n" |
| 369 | "}\n" |
| 370 | "}"); |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 371 | verifyFormat("using namespace some_namespace;\n" |
| 372 | "class A {\n" |
| 373 | "};\n" |
| 374 | "void f() {\n" |
| 375 | " f();\n" |
| 376 | "}"); |
| 377 | } |
| 378 | |
Daniel Jasper | 05b1ac8 | 2012-12-17 11:29:41 +0000 | [diff] [blame] | 379 | TEST_F(FormatTest, StaticInitializers) { |
| 380 | verifyFormat("static SomeClass SC = { 1, 'a' };"); |
| 381 | |
| 382 | // FIXME: Format like enums if the static initializer does not fit on a line. |
| 383 | verifyFormat( |
Daniel Jasper | 9cda800 | 2013-01-07 13:08:40 +0000 | [diff] [blame] | 384 | "static SomeClass WithALoooooooooooooooooooongName = {\n" |
| 385 | " 100000000, \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" };"); |
Daniel Jasper | 05b1ac8 | 2012-12-17 11:29:41 +0000 | [diff] [blame] | 386 | } |
| 387 | |
Manuel Klimek | a080a18 | 2013-01-02 16:30:12 +0000 | [diff] [blame] | 388 | TEST_F(FormatTest, FormatsSmallMacroDefinitionsInSingleLine) { |
| 389 | verifyFormat("#define ALooooooooooooooooooooooooooooooooooooooongMacro(" |
| 390 | " \\\n" |
| 391 | " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)"); |
| 392 | } |
| 393 | |
Daniel Jasper | 7160751 | 2013-01-07 10:48:50 +0000 | [diff] [blame] | 394 | TEST_F(FormatTest, DoesNotBreakPureVirtualFunctionDefinition) { |
Daniel Jasper | cd16238 | 2013-01-07 13:26:07 +0000 | [diff] [blame] | 395 | verifyFormat("virtual void write(ELFWriter *writerrr,\n" |
| 396 | " OwningPtr<FileOutputBuffer> &buffer) = 0;"); |
Daniel Jasper | 7160751 | 2013-01-07 10:48:50 +0000 | [diff] [blame] | 397 | } |
| 398 | |
Manuel Klimek | a080a18 | 2013-01-02 16:30:12 +0000 | [diff] [blame] | 399 | TEST_F(FormatTest, BreaksOnHashWhenDirectiveIsInvalid) { |
| 400 | EXPECT_EQ("#\n;", format("#;")); |
Manuel Klimek | dd5b101 | 2013-01-07 10:03:37 +0000 | [diff] [blame] | 401 | verifyFormat("#\n;\n;\n;"); |
Manuel Klimek | a080a18 | 2013-01-02 16:30:12 +0000 | [diff] [blame] | 402 | } |
| 403 | |
| 404 | TEST_F(FormatTest, UnescapedEndOfLineEndsPPDirective) { |
| 405 | EXPECT_EQ("#line 42 \"test\"\n", |
| 406 | format("# \\\n line \\\n 42 \\\n \"test\"\n")); |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 407 | EXPECT_EQ("#define A \\\n B\n", |
| 408 | format("# \\\n define \\\n A \\\n B\n", |
| 409 | getLLVMStyleWithColumns(12))); |
Manuel Klimek | a080a18 | 2013-01-02 16:30:12 +0000 | [diff] [blame] | 410 | } |
| 411 | |
| 412 | TEST_F(FormatTest, EndOfFileEndsPPDirective) { |
| 413 | EXPECT_EQ("#line 42 \"test\"", |
| 414 | format("# \\\n line \\\n 42 \\\n \"test\"")); |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 415 | EXPECT_EQ("#define A \\\n B", |
| 416 | format("# \\\n define \\\n A \\\n B", |
| 417 | getLLVMStyleWithColumns(12))); |
Manuel Klimek | a080a18 | 2013-01-02 16:30:12 +0000 | [diff] [blame] | 418 | } |
| 419 | |
Manuel Klimek | 060143e | 2013-01-02 18:33:23 +0000 | [diff] [blame] | 420 | TEST_F(FormatTest, IndentsPPDirectiveInReducedSpace) { |
Manuel Klimek | d544c57 | 2013-01-07 09:24:17 +0000 | [diff] [blame] | 421 | // If the macro fits in one line, we still do not get the full |
| 422 | // line, as only the next line decides whether we need an escaped newline and |
| 423 | // thus use the last column. |
| 424 | verifyFormat("#define A(B)", getLLVMStyleWithColumns(13)); |
Manuel Klimek | 060143e | 2013-01-02 18:33:23 +0000 | [diff] [blame] | 425 | |
Manuel Klimek | d544c57 | 2013-01-07 09:24:17 +0000 | [diff] [blame] | 426 | verifyFormat("#define A( \\\n B)", getLLVMStyleWithColumns(12)); |
| 427 | verifyFormat("#define AA(\\\n B)", getLLVMStyleWithColumns(12)); |
Manuel Klimek | 060143e | 2013-01-02 18:33:23 +0000 | [diff] [blame] | 428 | verifyFormat("#define A( \\\n A, B)", getLLVMStyleWithColumns(12)); |
| 429 | } |
| 430 | |
| 431 | TEST_F(FormatTest, HandlePreprocessorDirectiveContext) { |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 432 | EXPECT_EQ("// some comment\n" |
| 433 | "#include \"a.h\"\n" |
| 434 | "#define A(A,\\\n" |
| 435 | " B)\n" |
| 436 | "#include \"b.h\"\n" |
| 437 | "// some comment\n", |
| 438 | format(" // some comment\n" |
| 439 | " #include \"a.h\"\n" |
| 440 | "#define A(A,\\\n" |
| 441 | " B)\n" |
| 442 | " #include \"b.h\"\n" |
| 443 | " // some comment\n", getLLVMStyleWithColumns(13))); |
Manuel Klimek | 060143e | 2013-01-02 18:33:23 +0000 | [diff] [blame] | 444 | } |
| 445 | |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 446 | TEST_F(FormatTest, LayoutSingleHash) { |
| 447 | EXPECT_EQ("#\na;", format("#\na;")); |
| 448 | } |
| 449 | |
| 450 | TEST_F(FormatTest, LayoutCodeInMacroDefinitions) { |
| 451 | EXPECT_EQ("#define A \\\n" |
| 452 | " c; \\\n" |
| 453 | " e;\n" |
| 454 | "f;", format("#define A c; e;\n" |
| 455 | "f;", getLLVMStyleWithColumns(14))); |
| 456 | } |
| 457 | |
| 458 | TEST_F(FormatTest, LayoutRemainingTokens) { |
| 459 | EXPECT_EQ("{\n}", format("{}")); |
| 460 | } |
| 461 | |
| 462 | TEST_F(FormatTest, LayoutSingleUnwrappedLineInMacro) { |
| 463 | EXPECT_EQ("#define A \\\n b;", |
| 464 | format("#define A b;", 10, 2, getLLVMStyleWithColumns(11))); |
| 465 | } |
| 466 | |
| 467 | TEST_F(FormatTest, MacroDefinitionInsideStatement) { |
| 468 | EXPECT_EQ("int x,\n#define A\ny;", format("int x,\n#define A\ny;")); |
| 469 | } |
| 470 | |
Manuel Klimek | 6f8424b | 2013-01-05 21:34:55 +0000 | [diff] [blame] | 471 | TEST_F(FormatTest, HashInMacroDefinition) { |
| 472 | verifyFormat("#define A \\\n b #c;", getLLVMStyleWithColumns(11)); |
| 473 | verifyFormat("#define A \\\n" |
| 474 | " { \\\n" |
| 475 | " f(#c);\\\n" |
| 476 | " }", getLLVMStyleWithColumns(11)); |
| 477 | } |
| 478 | |
Manuel Klimek | c37b4d6 | 2013-01-05 22:14:16 +0000 | [diff] [blame] | 479 | TEST_F(FormatTest, IndentPreprocessorDirectivesAtZero) { |
| 480 | EXPECT_EQ("{\n {\n#define A\n }\n}", format("{{\n#define A\n}}")); |
| 481 | } |
| 482 | |
Manuel Klimek | f6fd00b | 2013-01-05 22:56:06 +0000 | [diff] [blame] | 483 | TEST_F(FormatTest, FormatHashIfNotAtStartOfLine) { |
| 484 | verifyFormat("{\n {\n a #c;\n }\n}"); |
| 485 | } |
| 486 | |
Manuel Klimek | a5342db | 2013-01-06 20:07:31 +0000 | [diff] [blame] | 487 | TEST_F(FormatTest, FormatUnbalancedStructuralElements) { |
| 488 | EXPECT_EQ("#define A \\\n { \\\n {\nint i;", |
| 489 | format("#define A { {\nint i;", getLLVMStyleWithColumns(11))); |
| 490 | EXPECT_EQ("#define A \\\n } \\\n }\nint i;", |
| 491 | format("#define A } }\nint i;", getLLVMStyleWithColumns(11))); |
| 492 | } |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 493 | |
Manuel Klimek | 9541938 | 2013-01-07 07:56:50 +0000 | [diff] [blame] | 494 | TEST_F(FormatTest, EscapedNewlineAtStartOfTokenInMacroDefinition) { |
Daniel Jasper | cd16238 | 2013-01-07 13:26:07 +0000 | [diff] [blame] | 495 | EXPECT_EQ( |
| 496 | "#define A \\\n int i; \\\n int j;", |
| 497 | format("#define A \\\nint i;\\\n int j;", getLLVMStyleWithColumns(11))); |
Manuel Klimek | 9541938 | 2013-01-07 07:56:50 +0000 | [diff] [blame] | 498 | } |
| 499 | |
Manuel Klimek | d544c57 | 2013-01-07 09:24:17 +0000 | [diff] [blame] | 500 | TEST_F(FormatTest, CalculateSpaceOnConsecutiveLinesInMacro) { |
| 501 | verifyFormat("#define A \\\n" |
| 502 | " int v( \\\n" |
| 503 | " a); \\\n" |
| 504 | " int i;", getLLVMStyleWithColumns(11)); |
| 505 | } |
| 506 | |
Manuel Klimek | a080a18 | 2013-01-02 16:30:12 +0000 | [diff] [blame] | 507 | TEST_F(FormatTest, MixingPreprocessorDirectivesAndNormalCode) { |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 508 | EXPECT_EQ( |
| 509 | "#define ALooooooooooooooooooooooooooooooooooooooongMacro(" |
| 510 | " \\\n" |
| 511 | " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n" |
| 512 | "\n" |
| 513 | "AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n" |
| 514 | " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n", |
| 515 | format(" #define ALooooooooooooooooooooooooooooooooooooooongMacro(" |
| 516 | "\\\n" |
| 517 | "aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n" |
| 518 | " \n" |
| 519 | " AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n" |
| 520 | " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n")); |
Manuel Klimek | a080a18 | 2013-01-02 16:30:12 +0000 | [diff] [blame] | 521 | } |
| 522 | |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 523 | //===----------------------------------------------------------------------===// |
| 524 | // Line break tests. |
| 525 | //===----------------------------------------------------------------------===// |
| 526 | |
| 527 | TEST_F(FormatTest, FormatsFunctionDefinition) { |
| 528 | verifyFormat("void f(int a, int b, int c, int d, int e, int f, int g," |
| 529 | " int h, int j, int f,\n" |
| 530 | " int c, int ddddddddddddd) {\n" |
| 531 | "}"); |
| 532 | } |
| 533 | |
| 534 | TEST_F(FormatTest, FormatsAwesomeMethodCall) { |
| 535 | verifyFormat( |
Daniel Jasper | a4974cf | 2012-12-24 16:43:00 +0000 | [diff] [blame] | 536 | "SomeLongMethodName(SomeReallyLongMethod(\n" |
| 537 | " CallOtherReallyLongMethod(parameter, parameter, parameter)),\n" |
| 538 | " SecondLongCall(parameter));"); |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 539 | } |
| 540 | |
Daniel Jasper | 1321eb5 | 2012-12-18 21:05:13 +0000 | [diff] [blame] | 541 | TEST_F(FormatTest, ConstructorInitializers) { |
| 542 | verifyFormat("Constructor() : Initializer(FitsOnTheLine) {\n}"); |
| 543 | |
| 544 | verifyFormat( |
| 545 | "SomeClass::Constructor()\n" |
| 546 | " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {\n" |
| 547 | "}"); |
| 548 | |
| 549 | verifyFormat( |
| 550 | "SomeClass::Constructor()\n" |
| 551 | " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" |
| 552 | " aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {\n" |
| 553 | "}"); |
| 554 | |
| 555 | verifyFormat("Constructor()\n" |
| 556 | " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" |
| 557 | " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" |
| 558 | " aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" |
| 559 | " aaaaaaaaaaaaaaaaaaaaaaa() {\n" |
| 560 | "}"); |
| 561 | |
| 562 | // Here a line could be saved by splitting the second initializer onto two |
| 563 | // lines, but that is not desireable. |
| 564 | verifyFormat("Constructor()\n" |
| 565 | " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n" |
| 566 | " aaaaaaaaaaa(aaaaaaaaaaa),\n" |
| 567 | " aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" |
| 568 | "}"); |
| 569 | |
| 570 | verifyGoogleFormat("MyClass::MyClass(int var)\n" |
| 571 | " : some_var_(var), // 4 space indent\n" |
| 572 | " some_other_var_(var + 1) { // lined up\n" |
| 573 | "}"); |
| 574 | } |
| 575 | |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 576 | TEST_F(FormatTest, BreaksAsHighAsPossible) { |
| 577 | verifyFormat( |
| 578 | "if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa && aaaaaaaaaaaaaaaaaaaaaaaaaa) ||\n" |
| 579 | " (bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb && bbbbbbbbbbbbbbbbbbbbbbbbbb))\n" |
| 580 | " f();"); |
| 581 | } |
| 582 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 583 | TEST_F(FormatTest, BreaksDesireably) { |
| 584 | verifyFormat("if (aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n" |
| 585 | " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n" |
| 586 | " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa)) {\n};"); |
| 587 | |
| 588 | verifyFormat( |
| 589 | "aaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" |
| 590 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); |
| 591 | |
| 592 | verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" |
| 593 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" |
| 594 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));"); |
Daniel Jasper | a88bb45 | 2012-12-04 10:50:12 +0000 | [diff] [blame] | 595 | |
| 596 | verifyFormat( |
| 597 | "aaaaaaaa(aaaaaaaaaaaaa, aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" |
| 598 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n" |
| 599 | " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" |
| 600 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));"); |
Daniel Jasper | 33182dd | 2012-12-05 14:57:28 +0000 | [diff] [blame] | 601 | |
| 602 | verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" |
| 603 | " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); |
| 604 | |
Daniel Jasper | 723f030 | 2013-01-02 14:40:02 +0000 | [diff] [blame] | 605 | verifyFormat( |
| 606 | "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&\n" |
| 607 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); |
| 608 | |
Daniel Jasper | 33182dd | 2012-12-05 14:57:28 +0000 | [diff] [blame] | 609 | // This test case breaks on an incorrect memoization, i.e. an optimization not |
| 610 | // taking into account the StopAt value. |
| 611 | verifyFormat( |
| 612 | "return aaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n" |
Daniel Jasper | cf225b6 | 2012-12-24 13:43:52 +0000 | [diff] [blame] | 613 | " aaaaaaaaaaa(aaaaaaaaa) || aaaaaaaaaaaaaaaaaaaaaaa ||\n" |
| 614 | " aaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n" |
| 615 | " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); |
Daniel Jasper | a4974cf | 2012-12-24 16:43:00 +0000 | [diff] [blame] | 616 | |
Daniel Jasper | cd16238 | 2013-01-07 13:26:07 +0000 | [diff] [blame] | 617 | verifyFormat("{\n {\n {\n" |
| 618 | " Annotation.SpaceRequiredBefore =\n" |
| 619 | " Line.Tokens[i - 1].Tok.isNot(tok::l_paren) &&\n" |
| 620 | " Line.Tokens[i - 1].Tok.isNot(tok::l_square);\n" |
| 621 | " }\n }\n}"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 622 | } |
| 623 | |
Daniel Jasper | e2c7acf | 2012-12-24 00:13:23 +0000 | [diff] [blame] | 624 | TEST_F(FormatTest, BreaksAccordingToOperatorPrecedence) { |
| 625 | verifyFormat( |
| 626 | "if (aaaaaaaaaaaaaaaaaaaaaaaaa ||\n" |
| 627 | " bbbbbbbbbbbbbbbbbbbbbbbbb && ccccccccccccccccccccccccc) {\n}"); |
Daniel Jasper | cd16238 | 2013-01-07 13:26:07 +0000 | [diff] [blame] | 628 | verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa && bbbbbbbbbbbbbbbbbbbbbbbbb ||\n" |
| 629 | " ccccccccccccccccccccccccc) {\n}"); |
| 630 | verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb ||\n" |
| 631 | " ccccccccccccccccccccccccc) {\n}"); |
Daniel Jasper | e2c7acf | 2012-12-24 00:13:23 +0000 | [diff] [blame] | 632 | verifyFormat( |
| 633 | "if ((aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb) &&\n" |
| 634 | " ccccccccccccccccccccccccc) {\n}"); |
| 635 | } |
| 636 | |
Daniel Jasper | 9cda800 | 2013-01-07 13:08:40 +0000 | [diff] [blame] | 637 | TEST_F(FormatTest, PrefersNotToBreakAfterAssignments) { |
| 638 | verifyFormat( |
| 639 | "unsigned Cost = TTI.getMemoryOpCost(I->getOpcode(), VectorTy,\n" |
| 640 | " SI->getAlignment(),\n" |
| 641 | " SI->getPointerAddressSpaceee());\n"); |
| 642 | verifyFormat( |
| 643 | "CharSourceRange LineRange = CharSourceRange::getTokenRange(\n" |
| 644 | " Line.Tokens.front().Tok.getLocation(),\n" |
| 645 | " Line.Tokens.back().Tok.getLocation());"); |
| 646 | } |
| 647 | |
Daniel Jasper | cf225b6 | 2012-12-24 13:43:52 +0000 | [diff] [blame] | 648 | TEST_F(FormatTest, AlignsAfterAssignments) { |
| 649 | verifyFormat( |
| 650 | "int Result = aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" |
Daniel Jasper | 9cda800 | 2013-01-07 13:08:40 +0000 | [diff] [blame] | 651 | " aaaaaaaaaaaaaaaaaaaaaaaaa;"); |
Daniel Jasper | cf225b6 | 2012-12-24 13:43:52 +0000 | [diff] [blame] | 652 | verifyFormat( |
| 653 | "Result += aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" |
Daniel Jasper | 9cda800 | 2013-01-07 13:08:40 +0000 | [diff] [blame] | 654 | " aaaaaaaaaaaaaaaaaaaaaaaaa;"); |
Daniel Jasper | cf225b6 | 2012-12-24 13:43:52 +0000 | [diff] [blame] | 655 | verifyFormat( |
| 656 | "Result >>= aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" |
Daniel Jasper | 9cda800 | 2013-01-07 13:08:40 +0000 | [diff] [blame] | 657 | " aaaaaaaaaaaaaaaaaaaaaaaaa;"); |
Daniel Jasper | cf225b6 | 2012-12-24 13:43:52 +0000 | [diff] [blame] | 658 | verifyFormat( |
| 659 | "int Result = (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" |
Daniel Jasper | 9cda800 | 2013-01-07 13:08:40 +0000 | [diff] [blame] | 660 | " aaaaaaaaaaaaaaaaaaaaaaaaa);"); |
Daniel Jasper | cf225b6 | 2012-12-24 13:43:52 +0000 | [diff] [blame] | 661 | verifyFormat( |
Daniel Jasper | 9cda800 | 2013-01-07 13:08:40 +0000 | [diff] [blame] | 662 | "double LooooooooooooooooooooooooongResult = aaaaaaaaaaaaaaaaaaaaaaaa +\n" |
| 663 | " aaaaaaaaaaaaaaaaaaaaaaaa +\n" |
| 664 | " aaaaaaaaaaaaaaaaaaaaaaaa;"); |
Daniel Jasper | cf225b6 | 2012-12-24 13:43:52 +0000 | [diff] [blame] | 665 | } |
| 666 | |
| 667 | TEST_F(FormatTest, AlignsAfterReturn) { |
| 668 | verifyFormat( |
| 669 | "return aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" |
| 670 | " aaaaaaaaaaaaaaaaaaaaaaaaa;"); |
| 671 | verifyFormat( |
| 672 | "return (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" |
| 673 | " aaaaaaaaaaaaaaaaaaaaaaaaa);"); |
| 674 | } |
| 675 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 676 | TEST_F(FormatTest, AlignsStringLiterals) { |
| 677 | verifyFormat("loooooooooooooooooooooooooongFunction(\"short literal \"\n" |
| 678 | " \"short literal\");"); |
| 679 | verifyFormat( |
| 680 | "looooooooooooooooooooooooongFunction(\n" |
| 681 | " \"short literal\"\n" |
| 682 | " \"looooooooooooooooooooooooooooooooooooooooooooooooong literal\");"); |
| 683 | } |
| 684 | |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 685 | TEST_F(FormatTest, AlignsPipes) { |
| 686 | verifyFormat( |
| 687 | "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" |
| 688 | " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" |
| 689 | " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); |
| 690 | verifyFormat( |
| 691 | "aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa\n" |
| 692 | " << aaaaaaaaaaaaaaaaaaaa;"); |
| 693 | verifyFormat( |
| 694 | "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" |
| 695 | " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); |
| 696 | verifyFormat( |
| 697 | "llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n" |
| 698 | " \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\"\n" |
| 699 | " << \"ccccccccccccccccccccccccccccccccccccccccccccccccc\";"); |
| 700 | verifyFormat( |
| 701 | "aaaaaaaa << (aaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" |
| 702 | " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" |
| 703 | " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); |
| 704 | } |
| 705 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 706 | TEST_F(FormatTest, UnderstandsEquals) { |
| 707 | verifyFormat( |
| 708 | "aaaaaaaaaaaaaaaaa =\n" |
| 709 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); |
| 710 | verifyFormat( |
| 711 | "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" |
| 712 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" |
| 713 | "}"); |
| 714 | verifyFormat( |
| 715 | "if (a) {\n" |
| 716 | "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" |
| 717 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" |
| 718 | "}"); |
| 719 | |
Daniel Jasper | 9cda800 | 2013-01-07 13:08:40 +0000 | [diff] [blame] | 720 | verifyFormat( |
| 721 | "if (int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = 100000000 +\n" |
| 722 | " 10000000) {\n" |
| 723 | "}"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 724 | } |
| 725 | |
Daniel Jasper | 9a0b494 | 2012-12-17 14:34:14 +0000 | [diff] [blame] | 726 | TEST_F(FormatTest, WrapsAtFunctionCallsIfNecessary) { |
Daniel Jasper | 1321eb5 | 2012-12-18 21:05:13 +0000 | [diff] [blame] | 727 | verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n" |
| 728 | " .looooooooooooooooooooooooooooooooooooooongFunction();"); |
Daniel Jasper | 9a0b494 | 2012-12-17 14:34:14 +0000 | [diff] [blame] | 729 | |
Daniel Jasper | 1321eb5 | 2012-12-18 21:05:13 +0000 | [diff] [blame] | 730 | verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n" |
| 731 | " ->looooooooooooooooooooooooooooooooooooooongFunction();"); |
Daniel Jasper | 9a0b494 | 2012-12-17 14:34:14 +0000 | [diff] [blame] | 732 | |
| 733 | verifyFormat( |
| 734 | "LooooooooooooooooooooooooooooooooongObject->shortFunction(Parameter1,\n" |
| 735 | " Parameter2);"); |
| 736 | |
| 737 | verifyFormat( |
| 738 | "ShortObject->shortFunction(\n" |
| 739 | " LooooooooooooooooooooooooooooooooooooooooooooooongParameter1,\n" |
| 740 | " LooooooooooooooooooooooooooooooooooooooooooooooongParameter2);"); |
| 741 | |
| 742 | verifyFormat("loooooooooooooongFunction(\n" |
| 743 | " LoooooooooooooongObject->looooooooooooooooongFunction());"); |
| 744 | |
| 745 | verifyFormat( |
| 746 | "function(LoooooooooooooooooooooooooooooooooooongObject\n" |
| 747 | " ->loooooooooooooooooooooooooooooooooooooooongFunction());"); |
| 748 | |
Daniel Jasper | 46a46a2 | 2013-01-07 07:13:20 +0000 | [diff] [blame] | 749 | // Here, it is not necessary to wrap at "." or "->". |
Daniel Jasper | 1321eb5 | 2012-12-18 21:05:13 +0000 | [diff] [blame] | 750 | verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaa) ||\n" |
| 751 | " aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" |
| 752 | "}"); |
Daniel Jasper | 46a46a2 | 2013-01-07 07:13:20 +0000 | [diff] [blame] | 753 | verifyFormat( |
| 754 | "aaaaaaaaaaa->aaaaaaaaa(\n" |
| 755 | " aaaaaaaaaaaaaaaaaaaaaaaaa,\n" |
| 756 | " aaaaaaaaaaaaaaaaaa->aaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa));\n"); |
Daniel Jasper | 9a0b494 | 2012-12-17 14:34:14 +0000 | [diff] [blame] | 757 | } |
| 758 | |
Daniel Jasper | 9a64fb5 | 2013-01-02 15:08:56 +0000 | [diff] [blame] | 759 | TEST_F(FormatTest, WrapsTemplateDeclarations) { |
| 760 | verifyFormat("template <typename T>\n" |
| 761 | "virtual void loooooooooooongFunction(int Param1, int Param2);"); |
| 762 | verifyFormat( |
| 763 | "template <typename T> void f(int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram1,\n" |
| 764 | " int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram2);"); |
| 765 | verifyFormat( |
| 766 | "template <typename T>\n" |
| 767 | "void looooooooooooooooooooongFunction(int Paaaaaaaaaaaaaaaaaaaaram1,\n" |
| 768 | " int Paaaaaaaaaaaaaaaaaaaaram2);"); |
Daniel Jasper | 5eda31e | 2013-01-02 18:30:06 +0000 | [diff] [blame] | 769 | verifyFormat( |
| 770 | "template <typename T>\n" |
| 771 | "aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa,\n" |
| 772 | " aaaaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaa,\n" |
| 773 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); |
Daniel Jasper | 9a64fb5 | 2013-01-02 15:08:56 +0000 | [diff] [blame] | 774 | |
| 775 | } |
| 776 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 777 | TEST_F(FormatTest, UnderstandsTemplateParameters) { |
| 778 | verifyFormat("A<int> a;"); |
| 779 | verifyFormat("A<A<A<int> > > a;"); |
| 780 | verifyFormat("A<A<A<int, 2>, 3>, 4> a;"); |
| 781 | verifyFormat("bool x = a < 1 || 2 > a;"); |
| 782 | verifyFormat("bool x = 5 < f<int>();"); |
| 783 | verifyFormat("bool x = f<int>() > 5;"); |
| 784 | verifyFormat("bool x = 5 < a<int>::x;"); |
| 785 | verifyFormat("bool x = a < 4 ? a > 2 : false;"); |
| 786 | verifyFormat("bool x = f() ? a < 2 : a > 2;"); |
| 787 | |
| 788 | verifyGoogleFormat("A<A<int>> a;"); |
| 789 | verifyGoogleFormat("A<A<A<int>>> a;"); |
| 790 | verifyGoogleFormat("A<A<A<A<int>>>> a;"); |
| 791 | |
| 792 | verifyFormat("test >> a >> b;"); |
| 793 | verifyFormat("test << a >> b;"); |
| 794 | |
| 795 | verifyFormat("f<int>();"); |
| 796 | verifyFormat("template <typename T> void f() {\n}"); |
| 797 | } |
| 798 | |
Daniel Jasper | 98e6b4a | 2012-12-21 09:41:31 +0000 | [diff] [blame] | 799 | TEST_F(FormatTest, UnderstandsUnaryOperators) { |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 800 | verifyFormat("int a = -2;"); |
Daniel Jasper | 8822d3a | 2012-12-04 13:02:32 +0000 | [diff] [blame] | 801 | verifyFormat("f(-1, -2, -3);"); |
| 802 | verifyFormat("a[-1] = 5;"); |
| 803 | verifyFormat("int a = 5 + -2;"); |
Daniel Jasper | 112fb27 | 2012-12-05 07:51:39 +0000 | [diff] [blame] | 804 | verifyFormat("if (i == -1) {\n}"); |
| 805 | verifyFormat("if (i != -1) {\n}"); |
| 806 | verifyFormat("if (i > -1) {\n}"); |
| 807 | verifyFormat("if (i < -1) {\n}"); |
Daniel Jasper | d56a737 | 2012-12-06 13:16:39 +0000 | [diff] [blame] | 808 | verifyFormat("++(a->f());"); |
| 809 | verifyFormat("--(a->f());"); |
| 810 | verifyFormat("if (!(a->f())) {\n}"); |
Daniel Jasper | 98e6b4a | 2012-12-21 09:41:31 +0000 | [diff] [blame] | 811 | |
| 812 | verifyFormat("a-- > b;"); |
| 813 | verifyFormat("b ? -a : c;"); |
| 814 | verifyFormat("n * sizeof char16;"); |
Daniel Jasper | ba3d307 | 2013-01-02 17:21:36 +0000 | [diff] [blame] | 815 | verifyFormat("n * alignof char16;"); |
Daniel Jasper | 98e6b4a | 2012-12-21 09:41:31 +0000 | [diff] [blame] | 816 | verifyFormat("sizeof(char);"); |
Daniel Jasper | ba3d307 | 2013-01-02 17:21:36 +0000 | [diff] [blame] | 817 | verifyFormat("alignof(char);"); |
Daniel Jasper | 1f0754b | 2013-01-02 15:26:16 +0000 | [diff] [blame] | 818 | |
| 819 | verifyFormat("return -1;"); |
| 820 | verifyFormat("switch (a) {\n" |
| 821 | "case -1:\n" |
| 822 | " break;\n" |
| 823 | "}"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 824 | } |
| 825 | |
| 826 | TEST_F(FormatTest, UndestandsOverloadedOperators) { |
Daniel Jasper | f6aef6a | 2012-12-24 10:56:04 +0000 | [diff] [blame] | 827 | verifyFormat("bool operator<();"); |
| 828 | verifyFormat("bool operator>();"); |
| 829 | verifyFormat("bool operator=();"); |
| 830 | verifyFormat("bool operator==();"); |
| 831 | verifyFormat("bool operator!=();"); |
| 832 | verifyFormat("int operator+();"); |
| 833 | verifyFormat("int operator++();"); |
| 834 | verifyFormat("bool operator();"); |
| 835 | verifyFormat("bool operator()();"); |
| 836 | verifyFormat("bool operator[]();"); |
| 837 | verifyFormat("operator bool();"); |
| 838 | verifyFormat("operator SomeType<int>();"); |
| 839 | verifyFormat("void *operator new(std::size_t size);"); |
| 840 | verifyFormat("void *operator new[](std::size_t size);"); |
| 841 | verifyFormat("void operator delete(void *ptr);"); |
| 842 | verifyFormat("void operator delete[](void *ptr);"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 843 | } |
| 844 | |
Daniel Jasper | 5d33440 | 2013-01-02 08:57:10 +0000 | [diff] [blame] | 845 | TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) { |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 846 | verifyFormat("int *f(int *a) {\n}"); |
| 847 | verifyFormat("f(a, *a);"); |
| 848 | verifyFormat("f(*a);"); |
| 849 | verifyFormat("int a = b * 10;"); |
| 850 | verifyFormat("int a = 10 * b;"); |
Daniel Jasper | 112fb27 | 2012-12-05 07:51:39 +0000 | [diff] [blame] | 851 | verifyFormat("int a = b * c;"); |
Daniel Jasper | 33182dd | 2012-12-05 14:57:28 +0000 | [diff] [blame] | 852 | verifyFormat("int a += b * c;"); |
| 853 | verifyFormat("int a -= b * c;"); |
| 854 | verifyFormat("int a *= b * c;"); |
| 855 | verifyFormat("int a /= b * c;"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 856 | verifyFormat("int a = *b;"); |
Daniel Jasper | 112fb27 | 2012-12-05 07:51:39 +0000 | [diff] [blame] | 857 | verifyFormat("int a = *b * c;"); |
| 858 | verifyFormat("int a = b * *c;"); |
Daniel Jasper | c74e279 | 2012-12-07 09:52:15 +0000 | [diff] [blame] | 859 | verifyFormat("int main(int argc, char **argv) {\n}"); |
Nico Weber | 00d5a04 | 2012-12-23 01:07:46 +0000 | [diff] [blame] | 860 | verifyFormat("return 10 * b;"); |
| 861 | verifyFormat("return *b * *c;"); |
| 862 | verifyFormat("return a & ~b;"); |
Daniel Jasper | 5d33440 | 2013-01-02 08:57:10 +0000 | [diff] [blame] | 863 | verifyFormat("f(b ? *c : *d);"); |
| 864 | verifyFormat("int a = b ? *c : *d;"); |
Daniel Jasper | ba3d307 | 2013-01-02 17:21:36 +0000 | [diff] [blame] | 865 | verifyFormat("*b = a;"); |
| 866 | verifyFormat("a * ~b;"); |
| 867 | verifyFormat("a * !b;"); |
| 868 | verifyFormat("a * +b;"); |
| 869 | verifyFormat("a * -b;"); |
| 870 | verifyFormat("a * ++b;"); |
| 871 | verifyFormat("a * --b;"); |
Daniel Jasper | c74e279 | 2012-12-07 09:52:15 +0000 | [diff] [blame] | 872 | |
Daniel Jasper | 9bb0d28 | 2013-01-04 20:46:38 +0000 | [diff] [blame] | 873 | verifyFormat("InvalidRegions[*R] = 0;"); |
| 874 | |
Daniel Jasper | 8b39c66 | 2012-12-10 18:59:13 +0000 | [diff] [blame] | 875 | // FIXME: Is this desired for LLVM? Fix if not. |
| 876 | verifyFormat("A<int *> a;"); |
| 877 | verifyFormat("A<int **> a;"); |
| 878 | verifyFormat("A<int *, int *> a;"); |
| 879 | verifyFormat("A<int **, int **> a;"); |
Daniel Jasper | ef5b9c3 | 2013-01-02 15:46:59 +0000 | [diff] [blame] | 880 | verifyFormat("Type *A = static_cast<Type *>(P);"); |
| 881 | verifyFormat("Type *A = (Type *) P;"); |
| 882 | verifyFormat("Type *A = (vector<Type *, int *>) P;"); |
Daniel Jasper | 8b39c66 | 2012-12-10 18:59:13 +0000 | [diff] [blame] | 883 | |
Daniel Jasper | c74e279 | 2012-12-07 09:52:15 +0000 | [diff] [blame] | 884 | verifyGoogleFormat("int main(int argc, char** argv) {\n}"); |
Daniel Jasper | 8b39c66 | 2012-12-10 18:59:13 +0000 | [diff] [blame] | 885 | verifyGoogleFormat("A<int*> a;"); |
| 886 | verifyGoogleFormat("A<int**> a;"); |
| 887 | verifyGoogleFormat("A<int*, int*> a;"); |
| 888 | verifyGoogleFormat("A<int**, int**> a;"); |
Daniel Jasper | 5d33440 | 2013-01-02 08:57:10 +0000 | [diff] [blame] | 889 | verifyGoogleFormat("f(b ? *c : *d);"); |
| 890 | verifyGoogleFormat("int a = b ? *c : *d;"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 891 | } |
| 892 | |
Daniel Jasper | 4dc41de | 2013-01-02 08:44:14 +0000 | [diff] [blame] | 893 | TEST_F(FormatTest, DoesNotBreakBeforePointerOrReference) { |
Daniel Jasper | cd16238 | 2013-01-07 13:26:07 +0000 | [diff] [blame] | 894 | verifyFormat("int *someFunction(int LoooooooooooooooongParam1,\n" |
| 895 | " int LoooooooooooooooongParam2) {\n}"); |
Daniel Jasper | 4dc41de | 2013-01-02 08:44:14 +0000 | [diff] [blame] | 896 | verifyFormat( |
| 897 | "TypeSpecDecl *TypeSpecDecl::Create(ASTContext &C, DeclContext *DC,\n" |
| 898 | " SourceLocation L, IdentifierIn *II,\n" |
| 899 | " Type *T) {\n}"); |
| 900 | } |
| 901 | |
Daniel Jasper | 3b5943f | 2012-12-06 09:56:08 +0000 | [diff] [blame] | 902 | TEST_F(FormatTest, LineStartsWithSpecialCharacter) { |
| 903 | verifyFormat("(a)->b();"); |
| 904 | verifyFormat("--a;"); |
| 905 | } |
| 906 | |
Daniel Jasper | 8822d3a | 2012-12-04 13:02:32 +0000 | [diff] [blame] | 907 | TEST_F(FormatTest, HandlesIncludeDirectives) { |
| 908 | EXPECT_EQ("#include <string>\n", format("#include <string>\n")); |
Daniel Jasper | cd1a32b | 2012-12-21 17:58:39 +0000 | [diff] [blame] | 909 | EXPECT_EQ("#include <a/b/c.h>\n", format("#include <a/b/c.h>\n")); |
Daniel Jasper | 8822d3a | 2012-12-04 13:02:32 +0000 | [diff] [blame] | 910 | EXPECT_EQ("#include \"a/b/string\"\n", format("#include \"a/b/string\"\n")); |
| 911 | EXPECT_EQ("#include \"string.h\"\n", format("#include \"string.h\"\n")); |
| 912 | EXPECT_EQ("#include \"string.h\"\n", format("#include \"string.h\"\n")); |
Nico Weber | b23ae0c | 2012-12-21 18:21:56 +0000 | [diff] [blame] | 913 | |
| 914 | EXPECT_EQ("#import <string>\n", format("#import <string>\n")); |
| 915 | EXPECT_EQ("#import <a/b/c.h>\n", format("#import <a/b/c.h>\n")); |
| 916 | EXPECT_EQ("#import \"a/b/string\"\n", format("#import \"a/b/string\"\n")); |
| 917 | EXPECT_EQ("#import \"string.h\"\n", format("#import \"string.h\"\n")); |
| 918 | EXPECT_EQ("#import \"string.h\"\n", format("#import \"string.h\"\n")); |
Daniel Jasper | 8822d3a | 2012-12-04 13:02:32 +0000 | [diff] [blame] | 919 | } |
| 920 | |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 921 | //===----------------------------------------------------------------------===// |
| 922 | // Error recovery tests. |
| 923 | //===----------------------------------------------------------------------===// |
| 924 | |
Alexander Kornienko | 56e49c5 | 2012-12-10 16:34:48 +0000 | [diff] [blame] | 925 | TEST_F(FormatTest, IncorrectAccessSpecifier) { |
| 926 | verifyFormat("public:"); |
| 927 | verifyFormat("class A {\n" |
| 928 | "public\n" |
| 929 | " void f() {\n" |
| 930 | " }\n" |
| 931 | "};"); |
| 932 | verifyFormat("public\n" |
| 933 | "int qwerty;"); |
| 934 | verifyFormat("public\n" |
| 935 | "B {\n" |
| 936 | "};"); |
| 937 | verifyFormat("public\n" |
| 938 | "{\n" |
| 939 | "};"); |
| 940 | verifyFormat("public\n" |
| 941 | "B {\n" |
| 942 | " int x;\n" |
| 943 | "};"); |
| 944 | } |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 945 | |
Alexander Kornienko | 393b008 | 2012-12-04 15:40:36 +0000 | [diff] [blame] | 946 | TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) { |
| 947 | verifyFormat("{"); |
| 948 | } |
| 949 | |
| 950 | TEST_F(FormatTest, IncorrectCodeDoNoWhile) { |
| 951 | verifyFormat("do {\n" |
| 952 | "};"); |
| 953 | verifyFormat("do {\n" |
| 954 | "};\n" |
| 955 | "f();"); |
| 956 | verifyFormat("do {\n" |
| 957 | "}\n" |
| 958 | "wheeee(fun);"); |
| 959 | verifyFormat("do {\n" |
| 960 | " f();\n" |
| 961 | "};"); |
| 962 | } |
| 963 | |
Daniel Jasper | 1f42f11 | 2013-01-04 18:52:56 +0000 | [diff] [blame] | 964 | TEST_F(FormatTest, DoesNotTouchUnwrappedLinesWithErrors) { |
| 965 | verifyFormat("namespace {\n" |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 966 | "class Foo { Foo ( }; } // comment"); |
Daniel Jasper | 1f42f11 | 2013-01-04 18:52:56 +0000 | [diff] [blame] | 967 | } |
| 968 | |
Alexander Kornienko | cff563c | 2012-12-04 17:27:50 +0000 | [diff] [blame] | 969 | TEST_F(FormatTest, IncorrectCodeErrorDetection) { |
| 970 | EXPECT_EQ("{\n{\n}\n", format("{\n{\n}\n")); |
| 971 | EXPECT_EQ("{\n {\n}\n", format("{\n {\n}\n")); |
| 972 | EXPECT_EQ("{\n {\n }\n", format("{\n {\n }\n")); |
Alexander Kornienko | a3a2b3a | 2012-12-06 17:49:17 +0000 | [diff] [blame] | 973 | EXPECT_EQ("{\n {\n }\n }\n}\n", format("{\n {\n }\n }\n}\n")); |
Alexander Kornienko | cff563c | 2012-12-04 17:27:50 +0000 | [diff] [blame] | 974 | |
| 975 | FormatStyle Style = getLLVMStyle(); |
| 976 | Style.ColumnLimit = 10; |
| 977 | EXPECT_EQ("{\n" |
| 978 | " {\n" |
| 979 | " breakme(\n" |
| 980 | " qwe);\n" |
| 981 | "}\n", format("{\n" |
| 982 | " {\n" |
| 983 | " breakme(qwe);\n" |
| 984 | "}\n", Style)); |
| 985 | |
| 986 | } |
| 987 | |
Fariborz Jahanian | 154120c | 2012-12-20 19:54:13 +0000 | [diff] [blame] | 988 | TEST_F(FormatTest, FormatForObjectiveCMethodDecls) { |
| 989 | verifyFormat("- (void)sendAction:(SEL)aSelector to:(BOOL)anObject;"); |
| 990 | EXPECT_EQ("- (NSUInteger)indexOfObject:(id)anObject;", |
| 991 | format("-(NSUInteger)indexOfObject:(id)anObject;")); |
Daniel Jasper | cd16238 | 2013-01-07 13:26:07 +0000 | [diff] [blame] | 992 | EXPECT_EQ("- (NSInteger)Mthod1;", format("-(NSInteger)Mthod1;")); |
Fariborz Jahanian | 154120c | 2012-12-20 19:54:13 +0000 | [diff] [blame] | 993 | EXPECT_EQ("+ (id)Mthod2;", format("+(id)Mthod2;")); |
| 994 | EXPECT_EQ("- (NSInteger)Method3:(id)anObject;", |
| 995 | format("-(NSInteger)Method3:(id)anObject;")); |
| 996 | EXPECT_EQ("- (NSInteger)Method4:(id)anObject;", |
| 997 | format("-(NSInteger)Method4:(id)anObject;")); |
| 998 | EXPECT_EQ("- (NSInteger)Method5:(id)anObject:(id)AnotherObject;", |
| 999 | format("-(NSInteger)Method5:(id)anObject:(id)AnotherObject;")); |
| 1000 | EXPECT_EQ("- (id)Method6:(id)A:(id)B:(id)C:(id)D;", |
| 1001 | format("- (id)Method6:(id)A:(id)B:(id)C:(id)D;")); |
Daniel Jasper | cd16238 | 2013-01-07 13:26:07 +0000 | [diff] [blame] | 1002 | EXPECT_EQ( |
| 1003 | "- (void)sendAction:(SEL)aSelector to:(id)anObject forAllCells:(BOOL)flag;", |
| 1004 | format("- (void)sendAction:(SEL)aSelector to:(id)anObject forAllCells:(BOOL)flag;")); |
Fariborz Jahanian | 9b3f02c | 2012-12-21 22:51:18 +0000 | [diff] [blame] | 1005 | |
| 1006 | // Very long objectiveC method declaration. |
Daniel Jasper | cd16238 | 2013-01-07 13:26:07 +0000 | [diff] [blame] | 1007 | EXPECT_EQ( |
| 1008 | "- (NSUInteger)indexOfObject:(id)anObject inRange:(NSRange)range\n " |
| 1009 | "outRange:(NSRange)out_range outRange1:(NSRange)out_range1\n " |
| 1010 | "outRange2:(NSRange)out_range2 outRange3:(NSRange)out_range3\n " |
| 1011 | "outRange4:(NSRange)out_range4 outRange5:(NSRange)out_range5\n " |
| 1012 | "outRange6:(NSRange)out_range6 outRange7:(NSRange)out_range7\n " |
| 1013 | "outRange8:(NSRange)out_range8 outRange9:(NSRange)out_range9;", |
| 1014 | format( |
| 1015 | "- (NSUInteger)indexOfObject:(id)anObject inRange:(NSRange)range " |
| 1016 | "outRange:(NSRange) out_range outRange1:(NSRange) out_range1 " |
| 1017 | "outRange2:(NSRange) out_range2 outRange3:(NSRange) out_range3 " |
| 1018 | "outRange4:(NSRange) out_range4 outRange5:(NSRange) out_range5 " |
| 1019 | "outRange6:(NSRange) out_range6 outRange7:(NSRange) out_range7 " |
| 1020 | "outRange8:(NSRange) out_range8 outRange9:(NSRange) out_range9;")); |
Fariborz Jahanian | 154120c | 2012-12-20 19:54:13 +0000 | [diff] [blame] | 1021 | } |
| 1022 | |
Daniel Jasper | cd16238 | 2013-01-07 13:26:07 +0000 | [diff] [blame] | 1023 | } // end namespace tooling |
| 1024 | } // end namespace clang |