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; |
Nico Weber | efcfe73 | 2013-01-07 15:17:23 +0000 | [diff] [blame] | 32 | LangOpts.ObjC1 = 1; |
Nico Weber | 6092d4e | 2013-01-07 19:05:19 +0000 | [diff] [blame] | 33 | LangOpts.ObjC2 = 1; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 34 | Lexer Lex(ID, Context.Sources.getBuffer(ID), Context.Sources, LangOpts); |
Daniel Jasper | cd16238 | 2013-01-07 13:26:07 +0000 | [diff] [blame] | 35 | tooling::Replacements Replace = reformat(Style, Lex, Context.Sources, |
| 36 | Ranges); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 37 | EXPECT_TRUE(applyAllReplacements(Replace, Context.Rewrite)); |
| 38 | return Context.getRewrittenText(ID); |
| 39 | } |
| 40 | |
| 41 | std::string format(llvm::StringRef Code, |
| 42 | const FormatStyle &Style = getLLVMStyle()) { |
| 43 | return format(Code, 0, Code.size(), Style); |
| 44 | } |
| 45 | |
Daniel Jasper | e35ec2b | 2012-12-18 19:56:56 +0000 | [diff] [blame] | 46 | std::string messUp(llvm::StringRef Code) { |
| 47 | std::string MessedUp(Code.str()); |
| 48 | bool InComment = false; |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 49 | bool InPreprocessorDirective = false; |
Daniel Jasper | e35ec2b | 2012-12-18 19:56:56 +0000 | [diff] [blame] | 50 | bool JustReplacedNewline = false; |
| 51 | for (unsigned i = 0, e = MessedUp.size() - 1; i != e; ++i) { |
| 52 | if (MessedUp[i] == '/' && MessedUp[i + 1] == '/') { |
| 53 | if (JustReplacedNewline) |
| 54 | MessedUp[i - 1] = '\n'; |
| 55 | InComment = true; |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 56 | } else if (MessedUp[i] == '#' && JustReplacedNewline) { |
| 57 | MessedUp[i - 1] = '\n'; |
| 58 | InPreprocessorDirective = true; |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 59 | } else if (MessedUp[i] == '\\' && MessedUp[i + 1] == '\n') { |
| 60 | MessedUp[i] = ' '; |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 61 | MessedUp[i + 1] = ' '; |
Daniel Jasper | e35ec2b | 2012-12-18 19:56:56 +0000 | [diff] [blame] | 62 | } else if (MessedUp[i] == '\n') { |
| 63 | if (InComment) { |
| 64 | InComment = false; |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 65 | } else if (InPreprocessorDirective) { |
| 66 | InPreprocessorDirective = false; |
Daniel Jasper | e35ec2b | 2012-12-18 19:56:56 +0000 | [diff] [blame] | 67 | } else { |
| 68 | JustReplacedNewline = true; |
| 69 | MessedUp[i] = ' '; |
| 70 | } |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 71 | } else if (MessedUp[i] != ' ') { |
| 72 | JustReplacedNewline = false; |
Daniel Jasper | e35ec2b | 2012-12-18 19:56:56 +0000 | [diff] [blame] | 73 | } |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 74 | } |
Daniel Jasper | e35ec2b | 2012-12-18 19:56:56 +0000 | [diff] [blame] | 75 | return MessedUp; |
| 76 | } |
| 77 | |
Manuel Klimek | 060143e | 2013-01-02 18:33:23 +0000 | [diff] [blame] | 78 | FormatStyle getLLVMStyleWithColumns(unsigned ColumnLimit) { |
| 79 | FormatStyle Style = getLLVMStyle(); |
| 80 | Style.ColumnLimit = ColumnLimit; |
| 81 | return Style; |
| 82 | } |
| 83 | |
| 84 | void verifyFormat(llvm::StringRef Code, |
| 85 | const FormatStyle &Style = getLLVMStyle()) { |
| 86 | EXPECT_EQ(Code.str(), format(messUp(Code), Style)); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | void verifyGoogleFormat(llvm::StringRef Code) { |
Manuel Klimek | 060143e | 2013-01-02 18:33:23 +0000 | [diff] [blame] | 90 | verifyFormat(Code, getGoogleStyle()); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 91 | } |
| 92 | }; |
| 93 | |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 94 | TEST_F(FormatTest, MessUp) { |
| 95 | EXPECT_EQ("1 2 3", messUp("1 2 3")); |
| 96 | EXPECT_EQ("1 2 3\n", messUp("1\n2\n3\n")); |
| 97 | EXPECT_EQ("a\n//b\nc", messUp("a\n//b\nc")); |
| 98 | EXPECT_EQ("a\n#b\nc", messUp("a\n#b\nc")); |
| 99 | EXPECT_EQ("a\n#b c d\ne", messUp("a\n#b\\\nc\\\nd\ne")); |
| 100 | } |
| 101 | |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 102 | //===----------------------------------------------------------------------===// |
| 103 | // Basic function tests. |
| 104 | //===----------------------------------------------------------------------===// |
| 105 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 106 | TEST_F(FormatTest, DoesNotChangeCorrectlyFormatedCode) { |
| 107 | EXPECT_EQ(";", format(";")); |
| 108 | } |
| 109 | |
| 110 | TEST_F(FormatTest, FormatsGlobalStatementsAt0) { |
| 111 | EXPECT_EQ("int i;", format(" int i;")); |
| 112 | EXPECT_EQ("\nint i;", format(" \n\t \r int i;")); |
| 113 | EXPECT_EQ("int i;\nint j;", format(" int i; int j;")); |
| 114 | EXPECT_EQ("int i;\nint j;", format(" int i;\n int j;")); |
| 115 | } |
| 116 | |
| 117 | TEST_F(FormatTest, FormatsUnwrappedLinesAtFirstFormat) { |
| 118 | EXPECT_EQ("int i;", format("int\ni;")); |
| 119 | } |
| 120 | |
| 121 | TEST_F(FormatTest, FormatsNestedBlockStatements) { |
| 122 | EXPECT_EQ("{\n {\n {\n }\n }\n}", format("{{{}}}")); |
| 123 | } |
| 124 | |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 125 | TEST_F(FormatTest, FormatsNestedCall) { |
| 126 | verifyFormat("Method(f1, f2(f3));"); |
| 127 | verifyFormat("Method(f1(f2, f3()));"); |
| 128 | } |
| 129 | |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 130 | //===----------------------------------------------------------------------===// |
| 131 | // Tests for control statements. |
| 132 | //===----------------------------------------------------------------------===// |
| 133 | |
| 134 | TEST_F(FormatTest, FormatIfWithoutCompountStatement) { |
| 135 | verifyFormat("if (true)\n f();\ng();"); |
| 136 | verifyFormat("if (a)\n if (b)\n if (c)\n g();\nh();"); |
| 137 | verifyFormat("if (a)\n if (b) {\n f();\n }\ng();"); |
Daniel Jasper | e35ec2b | 2012-12-18 19:56:56 +0000 | [diff] [blame] | 138 | verifyFormat("if (a)\n" |
| 139 | " // comment\n" |
| 140 | " f();"); |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | TEST_F(FormatTest, ParseIfElse) { |
| 144 | verifyFormat("if (true)\n" |
| 145 | " if (true)\n" |
| 146 | " if (true)\n" |
| 147 | " f();\n" |
| 148 | " else\n" |
| 149 | " g();\n" |
| 150 | " else\n" |
| 151 | " h();\n" |
| 152 | "else\n" |
| 153 | " i();"); |
| 154 | verifyFormat("if (true)\n" |
| 155 | " if (true)\n" |
| 156 | " if (true) {\n" |
| 157 | " if (true)\n" |
| 158 | " f();\n" |
| 159 | " } else {\n" |
| 160 | " g();\n" |
| 161 | " }\n" |
| 162 | " else\n" |
| 163 | " h();\n" |
| 164 | "else {\n" |
| 165 | " i();\n" |
| 166 | "}"); |
| 167 | } |
| 168 | |
| 169 | TEST_F(FormatTest, ElseIf) { |
| 170 | verifyFormat("if (a) {\n" |
| 171 | "} else if (b) {\n" |
| 172 | "}"); |
| 173 | verifyFormat("if (a)\n" |
| 174 | " f();\n" |
| 175 | "else if (b)\n" |
| 176 | " g();\n" |
| 177 | "else\n" |
| 178 | " h();"); |
| 179 | } |
| 180 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 181 | TEST_F(FormatTest, FormatsForLoop) { |
| 182 | verifyFormat( |
| 183 | "for (int VeryVeryLongLoopVariable = 0; VeryVeryLongLoopVariable < 10;\n" |
Alexander Kornienko | 2e97cfc | 2012-12-05 15:06:06 +0000 | [diff] [blame] | 184 | " ++VeryVeryLongLoopVariable)\n" |
| 185 | " ;"); |
| 186 | verifyFormat("for (;;)\n" |
| 187 | " f();"); |
| 188 | verifyFormat("for (;;) {\n" |
| 189 | "}"); |
| 190 | verifyFormat("for (;;) {\n" |
| 191 | " f();\n" |
| 192 | "}"); |
Daniel Jasper | a324a0e | 2012-12-21 14:37:20 +0000 | [diff] [blame] | 193 | |
| 194 | verifyFormat( |
| 195 | "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n" |
| 196 | " E = UnwrappedLines.end();\n" |
| 197 | " I != E; ++I) {\n}"); |
| 198 | |
| 199 | verifyFormat( |
| 200 | "for (MachineFun::iterator IIII = PrevIt, EEEE = F.end(); IIII != EEEE;\n" |
| 201 | " ++IIIII) {\n}"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | TEST_F(FormatTest, FormatsWhileLoop) { |
| 205 | verifyFormat("while (true) {\n}"); |
Alexander Kornienko | 2e97cfc | 2012-12-05 15:06:06 +0000 | [diff] [blame] | 206 | verifyFormat("while (true)\n" |
| 207 | " f();"); |
| 208 | verifyFormat("while () {\n" |
| 209 | "}"); |
| 210 | verifyFormat("while () {\n" |
| 211 | " f();\n" |
| 212 | "}"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 213 | } |
| 214 | |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 215 | TEST_F(FormatTest, FormatsDoWhile) { |
| 216 | verifyFormat("do {\n" |
| 217 | " do_something();\n" |
| 218 | "} while (something());"); |
| 219 | verifyFormat("do\n" |
| 220 | " do_something();\n" |
| 221 | "while (something());"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 222 | } |
| 223 | |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 224 | TEST_F(FormatTest, FormatsSwitchStatement) { |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 225 | verifyFormat("switch (x) {\n" |
| 226 | "case 1:\n" |
| 227 | " f();\n" |
| 228 | " break;\n" |
| 229 | "case kFoo:\n" |
| 230 | "case ns::kBar:\n" |
| 231 | "case kBaz:\n" |
| 232 | " break;\n" |
| 233 | "default:\n" |
| 234 | " g();\n" |
| 235 | " break;\n" |
| 236 | "}"); |
| 237 | verifyFormat("switch (x) {\n" |
| 238 | "case 1: {\n" |
| 239 | " f();\n" |
| 240 | " break;\n" |
| 241 | "}\n" |
| 242 | "}"); |
| 243 | verifyFormat("switch (test)\n" |
| 244 | " ;"); |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 245 | verifyGoogleFormat("switch (x) {\n" |
| 246 | " case 1:\n" |
| 247 | " f();\n" |
| 248 | " break;\n" |
| 249 | " case kFoo:\n" |
| 250 | " case ns::kBar:\n" |
| 251 | " case kBaz:\n" |
| 252 | " break;\n" |
| 253 | " default:\n" |
| 254 | " g();\n" |
| 255 | " break;\n" |
| 256 | "}"); |
| 257 | verifyGoogleFormat("switch (x) {\n" |
| 258 | " case 1: {\n" |
| 259 | " f();\n" |
| 260 | " break;\n" |
| 261 | " }\n" |
| 262 | "}"); |
| 263 | verifyGoogleFormat("switch (test)\n" |
| 264 | " ;"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 265 | } |
| 266 | |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 267 | TEST_F(FormatTest, FormatsLabels) { |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 268 | verifyFormat("void f() {\n" |
| 269 | " some_code();\n" |
| 270 | "test_label:\n" |
| 271 | " some_other_code();\n" |
| 272 | " {\n" |
| 273 | " some_more_code();\n" |
| 274 | " another_label:\n" |
| 275 | " some_more_code();\n" |
| 276 | " }\n" |
| 277 | "}"); |
| 278 | verifyFormat("some_code();\n" |
| 279 | "test_label:\n" |
| 280 | "some_other_code();"); |
| 281 | } |
| 282 | |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 283 | //===----------------------------------------------------------------------===// |
| 284 | // Tests for comments. |
| 285 | //===----------------------------------------------------------------------===// |
| 286 | |
| 287 | TEST_F(FormatTest, UnderstandsSingleLineComments) { |
Daniel Jasper | e35ec2b | 2012-12-18 19:56:56 +0000 | [diff] [blame] | 288 | verifyFormat("// line 1\n" |
| 289 | "// line 2\n" |
| 290 | "void f() {\n}\n"); |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 291 | |
Daniel Jasper | e35ec2b | 2012-12-18 19:56:56 +0000 | [diff] [blame] | 292 | verifyFormat("void f() {\n" |
| 293 | " // Doesn't do anything\n" |
| 294 | "}"); |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 295 | |
Daniel Jasper | 7ad4eff | 2013-01-07 11:09:06 +0000 | [diff] [blame] | 296 | verifyFormat("int i // This is a fancy variable\n" |
Daniel Jasper | e35ec2b | 2012-12-18 19:56:56 +0000 | [diff] [blame] | 297 | " = 5;"); |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 298 | |
Daniel Jasper | e35ec2b | 2012-12-18 19:56:56 +0000 | [diff] [blame] | 299 | verifyFormat("enum E {\n" |
| 300 | " // comment\n" |
Daniel Jasper | 7ad4eff | 2013-01-07 11:09:06 +0000 | [diff] [blame] | 301 | " VAL_A, // comment\n" |
Daniel Jasper | e35ec2b | 2012-12-18 19:56:56 +0000 | [diff] [blame] | 302 | " VAL_B\n" |
| 303 | "};"); |
Daniel Jasper | 05b1ac8 | 2012-12-17 11:29:41 +0000 | [diff] [blame] | 304 | |
| 305 | verifyFormat( |
| 306 | "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" |
Daniel Jasper | 7ad4eff | 2013-01-07 11:09:06 +0000 | [diff] [blame] | 307 | " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; // Trailing comment"); |
Daniel Jasper | 043835a | 2013-01-09 09:33:39 +0000 | [diff] [blame] | 308 | verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" |
| 309 | " // Comment inside a statement.\n" |
| 310 | " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;"); |
Manuel Klimek | 6cf5814 | 2013-01-07 08:54:53 +0000 | [diff] [blame] | 311 | |
Daniel Jasper | 7ad4eff | 2013-01-07 11:09:06 +0000 | [diff] [blame] | 312 | EXPECT_EQ("int i; // single line trailing comment", |
Manuel Klimek | 6cf5814 | 2013-01-07 08:54:53 +0000 | [diff] [blame] | 313 | format("int i;\\\n// single line trailing comment")); |
Daniel Jasper | 7ad4eff | 2013-01-07 11:09:06 +0000 | [diff] [blame] | 314 | |
| 315 | verifyGoogleFormat("int a; // Trailing comment."); |
Daniel Jasper | 05b1ac8 | 2012-12-17 11:29:41 +0000 | [diff] [blame] | 316 | } |
| 317 | |
| 318 | TEST_F(FormatTest, UnderstandsMultiLineComments) { |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 319 | verifyFormat("f(/*test=*/ true);"); |
| 320 | } |
| 321 | |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 322 | //===----------------------------------------------------------------------===// |
| 323 | // Tests for classes, namespaces, etc. |
| 324 | //===----------------------------------------------------------------------===// |
| 325 | |
| 326 | TEST_F(FormatTest, DoesNotBreakSemiAfterClassDecl) { |
| 327 | verifyFormat("class A {\n};"); |
| 328 | } |
| 329 | |
| 330 | TEST_F(FormatTest, UnderstandsAccessSpecifiers) { |
| 331 | verifyFormat("class A {\n" |
| 332 | "public:\n" |
| 333 | "protected:\n" |
| 334 | "private:\n" |
| 335 | " void f() {\n" |
| 336 | " }\n" |
| 337 | "};"); |
| 338 | verifyGoogleFormat("class A {\n" |
| 339 | " public:\n" |
| 340 | " protected:\n" |
| 341 | " private:\n" |
| 342 | " void f() {\n" |
| 343 | " }\n" |
| 344 | "};"); |
| 345 | } |
| 346 | |
| 347 | TEST_F(FormatTest, FormatsDerivedClass) { |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 348 | verifyFormat("class A : public B {\n" |
| 349 | "};"); |
Daniel Jasper | c74e279 | 2012-12-07 09:52:15 +0000 | [diff] [blame] | 350 | verifyFormat("class A : public ::B {\n" |
| 351 | "};"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 352 | } |
| 353 | |
Manuel Klimek | de76854 | 2013-01-07 18:10:23 +0000 | [diff] [blame] | 354 | TEST_F(FormatTest, FormatsVariableDeclarationsAfterStructOrClass) { |
| 355 | verifyFormat("class A {\n" |
| 356 | "} a, b;"); |
| 357 | verifyFormat("struct A {\n" |
| 358 | "} a, b;"); |
| 359 | } |
| 360 | |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 361 | TEST_F(FormatTest, FormatsEnum) { |
Alexander Kornienko | a166e73 | 2012-12-04 14:46:19 +0000 | [diff] [blame] | 362 | verifyFormat("enum {\n" |
| 363 | " Zero,\n" |
| 364 | " One = 1,\n" |
| 365 | " Two = One + 1,\n" |
| 366 | " Three = (One + Two),\n" |
| 367 | " Four = (Zero && (One ^ Two)) | (One << Two),\n" |
| 368 | " Five = (One, Two, Three, Four, 5)\n" |
| 369 | "};"); |
| 370 | verifyFormat("enum Enum {\n" |
| 371 | "};"); |
| 372 | verifyFormat("enum {\n" |
| 373 | "};"); |
| 374 | } |
| 375 | |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 376 | TEST_F(FormatTest, FormatsNamespaces) { |
| 377 | verifyFormat("namespace some_namespace {\n" |
| 378 | "class A {\n" |
| 379 | "};\n" |
| 380 | "void f() {\n" |
| 381 | " f();\n" |
| 382 | "}\n" |
| 383 | "}"); |
| 384 | verifyFormat("namespace {\n" |
| 385 | "class A {\n" |
| 386 | "};\n" |
| 387 | "void f() {\n" |
| 388 | " f();\n" |
| 389 | "}\n" |
| 390 | "}"); |
Dmitri Gribenko | 1f94f2b | 2012-12-30 21:27:25 +0000 | [diff] [blame] | 391 | verifyFormat("inline namespace X {\n" |
| 392 | "class A {\n" |
| 393 | "};\n" |
| 394 | "void f() {\n" |
| 395 | " f();\n" |
| 396 | "}\n" |
| 397 | "}"); |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 398 | verifyFormat("using namespace some_namespace;\n" |
| 399 | "class A {\n" |
| 400 | "};\n" |
| 401 | "void f() {\n" |
| 402 | " f();\n" |
| 403 | "}"); |
| 404 | } |
| 405 | |
Nico Weber | a9ccdd1 | 2013-01-07 16:36:17 +0000 | [diff] [blame] | 406 | TEST_F(FormatTest, FormatTryCatch) { |
| 407 | verifyFormat("try {\n" |
| 408 | " throw a * b;\n" |
| 409 | "}\n" |
| 410 | "catch (int a) {\n" |
| 411 | " // Do nothing.\n" |
| 412 | "}\n" |
| 413 | "catch (...) {\n" |
| 414 | " exit(42);\n" |
| 415 | "}"); |
| 416 | |
| 417 | // Function-level try statements. |
| 418 | verifyFormat("int f() try {\n" |
| 419 | " return 4;\n" |
| 420 | "}\n" |
| 421 | "catch (...) {\n" |
| 422 | " return 5;\n" |
| 423 | "}"); |
| 424 | verifyFormat("class A {\n" |
| 425 | " int a;\n" |
| 426 | " A() try : a(0) {\n" |
| 427 | " }\n" |
| 428 | " catch (...) {\n" |
| 429 | " throw;\n" |
| 430 | " }\n" |
| 431 | "};\n"); |
| 432 | } |
| 433 | |
| 434 | TEST_F(FormatTest, FormatObjCTryCatch) { |
| 435 | verifyFormat("@try {\n" |
| 436 | " f();\n" |
| 437 | "}\n" |
| 438 | "@catch (NSException e) {\n" |
| 439 | " @throw;\n" |
| 440 | "}\n" |
| 441 | "@finally {\n" |
| 442 | " exit(42);\n" |
| 443 | "}"); |
| 444 | } |
| 445 | |
Daniel Jasper | 05b1ac8 | 2012-12-17 11:29:41 +0000 | [diff] [blame] | 446 | TEST_F(FormatTest, StaticInitializers) { |
| 447 | verifyFormat("static SomeClass SC = { 1, 'a' };"); |
| 448 | |
| 449 | // FIXME: Format like enums if the static initializer does not fit on a line. |
| 450 | verifyFormat( |
Daniel Jasper | 9cda800 | 2013-01-07 13:08:40 +0000 | [diff] [blame] | 451 | "static SomeClass WithALoooooooooooooooooooongName = {\n" |
| 452 | " 100000000, \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" };"); |
Daniel Jasper | 05b1ac8 | 2012-12-17 11:29:41 +0000 | [diff] [blame] | 453 | } |
| 454 | |
Manuel Klimek | a080a18 | 2013-01-02 16:30:12 +0000 | [diff] [blame] | 455 | TEST_F(FormatTest, FormatsSmallMacroDefinitionsInSingleLine) { |
| 456 | verifyFormat("#define ALooooooooooooooooooooooooooooooooooooooongMacro(" |
| 457 | " \\\n" |
| 458 | " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)"); |
| 459 | } |
| 460 | |
Daniel Jasper | 7160751 | 2013-01-07 10:48:50 +0000 | [diff] [blame] | 461 | TEST_F(FormatTest, DoesNotBreakPureVirtualFunctionDefinition) { |
Daniel Jasper | cd16238 | 2013-01-07 13:26:07 +0000 | [diff] [blame] | 462 | verifyFormat("virtual void write(ELFWriter *writerrr,\n" |
| 463 | " OwningPtr<FileOutputBuffer> &buffer) = 0;"); |
Daniel Jasper | 7160751 | 2013-01-07 10:48:50 +0000 | [diff] [blame] | 464 | } |
| 465 | |
Manuel Klimek | a080a18 | 2013-01-02 16:30:12 +0000 | [diff] [blame] | 466 | TEST_F(FormatTest, BreaksOnHashWhenDirectiveIsInvalid) { |
| 467 | EXPECT_EQ("#\n;", format("#;")); |
Manuel Klimek | dd5b101 | 2013-01-07 10:03:37 +0000 | [diff] [blame] | 468 | verifyFormat("#\n;\n;\n;"); |
Manuel Klimek | a080a18 | 2013-01-02 16:30:12 +0000 | [diff] [blame] | 469 | } |
| 470 | |
| 471 | TEST_F(FormatTest, UnescapedEndOfLineEndsPPDirective) { |
| 472 | EXPECT_EQ("#line 42 \"test\"\n", |
| 473 | format("# \\\n line \\\n 42 \\\n \"test\"\n")); |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 474 | EXPECT_EQ("#define A \\\n B\n", |
| 475 | format("# \\\n define \\\n A \\\n B\n", |
| 476 | getLLVMStyleWithColumns(12))); |
Manuel Klimek | a080a18 | 2013-01-02 16:30:12 +0000 | [diff] [blame] | 477 | } |
| 478 | |
| 479 | TEST_F(FormatTest, EndOfFileEndsPPDirective) { |
| 480 | EXPECT_EQ("#line 42 \"test\"", |
| 481 | format("# \\\n line \\\n 42 \\\n \"test\"")); |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 482 | EXPECT_EQ("#define A \\\n B", |
| 483 | format("# \\\n define \\\n A \\\n B", |
| 484 | getLLVMStyleWithColumns(12))); |
Manuel Klimek | a080a18 | 2013-01-02 16:30:12 +0000 | [diff] [blame] | 485 | } |
| 486 | |
Manuel Klimek | 060143e | 2013-01-02 18:33:23 +0000 | [diff] [blame] | 487 | TEST_F(FormatTest, IndentsPPDirectiveInReducedSpace) { |
Manuel Klimek | d544c57 | 2013-01-07 09:24:17 +0000 | [diff] [blame] | 488 | // If the macro fits in one line, we still do not get the full |
| 489 | // line, as only the next line decides whether we need an escaped newline and |
| 490 | // thus use the last column. |
| 491 | verifyFormat("#define A(B)", getLLVMStyleWithColumns(13)); |
Manuel Klimek | 060143e | 2013-01-02 18:33:23 +0000 | [diff] [blame] | 492 | |
Manuel Klimek | d544c57 | 2013-01-07 09:24:17 +0000 | [diff] [blame] | 493 | verifyFormat("#define A( \\\n B)", getLLVMStyleWithColumns(12)); |
| 494 | verifyFormat("#define AA(\\\n B)", getLLVMStyleWithColumns(12)); |
Manuel Klimek | 060143e | 2013-01-02 18:33:23 +0000 | [diff] [blame] | 495 | verifyFormat("#define A( \\\n A, B)", getLLVMStyleWithColumns(12)); |
| 496 | } |
| 497 | |
| 498 | TEST_F(FormatTest, HandlePreprocessorDirectiveContext) { |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 499 | EXPECT_EQ("// some comment\n" |
| 500 | "#include \"a.h\"\n" |
| 501 | "#define A(A,\\\n" |
| 502 | " B)\n" |
| 503 | "#include \"b.h\"\n" |
| 504 | "// some comment\n", |
| 505 | format(" // some comment\n" |
| 506 | " #include \"a.h\"\n" |
| 507 | "#define A(A,\\\n" |
| 508 | " B)\n" |
| 509 | " #include \"b.h\"\n" |
| 510 | " // some comment\n", getLLVMStyleWithColumns(13))); |
Manuel Klimek | 060143e | 2013-01-02 18:33:23 +0000 | [diff] [blame] | 511 | } |
| 512 | |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 513 | TEST_F(FormatTest, LayoutSingleHash) { |
| 514 | EXPECT_EQ("#\na;", format("#\na;")); |
| 515 | } |
| 516 | |
| 517 | TEST_F(FormatTest, LayoutCodeInMacroDefinitions) { |
| 518 | EXPECT_EQ("#define A \\\n" |
| 519 | " c; \\\n" |
| 520 | " e;\n" |
| 521 | "f;", format("#define A c; e;\n" |
| 522 | "f;", getLLVMStyleWithColumns(14))); |
| 523 | } |
| 524 | |
| 525 | TEST_F(FormatTest, LayoutRemainingTokens) { |
| 526 | EXPECT_EQ("{\n}", format("{}")); |
| 527 | } |
| 528 | |
| 529 | TEST_F(FormatTest, LayoutSingleUnwrappedLineInMacro) { |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 530 | EXPECT_EQ("# define A\\\n b;", |
| 531 | format("# define A b;", 11, 2, getLLVMStyleWithColumns(11))); |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 532 | } |
| 533 | |
| 534 | TEST_F(FormatTest, MacroDefinitionInsideStatement) { |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 535 | EXPECT_EQ("int x,\n" |
| 536 | "#define A\n" |
| 537 | " y;", format("int x,\n#define A\ny;")); |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 538 | } |
| 539 | |
Manuel Klimek | 6f8424b | 2013-01-05 21:34:55 +0000 | [diff] [blame] | 540 | TEST_F(FormatTest, HashInMacroDefinition) { |
| 541 | verifyFormat("#define A \\\n b #c;", getLLVMStyleWithColumns(11)); |
| 542 | verifyFormat("#define A \\\n" |
| 543 | " { \\\n" |
| 544 | " f(#c);\\\n" |
| 545 | " }", getLLVMStyleWithColumns(11)); |
Daniel Jasper | 765561f | 2013-01-08 16:17:54 +0000 | [diff] [blame] | 546 | |
| 547 | verifyFormat("#define A(X) \\\n" |
| 548 | " void function##X()", getLLVMStyleWithColumns(22)); |
| 549 | |
| 550 | verifyFormat("#define A(a, b, c) \\\n" |
| 551 | " void a##b##c()", getLLVMStyleWithColumns(22)); |
| 552 | |
| 553 | verifyFormat("#define A \\\n" |
| 554 | " void # ## #", getLLVMStyleWithColumns(22)); |
Manuel Klimek | 6f8424b | 2013-01-05 21:34:55 +0000 | [diff] [blame] | 555 | } |
| 556 | |
Manuel Klimek | c37b4d6 | 2013-01-05 22:14:16 +0000 | [diff] [blame] | 557 | TEST_F(FormatTest, IndentPreprocessorDirectivesAtZero) { |
| 558 | EXPECT_EQ("{\n {\n#define A\n }\n}", format("{{\n#define A\n}}")); |
| 559 | } |
| 560 | |
Manuel Klimek | f6fd00b | 2013-01-05 22:56:06 +0000 | [diff] [blame] | 561 | TEST_F(FormatTest, FormatHashIfNotAtStartOfLine) { |
| 562 | verifyFormat("{\n {\n a #c;\n }\n}"); |
| 563 | } |
| 564 | |
Manuel Klimek | a5342db | 2013-01-06 20:07:31 +0000 | [diff] [blame] | 565 | TEST_F(FormatTest, FormatUnbalancedStructuralElements) { |
| 566 | EXPECT_EQ("#define A \\\n { \\\n {\nint i;", |
| 567 | format("#define A { {\nint i;", getLLVMStyleWithColumns(11))); |
| 568 | EXPECT_EQ("#define A \\\n } \\\n }\nint i;", |
| 569 | format("#define A } }\nint i;", getLLVMStyleWithColumns(11))); |
| 570 | } |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 571 | |
Manuel Klimek | 9541938 | 2013-01-07 07:56:50 +0000 | [diff] [blame] | 572 | TEST_F(FormatTest, EscapedNewlineAtStartOfTokenInMacroDefinition) { |
Daniel Jasper | cd16238 | 2013-01-07 13:26:07 +0000 | [diff] [blame] | 573 | EXPECT_EQ( |
| 574 | "#define A \\\n int i; \\\n int j;", |
| 575 | format("#define A \\\nint i;\\\n int j;", getLLVMStyleWithColumns(11))); |
Manuel Klimek | 9541938 | 2013-01-07 07:56:50 +0000 | [diff] [blame] | 576 | } |
| 577 | |
Manuel Klimek | d544c57 | 2013-01-07 09:24:17 +0000 | [diff] [blame] | 578 | TEST_F(FormatTest, CalculateSpaceOnConsecutiveLinesInMacro) { |
| 579 | verifyFormat("#define A \\\n" |
| 580 | " int v( \\\n" |
| 581 | " a); \\\n" |
| 582 | " int i;", getLLVMStyleWithColumns(11)); |
| 583 | } |
| 584 | |
Manuel Klimek | a080a18 | 2013-01-02 16:30:12 +0000 | [diff] [blame] | 585 | TEST_F(FormatTest, MixingPreprocessorDirectivesAndNormalCode) { |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 586 | EXPECT_EQ( |
| 587 | "#define ALooooooooooooooooooooooooooooooooooooooongMacro(" |
| 588 | " \\\n" |
| 589 | " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n" |
| 590 | "\n" |
| 591 | "AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n" |
| 592 | " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n", |
| 593 | format(" #define ALooooooooooooooooooooooooooooooooooooooongMacro(" |
| 594 | "\\\n" |
| 595 | "aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n" |
| 596 | " \n" |
| 597 | " AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n" |
| 598 | " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n")); |
Manuel Klimek | a080a18 | 2013-01-02 16:30:12 +0000 | [diff] [blame] | 599 | } |
| 600 | |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 601 | TEST_F(FormatTest, LayoutStatementsAroundPreprocessorDirectives) { |
| 602 | EXPECT_EQ("int\n" |
| 603 | "#define A\n" |
| 604 | " a;", |
| 605 | format("int\n#define A\na;")); |
| 606 | verifyFormat( |
| 607 | "functionCallTo(someOtherFunction(\n" |
| 608 | " withSomeParameters, whichInSequence,\n" |
| 609 | " areLongerThanALine(andAnotherCall,\n" |
| 610 | "#define A \\\n" |
| 611 | " B\n" |
| 612 | " withMoreParamters,\n" |
| 613 | " whichStronglyInfluenceTheLayout),\n" |
| 614 | " andMoreParameters),\n" |
| 615 | " trailing);", getLLVMStyleWithColumns(69)); |
| 616 | } |
| 617 | |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 618 | //===----------------------------------------------------------------------===// |
| 619 | // Line break tests. |
| 620 | //===----------------------------------------------------------------------===// |
| 621 | |
| 622 | TEST_F(FormatTest, FormatsFunctionDefinition) { |
| 623 | verifyFormat("void f(int a, int b, int c, int d, int e, int f, int g," |
| 624 | " int h, int j, int f,\n" |
| 625 | " int c, int ddddddddddddd) {\n" |
| 626 | "}"); |
| 627 | } |
| 628 | |
| 629 | TEST_F(FormatTest, FormatsAwesomeMethodCall) { |
| 630 | verifyFormat( |
Daniel Jasper | a4974cf | 2012-12-24 16:43:00 +0000 | [diff] [blame] | 631 | "SomeLongMethodName(SomeReallyLongMethod(\n" |
| 632 | " CallOtherReallyLongMethod(parameter, parameter, parameter)),\n" |
| 633 | " SecondLongCall(parameter));"); |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 634 | } |
| 635 | |
Daniel Jasper | 1321eb5 | 2012-12-18 21:05:13 +0000 | [diff] [blame] | 636 | TEST_F(FormatTest, ConstructorInitializers) { |
| 637 | verifyFormat("Constructor() : Initializer(FitsOnTheLine) {\n}"); |
| 638 | |
| 639 | verifyFormat( |
| 640 | "SomeClass::Constructor()\n" |
| 641 | " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {\n" |
| 642 | "}"); |
| 643 | |
| 644 | verifyFormat( |
| 645 | "SomeClass::Constructor()\n" |
| 646 | " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" |
| 647 | " aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {\n" |
| 648 | "}"); |
| 649 | |
| 650 | verifyFormat("Constructor()\n" |
| 651 | " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" |
| 652 | " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" |
| 653 | " aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" |
| 654 | " aaaaaaaaaaaaaaaaaaaaaaa() {\n" |
| 655 | "}"); |
| 656 | |
| 657 | // Here a line could be saved by splitting the second initializer onto two |
| 658 | // lines, but that is not desireable. |
| 659 | verifyFormat("Constructor()\n" |
| 660 | " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n" |
| 661 | " aaaaaaaaaaa(aaaaaaaaaaa),\n" |
| 662 | " aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" |
| 663 | "}"); |
| 664 | |
| 665 | verifyGoogleFormat("MyClass::MyClass(int var)\n" |
| 666 | " : some_var_(var), // 4 space indent\n" |
| 667 | " some_other_var_(var + 1) { // lined up\n" |
| 668 | "}"); |
| 669 | } |
| 670 | |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 671 | TEST_F(FormatTest, BreaksAsHighAsPossible) { |
| 672 | verifyFormat( |
| 673 | "if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa && aaaaaaaaaaaaaaaaaaaaaaaaaa) ||\n" |
| 674 | " (bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb && bbbbbbbbbbbbbbbbbbbbbbbbbb))\n" |
| 675 | " f();"); |
| 676 | } |
| 677 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 678 | TEST_F(FormatTest, BreaksDesireably) { |
| 679 | verifyFormat("if (aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n" |
| 680 | " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n" |
Manuel Klimek | de76854 | 2013-01-07 18:10:23 +0000 | [diff] [blame] | 681 | " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa)) {\n}"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 682 | |
| 683 | verifyFormat( |
| 684 | "aaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" |
| 685 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); |
| 686 | |
| 687 | verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" |
| 688 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" |
| 689 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));"); |
Daniel Jasper | a88bb45 | 2012-12-04 10:50:12 +0000 | [diff] [blame] | 690 | |
| 691 | verifyFormat( |
| 692 | "aaaaaaaa(aaaaaaaaaaaaa, aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" |
| 693 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n" |
| 694 | " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" |
| 695 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));"); |
Daniel Jasper | 33182dd | 2012-12-05 14:57:28 +0000 | [diff] [blame] | 696 | |
| 697 | verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" |
| 698 | " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); |
| 699 | |
Daniel Jasper | 723f030 | 2013-01-02 14:40:02 +0000 | [diff] [blame] | 700 | verifyFormat( |
| 701 | "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&\n" |
| 702 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); |
| 703 | |
Daniel Jasper | 33182dd | 2012-12-05 14:57:28 +0000 | [diff] [blame] | 704 | // This test case breaks on an incorrect memoization, i.e. an optimization not |
| 705 | // taking into account the StopAt value. |
| 706 | verifyFormat( |
| 707 | "return aaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n" |
Daniel Jasper | cf225b6 | 2012-12-24 13:43:52 +0000 | [diff] [blame] | 708 | " aaaaaaaaaaa(aaaaaaaaa) || aaaaaaaaaaaaaaaaaaaaaaa ||\n" |
| 709 | " aaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n" |
| 710 | " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); |
Daniel Jasper | a4974cf | 2012-12-24 16:43:00 +0000 | [diff] [blame] | 711 | |
Daniel Jasper | cd16238 | 2013-01-07 13:26:07 +0000 | [diff] [blame] | 712 | verifyFormat("{\n {\n {\n" |
| 713 | " Annotation.SpaceRequiredBefore =\n" |
| 714 | " Line.Tokens[i - 1].Tok.isNot(tok::l_paren) &&\n" |
| 715 | " Line.Tokens[i - 1].Tok.isNot(tok::l_square);\n" |
| 716 | " }\n }\n}"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 717 | } |
| 718 | |
Daniel Jasper | e2c7acf | 2012-12-24 00:13:23 +0000 | [diff] [blame] | 719 | TEST_F(FormatTest, BreaksAccordingToOperatorPrecedence) { |
| 720 | verifyFormat( |
| 721 | "if (aaaaaaaaaaaaaaaaaaaaaaaaa ||\n" |
| 722 | " bbbbbbbbbbbbbbbbbbbbbbbbb && ccccccccccccccccccccccccc) {\n}"); |
Daniel Jasper | cd16238 | 2013-01-07 13:26:07 +0000 | [diff] [blame] | 723 | verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa && bbbbbbbbbbbbbbbbbbbbbbbbb ||\n" |
| 724 | " ccccccccccccccccccccccccc) {\n}"); |
| 725 | verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb ||\n" |
| 726 | " ccccccccccccccccccccccccc) {\n}"); |
Daniel Jasper | e2c7acf | 2012-12-24 00:13:23 +0000 | [diff] [blame] | 727 | verifyFormat( |
| 728 | "if ((aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb) &&\n" |
| 729 | " ccccccccccccccccccccccccc) {\n}"); |
| 730 | } |
| 731 | |
Daniel Jasper | 9cda800 | 2013-01-07 13:08:40 +0000 | [diff] [blame] | 732 | TEST_F(FormatTest, PrefersNotToBreakAfterAssignments) { |
| 733 | verifyFormat( |
| 734 | "unsigned Cost = TTI.getMemoryOpCost(I->getOpcode(), VectorTy,\n" |
| 735 | " SI->getAlignment(),\n" |
| 736 | " SI->getPointerAddressSpaceee());\n"); |
| 737 | verifyFormat( |
| 738 | "CharSourceRange LineRange = CharSourceRange::getTokenRange(\n" |
| 739 | " Line.Tokens.front().Tok.getLocation(),\n" |
| 740 | " Line.Tokens.back().Tok.getLocation());"); |
| 741 | } |
| 742 | |
Daniel Jasper | cf225b6 | 2012-12-24 13:43:52 +0000 | [diff] [blame] | 743 | TEST_F(FormatTest, AlignsAfterAssignments) { |
| 744 | verifyFormat( |
| 745 | "int Result = aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" |
Daniel Jasper | 9cda800 | 2013-01-07 13:08:40 +0000 | [diff] [blame] | 746 | " aaaaaaaaaaaaaaaaaaaaaaaaa;"); |
Daniel Jasper | cf225b6 | 2012-12-24 13:43:52 +0000 | [diff] [blame] | 747 | verifyFormat( |
| 748 | "Result += aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" |
Daniel Jasper | 9cda800 | 2013-01-07 13:08:40 +0000 | [diff] [blame] | 749 | " aaaaaaaaaaaaaaaaaaaaaaaaa;"); |
Daniel Jasper | cf225b6 | 2012-12-24 13:43:52 +0000 | [diff] [blame] | 750 | verifyFormat( |
| 751 | "Result >>= aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" |
Daniel Jasper | 9cda800 | 2013-01-07 13:08:40 +0000 | [diff] [blame] | 752 | " aaaaaaaaaaaaaaaaaaaaaaaaa;"); |
Daniel Jasper | cf225b6 | 2012-12-24 13:43:52 +0000 | [diff] [blame] | 753 | verifyFormat( |
| 754 | "int Result = (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" |
Daniel Jasper | 9cda800 | 2013-01-07 13:08:40 +0000 | [diff] [blame] | 755 | " aaaaaaaaaaaaaaaaaaaaaaaaa);"); |
Daniel Jasper | cf225b6 | 2012-12-24 13:43:52 +0000 | [diff] [blame] | 756 | verifyFormat( |
Daniel Jasper | 9cda800 | 2013-01-07 13:08:40 +0000 | [diff] [blame] | 757 | "double LooooooooooooooooooooooooongResult = aaaaaaaaaaaaaaaaaaaaaaaa +\n" |
| 758 | " aaaaaaaaaaaaaaaaaaaaaaaa +\n" |
| 759 | " aaaaaaaaaaaaaaaaaaaaaaaa;"); |
Daniel Jasper | cf225b6 | 2012-12-24 13:43:52 +0000 | [diff] [blame] | 760 | } |
| 761 | |
| 762 | TEST_F(FormatTest, AlignsAfterReturn) { |
| 763 | verifyFormat( |
| 764 | "return aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" |
| 765 | " aaaaaaaaaaaaaaaaaaaaaaaaa;"); |
| 766 | verifyFormat( |
| 767 | "return (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" |
| 768 | " aaaaaaaaaaaaaaaaaaaaaaaaa);"); |
| 769 | } |
| 770 | |
Daniel Jasper | 9c837d0 | 2013-01-09 07:06:56 +0000 | [diff] [blame] | 771 | TEST_F(FormatTest, BreaksConditionalExpressions) { |
| 772 | verifyFormat( |
| 773 | "aaaa(aaaaaaaaaaaaaaaaaaaa,\n" |
| 774 | " aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" |
| 775 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); |
| 776 | verifyFormat("aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" |
| 777 | " aaaaaaaaaaaaaaaaaaaaaaa : aaaaaaaaaaaaaaaaaaaaa);"); |
| 778 | } |
| 779 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 780 | TEST_F(FormatTest, AlignsStringLiterals) { |
| 781 | verifyFormat("loooooooooooooooooooooooooongFunction(\"short literal \"\n" |
| 782 | " \"short literal\");"); |
| 783 | verifyFormat( |
| 784 | "looooooooooooooooooooooooongFunction(\n" |
| 785 | " \"short literal\"\n" |
| 786 | " \"looooooooooooooooooooooooooooooooooooooooooooooooong literal\");"); |
| 787 | } |
| 788 | |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 789 | TEST_F(FormatTest, AlignsPipes) { |
| 790 | verifyFormat( |
| 791 | "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" |
| 792 | " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" |
| 793 | " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); |
| 794 | verifyFormat( |
| 795 | "aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa\n" |
| 796 | " << aaaaaaaaaaaaaaaaaaaa;"); |
| 797 | verifyFormat( |
| 798 | "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" |
| 799 | " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); |
| 800 | verifyFormat( |
| 801 | "llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n" |
| 802 | " \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\"\n" |
| 803 | " << \"ccccccccccccccccccccccccccccccccccccccccccccccccc\";"); |
| 804 | verifyFormat( |
| 805 | "aaaaaaaa << (aaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" |
| 806 | " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" |
| 807 | " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); |
| 808 | } |
| 809 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 810 | TEST_F(FormatTest, UnderstandsEquals) { |
| 811 | verifyFormat( |
| 812 | "aaaaaaaaaaaaaaaaa =\n" |
| 813 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); |
| 814 | verifyFormat( |
| 815 | "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" |
| 816 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" |
| 817 | "}"); |
| 818 | verifyFormat( |
| 819 | "if (a) {\n" |
| 820 | "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" |
| 821 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" |
| 822 | "}"); |
| 823 | |
Daniel Jasper | 9cda800 | 2013-01-07 13:08:40 +0000 | [diff] [blame] | 824 | verifyFormat( |
Daniel Jasper | 3fc0bb7 | 2013-01-09 10:40:23 +0000 | [diff] [blame] | 825 | // FIXME: Does an expression like this ever make sense? If yes, fix. |
Daniel Jasper | 9cda800 | 2013-01-07 13:08:40 +0000 | [diff] [blame] | 826 | "if (int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = 100000000 +\n" |
Daniel Jasper | 3fc0bb7 | 2013-01-09 10:40:23 +0000 | [diff] [blame] | 827 | " 10000000) {\n" |
Daniel Jasper | 9cda800 | 2013-01-07 13:08:40 +0000 | [diff] [blame] | 828 | "}"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 829 | } |
| 830 | |
Daniel Jasper | 9a0b494 | 2012-12-17 14:34:14 +0000 | [diff] [blame] | 831 | TEST_F(FormatTest, WrapsAtFunctionCallsIfNecessary) { |
Daniel Jasper | 1321eb5 | 2012-12-18 21:05:13 +0000 | [diff] [blame] | 832 | verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n" |
| 833 | " .looooooooooooooooooooooooooooooooooooooongFunction();"); |
Daniel Jasper | 9a0b494 | 2012-12-17 14:34:14 +0000 | [diff] [blame] | 834 | |
Daniel Jasper | 1321eb5 | 2012-12-18 21:05:13 +0000 | [diff] [blame] | 835 | verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n" |
| 836 | " ->looooooooooooooooooooooooooooooooooooooongFunction();"); |
Daniel Jasper | 9a0b494 | 2012-12-17 14:34:14 +0000 | [diff] [blame] | 837 | |
| 838 | verifyFormat( |
| 839 | "LooooooooooooooooooooooooooooooooongObject->shortFunction(Parameter1,\n" |
| 840 | " Parameter2);"); |
| 841 | |
| 842 | verifyFormat( |
| 843 | "ShortObject->shortFunction(\n" |
| 844 | " LooooooooooooooooooooooooooooooooooooooooooooooongParameter1,\n" |
| 845 | " LooooooooooooooooooooooooooooooooooooooooooooooongParameter2);"); |
| 846 | |
| 847 | verifyFormat("loooooooooooooongFunction(\n" |
| 848 | " LoooooooooooooongObject->looooooooooooooooongFunction());"); |
| 849 | |
| 850 | verifyFormat( |
| 851 | "function(LoooooooooooooooooooooooooooooooooooongObject\n" |
| 852 | " ->loooooooooooooooooooooooooooooooooooooooongFunction());"); |
| 853 | |
Daniel Jasper | 46a46a2 | 2013-01-07 07:13:20 +0000 | [diff] [blame] | 854 | // Here, it is not necessary to wrap at "." or "->". |
Daniel Jasper | 1321eb5 | 2012-12-18 21:05:13 +0000 | [diff] [blame] | 855 | verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaa) ||\n" |
| 856 | " aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" |
| 857 | "}"); |
Daniel Jasper | 46a46a2 | 2013-01-07 07:13:20 +0000 | [diff] [blame] | 858 | verifyFormat( |
| 859 | "aaaaaaaaaaa->aaaaaaaaa(\n" |
| 860 | " aaaaaaaaaaaaaaaaaaaaaaaaa,\n" |
| 861 | " aaaaaaaaaaaaaaaaaa->aaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa));\n"); |
Daniel Jasper | 9a0b494 | 2012-12-17 14:34:14 +0000 | [diff] [blame] | 862 | } |
| 863 | |
Daniel Jasper | 9a64fb5 | 2013-01-02 15:08:56 +0000 | [diff] [blame] | 864 | TEST_F(FormatTest, WrapsTemplateDeclarations) { |
| 865 | verifyFormat("template <typename T>\n" |
| 866 | "virtual void loooooooooooongFunction(int Param1, int Param2);"); |
| 867 | verifyFormat( |
| 868 | "template <typename T> void f(int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram1,\n" |
| 869 | " int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram2);"); |
| 870 | verifyFormat( |
| 871 | "template <typename T>\n" |
| 872 | "void looooooooooooooooooooongFunction(int Paaaaaaaaaaaaaaaaaaaaram1,\n" |
| 873 | " int Paaaaaaaaaaaaaaaaaaaaram2);"); |
Daniel Jasper | 5eda31e | 2013-01-02 18:30:06 +0000 | [diff] [blame] | 874 | verifyFormat( |
| 875 | "template <typename T>\n" |
| 876 | "aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa,\n" |
| 877 | " aaaaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaa,\n" |
| 878 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); |
Daniel Jasper | d64f738 | 2013-01-09 09:50:48 +0000 | [diff] [blame] | 879 | verifyFormat("template <typename T>\n" |
| 880 | "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" |
| 881 | " int aaaaaaaaaaaaaaaaa);"); |
Daniel Jasper | 3fc0bb7 | 2013-01-09 10:40:23 +0000 | [diff] [blame] | 882 | verifyFormat( |
| 883 | "template <typename T1, typename T2 = char, typename T3 = char,\n" |
| 884 | " typename T4 = char>\n" |
| 885 | "void f();"); |
Daniel Jasper | 9a64fb5 | 2013-01-02 15:08:56 +0000 | [diff] [blame] | 886 | } |
| 887 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 888 | TEST_F(FormatTest, UnderstandsTemplateParameters) { |
| 889 | verifyFormat("A<int> a;"); |
| 890 | verifyFormat("A<A<A<int> > > a;"); |
| 891 | verifyFormat("A<A<A<int, 2>, 3>, 4> a;"); |
| 892 | verifyFormat("bool x = a < 1 || 2 > a;"); |
| 893 | verifyFormat("bool x = 5 < f<int>();"); |
| 894 | verifyFormat("bool x = f<int>() > 5;"); |
| 895 | verifyFormat("bool x = 5 < a<int>::x;"); |
| 896 | verifyFormat("bool x = a < 4 ? a > 2 : false;"); |
| 897 | verifyFormat("bool x = f() ? a < 2 : a > 2;"); |
| 898 | |
| 899 | verifyGoogleFormat("A<A<int>> a;"); |
| 900 | verifyGoogleFormat("A<A<A<int>>> a;"); |
| 901 | verifyGoogleFormat("A<A<A<A<int>>>> a;"); |
| 902 | |
| 903 | verifyFormat("test >> a >> b;"); |
| 904 | verifyFormat("test << a >> b;"); |
| 905 | |
| 906 | verifyFormat("f<int>();"); |
| 907 | verifyFormat("template <typename T> void f() {\n}"); |
| 908 | } |
| 909 | |
Daniel Jasper | 98e6b4a | 2012-12-21 09:41:31 +0000 | [diff] [blame] | 910 | TEST_F(FormatTest, UnderstandsUnaryOperators) { |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 911 | verifyFormat("int a = -2;"); |
Daniel Jasper | 8822d3a | 2012-12-04 13:02:32 +0000 | [diff] [blame] | 912 | verifyFormat("f(-1, -2, -3);"); |
| 913 | verifyFormat("a[-1] = 5;"); |
| 914 | verifyFormat("int a = 5 + -2;"); |
Daniel Jasper | 112fb27 | 2012-12-05 07:51:39 +0000 | [diff] [blame] | 915 | verifyFormat("if (i == -1) {\n}"); |
| 916 | verifyFormat("if (i != -1) {\n}"); |
| 917 | verifyFormat("if (i > -1) {\n}"); |
| 918 | verifyFormat("if (i < -1) {\n}"); |
Daniel Jasper | d56a737 | 2012-12-06 13:16:39 +0000 | [diff] [blame] | 919 | verifyFormat("++(a->f());"); |
| 920 | verifyFormat("--(a->f());"); |
| 921 | verifyFormat("if (!(a->f())) {\n}"); |
Daniel Jasper | 98e6b4a | 2012-12-21 09:41:31 +0000 | [diff] [blame] | 922 | |
| 923 | verifyFormat("a-- > b;"); |
| 924 | verifyFormat("b ? -a : c;"); |
| 925 | verifyFormat("n * sizeof char16;"); |
Daniel Jasper | ba3d307 | 2013-01-02 17:21:36 +0000 | [diff] [blame] | 926 | verifyFormat("n * alignof char16;"); |
Daniel Jasper | 98e6b4a | 2012-12-21 09:41:31 +0000 | [diff] [blame] | 927 | verifyFormat("sizeof(char);"); |
Daniel Jasper | ba3d307 | 2013-01-02 17:21:36 +0000 | [diff] [blame] | 928 | verifyFormat("alignof(char);"); |
Daniel Jasper | 1f0754b | 2013-01-02 15:26:16 +0000 | [diff] [blame] | 929 | |
| 930 | verifyFormat("return -1;"); |
| 931 | verifyFormat("switch (a) {\n" |
| 932 | "case -1:\n" |
| 933 | " break;\n" |
| 934 | "}"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 935 | } |
| 936 | |
| 937 | TEST_F(FormatTest, UndestandsOverloadedOperators) { |
Daniel Jasper | f6aef6a | 2012-12-24 10:56:04 +0000 | [diff] [blame] | 938 | verifyFormat("bool operator<();"); |
| 939 | verifyFormat("bool operator>();"); |
| 940 | verifyFormat("bool operator=();"); |
| 941 | verifyFormat("bool operator==();"); |
| 942 | verifyFormat("bool operator!=();"); |
| 943 | verifyFormat("int operator+();"); |
| 944 | verifyFormat("int operator++();"); |
| 945 | verifyFormat("bool operator();"); |
| 946 | verifyFormat("bool operator()();"); |
| 947 | verifyFormat("bool operator[]();"); |
| 948 | verifyFormat("operator bool();"); |
| 949 | verifyFormat("operator SomeType<int>();"); |
| 950 | verifyFormat("void *operator new(std::size_t size);"); |
| 951 | verifyFormat("void *operator new[](std::size_t size);"); |
| 952 | verifyFormat("void operator delete(void *ptr);"); |
| 953 | verifyFormat("void operator delete[](void *ptr);"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 954 | } |
| 955 | |
Daniel Jasper | 5d33440 | 2013-01-02 08:57:10 +0000 | [diff] [blame] | 956 | TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) { |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 957 | verifyFormat("int *f(int *a) {\n}"); |
| 958 | verifyFormat("f(a, *a);"); |
| 959 | verifyFormat("f(*a);"); |
| 960 | verifyFormat("int a = b * 10;"); |
| 961 | verifyFormat("int a = 10 * b;"); |
Daniel Jasper | 112fb27 | 2012-12-05 07:51:39 +0000 | [diff] [blame] | 962 | verifyFormat("int a = b * c;"); |
Daniel Jasper | 33182dd | 2012-12-05 14:57:28 +0000 | [diff] [blame] | 963 | verifyFormat("int a += b * c;"); |
| 964 | verifyFormat("int a -= b * c;"); |
| 965 | verifyFormat("int a *= b * c;"); |
| 966 | verifyFormat("int a /= b * c;"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 967 | verifyFormat("int a = *b;"); |
Daniel Jasper | 112fb27 | 2012-12-05 07:51:39 +0000 | [diff] [blame] | 968 | verifyFormat("int a = *b * c;"); |
| 969 | verifyFormat("int a = b * *c;"); |
Daniel Jasper | c74e279 | 2012-12-07 09:52:15 +0000 | [diff] [blame] | 970 | verifyFormat("int main(int argc, char **argv) {\n}"); |
Nico Weber | 00d5a04 | 2012-12-23 01:07:46 +0000 | [diff] [blame] | 971 | verifyFormat("return 10 * b;"); |
| 972 | verifyFormat("return *b * *c;"); |
| 973 | verifyFormat("return a & ~b;"); |
Daniel Jasper | 5d33440 | 2013-01-02 08:57:10 +0000 | [diff] [blame] | 974 | verifyFormat("f(b ? *c : *d);"); |
| 975 | verifyFormat("int a = b ? *c : *d;"); |
Daniel Jasper | ba3d307 | 2013-01-02 17:21:36 +0000 | [diff] [blame] | 976 | verifyFormat("*b = a;"); |
| 977 | verifyFormat("a * ~b;"); |
| 978 | verifyFormat("a * !b;"); |
| 979 | verifyFormat("a * +b;"); |
| 980 | verifyFormat("a * -b;"); |
| 981 | verifyFormat("a * ++b;"); |
| 982 | verifyFormat("a * --b;"); |
Daniel Jasper | c74e279 | 2012-12-07 09:52:15 +0000 | [diff] [blame] | 983 | |
Daniel Jasper | 9bb0d28 | 2013-01-04 20:46:38 +0000 | [diff] [blame] | 984 | verifyFormat("InvalidRegions[*R] = 0;"); |
| 985 | |
Daniel Jasper | 8b39c66 | 2012-12-10 18:59:13 +0000 | [diff] [blame] | 986 | verifyFormat("A<int *> a;"); |
| 987 | verifyFormat("A<int **> a;"); |
| 988 | verifyFormat("A<int *, int *> a;"); |
| 989 | verifyFormat("A<int **, int **> a;"); |
Daniel Jasper | ef5b9c3 | 2013-01-02 15:46:59 +0000 | [diff] [blame] | 990 | verifyFormat("Type *A = static_cast<Type *>(P);"); |
| 991 | verifyFormat("Type *A = (Type *) P;"); |
| 992 | verifyFormat("Type *A = (vector<Type *, int *>) P;"); |
Daniel Jasper | 8b39c66 | 2012-12-10 18:59:13 +0000 | [diff] [blame] | 993 | |
Daniel Jasper | 2db356d | 2013-01-08 20:03:18 +0000 | [diff] [blame] | 994 | verifyFormat( |
| 995 | "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" |
| 996 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaa, *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); |
| 997 | |
Daniel Jasper | c74e279 | 2012-12-07 09:52:15 +0000 | [diff] [blame] | 998 | verifyGoogleFormat("int main(int argc, char** argv) {\n}"); |
Daniel Jasper | 8b39c66 | 2012-12-10 18:59:13 +0000 | [diff] [blame] | 999 | verifyGoogleFormat("A<int*> a;"); |
| 1000 | verifyGoogleFormat("A<int**> a;"); |
| 1001 | verifyGoogleFormat("A<int*, int*> a;"); |
| 1002 | verifyGoogleFormat("A<int**, int**> a;"); |
Daniel Jasper | 5d33440 | 2013-01-02 08:57:10 +0000 | [diff] [blame] | 1003 | verifyGoogleFormat("f(b ? *c : *d);"); |
| 1004 | verifyGoogleFormat("int a = b ? *c : *d;"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1005 | } |
| 1006 | |
Daniel Jasper | 4dc41de | 2013-01-02 08:44:14 +0000 | [diff] [blame] | 1007 | TEST_F(FormatTest, DoesNotBreakBeforePointerOrReference) { |
Daniel Jasper | cd16238 | 2013-01-07 13:26:07 +0000 | [diff] [blame] | 1008 | verifyFormat("int *someFunction(int LoooooooooooooooongParam1,\n" |
| 1009 | " int LoooooooooooooooongParam2) {\n}"); |
Daniel Jasper | 4dc41de | 2013-01-02 08:44:14 +0000 | [diff] [blame] | 1010 | verifyFormat( |
| 1011 | "TypeSpecDecl *TypeSpecDecl::Create(ASTContext &C, DeclContext *DC,\n" |
| 1012 | " SourceLocation L, IdentifierIn *II,\n" |
| 1013 | " Type *T) {\n}"); |
| 1014 | } |
| 1015 | |
Daniel Jasper | 3b5943f | 2012-12-06 09:56:08 +0000 | [diff] [blame] | 1016 | TEST_F(FormatTest, LineStartsWithSpecialCharacter) { |
| 1017 | verifyFormat("(a)->b();"); |
| 1018 | verifyFormat("--a;"); |
| 1019 | } |
| 1020 | |
Daniel Jasper | 8822d3a | 2012-12-04 13:02:32 +0000 | [diff] [blame] | 1021 | TEST_F(FormatTest, HandlesIncludeDirectives) { |
| 1022 | EXPECT_EQ("#include <string>\n", format("#include <string>\n")); |
Daniel Jasper | cd1a32b | 2012-12-21 17:58:39 +0000 | [diff] [blame] | 1023 | 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] | 1024 | EXPECT_EQ("#include \"a/b/string\"\n", format("#include \"a/b/string\"\n")); |
| 1025 | EXPECT_EQ("#include \"string.h\"\n", format("#include \"string.h\"\n")); |
| 1026 | EXPECT_EQ("#include \"string.h\"\n", format("#include \"string.h\"\n")); |
Nico Weber | b23ae0c | 2012-12-21 18:21:56 +0000 | [diff] [blame] | 1027 | |
| 1028 | EXPECT_EQ("#import <string>\n", format("#import <string>\n")); |
| 1029 | EXPECT_EQ("#import <a/b/c.h>\n", format("#import <a/b/c.h>\n")); |
| 1030 | EXPECT_EQ("#import \"a/b/string\"\n", format("#import \"a/b/string\"\n")); |
| 1031 | EXPECT_EQ("#import \"string.h\"\n", format("#import \"string.h\"\n")); |
| 1032 | EXPECT_EQ("#import \"string.h\"\n", format("#import \"string.h\"\n")); |
Daniel Jasper | 8822d3a | 2012-12-04 13:02:32 +0000 | [diff] [blame] | 1033 | } |
| 1034 | |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 1035 | //===----------------------------------------------------------------------===// |
| 1036 | // Error recovery tests. |
| 1037 | //===----------------------------------------------------------------------===// |
| 1038 | |
Daniel Jasper | ceb99ab | 2013-01-09 10:16:05 +0000 | [diff] [blame] | 1039 | TEST_F(FormatTest, IndentationWithinColumnLimitNotPossible) { |
| 1040 | verifyFormat("int aaaaaaaa =\n" |
| 1041 | " // Overly long comment\n" |
| 1042 | " b;", getLLVMStyleWithColumns(20)); |
| 1043 | verifyFormat("function(\n" |
| 1044 | " ShortArgument,\n" |
| 1045 | " LoooooooooooongArgument);\n", getLLVMStyleWithColumns(20)); |
| 1046 | } |
| 1047 | |
Alexander Kornienko | 56e49c5 | 2012-12-10 16:34:48 +0000 | [diff] [blame] | 1048 | TEST_F(FormatTest, IncorrectAccessSpecifier) { |
| 1049 | verifyFormat("public:"); |
| 1050 | verifyFormat("class A {\n" |
| 1051 | "public\n" |
| 1052 | " void f() {\n" |
| 1053 | " }\n" |
| 1054 | "};"); |
| 1055 | verifyFormat("public\n" |
| 1056 | "int qwerty;"); |
| 1057 | verifyFormat("public\n" |
| 1058 | "B {\n" |
Manuel Klimek | de76854 | 2013-01-07 18:10:23 +0000 | [diff] [blame] | 1059 | "}"); |
Alexander Kornienko | 56e49c5 | 2012-12-10 16:34:48 +0000 | [diff] [blame] | 1060 | verifyFormat("public\n" |
| 1061 | "{\n" |
Manuel Klimek | de76854 | 2013-01-07 18:10:23 +0000 | [diff] [blame] | 1062 | "}"); |
Alexander Kornienko | 56e49c5 | 2012-12-10 16:34:48 +0000 | [diff] [blame] | 1063 | verifyFormat("public\n" |
| 1064 | "B {\n" |
| 1065 | " int x;\n" |
Manuel Klimek | de76854 | 2013-01-07 18:10:23 +0000 | [diff] [blame] | 1066 | "}"); |
Alexander Kornienko | 56e49c5 | 2012-12-10 16:34:48 +0000 | [diff] [blame] | 1067 | } |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1068 | |
Alexander Kornienko | 393b008 | 2012-12-04 15:40:36 +0000 | [diff] [blame] | 1069 | TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) { |
| 1070 | verifyFormat("{"); |
| 1071 | } |
| 1072 | |
| 1073 | TEST_F(FormatTest, IncorrectCodeDoNoWhile) { |
| 1074 | verifyFormat("do {\n" |
Manuel Klimek | de76854 | 2013-01-07 18:10:23 +0000 | [diff] [blame] | 1075 | "}"); |
Alexander Kornienko | 393b008 | 2012-12-04 15:40:36 +0000 | [diff] [blame] | 1076 | verifyFormat("do {\n" |
Manuel Klimek | de76854 | 2013-01-07 18:10:23 +0000 | [diff] [blame] | 1077 | "}\n" |
Alexander Kornienko | 393b008 | 2012-12-04 15:40:36 +0000 | [diff] [blame] | 1078 | "f();"); |
| 1079 | verifyFormat("do {\n" |
| 1080 | "}\n" |
| 1081 | "wheeee(fun);"); |
| 1082 | verifyFormat("do {\n" |
| 1083 | " f();\n" |
Manuel Klimek | de76854 | 2013-01-07 18:10:23 +0000 | [diff] [blame] | 1084 | "}"); |
Alexander Kornienko | 393b008 | 2012-12-04 15:40:36 +0000 | [diff] [blame] | 1085 | } |
| 1086 | |
Daniel Jasper | 1f42f11 | 2013-01-04 18:52:56 +0000 | [diff] [blame] | 1087 | TEST_F(FormatTest, DoesNotTouchUnwrappedLinesWithErrors) { |
| 1088 | verifyFormat("namespace {\n" |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 1089 | "class Foo { Foo ( }; } // comment"); |
Daniel Jasper | 1f42f11 | 2013-01-04 18:52:56 +0000 | [diff] [blame] | 1090 | } |
| 1091 | |
Alexander Kornienko | cff563c | 2012-12-04 17:27:50 +0000 | [diff] [blame] | 1092 | TEST_F(FormatTest, IncorrectCodeErrorDetection) { |
| 1093 | EXPECT_EQ("{\n{\n}\n", format("{\n{\n}\n")); |
| 1094 | EXPECT_EQ("{\n {\n}\n", format("{\n {\n}\n")); |
| 1095 | EXPECT_EQ("{\n {\n }\n", format("{\n {\n }\n")); |
Alexander Kornienko | a3a2b3a | 2012-12-06 17:49:17 +0000 | [diff] [blame] | 1096 | 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] | 1097 | |
| 1098 | FormatStyle Style = getLLVMStyle(); |
| 1099 | Style.ColumnLimit = 10; |
| 1100 | EXPECT_EQ("{\n" |
| 1101 | " {\n" |
| 1102 | " breakme(\n" |
| 1103 | " qwe);\n" |
| 1104 | "}\n", format("{\n" |
| 1105 | " {\n" |
| 1106 | " breakme(qwe);\n" |
| 1107 | "}\n", Style)); |
| 1108 | |
| 1109 | } |
| 1110 | |
Nico Weber | cf4a79c | 2013-01-08 17:56:31 +0000 | [diff] [blame] | 1111 | //===----------------------------------------------------------------------===// |
| 1112 | // Objective-C tests. |
| 1113 | //===----------------------------------------------------------------------===// |
| 1114 | |
Fariborz Jahanian | 154120c | 2012-12-20 19:54:13 +0000 | [diff] [blame] | 1115 | TEST_F(FormatTest, FormatForObjectiveCMethodDecls) { |
| 1116 | verifyFormat("- (void)sendAction:(SEL)aSelector to:(BOOL)anObject;"); |
| 1117 | EXPECT_EQ("- (NSUInteger)indexOfObject:(id)anObject;", |
| 1118 | format("-(NSUInteger)indexOfObject:(id)anObject;")); |
Daniel Jasper | cd16238 | 2013-01-07 13:26:07 +0000 | [diff] [blame] | 1119 | EXPECT_EQ("- (NSInteger)Mthod1;", format("-(NSInteger)Mthod1;")); |
Fariborz Jahanian | 154120c | 2012-12-20 19:54:13 +0000 | [diff] [blame] | 1120 | EXPECT_EQ("+ (id)Mthod2;", format("+(id)Mthod2;")); |
| 1121 | EXPECT_EQ("- (NSInteger)Method3:(id)anObject;", |
| 1122 | format("-(NSInteger)Method3:(id)anObject;")); |
| 1123 | EXPECT_EQ("- (NSInteger)Method4:(id)anObject;", |
| 1124 | format("-(NSInteger)Method4:(id)anObject;")); |
| 1125 | EXPECT_EQ("- (NSInteger)Method5:(id)anObject:(id)AnotherObject;", |
| 1126 | format("-(NSInteger)Method5:(id)anObject:(id)AnotherObject;")); |
| 1127 | EXPECT_EQ("- (id)Method6:(id)A:(id)B:(id)C:(id)D;", |
| 1128 | format("- (id)Method6:(id)A:(id)B:(id)C:(id)D;")); |
Daniel Jasper | cd16238 | 2013-01-07 13:26:07 +0000 | [diff] [blame] | 1129 | EXPECT_EQ( |
| 1130 | "- (void)sendAction:(SEL)aSelector to:(id)anObject forAllCells:(BOOL)flag;", |
| 1131 | format("- (void)sendAction:(SEL)aSelector to:(id)anObject forAllCells:(BOOL)flag;")); |
Fariborz Jahanian | 9b3f02c | 2012-12-21 22:51:18 +0000 | [diff] [blame] | 1132 | |
| 1133 | // Very long objectiveC method declaration. |
Daniel Jasper | cd16238 | 2013-01-07 13:26:07 +0000 | [diff] [blame] | 1134 | EXPECT_EQ( |
| 1135 | "- (NSUInteger)indexOfObject:(id)anObject inRange:(NSRange)range\n " |
| 1136 | "outRange:(NSRange)out_range outRange1:(NSRange)out_range1\n " |
| 1137 | "outRange2:(NSRange)out_range2 outRange3:(NSRange)out_range3\n " |
| 1138 | "outRange4:(NSRange)out_range4 outRange5:(NSRange)out_range5\n " |
| 1139 | "outRange6:(NSRange)out_range6 outRange7:(NSRange)out_range7\n " |
| 1140 | "outRange8:(NSRange)out_range8 outRange9:(NSRange)out_range9;", |
| 1141 | format( |
| 1142 | "- (NSUInteger)indexOfObject:(id)anObject inRange:(NSRange)range " |
| 1143 | "outRange:(NSRange) out_range outRange1:(NSRange) out_range1 " |
| 1144 | "outRange2:(NSRange) out_range2 outRange3:(NSRange) out_range3 " |
| 1145 | "outRange4:(NSRange) out_range4 outRange5:(NSRange) out_range5 " |
| 1146 | "outRange6:(NSRange) out_range6 outRange7:(NSRange) out_range7 " |
| 1147 | "outRange8:(NSRange) out_range8 outRange9:(NSRange) out_range9;")); |
Fariborz Jahanian | 154120c | 2012-12-20 19:54:13 +0000 | [diff] [blame] | 1148 | } |
| 1149 | |
Daniel Jasper | 886568d | 2013-01-09 08:36:49 +0000 | [diff] [blame] | 1150 | TEST_F(FormatTest, FormatObjCBlocks) { |
| 1151 | verifyFormat("int (^Block) (int, int);"); |
| 1152 | verifyFormat("int (^Block1) (int, int) = ^(int i, int j)"); |
| 1153 | } |
| 1154 | |
Nico Weber | 27d1367 | 2013-01-09 20:25:35 +0000 | [diff] [blame] | 1155 | TEST_F(FormatTest, FormatObjCInterface) { |
| 1156 | // FIXME: Handle comments like in "@interface /* wait for it */ Foo", PR14875 |
| 1157 | // FIXME: In google style, it's "+(id) init", not "+ (id)init". |
| 1158 | verifyFormat("@interface Foo : NSObject<NSSomeDelegate> {\n" |
| 1159 | "@public\n" |
| 1160 | " int field1;\n" |
| 1161 | "@protected\n" |
| 1162 | " int field2;\n" |
| 1163 | "@private\n" |
| 1164 | " int field3;\n" |
| 1165 | "@package\n" |
| 1166 | " int field4;\n" |
| 1167 | "}\n" |
| 1168 | "+ (id)init;\n" |
| 1169 | "@end"); |
| 1170 | |
| 1171 | // FIXME: In LLVM style, there should be a space before '<' for protocols. |
| 1172 | verifyGoogleFormat("@interface Foo : NSObject<NSSomeDelegate> {\n" |
| 1173 | " @public\n" |
| 1174 | " int field1;\n" |
| 1175 | " @protected\n" |
| 1176 | " int field2;\n" |
| 1177 | " @private\n" |
| 1178 | " int field3;\n" |
| 1179 | " @package\n" |
| 1180 | " int field4;\n" |
| 1181 | "}\n" |
| 1182 | "+ (id)init;\n" |
| 1183 | "@end"); |
| 1184 | |
| 1185 | verifyFormat("@interface Foo\n" |
| 1186 | "+ (id)init;\n" |
| 1187 | "// Look, a comment!\n" |
| 1188 | "- (int)answerWith:(int)i;\n" |
| 1189 | "@end"); |
| 1190 | |
| 1191 | verifyFormat("@interface Foo\n" |
Nico Weber | 049c447 | 2013-01-09 21:42:32 +0000 | [diff] [blame] | 1192 | "@end\n" |
| 1193 | "@interface Bar\n" |
Nico Weber | 27d1367 | 2013-01-09 20:25:35 +0000 | [diff] [blame] | 1194 | "@end"); |
| 1195 | |
| 1196 | verifyFormat("@interface Foo : Bar\n" |
| 1197 | "+ (id)init;\n" |
| 1198 | "@end"); |
| 1199 | |
| 1200 | verifyFormat("@interface Foo : Bar<Baz, Quux>\n" |
| 1201 | "+ (id)init;\n" |
| 1202 | "@end"); |
| 1203 | |
| 1204 | // FIXME: there should be a space before '(' for categories. |
| 1205 | verifyFormat("@interface Foo(HackStuff)\n" |
| 1206 | "+ (id)init;\n" |
| 1207 | "@end"); |
| 1208 | |
| 1209 | verifyFormat("@interface Foo()\n" |
| 1210 | "+ (id)init;\n" |
| 1211 | "@end"); |
| 1212 | |
| 1213 | verifyFormat("@interface Foo(HackStuff)<MyProtocol>\n" |
| 1214 | "+ (id)init;\n" |
| 1215 | "@end"); |
| 1216 | |
| 1217 | verifyFormat("@interface Foo {\n" |
| 1218 | " int _i;\n" |
| 1219 | "}\n" |
| 1220 | "+ (id)init;\n" |
| 1221 | "@end"); |
| 1222 | |
| 1223 | verifyFormat("@interface Foo : Bar {\n" |
| 1224 | " int _i;\n" |
| 1225 | "}\n" |
| 1226 | "+ (id)init;\n" |
| 1227 | "@end"); |
| 1228 | |
| 1229 | verifyFormat("@interface Foo : Bar<Baz, Quux> {\n" |
| 1230 | " int _i;\n" |
| 1231 | "}\n" |
| 1232 | "+ (id)init;\n" |
| 1233 | "@end"); |
| 1234 | |
| 1235 | verifyFormat("@interface Foo(HackStuff) {\n" |
| 1236 | " int _i;\n" |
| 1237 | "}\n" |
| 1238 | "+ (id)init;\n" |
| 1239 | "@end"); |
| 1240 | |
| 1241 | verifyFormat("@interface Foo() {\n" |
| 1242 | " int _i;\n" |
| 1243 | "}\n" |
| 1244 | "+ (id)init;\n" |
| 1245 | "@end"); |
| 1246 | |
| 1247 | verifyFormat("@interface Foo(HackStuff)<MyProtocol> {\n" |
| 1248 | " int _i;\n" |
| 1249 | "}\n" |
| 1250 | "+ (id)init;\n" |
| 1251 | "@end"); |
| 1252 | } |
| 1253 | |
Nico Weber | 50767d8 | 2013-01-09 23:25:37 +0000 | [diff] [blame] | 1254 | TEST_F(FormatTest, FormatObjCImplementation) { |
| 1255 | verifyFormat("@implementation Foo : NSObject {\n" |
| 1256 | "@public\n" |
| 1257 | " int field1;\n" |
| 1258 | "@protected\n" |
| 1259 | " int field2;\n" |
| 1260 | "@private\n" |
| 1261 | " int field3;\n" |
| 1262 | "@package\n" |
| 1263 | " int field4;\n" |
| 1264 | "}\n" |
| 1265 | "+ (id)init {\n" |
| 1266 | "}\n" |
| 1267 | "@end"); |
| 1268 | |
| 1269 | verifyGoogleFormat("@implementation Foo : NSObject {\n" |
| 1270 | " @public\n" |
| 1271 | " int field1;\n" |
| 1272 | " @protected\n" |
| 1273 | " int field2;\n" |
| 1274 | " @private\n" |
| 1275 | " int field3;\n" |
| 1276 | " @package\n" |
| 1277 | " int field4;\n" |
| 1278 | "}\n" |
| 1279 | "+ (id)init {\n" |
| 1280 | "}\n" |
| 1281 | "@end"); |
| 1282 | |
| 1283 | verifyFormat("@implementation Foo\n" |
| 1284 | "+ (id)init {\n" |
| 1285 | " if (true)\n" |
| 1286 | " return nil;\n" |
| 1287 | "}\n" |
| 1288 | "// Look, a comment!\n" |
| 1289 | "- (int)answerWith:(int)i {\n" |
| 1290 | " return i;\n" |
| 1291 | "}\n" |
| 1292 | "@end"); |
| 1293 | |
| 1294 | verifyFormat("@implementation Foo\n" |
| 1295 | "@end\n" |
| 1296 | "@implementation Bar\n" |
| 1297 | "@end"); |
| 1298 | |
| 1299 | verifyFormat("@implementation Foo : Bar\n" |
| 1300 | "+ (id)init {\n" |
| 1301 | "}\n" |
| 1302 | "@end"); |
| 1303 | |
| 1304 | verifyFormat("@implementation Foo {\n" |
| 1305 | " int _i;\n" |
| 1306 | "}\n" |
| 1307 | "+ (id)init {\n" |
| 1308 | "}\n" |
| 1309 | "@end"); |
| 1310 | |
| 1311 | verifyFormat("@implementation Foo : Bar {\n" |
| 1312 | " int _i;\n" |
| 1313 | "}\n" |
| 1314 | "+ (id)init {\n" |
| 1315 | "}\n" |
| 1316 | "@end"); |
| 1317 | |
| 1318 | // FIXME: there should be a space before '(' for categories. |
| 1319 | verifyFormat("@implementation Foo(HackStuff)\n" |
| 1320 | "+ (id)init {\n" |
| 1321 | "}\n" |
| 1322 | "@end"); |
| 1323 | } |
| 1324 | |
Nico Weber | 1abe6ea | 2013-01-09 21:15:03 +0000 | [diff] [blame] | 1325 | TEST_F(FormatTest, FormatObjCProtocol) { |
| 1326 | verifyFormat("@protocol Foo\n" |
| 1327 | "@property(weak) id delegate;\n" |
| 1328 | "- (NSUInteger)numberOfThings;\n" |
| 1329 | "@end"); |
| 1330 | |
| 1331 | // FIXME: In LLVM style, there should be a space before '<' for protocols. |
| 1332 | verifyFormat("@protocol MyProtocol<NSObject>\n" |
| 1333 | "- (NSUInteger)numberOfThings;\n" |
| 1334 | "@end"); |
| 1335 | |
| 1336 | verifyFormat("@protocol Foo;\n" |
| 1337 | "@protocol Bar;\n"); |
Nico Weber | 049c447 | 2013-01-09 21:42:32 +0000 | [diff] [blame] | 1338 | |
| 1339 | verifyFormat("@protocol Foo\n" |
| 1340 | "@end\n" |
| 1341 | "@protocol Bar\n" |
| 1342 | "@end"); |
Nico Weber | b530fa3 | 2013-01-10 00:25:19 +0000 | [diff] [blame] | 1343 | |
| 1344 | verifyFormat("@protocol myProtocol\n" |
| 1345 | "- (void)mandatoryWithInt:(int)i;\n" |
| 1346 | "@optional\n" |
| 1347 | "- (void)optional;\n" |
| 1348 | "@required\n" |
| 1349 | "- (void)required;\n" |
Nico Weber | 880e538 | 2013-01-10 00:42:07 +0000 | [diff] [blame] | 1350 | "@optional\n" |
| 1351 | "@property(assign) int madProp;\n" |
Nico Weber | b530fa3 | 2013-01-10 00:25:19 +0000 | [diff] [blame] | 1352 | "@end\n"); |
Nico Weber | 1abe6ea | 2013-01-09 21:15:03 +0000 | [diff] [blame] | 1353 | } |
| 1354 | |
Nico Weber | 581f557 | 2013-01-07 15:56:25 +0000 | [diff] [blame] | 1355 | TEST_F(FormatTest, ObjCAt) { |
Nico Weber | 4a293a2 | 2013-01-07 16:07:07 +0000 | [diff] [blame] | 1356 | verifyFormat("@autoreleasepool"); |
Nico Weber | d0af4b4 | 2013-01-07 16:14:28 +0000 | [diff] [blame] | 1357 | verifyFormat("@catch"); |
| 1358 | verifyFormat("@class"); |
Nico Weber | 4a293a2 | 2013-01-07 16:07:07 +0000 | [diff] [blame] | 1359 | verifyFormat("@compatibility_alias"); |
| 1360 | verifyFormat("@defs"); |
Nico Weber | efcfe73 | 2013-01-07 15:17:23 +0000 | [diff] [blame] | 1361 | verifyFormat("@dynamic"); |
Nico Weber | 4a293a2 | 2013-01-07 16:07:07 +0000 | [diff] [blame] | 1362 | verifyFormat("@encode"); |
| 1363 | verifyFormat("@end"); |
| 1364 | verifyFormat("@finally"); |
| 1365 | verifyFormat("@implementation"); |
| 1366 | verifyFormat("@import"); |
| 1367 | verifyFormat("@interface"); |
| 1368 | verifyFormat("@optional"); |
| 1369 | verifyFormat("@package"); |
Nico Weber | d0af4b4 | 2013-01-07 16:14:28 +0000 | [diff] [blame] | 1370 | verifyFormat("@private"); |
Nico Weber | 4a293a2 | 2013-01-07 16:07:07 +0000 | [diff] [blame] | 1371 | verifyFormat("@property"); |
Nico Weber | d0af4b4 | 2013-01-07 16:14:28 +0000 | [diff] [blame] | 1372 | verifyFormat("@protected"); |
Nico Weber | 4a293a2 | 2013-01-07 16:07:07 +0000 | [diff] [blame] | 1373 | verifyFormat("@protocol"); |
Nico Weber | d0af4b4 | 2013-01-07 16:14:28 +0000 | [diff] [blame] | 1374 | verifyFormat("@public"); |
Nico Weber | 4a293a2 | 2013-01-07 16:07:07 +0000 | [diff] [blame] | 1375 | verifyFormat("@required"); |
| 1376 | verifyFormat("@selector"); |
| 1377 | verifyFormat("@synchronized"); |
| 1378 | verifyFormat("@synthesize"); |
Nico Weber | d0af4b4 | 2013-01-07 16:14:28 +0000 | [diff] [blame] | 1379 | verifyFormat("@throw"); |
| 1380 | verifyFormat("@try"); |
Nico Weber | 4a293a2 | 2013-01-07 16:07:07 +0000 | [diff] [blame] | 1381 | |
Nico Weber | cb4d690 | 2013-01-08 19:40:21 +0000 | [diff] [blame] | 1382 | // FIXME: Make the uncommented lines below pass. |
| 1383 | verifyFormat("@\"String\""); |
| 1384 | verifyFormat("@1"); |
| 1385 | //verifyFormat("@+4.8"); |
| 1386 | //verifyFormat("@-4"); |
| 1387 | verifyFormat("@1LL"); |
| 1388 | verifyFormat("@.5"); |
| 1389 | verifyFormat("@'c'"); |
| 1390 | verifyFormat("@true"); |
| 1391 | verifyFormat("NSNumber *smallestInt = @(-INT_MAX - 1);"); |
| 1392 | verifyFormat("@["); |
| 1393 | verifyFormat("@{"); |
| 1394 | |
| 1395 | |
Nico Weber | 581f557 | 2013-01-07 15:56:25 +0000 | [diff] [blame] | 1396 | EXPECT_EQ("@interface", format("@ interface")); |
| 1397 | |
| 1398 | // The precise formatting of this doesn't matter, nobody writes code like |
| 1399 | // this. |
| 1400 | verifyFormat("@ /*foo*/ interface"); |
Nico Weber | efcfe73 | 2013-01-07 15:17:23 +0000 | [diff] [blame] | 1401 | } |
| 1402 | |
Nico Weber | c31689a | 2013-01-08 19:15:23 +0000 | [diff] [blame] | 1403 | TEST_F(FormatTest, ObjCSnippets) { |
| 1404 | // FIXME: Make the uncommented lines below pass. |
| 1405 | verifyFormat("@autoreleasepool {\n" |
| 1406 | " foo();\n" |
| 1407 | "}"); |
Nico Weber | cf4a79c | 2013-01-08 17:56:31 +0000 | [diff] [blame] | 1408 | verifyFormat("@class Foo, Bar;"); |
Nico Weber | c31689a | 2013-01-08 19:15:23 +0000 | [diff] [blame] | 1409 | verifyFormat("@compatibility_alias AliasName ExistingClass;"); |
Nico Weber | cf4a79c | 2013-01-08 17:56:31 +0000 | [diff] [blame] | 1410 | verifyFormat("@dynamic textColor;"); |
Nico Weber | c31689a | 2013-01-08 19:15:23 +0000 | [diff] [blame] | 1411 | //verifyFormat("char *buf1 = @encode(int **);"); |
Nico Weber | 3a2673e | 2013-01-08 20:16:23 +0000 | [diff] [blame] | 1412 | verifyFormat("Protocol *proto = @protocol(p1);"); |
Nico Weber | c31689a | 2013-01-08 19:15:23 +0000 | [diff] [blame] | 1413 | //verifyFormat("SEL s = @selector(foo:);"); |
Nico Weber | 3a2673e | 2013-01-08 20:16:23 +0000 | [diff] [blame] | 1414 | verifyFormat("@synchronized(self) {\n" |
| 1415 | " f();\n" |
| 1416 | "}"); |
Nico Weber | c31689a | 2013-01-08 19:15:23 +0000 | [diff] [blame] | 1417 | verifyFormat("@synthesize dropArrowPosition = dropArrowPosition_;"); |
Nico Weber | cf4a79c | 2013-01-08 17:56:31 +0000 | [diff] [blame] | 1418 | |
| 1419 | // FIXME: "getter=bar" should not be surround by spaces in @property. |
| 1420 | verifyFormat("@property(assign, nonatomic) CGFloat hoverAlpha;"); |
| 1421 | } |
| 1422 | |
Daniel Jasper | cd16238 | 2013-01-07 13:26:07 +0000 | [diff] [blame] | 1423 | } // end namespace tooling |
| 1424 | } // end namespace clang |