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