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 | |
Manuel Klimek | ca547db | 2013-01-16 14:55:28 +0000 | [diff] [blame] | 10 | #define DEBUG_TYPE "format-test" |
| 11 | |
Chandler Carruth | 1050e8b | 2012-12-04 09:45:34 +0000 | [diff] [blame] | 12 | #include "clang/Format/Format.h" |
Manuel Klimek | ca547db | 2013-01-16 14:55:28 +0000 | [diff] [blame] | 13 | #include "../Tooling/RewriterTestContext.h" |
Chandler Carruth | b1ba0ef | 2013-01-19 08:09:44 +0000 | [diff] [blame] | 14 | #include "clang/Lex/Lexer.h" |
| 15 | #include "llvm/Support/Debug.h" |
| 16 | #include "gtest/gtest.h" |
Manuel Klimek | ca547db | 2013-01-16 14:55:28 +0000 | [diff] [blame] | 17 | |
| 18 | // Uncomment to get debug output from tests: |
| 19 | // #define DEBUG_WITH_TYPE(T, X) do { X; } while(0) |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 20 | |
| 21 | namespace clang { |
| 22 | namespace format { |
| 23 | |
| 24 | class FormatTest : public ::testing::Test { |
| 25 | protected: |
| 26 | std::string format(llvm::StringRef Code, unsigned Offset, unsigned Length, |
| 27 | const FormatStyle &Style) { |
Manuel Klimek | ca547db | 2013-01-16 14:55:28 +0000 | [diff] [blame] | 28 | DEBUG(llvm::errs() << "---\n"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 29 | RewriterTestContext Context; |
| 30 | FileID ID = Context.createInMemoryFile("input.cc", Code); |
| 31 | SourceLocation Start = |
| 32 | Context.Sources.getLocForStartOfFile(ID).getLocWithOffset(Offset); |
| 33 | std::vector<CharSourceRange> Ranges( |
| 34 | 1, |
| 35 | CharSourceRange::getCharRange(Start, Start.getLocWithOffset(Length))); |
Daniel Jasper | 46ef852 | 2013-01-10 13:08:12 +0000 | [diff] [blame] | 36 | Lexer Lex(ID, Context.Sources.getBuffer(ID), Context.Sources, |
| 37 | getFormattingLangOpts()); |
Daniel Jasper | cd16238 | 2013-01-07 13:26:07 +0000 | [diff] [blame] | 38 | tooling::Replacements Replace = reformat(Style, Lex, Context.Sources, |
Alexander Kornienko | a4ae9f3 | 2013-01-14 11:34:14 +0000 | [diff] [blame] | 39 | Ranges, |
| 40 | new IgnoringDiagConsumer()); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 41 | EXPECT_TRUE(applyAllReplacements(Replace, Context.Rewrite)); |
Manuel Klimek | ca547db | 2013-01-16 14:55:28 +0000 | [diff] [blame] | 42 | DEBUG(llvm::errs() << "\n" << Context.getRewrittenText(ID) << "\n\n"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 43 | return Context.getRewrittenText(ID); |
| 44 | } |
| 45 | |
| 46 | std::string format(llvm::StringRef Code, |
| 47 | const FormatStyle &Style = getLLVMStyle()) { |
| 48 | return format(Code, 0, Code.size(), Style); |
| 49 | } |
| 50 | |
Daniel Jasper | e35ec2b | 2012-12-18 19:56:56 +0000 | [diff] [blame] | 51 | std::string messUp(llvm::StringRef Code) { |
| 52 | std::string MessedUp(Code.str()); |
| 53 | bool InComment = false; |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 54 | bool InPreprocessorDirective = false; |
Daniel Jasper | e35ec2b | 2012-12-18 19:56:56 +0000 | [diff] [blame] | 55 | bool JustReplacedNewline = false; |
| 56 | for (unsigned i = 0, e = MessedUp.size() - 1; i != e; ++i) { |
| 57 | if (MessedUp[i] == '/' && MessedUp[i + 1] == '/') { |
| 58 | if (JustReplacedNewline) |
| 59 | MessedUp[i - 1] = '\n'; |
| 60 | InComment = true; |
Daniel Jasper | e0b15ea | 2013-01-14 15:40:57 +0000 | [diff] [blame] | 61 | } else if (MessedUp[i] == '#' && (JustReplacedNewline || i == 0)) { |
| 62 | if (i != 0) MessedUp[i - 1] = '\n'; |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 63 | InPreprocessorDirective = true; |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 64 | } else if (MessedUp[i] == '\\' && MessedUp[i + 1] == '\n') { |
| 65 | MessedUp[i] = ' '; |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 66 | MessedUp[i + 1] = ' '; |
Daniel Jasper | e35ec2b | 2012-12-18 19:56:56 +0000 | [diff] [blame] | 67 | } else if (MessedUp[i] == '\n') { |
| 68 | if (InComment) { |
| 69 | InComment = false; |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 70 | } else if (InPreprocessorDirective) { |
| 71 | InPreprocessorDirective = false; |
Daniel Jasper | e35ec2b | 2012-12-18 19:56:56 +0000 | [diff] [blame] | 72 | } else { |
| 73 | JustReplacedNewline = true; |
| 74 | MessedUp[i] = ' '; |
| 75 | } |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 76 | } else if (MessedUp[i] != ' ') { |
| 77 | JustReplacedNewline = false; |
Daniel Jasper | e35ec2b | 2012-12-18 19:56:56 +0000 | [diff] [blame] | 78 | } |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 79 | } |
Daniel Jasper | e35ec2b | 2012-12-18 19:56:56 +0000 | [diff] [blame] | 80 | return MessedUp; |
| 81 | } |
| 82 | |
Manuel Klimek | 060143e | 2013-01-02 18:33:23 +0000 | [diff] [blame] | 83 | FormatStyle getLLVMStyleWithColumns(unsigned ColumnLimit) { |
| 84 | FormatStyle Style = getLLVMStyle(); |
| 85 | Style.ColumnLimit = ColumnLimit; |
| 86 | return Style; |
| 87 | } |
| 88 | |
Daniel Jasper | 6f5bb2c | 2013-01-14 16:24:39 +0000 | [diff] [blame] | 89 | FormatStyle getGoogleStyleWithColumns(unsigned ColumnLimit) { |
| 90 | FormatStyle Style = getGoogleStyle(); |
| 91 | Style.ColumnLimit = ColumnLimit; |
| 92 | return Style; |
| 93 | } |
| 94 | |
Manuel Klimek | 060143e | 2013-01-02 18:33:23 +0000 | [diff] [blame] | 95 | void verifyFormat(llvm::StringRef Code, |
| 96 | const FormatStyle &Style = getLLVMStyle()) { |
| 97 | EXPECT_EQ(Code.str(), format(messUp(Code), Style)); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | void verifyGoogleFormat(llvm::StringRef Code) { |
Manuel Klimek | 060143e | 2013-01-02 18:33:23 +0000 | [diff] [blame] | 101 | verifyFormat(Code, getGoogleStyle()); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 102 | } |
Daniel Jasper | 4bfc65a | 2013-01-23 12:10:53 +0000 | [diff] [blame] | 103 | |
| 104 | void verifyIndependentOfContext(llvm::StringRef text) { |
| 105 | verifyFormat(text); |
| 106 | verifyFormat(llvm::Twine("void f() { " + text + " }").str()); |
| 107 | } |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 108 | }; |
| 109 | |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 110 | TEST_F(FormatTest, MessUp) { |
| 111 | EXPECT_EQ("1 2 3", messUp("1 2 3")); |
| 112 | EXPECT_EQ("1 2 3\n", messUp("1\n2\n3\n")); |
| 113 | EXPECT_EQ("a\n//b\nc", messUp("a\n//b\nc")); |
| 114 | EXPECT_EQ("a\n#b\nc", messUp("a\n#b\nc")); |
| 115 | EXPECT_EQ("a\n#b c d\ne", messUp("a\n#b\\\nc\\\nd\ne")); |
| 116 | } |
| 117 | |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 118 | //===----------------------------------------------------------------------===// |
| 119 | // Basic function tests. |
| 120 | //===----------------------------------------------------------------------===// |
| 121 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 122 | TEST_F(FormatTest, DoesNotChangeCorrectlyFormatedCode) { |
| 123 | EXPECT_EQ(";", format(";")); |
| 124 | } |
| 125 | |
| 126 | TEST_F(FormatTest, FormatsGlobalStatementsAt0) { |
| 127 | EXPECT_EQ("int i;", format(" int i;")); |
| 128 | EXPECT_EQ("\nint i;", format(" \n\t \r int i;")); |
| 129 | EXPECT_EQ("int i;\nint j;", format(" int i; int j;")); |
| 130 | EXPECT_EQ("int i;\nint j;", format(" int i;\n int j;")); |
| 131 | } |
| 132 | |
| 133 | TEST_F(FormatTest, FormatsUnwrappedLinesAtFirstFormat) { |
| 134 | EXPECT_EQ("int i;", format("int\ni;")); |
| 135 | } |
| 136 | |
| 137 | TEST_F(FormatTest, FormatsNestedBlockStatements) { |
Manuel Klimek | 36fab8d | 2013-01-10 13:24:24 +0000 | [diff] [blame] | 138 | EXPECT_EQ("{\n {\n {}\n }\n}", format("{{{}}}")); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 139 | } |
| 140 | |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 141 | TEST_F(FormatTest, FormatsNestedCall) { |
| 142 | verifyFormat("Method(f1, f2(f3));"); |
| 143 | verifyFormat("Method(f1(f2, f3()));"); |
Daniel Jasper | 4e9008a | 2013-01-13 08:19:51 +0000 | [diff] [blame] | 144 | verifyFormat("Method(f1(f2, (f3())));"); |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 145 | } |
| 146 | |
Daniel Jasper | 6b825c2 | 2013-01-16 07:19:28 +0000 | [diff] [blame] | 147 | TEST_F(FormatTest, ImportantSpaces) { |
| 148 | verifyFormat("vector< ::Type> v;"); |
| 149 | } |
| 150 | |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 151 | //===----------------------------------------------------------------------===// |
| 152 | // Tests for control statements. |
| 153 | //===----------------------------------------------------------------------===// |
| 154 | |
| 155 | TEST_F(FormatTest, FormatIfWithoutCompountStatement) { |
Daniel Jasper | 6f5bb2c | 2013-01-14 16:24:39 +0000 | [diff] [blame] | 156 | verifyFormat("if (true)\n f();\ng();"); |
| 157 | verifyFormat("if (a)\n if (b)\n if (c)\n g();\nh();"); |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 158 | verifyFormat("if (a)\n if (b) {\n f();\n }\ng();"); |
Daniel Jasper | df3736a | 2013-01-16 15:44:34 +0000 | [diff] [blame] | 159 | |
| 160 | FormatStyle AllowsMergedIf = getGoogleStyle(); |
| 161 | AllowsMergedIf.AllowShortIfStatementsOnASingleLine = true; |
| 162 | verifyFormat("if (a)\n" |
| 163 | " // comment\n" |
| 164 | " f();", AllowsMergedIf); |
| 165 | |
| 166 | verifyFormat("if (a) // Can't merge this\n" |
| 167 | " f();\n", AllowsMergedIf); |
| 168 | verifyFormat("if (a) /* still don't merge */\n" |
| 169 | " f();", AllowsMergedIf); |
| 170 | verifyFormat("if (a) { // Never merge this\n" |
| 171 | " f();\n" |
| 172 | "}", AllowsMergedIf); |
| 173 | verifyFormat("if (a) { /* Never merge this */\n" |
| 174 | " f();\n" |
| 175 | "}", AllowsMergedIf); |
| 176 | |
| 177 | AllowsMergedIf.ColumnLimit = 14; |
| 178 | verifyFormat("if (a) return;", AllowsMergedIf); |
Daniel Jasper | fd0ca97 | 2013-01-14 16:02:06 +0000 | [diff] [blame] | 179 | verifyFormat("if (aaaaaaaaa)\n" |
Daniel Jasper | df3736a | 2013-01-16 15:44:34 +0000 | [diff] [blame] | 180 | " return;", AllowsMergedIf); |
| 181 | |
| 182 | AllowsMergedIf.ColumnLimit = 13; |
| 183 | verifyFormat("if (a)\n return;", AllowsMergedIf); |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | TEST_F(FormatTest, ParseIfElse) { |
| 187 | verifyFormat("if (true)\n" |
| 188 | " if (true)\n" |
| 189 | " if (true)\n" |
| 190 | " f();\n" |
| 191 | " else\n" |
| 192 | " g();\n" |
| 193 | " else\n" |
| 194 | " h();\n" |
| 195 | "else\n" |
| 196 | " i();"); |
| 197 | verifyFormat("if (true)\n" |
| 198 | " if (true)\n" |
| 199 | " if (true) {\n" |
Daniel Jasper | 6f5bb2c | 2013-01-14 16:24:39 +0000 | [diff] [blame] | 200 | " if (true)\n" |
| 201 | " f();\n" |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 202 | " } else {\n" |
| 203 | " g();\n" |
| 204 | " }\n" |
| 205 | " else\n" |
| 206 | " h();\n" |
| 207 | "else {\n" |
| 208 | " i();\n" |
| 209 | "}"); |
| 210 | } |
| 211 | |
| 212 | TEST_F(FormatTest, ElseIf) { |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 213 | verifyFormat("if (a) {\n} else if (b) {\n}"); |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 214 | verifyFormat("if (a)\n" |
| 215 | " f();\n" |
| 216 | "else if (b)\n" |
| 217 | " g();\n" |
| 218 | "else\n" |
| 219 | " h();"); |
| 220 | } |
| 221 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 222 | TEST_F(FormatTest, FormatsForLoop) { |
| 223 | verifyFormat( |
| 224 | "for (int VeryVeryLongLoopVariable = 0; VeryVeryLongLoopVariable < 10;\n" |
Alexander Kornienko | 2e97cfc | 2012-12-05 15:06:06 +0000 | [diff] [blame] | 225 | " ++VeryVeryLongLoopVariable)\n" |
| 226 | " ;"); |
| 227 | verifyFormat("for (;;)\n" |
| 228 | " f();"); |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 229 | verifyFormat("for (;;) {\n}"); |
Alexander Kornienko | 2e97cfc | 2012-12-05 15:06:06 +0000 | [diff] [blame] | 230 | verifyFormat("for (;;) {\n" |
| 231 | " f();\n" |
| 232 | "}"); |
Daniel Jasper | a324a0e | 2012-12-21 14:37:20 +0000 | [diff] [blame] | 233 | |
| 234 | verifyFormat( |
| 235 | "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n" |
| 236 | " E = UnwrappedLines.end();\n" |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 237 | " I != E; ++I) {\n}"); |
Daniel Jasper | a324a0e | 2012-12-21 14:37:20 +0000 | [diff] [blame] | 238 | |
| 239 | verifyFormat( |
| 240 | "for (MachineFun::iterator IIII = PrevIt, EEEE = F.end(); IIII != EEEE;\n" |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 241 | " ++IIIII) {\n}"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | TEST_F(FormatTest, FormatsWhileLoop) { |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 245 | verifyFormat("while (true) {\n}"); |
Alexander Kornienko | 2e97cfc | 2012-12-05 15:06:06 +0000 | [diff] [blame] | 246 | verifyFormat("while (true)\n" |
| 247 | " f();"); |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 248 | verifyFormat("while () {\n}"); |
Alexander Kornienko | 2e97cfc | 2012-12-05 15:06:06 +0000 | [diff] [blame] | 249 | verifyFormat("while () {\n" |
| 250 | " f();\n" |
| 251 | "}"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 252 | } |
| 253 | |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 254 | TEST_F(FormatTest, FormatsDoWhile) { |
| 255 | verifyFormat("do {\n" |
| 256 | " do_something();\n" |
| 257 | "} while (something());"); |
| 258 | verifyFormat("do\n" |
| 259 | " do_something();\n" |
| 260 | "while (something());"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 261 | } |
| 262 | |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 263 | TEST_F(FormatTest, FormatsSwitchStatement) { |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 264 | verifyFormat("switch (x) {\n" |
| 265 | "case 1:\n" |
| 266 | " f();\n" |
| 267 | " break;\n" |
| 268 | "case kFoo:\n" |
| 269 | "case ns::kBar:\n" |
| 270 | "case kBaz:\n" |
| 271 | " break;\n" |
| 272 | "default:\n" |
| 273 | " g();\n" |
| 274 | " break;\n" |
| 275 | "}"); |
| 276 | verifyFormat("switch (x) {\n" |
| 277 | "case 1: {\n" |
| 278 | " f();\n" |
| 279 | " break;\n" |
| 280 | "}\n" |
| 281 | "}"); |
Nico Weber | 94fb729 | 2013-01-18 05:50:57 +0000 | [diff] [blame] | 282 | verifyFormat("switch (x) {\n" |
| 283 | "case 1: {\n" |
| 284 | " f();\n" |
| 285 | " {\n" |
| 286 | " g();\n" |
| 287 | " h();\n" |
| 288 | " }\n" |
| 289 | " break;\n" |
| 290 | "}\n" |
| 291 | "}"); |
| 292 | verifyFormat("switch (x) {\n" |
| 293 | "case 1: {\n" |
| 294 | " f();\n" |
| 295 | " if (foo) {\n" |
| 296 | " g();\n" |
| 297 | " h();\n" |
| 298 | " }\n" |
| 299 | " break;\n" |
| 300 | "}\n" |
| 301 | "}"); |
| 302 | verifyFormat("switch (x) {\n" |
| 303 | "case 1: {\n" |
| 304 | " f();\n" |
| 305 | " g();\n" |
| 306 | "} break;\n" |
| 307 | "}"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 308 | verifyFormat("switch (test)\n" |
| 309 | " ;"); |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 310 | verifyGoogleFormat("switch (x) {\n" |
| 311 | " case 1:\n" |
| 312 | " f();\n" |
| 313 | " break;\n" |
| 314 | " case kFoo:\n" |
| 315 | " case ns::kBar:\n" |
| 316 | " case kBaz:\n" |
| 317 | " break;\n" |
| 318 | " default:\n" |
| 319 | " g();\n" |
| 320 | " break;\n" |
| 321 | "}"); |
| 322 | verifyGoogleFormat("switch (x) {\n" |
| 323 | " case 1: {\n" |
| 324 | " f();\n" |
| 325 | " break;\n" |
| 326 | " }\n" |
| 327 | "}"); |
| 328 | verifyGoogleFormat("switch (test)\n" |
| 329 | " ;"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 330 | } |
| 331 | |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 332 | TEST_F(FormatTest, FormatsLabels) { |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 333 | verifyFormat("void f() {\n" |
| 334 | " some_code();\n" |
| 335 | "test_label:\n" |
| 336 | " some_other_code();\n" |
| 337 | " {\n" |
| 338 | " some_more_code();\n" |
| 339 | " another_label:\n" |
| 340 | " some_more_code();\n" |
| 341 | " }\n" |
| 342 | "}"); |
| 343 | verifyFormat("some_code();\n" |
| 344 | "test_label:\n" |
| 345 | "some_other_code();"); |
| 346 | } |
| 347 | |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 348 | //===----------------------------------------------------------------------===// |
| 349 | // Tests for comments. |
| 350 | //===----------------------------------------------------------------------===// |
| 351 | |
| 352 | TEST_F(FormatTest, UnderstandsSingleLineComments) { |
Daniel Jasper | e35ec2b | 2012-12-18 19:56:56 +0000 | [diff] [blame] | 353 | verifyFormat("// line 1\n" |
| 354 | "// line 2\n" |
Manuel Klimek | 36fab8d | 2013-01-10 13:24:24 +0000 | [diff] [blame] | 355 | "void f() {}\n"); |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 356 | |
Daniel Jasper | e35ec2b | 2012-12-18 19:56:56 +0000 | [diff] [blame] | 357 | verifyFormat("void f() {\n" |
| 358 | " // Doesn't do anything\n" |
| 359 | "}"); |
Daniel Jasper | dcc2a62 | 2013-01-18 08:44:07 +0000 | [diff] [blame] | 360 | verifyFormat("void f(int i, // some comment (probably for i)\n" |
| 361 | " int j, // some comment (probably for j)\n" |
Daniel Jasper | 487f64b | 2013-01-13 16:10:20 +0000 | [diff] [blame] | 362 | " int k); // some comment (probably for k)"); |
| 363 | verifyFormat("void f(int i,\n" |
| 364 | " // some comment (probably for j)\n" |
| 365 | " int j,\n" |
| 366 | " // some comment (probably for k)\n" |
| 367 | " int k);"); |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 368 | |
Daniel Jasper | dcc2a62 | 2013-01-18 08:44:07 +0000 | [diff] [blame] | 369 | verifyFormat("int i // This is a fancy variable\n" |
| 370 | " = 5; // with nicely aligned comment."); |
| 371 | |
| 372 | verifyFormat("// Leading comment.\n" |
| 373 | "int a; // Trailing comment."); |
| 374 | verifyFormat("int a; // Trailing comment\n" |
| 375 | " // on 2\n" |
| 376 | " // or 3 lines.\n" |
| 377 | "int b;"); |
| 378 | verifyFormat("int a; // Trailing comment\n" |
| 379 | "\n" |
| 380 | "// Leading comment.\n" |
| 381 | "int b;"); |
| 382 | verifyFormat("int a; // Comment.\n" |
| 383 | " // More details.\n" |
| 384 | "int bbbb; // Another comment."); |
| 385 | verifyFormat( |
| 386 | "int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; // comment\n" |
| 387 | "int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; // comment\n" |
| 388 | "int cccccccccccccccccccccccccccccc; // comment\n" |
| 389 | "int ddd; // looooooooooooooooooooooooong comment\n" |
| 390 | "int aaaaaaaaaaaaaaaaaaaaaaa; // comment\n" |
| 391 | "int bbbbbbbbbbbbbbbbbbbbb; // comment\n" |
| 392 | "int ccccccccccccccccccc; // comment"); |
| 393 | |
Daniel Jasper | 7d1185d | 2013-01-18 09:19:33 +0000 | [diff] [blame] | 394 | verifyFormat("#include \"a\" // comment\n" |
| 395 | "#include \"a/b/c\" // comment"); |
| 396 | verifyFormat("#include <a> // comment\n" |
| 397 | "#include <a/b/c> // comment"); |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 398 | |
Daniel Jasper | e35ec2b | 2012-12-18 19:56:56 +0000 | [diff] [blame] | 399 | verifyFormat("enum E {\n" |
| 400 | " // comment\n" |
Daniel Jasper | 7ad4eff | 2013-01-07 11:09:06 +0000 | [diff] [blame] | 401 | " VAL_A, // comment\n" |
Daniel Jasper | e35ec2b | 2012-12-18 19:56:56 +0000 | [diff] [blame] | 402 | " VAL_B\n" |
| 403 | "};"); |
Daniel Jasper | 05b1ac8 | 2012-12-17 11:29:41 +0000 | [diff] [blame] | 404 | |
| 405 | verifyFormat( |
| 406 | "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" |
Daniel Jasper | 7ad4eff | 2013-01-07 11:09:06 +0000 | [diff] [blame] | 407 | " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; // Trailing comment"); |
Daniel Jasper | 043835a | 2013-01-09 09:33:39 +0000 | [diff] [blame] | 408 | verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" |
| 409 | " // Comment inside a statement.\n" |
| 410 | " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;"); |
Manuel Klimek | 6cf5814 | 2013-01-07 08:54:53 +0000 | [diff] [blame] | 411 | |
Daniel Jasper | 821627e | 2013-01-21 22:49:20 +0000 | [diff] [blame] | 412 | EXPECT_EQ("void f() { // This does something ..\n" |
| 413 | "}\n" |
| 414 | "int a; // This is unrelated", |
| 415 | format("void f() { // This does something ..\n" |
| 416 | " }\n" |
| 417 | "int a; // This is unrelated")); |
| 418 | EXPECT_EQ("void f() { // This does something ..\n" |
| 419 | "} // awesome..\n" |
| 420 | "\n" |
| 421 | "int a; // This is unrelated", |
| 422 | format("void f() { // This does something ..\n" |
| 423 | " } // awesome..\n" |
| 424 | " \n" |
| 425 | "int a; // This is unrelated")); |
| 426 | |
Daniel Jasper | 7ad4eff | 2013-01-07 11:09:06 +0000 | [diff] [blame] | 427 | EXPECT_EQ("int i; // single line trailing comment", |
Manuel Klimek | 6cf5814 | 2013-01-07 08:54:53 +0000 | [diff] [blame] | 428 | format("int i;\\\n// single line trailing comment")); |
Daniel Jasper | 7ad4eff | 2013-01-07 11:09:06 +0000 | [diff] [blame] | 429 | |
| 430 | verifyGoogleFormat("int a; // Trailing comment."); |
Daniel Jasper | 05b1ac8 | 2012-12-17 11:29:41 +0000 | [diff] [blame] | 431 | } |
| 432 | |
| 433 | TEST_F(FormatTest, UnderstandsMultiLineComments) { |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 434 | verifyFormat("f(/*test=*/ true);"); |
Daniel Jasper | 2c6cc48 | 2013-01-17 12:53:34 +0000 | [diff] [blame] | 435 | EXPECT_EQ( |
| 436 | "f(aaaaaaaaaaaaaaaaaaaaaaaaa, /* Trailing comment for aa... */\n" |
| 437 | " bbbbbbbbbbbbbbbbbbbbbbbbb);", |
| 438 | format("f(aaaaaaaaaaaaaaaaaaaaaaaaa , /* Trailing comment for aa... */\n" |
| 439 | " bbbbbbbbbbbbbbbbbbbbbbbbb);")); |
| 440 | EXPECT_EQ( |
| 441 | "f(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" |
| 442 | " /* Leading comment for bb... */ bbbbbbbbbbbbbbbbbbbbbbbbb);", |
| 443 | format("f(aaaaaaaaaaaaaaaaaaaaaaaaa , \n" |
| 444 | "/* Leading comment for bb... */ bbbbbbbbbbbbbbbbbbbbbbbbb);")); |
| 445 | } |
| 446 | |
| 447 | TEST_F(FormatTest, CommentsInStaticInitializers) { |
| 448 | EXPECT_EQ( |
| 449 | "static SomeType type = { aaaaaaaaaaaaaaaaaaaa, /* comment */\n" |
| 450 | " aaaaaaaaaaaaaaaaaaaa /* comment */,\n" |
| 451 | " /* comment */ aaaaaaaaaaaaaaaaaaaa,\n" |
| 452 | " aaaaaaaaaaaaaaaaaaaa, // comment\n" |
| 453 | " aaaaaaaaaaaaaaaaaaaa };", |
| 454 | format("static SomeType type = { aaaaaaaaaaaaaaaaaaaa , /* comment */\n" |
| 455 | " aaaaaaaaaaaaaaaaaaaa /* comment */ ,\n" |
| 456 | " /* comment */ aaaaaaaaaaaaaaaaaaaa ,\n" |
| 457 | " aaaaaaaaaaaaaaaaaaaa , // comment\n" |
| 458 | " aaaaaaaaaaaaaaaaaaaa };")); |
| 459 | verifyFormat("static SomeType type = { aaaaaaaaaaa, // comment for aa...\n" |
| 460 | " bbbbbbbbbbb, ccccccccccc };"); |
| 461 | verifyFormat("static SomeType type = { aaaaaaaaaaa,\n" |
| 462 | " // comment for bb....\n" |
| 463 | " bbbbbbbbbbb, ccccccccccc };"); |
| 464 | verifyGoogleFormat( |
| 465 | "static SomeType type = { aaaaaaaaaaa, // comment for aa...\n" |
| 466 | " bbbbbbbbbbb,\n" |
| 467 | " ccccccccccc };"); |
| 468 | verifyGoogleFormat("static SomeType type = { aaaaaaaaaaa,\n" |
| 469 | " // comment for bb....\n" |
| 470 | " bbbbbbbbbbb,\n" |
| 471 | " ccccccccccc };"); |
| 472 | |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 473 | } |
| 474 | |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 475 | //===----------------------------------------------------------------------===// |
| 476 | // Tests for classes, namespaces, etc. |
| 477 | //===----------------------------------------------------------------------===// |
| 478 | |
| 479 | TEST_F(FormatTest, DoesNotBreakSemiAfterClassDecl) { |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 480 | verifyFormat("class A {\n};"); |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 481 | } |
| 482 | |
| 483 | TEST_F(FormatTest, UnderstandsAccessSpecifiers) { |
| 484 | verifyFormat("class A {\n" |
| 485 | "public:\n" |
| 486 | "protected:\n" |
| 487 | "private:\n" |
Manuel Klimek | 36fab8d | 2013-01-10 13:24:24 +0000 | [diff] [blame] | 488 | " void f() {}\n" |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 489 | "};"); |
| 490 | verifyGoogleFormat("class A {\n" |
| 491 | " public:\n" |
| 492 | " protected:\n" |
| 493 | " private:\n" |
Manuel Klimek | 36fab8d | 2013-01-10 13:24:24 +0000 | [diff] [blame] | 494 | " void f() {}\n" |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 495 | "};"); |
| 496 | } |
| 497 | |
| 498 | TEST_F(FormatTest, FormatsDerivedClass) { |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 499 | verifyFormat("class A : public B {\n};"); |
| 500 | verifyFormat("class A : public ::B {\n};"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 501 | } |
| 502 | |
Manuel Klimek | de76854 | 2013-01-07 18:10:23 +0000 | [diff] [blame] | 503 | TEST_F(FormatTest, FormatsVariableDeclarationsAfterStructOrClass) { |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 504 | verifyFormat("class A {\n} a, b;"); |
| 505 | verifyFormat("struct A {\n} a, b;"); |
| 506 | verifyFormat("union A {\n} a;"); |
Manuel Klimek | de76854 | 2013-01-07 18:10:23 +0000 | [diff] [blame] | 507 | } |
| 508 | |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 509 | TEST_F(FormatTest, FormatsEnum) { |
Alexander Kornienko | a166e73 | 2012-12-04 14:46:19 +0000 | [diff] [blame] | 510 | verifyFormat("enum {\n" |
| 511 | " Zero,\n" |
| 512 | " One = 1,\n" |
| 513 | " Two = One + 1,\n" |
| 514 | " Three = (One + Two),\n" |
| 515 | " Four = (Zero && (One ^ Two)) | (One << Two),\n" |
| 516 | " Five = (One, Two, Three, Four, 5)\n" |
| 517 | "};"); |
| 518 | verifyFormat("enum Enum {\n" |
| 519 | "};"); |
| 520 | verifyFormat("enum {\n" |
| 521 | "};"); |
Manuel Klimek | 308232c | 2013-01-21 19:17:52 +0000 | [diff] [blame] | 522 | verifyFormat("enum X E {\n} d;"); |
| 523 | verifyFormat("enum __attribute__((...)) E {\n} d;"); |
| 524 | verifyFormat("enum __declspec__((...)) E {\n} d;"); |
| 525 | verifyFormat("enum X f() {\n a();\n return 42;\n}"); |
Alexander Kornienko | a166e73 | 2012-12-04 14:46:19 +0000 | [diff] [blame] | 526 | } |
| 527 | |
Nico Weber | efaddc0 | 2013-01-14 05:49:49 +0000 | [diff] [blame] | 528 | TEST_F(FormatTest, FormatsBitfields) { |
| 529 | verifyFormat("struct Bitfields {\n" |
| 530 | " unsigned sClass : 8;\n" |
| 531 | " unsigned ValueKind : 2;\n" |
| 532 | "};"); |
| 533 | } |
| 534 | |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 535 | TEST_F(FormatTest, FormatsNamespaces) { |
| 536 | verifyFormat("namespace some_namespace {\n" |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 537 | "class A {\n};\n" |
Manuel Klimek | 517e894 | 2013-01-11 17:54:10 +0000 | [diff] [blame] | 538 | "void f() { f(); }\n" |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 539 | "}"); |
| 540 | verifyFormat("namespace {\n" |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 541 | "class A {\n};\n" |
Manuel Klimek | 517e894 | 2013-01-11 17:54:10 +0000 | [diff] [blame] | 542 | "void f() { f(); }\n" |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 543 | "}"); |
Dmitri Gribenko | 1f94f2b | 2012-12-30 21:27:25 +0000 | [diff] [blame] | 544 | verifyFormat("inline namespace X {\n" |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 545 | "class A {\n};\n" |
Manuel Klimek | 517e894 | 2013-01-11 17:54:10 +0000 | [diff] [blame] | 546 | "void f() { f(); }\n" |
Dmitri Gribenko | 1f94f2b | 2012-12-30 21:27:25 +0000 | [diff] [blame] | 547 | "}"); |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 548 | verifyFormat("using namespace some_namespace;\n" |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 549 | "class A {\n};\n" |
Manuel Klimek | 517e894 | 2013-01-11 17:54:10 +0000 | [diff] [blame] | 550 | "void f() { f(); }"); |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 551 | } |
| 552 | |
Manuel Klimek | d19dc2d | 2013-01-21 14:32:05 +0000 | [diff] [blame] | 553 | TEST_F(FormatTest, FormatsExternC) { |
| 554 | verifyFormat("extern \"C\" {\nint a;"); |
| 555 | } |
| 556 | |
Nico Weber | a9ccdd1 | 2013-01-07 16:36:17 +0000 | [diff] [blame] | 557 | TEST_F(FormatTest, FormatTryCatch) { |
Manuel Klimek | 517e894 | 2013-01-11 17:54:10 +0000 | [diff] [blame] | 558 | // FIXME: Handle try-catch explicitly in the UnwrappedLineParser, then we'll |
| 559 | // also not create single-line-blocks. |
Nico Weber | a9ccdd1 | 2013-01-07 16:36:17 +0000 | [diff] [blame] | 560 | verifyFormat("try {\n" |
| 561 | " throw a * b;\n" |
| 562 | "}\n" |
| 563 | "catch (int a) {\n" |
| 564 | " // Do nothing.\n" |
| 565 | "}\n" |
| 566 | "catch (...) {\n" |
| 567 | " exit(42);\n" |
| 568 | "}"); |
| 569 | |
| 570 | // Function-level try statements. |
Manuel Klimek | 517e894 | 2013-01-11 17:54:10 +0000 | [diff] [blame] | 571 | verifyFormat("int f() try { return 4; }\n" |
Nico Weber | a9ccdd1 | 2013-01-07 16:36:17 +0000 | [diff] [blame] | 572 | "catch (...) {\n" |
| 573 | " return 5;\n" |
| 574 | "}"); |
| 575 | verifyFormat("class A {\n" |
| 576 | " int a;\n" |
Manuel Klimek | 36fab8d | 2013-01-10 13:24:24 +0000 | [diff] [blame] | 577 | " A() try : a(0) {}\n" |
Nico Weber | a9ccdd1 | 2013-01-07 16:36:17 +0000 | [diff] [blame] | 578 | " catch (...) {\n" |
| 579 | " throw;\n" |
| 580 | " }\n" |
| 581 | "};\n"); |
| 582 | } |
| 583 | |
| 584 | TEST_F(FormatTest, FormatObjCTryCatch) { |
Manuel Klimek | d5688cf | 2013-01-11 19:17:44 +0000 | [diff] [blame] | 585 | verifyFormat("@try {\n" |
| 586 | " f();\n" |
| 587 | "}\n" |
| 588 | "@catch (NSException e) {\n" |
| 589 | " @throw;\n" |
| 590 | "}\n" |
| 591 | "@finally {\n" |
| 592 | " exit(42);\n" |
| 593 | "}"); |
Nico Weber | a9ccdd1 | 2013-01-07 16:36:17 +0000 | [diff] [blame] | 594 | } |
| 595 | |
Daniel Jasper | 05b1ac8 | 2012-12-17 11:29:41 +0000 | [diff] [blame] | 596 | TEST_F(FormatTest, StaticInitializers) { |
| 597 | verifyFormat("static SomeClass SC = { 1, 'a' };"); |
| 598 | |
| 599 | // FIXME: Format like enums if the static initializer does not fit on a line. |
| 600 | verifyFormat( |
Daniel Jasper | 9cda800 | 2013-01-07 13:08:40 +0000 | [diff] [blame] | 601 | "static SomeClass WithALoooooooooooooooooooongName = {\n" |
Manuel Klimek | c8c8a47 | 2013-01-10 15:58:26 +0000 | [diff] [blame] | 602 | " 100000000, \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n" |
| 603 | "};"); |
| 604 | |
| 605 | verifyFormat( |
| 606 | "static SomeClass = { a, b, c, d, e, f, g, h, i, j,\n" |
| 607 | " looooooooooooooooooooooooooooooooooongname,\n" |
| 608 | " looooooooooooooooooooooooooooooong };"); |
Daniel Jasper | 05b1ac8 | 2012-12-17 11:29:41 +0000 | [diff] [blame] | 609 | } |
| 610 | |
Manuel Klimek | 2c7739e | 2013-01-14 16:41:43 +0000 | [diff] [blame] | 611 | TEST_F(FormatTest, NestedStaticInitializers) { |
| 612 | verifyFormat("static A x = { { {} } };\n"); |
| 613 | verifyFormat( |
Daniel Jasper | 0df6acd | 2013-01-16 14:59:02 +0000 | [diff] [blame] | 614 | "static A x = { { { init1, init2, init3, init4 },\n" |
| 615 | " { init1, init2, init3, init4 } } };"); |
| 616 | |
Nico Weber | 6a21a55 | 2013-01-18 02:43:57 +0000 | [diff] [blame] | 617 | // FIXME: Fix this in general and verify that it works in LLVM style again. |
Daniel Jasper | 0df6acd | 2013-01-16 14:59:02 +0000 | [diff] [blame] | 618 | verifyGoogleFormat( |
Manuel Klimek | 2c7739e | 2013-01-14 16:41:43 +0000 | [diff] [blame] | 619 | "somes Status::global_reps[3] = {\n" |
| 620 | " { kGlobalRef, OK_CODE, NULL, NULL, NULL },\n" |
| 621 | " { kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL },\n" |
| 622 | " { kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL }\n" |
| 623 | "};"); |
| 624 | verifyFormat( |
| 625 | "CGRect cg_rect = { { rect.fLeft, rect.fTop },\n" |
| 626 | " { rect.fRight - rect.fLeft, rect.fBottom - rect.fTop" |
| 627 | " } };"); |
| 628 | |
Nico Weber | 6a21a55 | 2013-01-18 02:43:57 +0000 | [diff] [blame] | 629 | // FIXME: We might at some point want to handle this similar to parameter |
Manuel Klimek | 2c7739e | 2013-01-14 16:41:43 +0000 | [diff] [blame] | 630 | // lists, where we have an option to put each on a single line. |
| 631 | verifyFormat("struct {\n" |
| 632 | " unsigned bit;\n" |
| 633 | " const char *const name;\n" |
| 634 | "} kBitsToOs[] = { { kOsMac, \"Mac\" }, { kOsWin, \"Windows\" },\n" |
| 635 | " { kOsLinux, \"Linux\" }, { kOsCrOS, \"Chrome OS\" } };"); |
| 636 | } |
| 637 | |
Manuel Klimek | a080a18 | 2013-01-02 16:30:12 +0000 | [diff] [blame] | 638 | TEST_F(FormatTest, FormatsSmallMacroDefinitionsInSingleLine) { |
| 639 | verifyFormat("#define ALooooooooooooooooooooooooooooooooooooooongMacro(" |
| 640 | " \\\n" |
| 641 | " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)"); |
| 642 | } |
| 643 | |
Daniel Jasper | 7160751 | 2013-01-07 10:48:50 +0000 | [diff] [blame] | 644 | TEST_F(FormatTest, DoesNotBreakPureVirtualFunctionDefinition) { |
Daniel Jasper | cd16238 | 2013-01-07 13:26:07 +0000 | [diff] [blame] | 645 | verifyFormat("virtual void write(ELFWriter *writerrr,\n" |
| 646 | " OwningPtr<FileOutputBuffer> &buffer) = 0;"); |
Daniel Jasper | 7160751 | 2013-01-07 10:48:50 +0000 | [diff] [blame] | 647 | } |
| 648 | |
Manuel Klimek | a080a18 | 2013-01-02 16:30:12 +0000 | [diff] [blame] | 649 | TEST_F(FormatTest, BreaksOnHashWhenDirectiveIsInvalid) { |
| 650 | EXPECT_EQ("#\n;", format("#;")); |
Manuel Klimek | dd5b101 | 2013-01-07 10:03:37 +0000 | [diff] [blame] | 651 | verifyFormat("#\n;\n;\n;"); |
Manuel Klimek | a080a18 | 2013-01-02 16:30:12 +0000 | [diff] [blame] | 652 | } |
| 653 | |
| 654 | TEST_F(FormatTest, UnescapedEndOfLineEndsPPDirective) { |
| 655 | EXPECT_EQ("#line 42 \"test\"\n", |
| 656 | format("# \\\n line \\\n 42 \\\n \"test\"\n")); |
Daniel Jasper | e0b15ea | 2013-01-14 15:40:57 +0000 | [diff] [blame] | 657 | EXPECT_EQ("#define A B\n", |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 658 | format("# \\\n define \\\n A \\\n B\n", |
| 659 | getLLVMStyleWithColumns(12))); |
Manuel Klimek | a080a18 | 2013-01-02 16:30:12 +0000 | [diff] [blame] | 660 | } |
| 661 | |
| 662 | TEST_F(FormatTest, EndOfFileEndsPPDirective) { |
| 663 | EXPECT_EQ("#line 42 \"test\"", |
| 664 | format("# \\\n line \\\n 42 \\\n \"test\"")); |
Daniel Jasper | e0b15ea | 2013-01-14 15:40:57 +0000 | [diff] [blame] | 665 | EXPECT_EQ("#define A B", |
| 666 | format("# \\\n define \\\n A \\\n B")); |
Manuel Klimek | a080a18 | 2013-01-02 16:30:12 +0000 | [diff] [blame] | 667 | } |
| 668 | |
Manuel Klimek | 060143e | 2013-01-02 18:33:23 +0000 | [diff] [blame] | 669 | TEST_F(FormatTest, IndentsPPDirectiveInReducedSpace) { |
Manuel Klimek | d544c57 | 2013-01-07 09:24:17 +0000 | [diff] [blame] | 670 | // If the macro fits in one line, we still do not get the full |
| 671 | // line, as only the next line decides whether we need an escaped newline and |
| 672 | // thus use the last column. |
| 673 | verifyFormat("#define A(B)", getLLVMStyleWithColumns(13)); |
Manuel Klimek | 060143e | 2013-01-02 18:33:23 +0000 | [diff] [blame] | 674 | |
Manuel Klimek | d544c57 | 2013-01-07 09:24:17 +0000 | [diff] [blame] | 675 | verifyFormat("#define A( \\\n B)", getLLVMStyleWithColumns(12)); |
| 676 | verifyFormat("#define AA(\\\n B)", getLLVMStyleWithColumns(12)); |
Manuel Klimek | 060143e | 2013-01-02 18:33:23 +0000 | [diff] [blame] | 677 | verifyFormat("#define A( \\\n A, B)", getLLVMStyleWithColumns(12)); |
Daniel Jasper | e0b15ea | 2013-01-14 15:40:57 +0000 | [diff] [blame] | 678 | |
| 679 | verifyFormat("#define A A\n#define A A"); |
| 680 | verifyFormat("#define A(X) A\n#define A A"); |
| 681 | |
| 682 | verifyFormat("#define Something Other", getLLVMStyleWithColumns(24)); |
| 683 | verifyFormat("#define Something \\\n" |
| 684 | " Other", getLLVMStyleWithColumns(23)); |
Manuel Klimek | 060143e | 2013-01-02 18:33:23 +0000 | [diff] [blame] | 685 | } |
| 686 | |
| 687 | TEST_F(FormatTest, HandlePreprocessorDirectiveContext) { |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 688 | EXPECT_EQ("// some comment\n" |
| 689 | "#include \"a.h\"\n" |
| 690 | "#define A(A,\\\n" |
| 691 | " B)\n" |
| 692 | "#include \"b.h\"\n" |
| 693 | "// some comment\n", |
| 694 | format(" // some comment\n" |
| 695 | " #include \"a.h\"\n" |
| 696 | "#define A(A,\\\n" |
| 697 | " B)\n" |
| 698 | " #include \"b.h\"\n" |
| 699 | " // some comment\n", getLLVMStyleWithColumns(13))); |
Manuel Klimek | 060143e | 2013-01-02 18:33:23 +0000 | [diff] [blame] | 700 | } |
| 701 | |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 702 | TEST_F(FormatTest, LayoutSingleHash) { |
| 703 | EXPECT_EQ("#\na;", format("#\na;")); |
| 704 | } |
| 705 | |
| 706 | TEST_F(FormatTest, LayoutCodeInMacroDefinitions) { |
| 707 | EXPECT_EQ("#define A \\\n" |
| 708 | " c; \\\n" |
| 709 | " e;\n" |
| 710 | "f;", format("#define A c; e;\n" |
| 711 | "f;", getLLVMStyleWithColumns(14))); |
| 712 | } |
| 713 | |
| 714 | TEST_F(FormatTest, LayoutRemainingTokens) { |
Manuel Klimek | 36fab8d | 2013-01-10 13:24:24 +0000 | [diff] [blame] | 715 | EXPECT_EQ("{}", format("{}")); |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 716 | } |
| 717 | |
| 718 | TEST_F(FormatTest, LayoutSingleUnwrappedLineInMacro) { |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 719 | EXPECT_EQ("# define A\\\n b;", |
| 720 | format("# define A b;", 11, 2, getLLVMStyleWithColumns(11))); |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 721 | } |
| 722 | |
| 723 | TEST_F(FormatTest, MacroDefinitionInsideStatement) { |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 724 | EXPECT_EQ("int x,\n" |
| 725 | "#define A\n" |
| 726 | " y;", format("int x,\n#define A\ny;")); |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 727 | } |
| 728 | |
Manuel Klimek | 6f8424b | 2013-01-05 21:34:55 +0000 | [diff] [blame] | 729 | TEST_F(FormatTest, HashInMacroDefinition) { |
| 730 | verifyFormat("#define A \\\n b #c;", getLLVMStyleWithColumns(11)); |
| 731 | verifyFormat("#define A \\\n" |
| 732 | " { \\\n" |
| 733 | " f(#c);\\\n" |
| 734 | " }", getLLVMStyleWithColumns(11)); |
Daniel Jasper | 765561f | 2013-01-08 16:17:54 +0000 | [diff] [blame] | 735 | |
| 736 | verifyFormat("#define A(X) \\\n" |
| 737 | " void function##X()", getLLVMStyleWithColumns(22)); |
| 738 | |
| 739 | verifyFormat("#define A(a, b, c) \\\n" |
| 740 | " void a##b##c()", getLLVMStyleWithColumns(22)); |
| 741 | |
Daniel Jasper | e0b15ea | 2013-01-14 15:40:57 +0000 | [diff] [blame] | 742 | verifyFormat("#define A void # ## #", getLLVMStyleWithColumns(22)); |
Manuel Klimek | 6f8424b | 2013-01-05 21:34:55 +0000 | [diff] [blame] | 743 | } |
| 744 | |
Manuel Klimek | 7ccbc21 | 2013-01-23 14:37:36 +0000 | [diff] [blame] | 745 | TEST_F(FormatTest, RespectWhitespaceInMacroDefinitions) { |
| 746 | verifyFormat("#define A (1)"); |
| 747 | } |
| 748 | |
Manuel Klimek | c37b4d6 | 2013-01-05 22:14:16 +0000 | [diff] [blame] | 749 | TEST_F(FormatTest, IndentPreprocessorDirectivesAtZero) { |
| 750 | EXPECT_EQ("{\n {\n#define A\n }\n}", format("{{\n#define A\n}}")); |
| 751 | } |
| 752 | |
Manuel Klimek | f6fd00b | 2013-01-05 22:56:06 +0000 | [diff] [blame] | 753 | TEST_F(FormatTest, FormatHashIfNotAtStartOfLine) { |
Manuel Klimek | 517e894 | 2013-01-11 17:54:10 +0000 | [diff] [blame] | 754 | verifyFormat("{\n { a #c; }\n}"); |
Manuel Klimek | f6fd00b | 2013-01-05 22:56:06 +0000 | [diff] [blame] | 755 | } |
| 756 | |
Manuel Klimek | a5342db | 2013-01-06 20:07:31 +0000 | [diff] [blame] | 757 | TEST_F(FormatTest, FormatUnbalancedStructuralElements) { |
| 758 | EXPECT_EQ("#define A \\\n { \\\n {\nint i;", |
| 759 | format("#define A { {\nint i;", getLLVMStyleWithColumns(11))); |
| 760 | EXPECT_EQ("#define A \\\n } \\\n }\nint i;", |
| 761 | format("#define A } }\nint i;", getLLVMStyleWithColumns(11))); |
| 762 | } |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 763 | |
Manuel Klimek | 9541938 | 2013-01-07 07:56:50 +0000 | [diff] [blame] | 764 | TEST_F(FormatTest, EscapedNewlineAtStartOfTokenInMacroDefinition) { |
Daniel Jasper | cd16238 | 2013-01-07 13:26:07 +0000 | [diff] [blame] | 765 | EXPECT_EQ( |
| 766 | "#define A \\\n int i; \\\n int j;", |
| 767 | format("#define A \\\nint i;\\\n int j;", getLLVMStyleWithColumns(11))); |
Manuel Klimek | 9541938 | 2013-01-07 07:56:50 +0000 | [diff] [blame] | 768 | } |
| 769 | |
Manuel Klimek | d544c57 | 2013-01-07 09:24:17 +0000 | [diff] [blame] | 770 | TEST_F(FormatTest, CalculateSpaceOnConsecutiveLinesInMacro) { |
| 771 | verifyFormat("#define A \\\n" |
| 772 | " int v( \\\n" |
| 773 | " a); \\\n" |
| 774 | " int i;", getLLVMStyleWithColumns(11)); |
| 775 | } |
| 776 | |
Manuel Klimek | a080a18 | 2013-01-02 16:30:12 +0000 | [diff] [blame] | 777 | TEST_F(FormatTest, MixingPreprocessorDirectivesAndNormalCode) { |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 778 | EXPECT_EQ( |
| 779 | "#define ALooooooooooooooooooooooooooooooooooooooongMacro(" |
| 780 | " \\\n" |
| 781 | " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n" |
| 782 | "\n" |
| 783 | "AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n" |
| 784 | " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n", |
| 785 | format(" #define ALooooooooooooooooooooooooooooooooooooooongMacro(" |
| 786 | "\\\n" |
| 787 | "aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n" |
| 788 | " \n" |
| 789 | " AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n" |
| 790 | " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n")); |
Manuel Klimek | a080a18 | 2013-01-02 16:30:12 +0000 | [diff] [blame] | 791 | } |
| 792 | |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 793 | TEST_F(FormatTest, LayoutStatementsAroundPreprocessorDirectives) { |
| 794 | EXPECT_EQ("int\n" |
| 795 | "#define A\n" |
| 796 | " a;", |
| 797 | format("int\n#define A\na;")); |
| 798 | verifyFormat( |
| 799 | "functionCallTo(someOtherFunction(\n" |
| 800 | " withSomeParameters, whichInSequence,\n" |
| 801 | " areLongerThanALine(andAnotherCall,\n" |
Daniel Jasper | e0b15ea | 2013-01-14 15:40:57 +0000 | [diff] [blame] | 802 | "#define A B\n" |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 803 | " withMoreParamters,\n" |
| 804 | " whichStronglyInfluenceTheLayout),\n" |
Daniel Jasper | 0df6acd | 2013-01-16 14:59:02 +0000 | [diff] [blame] | 805 | " andMoreParameters), trailing);", getLLVMStyleWithColumns(69)); |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 806 | } |
| 807 | |
Manuel Klimek | bb42bf1 | 2013-01-10 11:52:21 +0000 | [diff] [blame] | 808 | TEST_F(FormatTest, LayoutBlockInsideParens) { |
| 809 | EXPECT_EQ("functionCall({\n" |
| 810 | " int i;\n" |
| 811 | "});", format(" functionCall ( {int i;} );")); |
| 812 | } |
| 813 | |
| 814 | TEST_F(FormatTest, LayoutBlockInsideStatement) { |
Manuel Klimek | 517e894 | 2013-01-11 17:54:10 +0000 | [diff] [blame] | 815 | EXPECT_EQ("SOME_MACRO { int i; }\n" |
Manuel Klimek | bb42bf1 | 2013-01-10 11:52:21 +0000 | [diff] [blame] | 816 | "int i;", format(" SOME_MACRO {int i;} int i;")); |
| 817 | } |
| 818 | |
| 819 | TEST_F(FormatTest, LayoutNestedBlocks) { |
| 820 | verifyFormat("void AddOsStrings(unsigned bitmask) {\n" |
| 821 | " struct s {\n" |
| 822 | " int i;\n" |
| 823 | " };\n" |
| 824 | " s kBitsToOs[] = { { 10 } };\n" |
| 825 | " for (int i = 0; i < 10; ++i)\n" |
| 826 | " return;\n" |
| 827 | "}"); |
| 828 | } |
| 829 | |
Manuel Klimek | 36fab8d | 2013-01-10 13:24:24 +0000 | [diff] [blame] | 830 | TEST_F(FormatTest, PutEmptyBlocksIntoOneLine) { |
| 831 | EXPECT_EQ("{}", format("{}")); |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 832 | |
| 833 | // Negative test for enum. |
| 834 | verifyFormat("enum E {\n};"); |
| 835 | |
| 836 | // Note that when there's a missing ';', we still join... |
| 837 | verifyFormat("enum E {}"); |
Manuel Klimek | 36fab8d | 2013-01-10 13:24:24 +0000 | [diff] [blame] | 838 | } |
| 839 | |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 840 | //===----------------------------------------------------------------------===// |
| 841 | // Line break tests. |
| 842 | //===----------------------------------------------------------------------===// |
| 843 | |
| 844 | TEST_F(FormatTest, FormatsFunctionDefinition) { |
| 845 | verifyFormat("void f(int a, int b, int c, int d, int e, int f, int g," |
| 846 | " int h, int j, int f,\n" |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 847 | " int c, int ddddddddddddd) {\n}"); |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 848 | } |
| 849 | |
| 850 | TEST_F(FormatTest, FormatsAwesomeMethodCall) { |
| 851 | verifyFormat( |
Daniel Jasper | a4974cf | 2012-12-24 16:43:00 +0000 | [diff] [blame] | 852 | "SomeLongMethodName(SomeReallyLongMethod(\n" |
| 853 | " CallOtherReallyLongMethod(parameter, parameter, parameter)),\n" |
| 854 | " SecondLongCall(parameter));"); |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 855 | } |
| 856 | |
Daniel Jasper | 1321eb5 | 2012-12-18 21:05:13 +0000 | [diff] [blame] | 857 | TEST_F(FormatTest, ConstructorInitializers) { |
Manuel Klimek | 36fab8d | 2013-01-10 13:24:24 +0000 | [diff] [blame] | 858 | verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}"); |
Daniel Jasper | 7e9bf8c | 2013-01-11 11:37:55 +0000 | [diff] [blame] | 859 | verifyFormat("Constructor() : Inttializer(FitsOnTheLine) {}", |
| 860 | getLLVMStyleWithColumns(45)); |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 861 | verifyFormat("Constructor() : Inttializer(FitsOnTheLine) {\n}", |
Daniel Jasper | 7e9bf8c | 2013-01-11 11:37:55 +0000 | [diff] [blame] | 862 | getLLVMStyleWithColumns(44)); |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 863 | verifyFormat("Constructor()\n" |
| 864 | " : Inttializer(FitsOnTheLine) {\n}", |
| 865 | getLLVMStyleWithColumns(43)); |
Daniel Jasper | 1321eb5 | 2012-12-18 21:05:13 +0000 | [diff] [blame] | 866 | |
| 867 | verifyFormat( |
| 868 | "SomeClass::Constructor()\n" |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 869 | " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {\n}"); |
Daniel Jasper | 1321eb5 | 2012-12-18 21:05:13 +0000 | [diff] [blame] | 870 | |
| 871 | verifyFormat( |
| 872 | "SomeClass::Constructor()\n" |
Daniel Jasper | 7e9bf8c | 2013-01-11 11:37:55 +0000 | [diff] [blame] | 873 | " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 874 | " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {\n}"); |
Daniel Jasper | 7e9bf8c | 2013-01-11 11:37:55 +0000 | [diff] [blame] | 875 | verifyGoogleFormat( |
| 876 | "SomeClass::Constructor()\n" |
| 877 | " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" |
| 878 | " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 879 | " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {\n}"); |
Daniel Jasper | 1661824 | 2013-01-16 17:00:50 +0000 | [diff] [blame] | 880 | verifyGoogleFormat( |
| 881 | "SomeClass::Constructor()\n" |
| 882 | " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n" |
| 883 | " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 884 | " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {\n}"); |
Daniel Jasper | 7e9bf8c | 2013-01-11 11:37:55 +0000 | [diff] [blame] | 885 | |
| 886 | verifyFormat( |
| 887 | "SomeClass::Constructor()\n" |
Daniel Jasper | 1321eb5 | 2012-12-18 21:05:13 +0000 | [diff] [blame] | 888 | " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 889 | " aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {\n}"); |
Daniel Jasper | 1321eb5 | 2012-12-18 21:05:13 +0000 | [diff] [blame] | 890 | |
| 891 | verifyFormat("Constructor()\n" |
| 892 | " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" |
| 893 | " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" |
| 894 | " aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 895 | " aaaaaaaaaaaaaaaaaaaaaaa() {\n}"); |
Daniel Jasper | 1321eb5 | 2012-12-18 21:05:13 +0000 | [diff] [blame] | 896 | |
| 897 | // Here a line could be saved by splitting the second initializer onto two |
| 898 | // lines, but that is not desireable. |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 899 | verifyFormat( |
| 900 | "Constructor()\n" |
| 901 | " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n" |
| 902 | " aaaaaaaaaaa(aaaaaaaaaaa),\n" |
| 903 | " aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); |
Daniel Jasper | 1321eb5 | 2012-12-18 21:05:13 +0000 | [diff] [blame] | 904 | |
| 905 | verifyGoogleFormat("MyClass::MyClass(int var)\n" |
Daniel Jasper | dcc2a62 | 2013-01-18 08:44:07 +0000 | [diff] [blame] | 906 | " : some_var_(var), // 4 space indent\n" |
Daniel Jasper | 1321eb5 | 2012-12-18 21:05:13 +0000 | [diff] [blame] | 907 | " some_other_var_(var + 1) { // lined up\n" |
| 908 | "}"); |
Daniel Jasper | b312314 | 2013-01-12 07:36:22 +0000 | [diff] [blame] | 909 | |
| 910 | // This test takes VERY long when memoization is broken. |
Daniel Jasper | 0df6acd | 2013-01-16 14:59:02 +0000 | [diff] [blame] | 911 | std::string input = "Constructor()\n" |
Daniel Jasper | 3499dda | 2013-01-25 15:43:32 +0000 | [diff] [blame] | 912 | " : aaaa(a,\n"; |
Daniel Jasper | 0df6acd | 2013-01-16 14:59:02 +0000 | [diff] [blame] | 913 | for (unsigned i = 0, e = 80; i != e; ++i) { |
| 914 | input += " a,\n"; |
| 915 | } |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 916 | input += " a) {\n}"; |
Daniel Jasper | 0df6acd | 2013-01-16 14:59:02 +0000 | [diff] [blame] | 917 | verifyGoogleFormat(input); |
Daniel Jasper | 1321eb5 | 2012-12-18 21:05:13 +0000 | [diff] [blame] | 918 | } |
| 919 | |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 920 | TEST_F(FormatTest, BreaksAsHighAsPossible) { |
| 921 | verifyFormat( |
| 922 | "if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa && aaaaaaaaaaaaaaaaaaaaaaaaaa) ||\n" |
| 923 | " (bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb && bbbbbbbbbbbbbbbbbbbbbbbbbb))\n" |
| 924 | " f();"); |
| 925 | } |
| 926 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 927 | TEST_F(FormatTest, BreaksDesireably) { |
| 928 | verifyFormat("if (aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n" |
| 929 | " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n" |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 930 | " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa)) {\n}"); |
Daniel Jasper | e438bac | 2013-01-23 20:41:06 +0000 | [diff] [blame] | 931 | verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" |
| 932 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n" |
| 933 | "}"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 934 | |
| 935 | verifyFormat( |
| 936 | "aaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 937 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 938 | |
| 939 | verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" |
| 940 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" |
| 941 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));"); |
Daniel Jasper | a88bb45 | 2012-12-04 10:50:12 +0000 | [diff] [blame] | 942 | |
| 943 | verifyFormat( |
| 944 | "aaaaaaaa(aaaaaaaaaaaaa, aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" |
| 945 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n" |
| 946 | " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" |
| 947 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));"); |
Daniel Jasper | 33182dd | 2012-12-05 14:57:28 +0000 | [diff] [blame] | 948 | |
| 949 | verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" |
| 950 | " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); |
| 951 | |
Daniel Jasper | 723f030 | 2013-01-02 14:40:02 +0000 | [diff] [blame] | 952 | verifyFormat( |
| 953 | "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&\n" |
| 954 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); |
Daniel Jasper | 3499dda | 2013-01-25 15:43:32 +0000 | [diff] [blame] | 955 | verifyFormat( |
| 956 | "aaaaaa(new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" |
| 957 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));"); |
| 958 | verifyFormat( |
| 959 | "aaaaaa(aaa, new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" |
| 960 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));"); |
Daniel Jasper | 723f030 | 2013-01-02 14:40:02 +0000 | [diff] [blame] | 961 | |
Daniel Jasper | 33182dd | 2012-12-05 14:57:28 +0000 | [diff] [blame] | 962 | // This test case breaks on an incorrect memoization, i.e. an optimization not |
| 963 | // taking into account the StopAt value. |
| 964 | verifyFormat( |
| 965 | "return aaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n" |
Daniel Jasper | cf225b6 | 2012-12-24 13:43:52 +0000 | [diff] [blame] | 966 | " aaaaaaaaaaa(aaaaaaaaa) || aaaaaaaaaaaaaaaaaaaaaaa ||\n" |
| 967 | " aaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n" |
| 968 | " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); |
Daniel Jasper | a4974cf | 2012-12-24 16:43:00 +0000 | [diff] [blame] | 969 | |
Daniel Jasper | cd16238 | 2013-01-07 13:26:07 +0000 | [diff] [blame] | 970 | verifyFormat("{\n {\n {\n" |
| 971 | " Annotation.SpaceRequiredBefore =\n" |
| 972 | " Line.Tokens[i - 1].Tok.isNot(tok::l_paren) &&\n" |
| 973 | " Line.Tokens[i - 1].Tok.isNot(tok::l_square);\n" |
| 974 | " }\n }\n}"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 975 | } |
| 976 | |
Daniel Jasper | 0df6acd | 2013-01-16 14:59:02 +0000 | [diff] [blame] | 977 | TEST_F(FormatTest, FormatsOneParameterPerLineIfNecessary) { |
| 978 | verifyGoogleFormat( |
| 979 | "aaaaaaaa(aaaaaaaaaaaaa,\n" |
| 980 | " aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" |
| 981 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n" |
| 982 | " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" |
| 983 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));"); |
| 984 | verifyGoogleFormat( |
Daniel Jasper | 8f4bd7a | 2013-01-23 10:08:28 +0000 | [diff] [blame] | 985 | "aaaaaaaaaaaaaaa(\n" |
| 986 | " aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa).aaaaaaaaaaaaaaaaaa();"); |
| 987 | verifyGoogleFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" |
| 988 | " aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaa);"); |
| 989 | |
| 990 | verifyGoogleFormat( |
| 991 | "aaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa,\n" |
| 992 | " aaaaaaaaaaaa,\n" |
| 993 | " aaaaaaaaaaaa);"); |
Daniel Jasper | 0df6acd | 2013-01-16 14:59:02 +0000 | [diff] [blame] | 994 | verifyGoogleFormat( |
| 995 | "somefunction(someotherFunction(ddddddddddddddddddddddddddddddddddd,\n" |
| 996 | " ddddddddddddddddddddddddddddd),\n" |
| 997 | " test);"); |
| 998 | |
| 999 | verifyGoogleFormat( |
| 1000 | "std::vector<aaaaaaaaaaaaaaaaaaaaaaa,\n" |
| 1001 | " aaaaaaaaaaaaaaaaaaaaaaa,\n" |
| 1002 | " aaaaaaaaaaaaaaaaaaaaaaa> aaaaaaaaaaaaaaaaaa;"); |
| 1003 | verifyGoogleFormat("a(\"a\"\n" |
| 1004 | " \"a\",\n" |
| 1005 | " a);"); |
Daniel Jasper | 8f4bd7a | 2013-01-23 10:08:28 +0000 | [diff] [blame] | 1006 | |
| 1007 | FormatStyle Style = getGoogleStyle(); |
| 1008 | Style.AllowAllParametersOnNextLine = false; |
| 1009 | verifyFormat("aaaaaaaaaaaaaaa(aaaaaaaaa,\n" |
| 1010 | " aaaaaaaaa,\n" |
| 1011 | " aaaaaaaaaaaaaaaaaaaaa).aaaaaaaaaaaaaaaaaa();", |
| 1012 | Style); |
Daniel Jasper | 0df6acd | 2013-01-16 14:59:02 +0000 | [diff] [blame] | 1013 | } |
| 1014 | |
Daniel Jasper | c79afda | 2013-01-18 10:56:38 +0000 | [diff] [blame] | 1015 | TEST_F(FormatTest, FormatsBuilderPattern) { |
| 1016 | verifyFormat( |
| 1017 | "return llvm::StringSwitch<Reference::Kind>(name)\n" |
Daniel Jasper | f39c885 | 2013-01-23 16:58:21 +0000 | [diff] [blame] | 1018 | " .StartsWith(\".eh_frame_hdr\", ORDER_EH_FRAMEHDR)\n" |
| 1019 | " .StartsWith(\".eh_frame\", ORDER_EH_FRAME).StartsWith(\".init\", ORDER_INIT)\n" |
| 1020 | " .StartsWith(\".fini\", ORDER_FINI).StartsWith(\".hash\", ORDER_HASH)\n" |
| 1021 | " .Default(ORDER_TEXT);\n"); |
Daniel Jasper | c79afda | 2013-01-18 10:56:38 +0000 | [diff] [blame] | 1022 | } |
| 1023 | |
Daniel Jasper | 7d19bc2 | 2013-01-11 14:23:32 +0000 | [diff] [blame] | 1024 | TEST_F(FormatTest, DoesNotBreakTrailingAnnotation) { |
| 1025 | verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" |
| 1026 | " GUARDED_BY(aaaaaaaaaaaaa);"); |
Daniel Jasper | 60ca75d | 2013-01-17 13:31:52 +0000 | [diff] [blame] | 1027 | verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n" |
| 1028 | " GUARDED_BY(aaaaaaaaaaaaa);"); |
| 1029 | verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n" |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 1030 | " GUARDED_BY(aaaaaaaaaaaaa) {\n}"); |
Daniel Jasper | 7d19bc2 | 2013-01-11 14:23:32 +0000 | [diff] [blame] | 1031 | } |
| 1032 | |
Daniel Jasper | e2c7acf | 2012-12-24 00:13:23 +0000 | [diff] [blame] | 1033 | TEST_F(FormatTest, BreaksAccordingToOperatorPrecedence) { |
| 1034 | verifyFormat( |
| 1035 | "if (aaaaaaaaaaaaaaaaaaaaaaaaa ||\n" |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 1036 | " bbbbbbbbbbbbbbbbbbbbbbbbb && ccccccccccccccccccccccccc) {\n}"); |
Daniel Jasper | cd16238 | 2013-01-07 13:26:07 +0000 | [diff] [blame] | 1037 | verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa && bbbbbbbbbbbbbbbbbbbbbbbbb ||\n" |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 1038 | " ccccccccccccccccccccccccc) {\n}"); |
Daniel Jasper | cd16238 | 2013-01-07 13:26:07 +0000 | [diff] [blame] | 1039 | verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb ||\n" |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 1040 | " ccccccccccccccccccccccccc) {\n}"); |
Daniel Jasper | e2c7acf | 2012-12-24 00:13:23 +0000 | [diff] [blame] | 1041 | verifyFormat( |
| 1042 | "if ((aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb) &&\n" |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 1043 | " ccccccccccccccccccccccccc) {\n}"); |
Daniel Jasper | e2c7acf | 2012-12-24 00:13:23 +0000 | [diff] [blame] | 1044 | } |
| 1045 | |
Daniel Jasper | 7006e7e | 2013-01-23 12:27:43 +0000 | [diff] [blame] | 1046 | TEST_F(FormatTest, BreaksAfterAssignments) { |
Daniel Jasper | 9cda800 | 2013-01-07 13:08:40 +0000 | [diff] [blame] | 1047 | verifyFormat( |
Daniel Jasper | 7006e7e | 2013-01-23 12:27:43 +0000 | [diff] [blame] | 1048 | "unsigned Cost =\n" |
| 1049 | " TTI.getMemoryOpCost(I->getOpcode(), VectorTy, SI->getAlignment(),\n" |
| 1050 | " SI->getPointerAddressSpaceee());\n"); |
Daniel Jasper | 9cda800 | 2013-01-07 13:08:40 +0000 | [diff] [blame] | 1051 | verifyFormat( |
Daniel Jasper | 13cb7c2 | 2013-01-23 15:55:19 +0000 | [diff] [blame] | 1052 | "CharSourceRange LineRange = CharSourceRange::getTokenRange(\n" |
| 1053 | " Line.Tokens.front().Tok.getLo(), Line.Tokens.back().Tok.getLoc());"); |
Daniel Jasper | f39c885 | 2013-01-23 16:58:21 +0000 | [diff] [blame] | 1054 | |
| 1055 | verifyFormat( |
| 1056 | "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa = aaaaaaaaaaaaaa(0).aaaa()\n" |
| 1057 | " .aaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaa);"); |
Daniel Jasper | 9cda800 | 2013-01-07 13:08:40 +0000 | [diff] [blame] | 1058 | } |
| 1059 | |
Daniel Jasper | cf225b6 | 2012-12-24 13:43:52 +0000 | [diff] [blame] | 1060 | TEST_F(FormatTest, AlignsAfterAssignments) { |
| 1061 | verifyFormat( |
| 1062 | "int Result = aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" |
Daniel Jasper | 9cda800 | 2013-01-07 13:08:40 +0000 | [diff] [blame] | 1063 | " aaaaaaaaaaaaaaaaaaaaaaaaa;"); |
Daniel Jasper | cf225b6 | 2012-12-24 13:43:52 +0000 | [diff] [blame] | 1064 | verifyFormat( |
| 1065 | "Result += aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" |
Daniel Jasper | 9cda800 | 2013-01-07 13:08:40 +0000 | [diff] [blame] | 1066 | " aaaaaaaaaaaaaaaaaaaaaaaaa;"); |
Daniel Jasper | cf225b6 | 2012-12-24 13:43:52 +0000 | [diff] [blame] | 1067 | verifyFormat( |
| 1068 | "Result >>= aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" |
Daniel Jasper | 9cda800 | 2013-01-07 13:08:40 +0000 | [diff] [blame] | 1069 | " aaaaaaaaaaaaaaaaaaaaaaaaa;"); |
Daniel Jasper | cf225b6 | 2012-12-24 13:43:52 +0000 | [diff] [blame] | 1070 | verifyFormat( |
| 1071 | "int Result = (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" |
Daniel Jasper | 9cda800 | 2013-01-07 13:08:40 +0000 | [diff] [blame] | 1072 | " aaaaaaaaaaaaaaaaaaaaaaaaa);"); |
Daniel Jasper | cf225b6 | 2012-12-24 13:43:52 +0000 | [diff] [blame] | 1073 | verifyFormat( |
Daniel Jasper | 7006e7e | 2013-01-23 12:27:43 +0000 | [diff] [blame] | 1074 | "double LooooooooooooooooooooooooongResult =\n" |
| 1075 | " aaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaa +\n" |
| 1076 | " aaaaaaaaaaaaaaaaaaaaaaaa;"); |
Daniel Jasper | cf225b6 | 2012-12-24 13:43:52 +0000 | [diff] [blame] | 1077 | } |
| 1078 | |
| 1079 | TEST_F(FormatTest, AlignsAfterReturn) { |
| 1080 | verifyFormat( |
| 1081 | "return aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" |
| 1082 | " aaaaaaaaaaaaaaaaaaaaaaaaa;"); |
| 1083 | verifyFormat( |
| 1084 | "return (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" |
| 1085 | " aaaaaaaaaaaaaaaaaaaaaaaaa);"); |
| 1086 | } |
| 1087 | |
Daniel Jasper | 9c837d0 | 2013-01-09 07:06:56 +0000 | [diff] [blame] | 1088 | TEST_F(FormatTest, BreaksConditionalExpressions) { |
| 1089 | verifyFormat( |
| 1090 | "aaaa(aaaaaaaaaaaaaaaaaaaa,\n" |
| 1091 | " aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" |
| 1092 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); |
| 1093 | verifyFormat("aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" |
| 1094 | " aaaaaaaaaaaaaaaaaaaaaaa : aaaaaaaaaaaaaaaaaaaaa);"); |
Daniel Jasper | 1f2b078 | 2013-01-16 16:23:19 +0000 | [diff] [blame] | 1095 | verifyFormat( |
| 1096 | "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa) :\n" |
| 1097 | " aaaaaaaaaaaaa);"); |
Daniel Jasper | 9c837d0 | 2013-01-09 07:06:56 +0000 | [diff] [blame] | 1098 | } |
| 1099 | |
Nico Weber | 7d37b8b | 2013-01-12 01:28:06 +0000 | [diff] [blame] | 1100 | TEST_F(FormatTest, ConditionalExpressionsInBrackets) { |
| 1101 | verifyFormat("arr[foo ? bar : baz];"); |
| 1102 | verifyFormat("f()[foo ? bar : baz];"); |
| 1103 | verifyFormat("(a + b)[foo ? bar : baz];"); |
| 1104 | verifyFormat("arr[foo ? (4 > 5 ? 4 : 5) : 5 < 5 ? 5 : 7];"); |
| 1105 | } |
| 1106 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1107 | TEST_F(FormatTest, AlignsStringLiterals) { |
| 1108 | verifyFormat("loooooooooooooooooooooooooongFunction(\"short literal \"\n" |
| 1109 | " \"short literal\");"); |
| 1110 | verifyFormat( |
| 1111 | "looooooooooooooooooooooooongFunction(\n" |
| 1112 | " \"short literal\"\n" |
| 1113 | " \"looooooooooooooooooooooooooooooooooooooooooooooooong literal\");"); |
| 1114 | } |
| 1115 | |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 1116 | TEST_F(FormatTest, AlignsPipes) { |
| 1117 | verifyFormat( |
| 1118 | "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" |
| 1119 | " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" |
| 1120 | " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); |
| 1121 | verifyFormat( |
| 1122 | "aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa\n" |
| 1123 | " << aaaaaaaaaaaaaaaaaaaa;"); |
| 1124 | verifyFormat( |
| 1125 | "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" |
| 1126 | " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); |
| 1127 | verifyFormat( |
| 1128 | "llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n" |
| 1129 | " \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\"\n" |
| 1130 | " << \"ccccccccccccccccccccccccccccccccccccccccccccccccc\";"); |
| 1131 | verifyFormat( |
| 1132 | "aaaaaaaa << (aaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" |
| 1133 | " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" |
| 1134 | " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); |
| 1135 | } |
| 1136 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1137 | TEST_F(FormatTest, UnderstandsEquals) { |
| 1138 | verifyFormat( |
| 1139 | "aaaaaaaaaaaaaaaaa =\n" |
| 1140 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); |
| 1141 | verifyFormat( |
| 1142 | "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 1143 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1144 | verifyFormat( |
| 1145 | "if (a) {\n" |
Manuel Klimek | 36fab8d | 2013-01-10 13:24:24 +0000 | [diff] [blame] | 1146 | " f();\n" |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1147 | "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 1148 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" |
| 1149 | "}"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1150 | |
Daniel Jasper | 9cda800 | 2013-01-07 13:08:40 +0000 | [diff] [blame] | 1151 | verifyFormat( |
Daniel Jasper | 7006e7e | 2013-01-23 12:27:43 +0000 | [diff] [blame] | 1152 | "if (int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" |
| 1153 | " 100000000 + 10000000) {\n}"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1154 | } |
| 1155 | |
Daniel Jasper | 9a0b494 | 2012-12-17 14:34:14 +0000 | [diff] [blame] | 1156 | TEST_F(FormatTest, WrapsAtFunctionCallsIfNecessary) { |
Daniel Jasper | 1321eb5 | 2012-12-18 21:05:13 +0000 | [diff] [blame] | 1157 | verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n" |
| 1158 | " .looooooooooooooooooooooooooooooooooooooongFunction();"); |
Daniel Jasper | 9a0b494 | 2012-12-17 14:34:14 +0000 | [diff] [blame] | 1159 | |
Daniel Jasper | 1321eb5 | 2012-12-18 21:05:13 +0000 | [diff] [blame] | 1160 | verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n" |
| 1161 | " ->looooooooooooooooooooooooooooooooooooooongFunction();"); |
Daniel Jasper | 9a0b494 | 2012-12-17 14:34:14 +0000 | [diff] [blame] | 1162 | |
| 1163 | verifyFormat( |
| 1164 | "LooooooooooooooooooooooooooooooooongObject->shortFunction(Parameter1,\n" |
| 1165 | " Parameter2);"); |
| 1166 | |
| 1167 | verifyFormat( |
| 1168 | "ShortObject->shortFunction(\n" |
| 1169 | " LooooooooooooooooooooooooooooooooooooooooooooooongParameter1,\n" |
| 1170 | " LooooooooooooooooooooooooooooooooooooooooooooooongParameter2);"); |
| 1171 | |
| 1172 | verifyFormat("loooooooooooooongFunction(\n" |
| 1173 | " LoooooooooooooongObject->looooooooooooooooongFunction());"); |
| 1174 | |
| 1175 | verifyFormat( |
| 1176 | "function(LoooooooooooooooooooooooooooooooooooongObject\n" |
| 1177 | " ->loooooooooooooooooooooooooooooooooooooooongFunction());"); |
| 1178 | |
Daniel Jasper | 46a46a2 | 2013-01-07 07:13:20 +0000 | [diff] [blame] | 1179 | // Here, it is not necessary to wrap at "." or "->". |
Daniel Jasper | 1321eb5 | 2012-12-18 21:05:13 +0000 | [diff] [blame] | 1180 | verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaa) ||\n" |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 1181 | " aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); |
Daniel Jasper | 46a46a2 | 2013-01-07 07:13:20 +0000 | [diff] [blame] | 1182 | verifyFormat( |
| 1183 | "aaaaaaaaaaa->aaaaaaaaa(\n" |
| 1184 | " aaaaaaaaaaaaaaaaaaaaaaaaa,\n" |
| 1185 | " aaaaaaaaaaaaaaaaaa->aaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa));\n"); |
Daniel Jasper | 9a0b494 | 2012-12-17 14:34:14 +0000 | [diff] [blame] | 1186 | } |
| 1187 | |
Daniel Jasper | 9a64fb5 | 2013-01-02 15:08:56 +0000 | [diff] [blame] | 1188 | TEST_F(FormatTest, WrapsTemplateDeclarations) { |
| 1189 | verifyFormat("template <typename T>\n" |
| 1190 | "virtual void loooooooooooongFunction(int Param1, int Param2);"); |
| 1191 | verifyFormat( |
Daniel Jasper | c79afda | 2013-01-18 10:56:38 +0000 | [diff] [blame] | 1192 | "template <typename T>\n" |
| 1193 | "void f(int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram1,\n" |
| 1194 | " int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram2);"); |
Daniel Jasper | 9a64fb5 | 2013-01-02 15:08:56 +0000 | [diff] [blame] | 1195 | verifyFormat( |
| 1196 | "template <typename T>\n" |
| 1197 | "void looooooooooooooooooooongFunction(int Paaaaaaaaaaaaaaaaaaaaram1,\n" |
| 1198 | " int Paaaaaaaaaaaaaaaaaaaaram2);"); |
Daniel Jasper | 5eda31e | 2013-01-02 18:30:06 +0000 | [diff] [blame] | 1199 | verifyFormat( |
| 1200 | "template <typename T>\n" |
| 1201 | "aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa,\n" |
| 1202 | " aaaaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaa,\n" |
| 1203 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); |
Daniel Jasper | d64f738 | 2013-01-09 09:50:48 +0000 | [diff] [blame] | 1204 | verifyFormat("template <typename T>\n" |
| 1205 | "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" |
| 1206 | " int aaaaaaaaaaaaaaaaa);"); |
Daniel Jasper | 3fc0bb7 | 2013-01-09 10:40:23 +0000 | [diff] [blame] | 1207 | verifyFormat( |
| 1208 | "template <typename T1, typename T2 = char, typename T3 = char,\n" |
| 1209 | " typename T4 = char>\n" |
| 1210 | "void f();"); |
Daniel Jasper | 3499dda | 2013-01-25 15:43:32 +0000 | [diff] [blame] | 1211 | verifyFormat( |
| 1212 | "aaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>(\n" |
| 1213 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); |
Daniel Jasper | 9a64fb5 | 2013-01-02 15:08:56 +0000 | [diff] [blame] | 1214 | } |
| 1215 | |
Daniel Jasper | 63f0036 | 2013-01-25 10:57:27 +0000 | [diff] [blame] | 1216 | TEST_F(FormatTest, WrapsAtNestedNameSpecifiers) { |
| 1217 | verifyFormat( |
| 1218 | "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" |
| 1219 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); |
| 1220 | verifyFormat( |
| 1221 | "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" |
| 1222 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" |
| 1223 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());"); |
| 1224 | |
| 1225 | // FIXME: Should we have an extra indent after the second break? |
| 1226 | verifyFormat( |
| 1227 | "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" |
| 1228 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" |
| 1229 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); |
| 1230 | |
| 1231 | // FIXME: Look into whether we should indent 4 from the start or 4 from |
| 1232 | // "bbbbb..." here instead of what we are doing now. |
| 1233 | verifyFormat( |
| 1234 | "aaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb::\n" |
| 1235 | " cccccccccccccccccccccccccccccccccccccccccccccccccc());"); |
| 1236 | |
| 1237 | // Breaking at nested name specifiers is generally not desirable. |
| 1238 | verifyFormat( |
| 1239 | "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" |
| 1240 | " aaaaaaaaaaaaaaaaaaaaaaa);"); |
| 1241 | } |
| 1242 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1243 | TEST_F(FormatTest, UnderstandsTemplateParameters) { |
| 1244 | verifyFormat("A<int> a;"); |
| 1245 | verifyFormat("A<A<A<int> > > a;"); |
| 1246 | verifyFormat("A<A<A<int, 2>, 3>, 4> a;"); |
| 1247 | verifyFormat("bool x = a < 1 || 2 > a;"); |
| 1248 | verifyFormat("bool x = 5 < f<int>();"); |
| 1249 | verifyFormat("bool x = f<int>() > 5;"); |
| 1250 | verifyFormat("bool x = 5 < a<int>::x;"); |
| 1251 | verifyFormat("bool x = a < 4 ? a > 2 : false;"); |
| 1252 | verifyFormat("bool x = f() ? a < 2 : a > 2;"); |
| 1253 | |
| 1254 | verifyGoogleFormat("A<A<int>> a;"); |
| 1255 | verifyGoogleFormat("A<A<A<int>>> a;"); |
| 1256 | verifyGoogleFormat("A<A<A<A<int>>>> a;"); |
| 1257 | |
| 1258 | verifyFormat("test >> a >> b;"); |
| 1259 | verifyFormat("test << a >> b;"); |
| 1260 | |
| 1261 | verifyFormat("f<int>();"); |
Manuel Klimek | 36fab8d | 2013-01-10 13:24:24 +0000 | [diff] [blame] | 1262 | verifyFormat("template <typename T> void f() {}"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1263 | } |
| 1264 | |
Daniel Jasper | 98e6b4a | 2012-12-21 09:41:31 +0000 | [diff] [blame] | 1265 | TEST_F(FormatTest, UnderstandsUnaryOperators) { |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1266 | verifyFormat("int a = -2;"); |
Daniel Jasper | 8822d3a | 2012-12-04 13:02:32 +0000 | [diff] [blame] | 1267 | verifyFormat("f(-1, -2, -3);"); |
| 1268 | verifyFormat("a[-1] = 5;"); |
| 1269 | verifyFormat("int a = 5 + -2;"); |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 1270 | verifyFormat("if (i == -1) {\n}"); |
| 1271 | verifyFormat("if (i != -1) {\n}"); |
| 1272 | verifyFormat("if (i > -1) {\n}"); |
| 1273 | verifyFormat("if (i < -1) {\n}"); |
Daniel Jasper | d56a737 | 2012-12-06 13:16:39 +0000 | [diff] [blame] | 1274 | verifyFormat("++(a->f());"); |
| 1275 | verifyFormat("--(a->f());"); |
Daniel Jasper | 4abbb53 | 2013-01-14 12:18:19 +0000 | [diff] [blame] | 1276 | verifyFormat("(a->f())++;"); |
| 1277 | verifyFormat("a[42]++;"); |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 1278 | verifyFormat("if (!(a->f())) {\n}"); |
Daniel Jasper | 98e6b4a | 2012-12-21 09:41:31 +0000 | [diff] [blame] | 1279 | |
| 1280 | verifyFormat("a-- > b;"); |
| 1281 | verifyFormat("b ? -a : c;"); |
| 1282 | verifyFormat("n * sizeof char16;"); |
Daniel Jasper | ba3d307 | 2013-01-02 17:21:36 +0000 | [diff] [blame] | 1283 | verifyFormat("n * alignof char16;"); |
Daniel Jasper | 98e6b4a | 2012-12-21 09:41:31 +0000 | [diff] [blame] | 1284 | verifyFormat("sizeof(char);"); |
Daniel Jasper | ba3d307 | 2013-01-02 17:21:36 +0000 | [diff] [blame] | 1285 | verifyFormat("alignof(char);"); |
Daniel Jasper | 1f0754b | 2013-01-02 15:26:16 +0000 | [diff] [blame] | 1286 | |
| 1287 | verifyFormat("return -1;"); |
| 1288 | verifyFormat("switch (a) {\n" |
| 1289 | "case -1:\n" |
| 1290 | " break;\n" |
| 1291 | "}"); |
Nico Weber | cc191d1 | 2013-01-12 05:41:23 +0000 | [diff] [blame] | 1292 | |
| 1293 | verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = { -5, +3 };"); |
| 1294 | verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = { +5, -3 };"); |
Daniel Jasper | b369c2c | 2013-01-15 14:27:39 +0000 | [diff] [blame] | 1295 | |
| 1296 | verifyFormat("int a = /* confusing comment */ -1;"); |
| 1297 | // FIXME: The space after 'i' is wrong, but hopefully, this is a rare case. |
| 1298 | verifyFormat("int a = i /* confusing comment */++;"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1299 | } |
| 1300 | |
| 1301 | TEST_F(FormatTest, UndestandsOverloadedOperators) { |
Daniel Jasper | f6aef6a | 2012-12-24 10:56:04 +0000 | [diff] [blame] | 1302 | verifyFormat("bool operator<();"); |
| 1303 | verifyFormat("bool operator>();"); |
| 1304 | verifyFormat("bool operator=();"); |
| 1305 | verifyFormat("bool operator==();"); |
| 1306 | verifyFormat("bool operator!=();"); |
| 1307 | verifyFormat("int operator+();"); |
| 1308 | verifyFormat("int operator++();"); |
| 1309 | verifyFormat("bool operator();"); |
| 1310 | verifyFormat("bool operator()();"); |
| 1311 | verifyFormat("bool operator[]();"); |
| 1312 | verifyFormat("operator bool();"); |
| 1313 | verifyFormat("operator SomeType<int>();"); |
| 1314 | verifyFormat("void *operator new(std::size_t size);"); |
| 1315 | verifyFormat("void *operator new[](std::size_t size);"); |
| 1316 | verifyFormat("void operator delete(void *ptr);"); |
| 1317 | verifyFormat("void operator delete[](void *ptr);"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1318 | } |
| 1319 | |
Daniel Jasper | 088dab5 | 2013-01-11 16:09:04 +0000 | [diff] [blame] | 1320 | TEST_F(FormatTest, UnderstandsNewAndDelete) { |
| 1321 | verifyFormat("A *a = new A;"); |
| 1322 | verifyFormat("A *a = new (placement) A;"); |
| 1323 | verifyFormat("delete a;"); |
| 1324 | verifyFormat("delete (A *)a;"); |
| 1325 | } |
| 1326 | |
Daniel Jasper | 5d33440 | 2013-01-02 08:57:10 +0000 | [diff] [blame] | 1327 | TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) { |
Manuel Klimek | 36fab8d | 2013-01-10 13:24:24 +0000 | [diff] [blame] | 1328 | verifyFormat("int *f(int *a) {}"); |
Manuel Klimek | 36fab8d | 2013-01-10 13:24:24 +0000 | [diff] [blame] | 1329 | verifyFormat("int main(int argc, char **argv) {}"); |
Daniel Jasper | 4bfc65a | 2013-01-23 12:10:53 +0000 | [diff] [blame] | 1330 | verifyIndependentOfContext("f(a, *a);"); |
| 1331 | verifyIndependentOfContext("f(*a);"); |
| 1332 | verifyIndependentOfContext("int a = b * 10;"); |
| 1333 | verifyIndependentOfContext("int a = 10 * b;"); |
| 1334 | verifyIndependentOfContext("int a = b * c;"); |
| 1335 | verifyIndependentOfContext("int a += b * c;"); |
| 1336 | verifyIndependentOfContext("int a -= b * c;"); |
| 1337 | verifyIndependentOfContext("int a *= b * c;"); |
| 1338 | verifyIndependentOfContext("int a /= b * c;"); |
| 1339 | verifyIndependentOfContext("int a = *b;"); |
| 1340 | verifyIndependentOfContext("int a = *b * c;"); |
| 1341 | verifyIndependentOfContext("int a = b * *c;"); |
| 1342 | verifyIndependentOfContext("return 10 * b;"); |
| 1343 | verifyIndependentOfContext("return *b * *c;"); |
| 1344 | verifyIndependentOfContext("return a & ~b;"); |
| 1345 | verifyIndependentOfContext("f(b ? *c : *d);"); |
| 1346 | verifyIndependentOfContext("int a = b ? *c : *d;"); |
| 1347 | verifyIndependentOfContext("*b = a;"); |
| 1348 | verifyIndependentOfContext("a * ~b;"); |
| 1349 | verifyIndependentOfContext("a * !b;"); |
| 1350 | verifyIndependentOfContext("a * +b;"); |
| 1351 | verifyIndependentOfContext("a * -b;"); |
| 1352 | verifyIndependentOfContext("a * ++b;"); |
| 1353 | verifyIndependentOfContext("a * --b;"); |
| 1354 | verifyIndependentOfContext("a[4] * b;"); |
| 1355 | verifyIndependentOfContext("f() * b;"); |
| 1356 | verifyIndependentOfContext("a * [self dostuff];"); |
| 1357 | verifyIndependentOfContext("a * (a + b);"); |
| 1358 | verifyIndependentOfContext("(a *)(a + b);"); |
| 1359 | verifyIndependentOfContext("int *pa = (int *)&a;"); |
Daniel Jasper | c74e279 | 2012-12-07 09:52:15 +0000 | [diff] [blame] | 1360 | |
Daniel Jasper | 4bfc65a | 2013-01-23 12:10:53 +0000 | [diff] [blame] | 1361 | verifyIndependentOfContext("InvalidRegions[*R] = 0;"); |
Daniel Jasper | 9bb0d28 | 2013-01-04 20:46:38 +0000 | [diff] [blame] | 1362 | |
Daniel Jasper | 4bfc65a | 2013-01-23 12:10:53 +0000 | [diff] [blame] | 1363 | verifyIndependentOfContext("A<int *> a;"); |
| 1364 | verifyIndependentOfContext("A<int **> a;"); |
| 1365 | verifyIndependentOfContext("A<int *, int *> a;"); |
| 1366 | verifyIndependentOfContext("A<int **, int **> a;"); |
Daniel Jasper | 8b39c66 | 2012-12-10 18:59:13 +0000 | [diff] [blame] | 1367 | |
Daniel Jasper | 2db356d | 2013-01-08 20:03:18 +0000 | [diff] [blame] | 1368 | verifyFormat( |
| 1369 | "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" |
| 1370 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaa, *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); |
| 1371 | |
Manuel Klimek | 36fab8d | 2013-01-10 13:24:24 +0000 | [diff] [blame] | 1372 | verifyGoogleFormat("int main(int argc, char** argv) {}"); |
Daniel Jasper | 8b39c66 | 2012-12-10 18:59:13 +0000 | [diff] [blame] | 1373 | verifyGoogleFormat("A<int*> a;"); |
| 1374 | verifyGoogleFormat("A<int**> a;"); |
| 1375 | verifyGoogleFormat("A<int*, int*> a;"); |
| 1376 | verifyGoogleFormat("A<int**, int**> a;"); |
Daniel Jasper | 5d33440 | 2013-01-02 08:57:10 +0000 | [diff] [blame] | 1377 | verifyGoogleFormat("f(b ? *c : *d);"); |
| 1378 | verifyGoogleFormat("int a = b ? *c : *d;"); |
Daniel Jasper | 48bd7b7 | 2013-01-16 16:04:06 +0000 | [diff] [blame] | 1379 | verifyGoogleFormat("Type* t = **x;"); |
| 1380 | verifyGoogleFormat("Type* t = *++*x;"); |
| 1381 | verifyGoogleFormat("*++*x;"); |
| 1382 | verifyGoogleFormat("Type* t = const_cast<T*>(&*x);"); |
| 1383 | verifyGoogleFormat("Type* t = x++ * y;"); |
Manuel Klimek | 1b6f4bd | 2013-01-14 10:58:01 +0000 | [diff] [blame] | 1384 | |
Daniel Jasper | 4bfc65a | 2013-01-23 12:10:53 +0000 | [diff] [blame] | 1385 | verifyIndependentOfContext("a = *(x + y);"); |
| 1386 | verifyIndependentOfContext("a = &(x + y);"); |
| 1387 | verifyIndependentOfContext("*(x + y).call();"); |
| 1388 | verifyIndependentOfContext("&(x + y)->call();"); |
| 1389 | verifyIndependentOfContext("&(*I).first"); |
Daniel Jasper | b369c2c | 2013-01-15 14:27:39 +0000 | [diff] [blame] | 1390 | |
Daniel Jasper | 4bfc65a | 2013-01-23 12:10:53 +0000 | [diff] [blame] | 1391 | verifyIndependentOfContext("f(b * /* confusing comment */ ++c);"); |
Daniel Jasper | b369c2c | 2013-01-15 14:27:39 +0000 | [diff] [blame] | 1392 | verifyFormat( |
| 1393 | "int *MyValues = {\n" |
| 1394 | " *A, // Operator detection might be confused by the '{'\n" |
| 1395 | " *BB // Operator detection might be confused by previous comment\n" |
| 1396 | "};"); |
Nico Weber | 5096a44 | 2013-01-17 17:17:19 +0000 | [diff] [blame] | 1397 | |
Daniel Jasper | 4bfc65a | 2013-01-23 12:10:53 +0000 | [diff] [blame] | 1398 | verifyIndependentOfContext("if (int *a = &b)"); |
| 1399 | verifyIndependentOfContext("if (int &a = *b)"); |
| 1400 | verifyIndependentOfContext("if (a & b[i])"); |
| 1401 | verifyIndependentOfContext("if (a::b::c::d & b[i])"); |
| 1402 | verifyIndependentOfContext("if (*b[i])"); |
| 1403 | verifyIndependentOfContext("if (int *a = (&b))"); |
| 1404 | verifyIndependentOfContext("while (int *a = &b)"); |
Daniel Jasper | 20d3583 | 2013-01-23 12:58:14 +0000 | [diff] [blame] | 1405 | verifyFormat("void f() {\n" |
| 1406 | " for (const int &v : Values) {\n" |
| 1407 | " }\n" |
| 1408 | "}"); |
Daniel Jasper | ffee171 | 2013-01-22 11:46:26 +0000 | [diff] [blame] | 1409 | |
Daniel Jasper | 4bfc65a | 2013-01-23 12:10:53 +0000 | [diff] [blame] | 1410 | verifyIndependentOfContext("A = new SomeType *[Length]();"); |
Daniel Jasper | ffee171 | 2013-01-22 11:46:26 +0000 | [diff] [blame] | 1411 | verifyGoogleFormat("A = new SomeType* [Length]();"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1412 | } |
| 1413 | |
Manuel Klimek | a32a7fd | 2013-01-23 14:08:21 +0000 | [diff] [blame] | 1414 | TEST_F(FormatTest, FormatsBinaryOperatorsPrecedingEquals) { |
| 1415 | verifyFormat("void f() {\n" |
| 1416 | " x[aaaaaaaaa -\n" |
| 1417 | " b] = 23;\n" |
| 1418 | "}", getLLVMStyleWithColumns(15)); |
| 1419 | } |
| 1420 | |
Daniel Jasper | 4981bd0 | 2013-01-13 08:01:36 +0000 | [diff] [blame] | 1421 | TEST_F(FormatTest, FormatsCasts) { |
| 1422 | verifyFormat("Type *A = static_cast<Type *>(P);"); |
| 1423 | verifyFormat("Type *A = (Type *)P;"); |
| 1424 | verifyFormat("Type *A = (vector<Type *, int *>)P;"); |
| 1425 | verifyFormat("int a = (int)(2.0f);"); |
| 1426 | |
| 1427 | // FIXME: These also need to be identified. |
| 1428 | verifyFormat("int a = (int) 2.0f;"); |
| 1429 | verifyFormat("int a = (int) * b;"); |
| 1430 | |
| 1431 | // These are not casts. |
| 1432 | verifyFormat("void f(int *) {}"); |
| 1433 | verifyFormat("void f(int *);"); |
| 1434 | verifyFormat("void f(int *) = 0;"); |
| 1435 | verifyFormat("void f(SmallVector<int>) {}"); |
| 1436 | verifyFormat("void f(SmallVector<int>);"); |
| 1437 | verifyFormat("void f(SmallVector<int>) = 0;"); |
| 1438 | } |
| 1439 | |
Daniel Jasper | 46ef852 | 2013-01-10 13:08:12 +0000 | [diff] [blame] | 1440 | TEST_F(FormatTest, FormatsFunctionTypes) { |
| 1441 | // FIXME: Determine the cases that need a space after the return type and fix. |
| 1442 | verifyFormat("A<bool()> a;"); |
| 1443 | verifyFormat("A<SomeType()> a;"); |
| 1444 | verifyFormat("A<void(*)(int, std::string)> a;"); |
| 1445 | |
| 1446 | verifyFormat("int(*func)(void *);"); |
| 1447 | } |
| 1448 | |
Daniel Jasper | 4dc41de | 2013-01-02 08:44:14 +0000 | [diff] [blame] | 1449 | TEST_F(FormatTest, DoesNotBreakBeforePointerOrReference) { |
Daniel Jasper | cd16238 | 2013-01-07 13:26:07 +0000 | [diff] [blame] | 1450 | verifyFormat("int *someFunction(int LoooooooooooooooongParam1,\n" |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 1451 | " int LoooooooooooooooongParam2) {\n}"); |
Daniel Jasper | 4dc41de | 2013-01-02 08:44:14 +0000 | [diff] [blame] | 1452 | verifyFormat( |
| 1453 | "TypeSpecDecl *TypeSpecDecl::Create(ASTContext &C, DeclContext *DC,\n" |
| 1454 | " SourceLocation L, IdentifierIn *II,\n" |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 1455 | " Type *T) {\n}"); |
Daniel Jasper | 4dc41de | 2013-01-02 08:44:14 +0000 | [diff] [blame] | 1456 | } |
| 1457 | |
Daniel Jasper | 3b5943f | 2012-12-06 09:56:08 +0000 | [diff] [blame] | 1458 | TEST_F(FormatTest, LineStartsWithSpecialCharacter) { |
| 1459 | verifyFormat("(a)->b();"); |
| 1460 | verifyFormat("--a;"); |
| 1461 | } |
| 1462 | |
Daniel Jasper | 8822d3a | 2012-12-04 13:02:32 +0000 | [diff] [blame] | 1463 | TEST_F(FormatTest, HandlesIncludeDirectives) { |
Daniel Jasper | 2b9c10b | 2013-01-14 15:52:06 +0000 | [diff] [blame] | 1464 | verifyFormat("#include <string>\n" |
| 1465 | "#include <a/b/c.h>\n" |
| 1466 | "#include \"a/b/string\"\n" |
| 1467 | "#include \"string.h\"\n" |
| 1468 | "#include \"string.h\"\n" |
Manuel Klimek | 407a31a | 2013-01-15 15:50:27 +0000 | [diff] [blame] | 1469 | "#include <a-a>\n" |
| 1470 | "#include < path with space >\n"); |
Nico Weber | b23ae0c | 2012-12-21 18:21:56 +0000 | [diff] [blame] | 1471 | |
Daniel Jasper | 8134e1e | 2013-01-13 08:12:18 +0000 | [diff] [blame] | 1472 | verifyFormat("#import <string>"); |
| 1473 | verifyFormat("#import <a/b/c.h>"); |
| 1474 | verifyFormat("#import \"a/b/string\""); |
| 1475 | verifyFormat("#import \"string.h\""); |
| 1476 | verifyFormat("#import \"string.h\""); |
Daniel Jasper | 8822d3a | 2012-12-04 13:02:32 +0000 | [diff] [blame] | 1477 | } |
| 1478 | |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 1479 | //===----------------------------------------------------------------------===// |
| 1480 | // Error recovery tests. |
| 1481 | //===----------------------------------------------------------------------===// |
| 1482 | |
Daniel Jasper | 700e710 | 2013-01-10 09:26:47 +0000 | [diff] [blame] | 1483 | TEST_F(FormatTest, IncorrectCodeTrailingStuff) { |
Alexander Kornienko | 51c23fa | 2013-01-16 11:45:16 +0000 | [diff] [blame] | 1484 | verifyFormat("void f() { return; }\n42"); |
| 1485 | verifyFormat("void f() {\n" |
| 1486 | " if (0)\n" |
| 1487 | " return;\n" |
| 1488 | "}\n" |
| 1489 | "42"); |
Alexander Kornienko | d881875 | 2013-01-16 11:43:46 +0000 | [diff] [blame] | 1490 | verifyFormat("void f() { return }\n42"); |
| 1491 | verifyFormat("void f() {\n" |
| 1492 | " if (0)\n" |
| 1493 | " return\n" |
| 1494 | "}\n" |
| 1495 | "42"); |
| 1496 | } |
| 1497 | |
| 1498 | TEST_F(FormatTest, IncorrectCodeMissingSemicolon) { |
| 1499 | EXPECT_EQ("void f() { return }", format("void f ( ) { return }")); |
| 1500 | EXPECT_EQ("void f() {\n" |
| 1501 | " if (a)\n" |
| 1502 | " return\n" |
| 1503 | "}", format("void f ( ) { if ( a ) return }")); |
| 1504 | EXPECT_EQ("namespace N { void f() }", format("namespace N { void f() }")); |
| 1505 | EXPECT_EQ("namespace N {\n" |
| 1506 | "void f() {}\n" |
| 1507 | "void g()\n" |
| 1508 | "}", format("namespace N { void f( ) { } void g( ) }")); |
Daniel Jasper | 700e710 | 2013-01-10 09:26:47 +0000 | [diff] [blame] | 1509 | } |
| 1510 | |
Daniel Jasper | ceb99ab | 2013-01-09 10:16:05 +0000 | [diff] [blame] | 1511 | TEST_F(FormatTest, IndentationWithinColumnLimitNotPossible) { |
| 1512 | verifyFormat("int aaaaaaaa =\n" |
| 1513 | " // Overly long comment\n" |
| 1514 | " b;", getLLVMStyleWithColumns(20)); |
| 1515 | verifyFormat("function(\n" |
| 1516 | " ShortArgument,\n" |
| 1517 | " LoooooooooooongArgument);\n", getLLVMStyleWithColumns(20)); |
| 1518 | } |
| 1519 | |
Alexander Kornienko | 56e49c5 | 2012-12-10 16:34:48 +0000 | [diff] [blame] | 1520 | TEST_F(FormatTest, IncorrectAccessSpecifier) { |
| 1521 | verifyFormat("public:"); |
| 1522 | verifyFormat("class A {\n" |
| 1523 | "public\n" |
Manuel Klimek | 36fab8d | 2013-01-10 13:24:24 +0000 | [diff] [blame] | 1524 | " void f() {}\n" |
Alexander Kornienko | 56e49c5 | 2012-12-10 16:34:48 +0000 | [diff] [blame] | 1525 | "};"); |
| 1526 | verifyFormat("public\n" |
| 1527 | "int qwerty;"); |
| 1528 | verifyFormat("public\n" |
Manuel Klimek | 36fab8d | 2013-01-10 13:24:24 +0000 | [diff] [blame] | 1529 | "B {}"); |
Alexander Kornienko | 56e49c5 | 2012-12-10 16:34:48 +0000 | [diff] [blame] | 1530 | verifyFormat("public\n" |
Manuel Klimek | 36fab8d | 2013-01-10 13:24:24 +0000 | [diff] [blame] | 1531 | "{}"); |
Alexander Kornienko | 56e49c5 | 2012-12-10 16:34:48 +0000 | [diff] [blame] | 1532 | verifyFormat("public\n" |
Manuel Klimek | 517e894 | 2013-01-11 17:54:10 +0000 | [diff] [blame] | 1533 | "B { int x; }"); |
Alexander Kornienko | 56e49c5 | 2012-12-10 16:34:48 +0000 | [diff] [blame] | 1534 | } |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1535 | |
Alexander Kornienko | 393b008 | 2012-12-04 15:40:36 +0000 | [diff] [blame] | 1536 | TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) { |
| 1537 | verifyFormat("{"); |
| 1538 | } |
| 1539 | |
| 1540 | TEST_F(FormatTest, IncorrectCodeDoNoWhile) { |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 1541 | verifyFormat("do {\n}"); |
| 1542 | verifyFormat("do {\n}\n" |
Alexander Kornienko | 393b008 | 2012-12-04 15:40:36 +0000 | [diff] [blame] | 1543 | "f();"); |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 1544 | verifyFormat("do {\n}\n" |
Alexander Kornienko | 393b008 | 2012-12-04 15:40:36 +0000 | [diff] [blame] | 1545 | "wheeee(fun);"); |
| 1546 | verifyFormat("do {\n" |
| 1547 | " f();\n" |
Manuel Klimek | de76854 | 2013-01-07 18:10:23 +0000 | [diff] [blame] | 1548 | "}"); |
Alexander Kornienko | 393b008 | 2012-12-04 15:40:36 +0000 | [diff] [blame] | 1549 | } |
| 1550 | |
Manuel Klimek | 6eca03f | 2013-01-11 19:23:05 +0000 | [diff] [blame] | 1551 | TEST_F(FormatTest, IncorrectCodeMissingParens) { |
Manuel Klimek | d465843 | 2013-01-11 18:28:36 +0000 | [diff] [blame] | 1552 | verifyFormat("if {\n foo;\n foo();\n}"); |
Manuel Klimek | 6eca03f | 2013-01-11 19:23:05 +0000 | [diff] [blame] | 1553 | verifyFormat("switch {\n foo;\n foo();\n}"); |
| 1554 | verifyFormat("for {\n foo;\n foo();\n}"); |
| 1555 | verifyFormat("while {\n foo;\n foo();\n}"); |
| 1556 | verifyFormat("do {\n foo;\n foo();\n} while;"); |
Manuel Klimek | d465843 | 2013-01-11 18:28:36 +0000 | [diff] [blame] | 1557 | } |
| 1558 | |
Daniel Jasper | 1f42f11 | 2013-01-04 18:52:56 +0000 | [diff] [blame] | 1559 | TEST_F(FormatTest, DoesNotTouchUnwrappedLinesWithErrors) { |
| 1560 | verifyFormat("namespace {\n" |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 1561 | "class Foo { Foo ( }; } // comment"); |
Daniel Jasper | 1f42f11 | 2013-01-04 18:52:56 +0000 | [diff] [blame] | 1562 | } |
| 1563 | |
Alexander Kornienko | cff563c | 2012-12-04 17:27:50 +0000 | [diff] [blame] | 1564 | TEST_F(FormatTest, IncorrectCodeErrorDetection) { |
Manuel Klimek | 36fab8d | 2013-01-10 13:24:24 +0000 | [diff] [blame] | 1565 | EXPECT_EQ("{\n{}\n", format("{\n{\n}\n")); |
| 1566 | EXPECT_EQ("{\n {}\n", format("{\n {\n}\n")); |
| 1567 | EXPECT_EQ("{\n {}\n", format("{\n {\n }\n")); |
| 1568 | EXPECT_EQ("{\n {}\n }\n}\n", format("{\n {\n }\n }\n}\n")); |
Alexander Kornienko | cff563c | 2012-12-04 17:27:50 +0000 | [diff] [blame] | 1569 | |
Alexander Kornienko | cff563c | 2012-12-04 17:27:50 +0000 | [diff] [blame] | 1570 | EXPECT_EQ("{\n" |
| 1571 | " {\n" |
| 1572 | " breakme(\n" |
| 1573 | " qwe);\n" |
| 1574 | "}\n", format("{\n" |
| 1575 | " {\n" |
| 1576 | " breakme(qwe);\n" |
Alexander Kornienko | a4ae9f3 | 2013-01-14 11:34:14 +0000 | [diff] [blame] | 1577 | "}\n", getLLVMStyleWithColumns(10))); |
Alexander Kornienko | cff563c | 2012-12-04 17:27:50 +0000 | [diff] [blame] | 1578 | } |
| 1579 | |
Manuel Klimek | 2851c16 | 2013-01-10 14:36:46 +0000 | [diff] [blame] | 1580 | TEST_F(FormatTest, LayoutCallsInsideBraceInitializers) { |
| 1581 | verifyFormat( |
| 1582 | "int x = {\n" |
| 1583 | " avariable,\n" |
Manuel Klimek | c8c8a47 | 2013-01-10 15:58:26 +0000 | [diff] [blame] | 1584 | " b(alongervariable)\n" |
| 1585 | "};", getLLVMStyleWithColumns(25)); |
Manuel Klimek | 2851c16 | 2013-01-10 14:36:46 +0000 | [diff] [blame] | 1586 | } |
| 1587 | |
Manuel Klimek | c44ee89 | 2013-01-21 10:07:49 +0000 | [diff] [blame] | 1588 | TEST_F(FormatTest, LayoutBraceInitializersInReturnStatement) { |
| 1589 | verifyFormat("return (a)(b) { 1, 2, 3 };"); |
| 1590 | } |
| 1591 | |
Manuel Klimek | 2851c16 | 2013-01-10 14:36:46 +0000 | [diff] [blame] | 1592 | TEST_F(FormatTest, LayoutTokensFollowingBlockInParentheses) { |
| 1593 | verifyFormat( |
| 1594 | "Aaa({\n" |
| 1595 | " int i;\n" |
| 1596 | "}, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n" |
| 1597 | " ccccccccccccccccc));"); |
| 1598 | } |
| 1599 | |
Manuel Klimek | 517e894 | 2013-01-11 17:54:10 +0000 | [diff] [blame] | 1600 | TEST_F(FormatTest, PullTrivialFunctionDefinitionsIntoSingleLine) { |
| 1601 | verifyFormat("void f() { return 42; }"); |
| 1602 | verifyFormat("void f() {\n" |
| 1603 | " // Comment\n" |
| 1604 | "}"); |
| 1605 | verifyFormat("{\n" |
| 1606 | "#error {\n" |
| 1607 | " int a;\n" |
| 1608 | "}"); |
| 1609 | verifyFormat("{\n" |
| 1610 | " int a;\n" |
| 1611 | "#error {\n" |
| 1612 | "}"); |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 1613 | |
| 1614 | verifyFormat("void f() { return 42; }", getLLVMStyleWithColumns(23)); |
| 1615 | verifyFormat("void f() {\n return 42;\n}", getLLVMStyleWithColumns(22)); |
| 1616 | |
| 1617 | verifyFormat("void f() {}", getLLVMStyleWithColumns(11)); |
| 1618 | verifyFormat("void f() {\n}", getLLVMStyleWithColumns(10)); |
Manuel Klimek | 517e894 | 2013-01-11 17:54:10 +0000 | [diff] [blame] | 1619 | } |
| 1620 | |
Manuel Klimek | 47ea7f6 | 2013-01-15 13:38:33 +0000 | [diff] [blame] | 1621 | TEST_F(FormatTest, UnderstandContextOfRecordTypeKeywords) { |
| 1622 | // Elaborate type variable declarations. |
Manuel Klimek | 606e07e | 2013-01-11 18:13:04 +0000 | [diff] [blame] | 1623 | verifyFormat("struct foo a = { bar };\nint n;"); |
Manuel Klimek | 47ea7f6 | 2013-01-15 13:38:33 +0000 | [diff] [blame] | 1624 | verifyFormat("class foo a = { bar };\nint n;"); |
| 1625 | verifyFormat("union foo a = { bar };\nint n;"); |
| 1626 | |
| 1627 | // Elaborate types inside function definitions. |
| 1628 | verifyFormat("struct foo f() {}\nint n;"); |
| 1629 | verifyFormat("class foo f() {}\nint n;"); |
| 1630 | verifyFormat("union foo f() {}\nint n;"); |
| 1631 | |
| 1632 | // Templates. |
| 1633 | verifyFormat("template <class X> void f() {}\nint n;"); |
| 1634 | verifyFormat("template <struct X> void f() {}\nint n;"); |
| 1635 | verifyFormat("template <union X> void f() {}\nint n;"); |
| 1636 | |
| 1637 | // Actual definitions... |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 1638 | verifyFormat("struct {\n} n;"); |
| 1639 | verifyFormat( |
| 1640 | "template <template <class T, class Y>, class Z> class X {\n} n;"); |
Manuel Klimek | 47ea7f6 | 2013-01-15 13:38:33 +0000 | [diff] [blame] | 1641 | verifyFormat("union Z {\n int n;\n} x;"); |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 1642 | verifyFormat("class MACRO Z {\n} n;"); |
| 1643 | verifyFormat("class MACRO(X) Z {\n} n;"); |
| 1644 | verifyFormat("class __attribute__(X) Z {\n} n;"); |
| 1645 | verifyFormat("class __declspec(X) Z {\n} n;"); |
Manuel Klimek | 47ea7f6 | 2013-01-15 13:38:33 +0000 | [diff] [blame] | 1646 | |
Manuel Klimek | 7f5b025 | 2013-01-21 10:17:14 +0000 | [diff] [blame] | 1647 | // Redefinition from nested context: |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 1648 | verifyFormat("class A::B::C {\n} n;"); |
Manuel Klimek | 7f5b025 | 2013-01-21 10:17:14 +0000 | [diff] [blame] | 1649 | |
Manuel Klimek | 3a3408c | 2013-01-21 13:58:54 +0000 | [diff] [blame] | 1650 | // Template definitions. |
| 1651 | // FIXME: This is still incorrectly handled at the formatter side. |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 1652 | verifyFormat("template <> struct X < 15, i < 3 && 42 < 50 && 33<28> {\n};"); |
Manuel Klimek | 3a3408c | 2013-01-21 13:58:54 +0000 | [diff] [blame] | 1653 | |
| 1654 | // FIXME: |
| 1655 | // This now gets parsed incorrectly as class definition. |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 1656 | // verifyFormat("class A<int> f() {\n}\nint n;"); |
Manuel Klimek | 3a3408c | 2013-01-21 13:58:54 +0000 | [diff] [blame] | 1657 | |
Manuel Klimek | 47ea7f6 | 2013-01-15 13:38:33 +0000 | [diff] [blame] | 1658 | // Elaborate types where incorrectly parsing the structural element would |
| 1659 | // break the indent. |
| 1660 | verifyFormat("if (true)\n" |
| 1661 | " class X x;\n" |
| 1662 | "else\n" |
| 1663 | " f();\n"); |
Manuel Klimek | 606e07e | 2013-01-11 18:13:04 +0000 | [diff] [blame] | 1664 | } |
| 1665 | |
Manuel Klimek | 407a31a | 2013-01-15 15:50:27 +0000 | [diff] [blame] | 1666 | TEST_F(FormatTest, DoNotInterfereWithErrorAndWarning) { |
| 1667 | verifyFormat("#error Leave all white!!!!! space* alone!\n"); |
| 1668 | verifyFormat("#warning Leave all white!!!!! space* alone!\n"); |
| 1669 | EXPECT_EQ("#error 1", format(" # error 1")); |
| 1670 | EXPECT_EQ("#warning 1", format(" # warning 1")); |
| 1671 | } |
| 1672 | |
Manuel Klimek | 525fe16 | 2013-01-18 14:04:34 +0000 | [diff] [blame] | 1673 | TEST_F(FormatTest, MergeHandlingInTheFaceOfPreprocessorDirectives) { |
| 1674 | FormatStyle AllowsMergedIf = getGoogleStyle(); |
| 1675 | AllowsMergedIf.AllowShortIfStatementsOnASingleLine = true; |
| 1676 | verifyFormat("void f() { f(); }\n#error E", AllowsMergedIf); |
| 1677 | verifyFormat("if (true) return 42;\n#error E", AllowsMergedIf); |
Manuel Klimek | 4c12812 | 2013-01-18 14:46:43 +0000 | [diff] [blame] | 1678 | verifyFormat("if (true)\n#error E\n return 42;", AllowsMergedIf); |
| 1679 | EXPECT_EQ("if (true) return 42;", |
| 1680 | format("if (true)\nreturn 42;", AllowsMergedIf)); |
| 1681 | FormatStyle ShortMergedIf = AllowsMergedIf; |
| 1682 | ShortMergedIf.ColumnLimit = 25; |
| 1683 | verifyFormat("#define A \\\n" |
| 1684 | " if (true) return 42;", ShortMergedIf); |
| 1685 | verifyFormat("#define A \\\n" |
| 1686 | " f(); \\\n" |
| 1687 | " if (true)\n" |
| 1688 | "#define B", ShortMergedIf); |
| 1689 | verifyFormat("#define A \\\n" |
| 1690 | " f(); \\\n" |
| 1691 | " if (true)\n" |
| 1692 | "g();", ShortMergedIf); |
Manuel Klimek | 0fbe008 | 2013-01-21 14:16:56 +0000 | [diff] [blame] | 1693 | verifyFormat("{\n" |
| 1694 | "#ifdef A\n" |
| 1695 | " // Comment\n" |
| 1696 | " if (true) continue;\n" |
| 1697 | "#endif\n" |
| 1698 | " // Comment\n" |
| 1699 | " if (true) continue;", ShortMergedIf); |
Manuel Klimek | 525fe16 | 2013-01-18 14:04:34 +0000 | [diff] [blame] | 1700 | } |
| 1701 | |
Manuel Klimek | 86721d2 | 2013-01-22 16:31:55 +0000 | [diff] [blame] | 1702 | TEST_F(FormatTest, BlockCommentsInControlLoops) { |
| 1703 | verifyFormat("if (0) /* a comment in a strange place */ {\n" |
| 1704 | " f();\n" |
| 1705 | "}"); |
| 1706 | verifyFormat("if (0) /* a comment in a strange place */ {\n" |
| 1707 | " f();\n" |
| 1708 | "} /* another comment */ else /* comment #3 */ {\n" |
| 1709 | " g();\n" |
| 1710 | "}"); |
| 1711 | verifyFormat("while (0) /* a comment in a strange place */ {\n" |
| 1712 | " f();\n" |
| 1713 | "}"); |
| 1714 | verifyFormat("for (;;) /* a comment in a strange place */ {\n" |
| 1715 | " f();\n" |
| 1716 | "}"); |
| 1717 | verifyFormat("do /* a comment in a strange place */ {\n" |
| 1718 | " f();\n" |
| 1719 | "} /* another comment */ while (0);"); |
| 1720 | } |
| 1721 | |
| 1722 | TEST_F(FormatTest, BlockComments) { |
| 1723 | EXPECT_EQ("/* */ /* */ /* */\n/* */ /* */ /* */", |
| 1724 | format("/* *//* */ /* */\n/* *//* */ /* */")); |
| 1725 | EXPECT_EQ("/* */ a /* */ b;", |
| 1726 | format(" /* */ a/* */ b;")); |
| 1727 | EXPECT_EQ("#define A /* */\\\n" |
| 1728 | " b\n" |
| 1729 | "/* */\n" |
| 1730 | "someCall(\n" |
| 1731 | " parameter);", |
| 1732 | format("#define A /* */ b\n" |
| 1733 | "/* */\n" |
| 1734 | "someCall(parameter);", getLLVMStyleWithColumns(15))); |
| 1735 | |
| 1736 | EXPECT_EQ("#define A\n" |
| 1737 | "/* */ someCall(\n" |
| 1738 | " parameter);", |
| 1739 | format("#define A\n" |
| 1740 | "/* */someCall(parameter);", getLLVMStyleWithColumns(15))); |
| 1741 | } |
| 1742 | |
Manuel Klimek | 092a2c7 | 2013-01-23 10:09:28 +0000 | [diff] [blame] | 1743 | TEST_F(FormatTest, FormatStarDependingOnContext) { |
Manuel Klimek | 70b03f4 | 2013-01-23 09:32:48 +0000 | [diff] [blame] | 1744 | verifyFormat("void f(int *a);"); |
| 1745 | verifyFormat("void f() { f(fint * b); }"); |
Manuel Klimek | 836b58f | 2013-01-23 11:03:04 +0000 | [diff] [blame] | 1746 | verifyFormat("class A {\n void f(int *a);\n};"); |
| 1747 | verifyFormat("class A {\n int *a;\n};"); |
| 1748 | verifyFormat("namespace a {\n" |
| 1749 | "namespace b {\n" |
| 1750 | "class A {\n" |
| 1751 | " void f() {}\n" |
| 1752 | " int *a;\n" |
| 1753 | "};\n" |
| 1754 | "}\n" |
| 1755 | "}"); |
Manuel Klimek | 70b03f4 | 2013-01-23 09:32:48 +0000 | [diff] [blame] | 1756 | } |
| 1757 | |
Manuel Klimek | 092a2c7 | 2013-01-23 10:09:28 +0000 | [diff] [blame] | 1758 | TEST_F(FormatTest, SpecialTokensAtEndOfLine) { |
| 1759 | verifyFormat("while"); |
| 1760 | verifyFormat("operator"); |
| 1761 | } |
| 1762 | |
Nico Weber | cf4a79c | 2013-01-08 17:56:31 +0000 | [diff] [blame] | 1763 | //===----------------------------------------------------------------------===// |
| 1764 | // Objective-C tests. |
| 1765 | //===----------------------------------------------------------------------===// |
| 1766 | |
Fariborz Jahanian | 154120c | 2012-12-20 19:54:13 +0000 | [diff] [blame] | 1767 | TEST_F(FormatTest, FormatForObjectiveCMethodDecls) { |
| 1768 | verifyFormat("- (void)sendAction:(SEL)aSelector to:(BOOL)anObject;"); |
| 1769 | EXPECT_EQ("- (NSUInteger)indexOfObject:(id)anObject;", |
| 1770 | format("-(NSUInteger)indexOfObject:(id)anObject;")); |
Daniel Jasper | cd16238 | 2013-01-07 13:26:07 +0000 | [diff] [blame] | 1771 | EXPECT_EQ("- (NSInteger)Mthod1;", format("-(NSInteger)Mthod1;")); |
Fariborz Jahanian | 154120c | 2012-12-20 19:54:13 +0000 | [diff] [blame] | 1772 | EXPECT_EQ("+ (id)Mthod2;", format("+(id)Mthod2;")); |
| 1773 | EXPECT_EQ("- (NSInteger)Method3:(id)anObject;", |
| 1774 | format("-(NSInteger)Method3:(id)anObject;")); |
| 1775 | EXPECT_EQ("- (NSInteger)Method4:(id)anObject;", |
| 1776 | format("-(NSInteger)Method4:(id)anObject;")); |
| 1777 | EXPECT_EQ("- (NSInteger)Method5:(id)anObject:(id)AnotherObject;", |
| 1778 | format("-(NSInteger)Method5:(id)anObject:(id)AnotherObject;")); |
| 1779 | EXPECT_EQ("- (id)Method6:(id)A:(id)B:(id)C:(id)D;", |
| 1780 | format("- (id)Method6:(id)A:(id)B:(id)C:(id)D;")); |
Daniel Jasper | cd16238 | 2013-01-07 13:26:07 +0000 | [diff] [blame] | 1781 | EXPECT_EQ( |
| 1782 | "- (void)sendAction:(SEL)aSelector to:(id)anObject forAllCells:(BOOL)flag;", |
| 1783 | format("- (void)sendAction:(SEL)aSelector to:(id)anObject forAllCells:(BOOL)flag;")); |
Fariborz Jahanian | 9b3f02c | 2012-12-21 22:51:18 +0000 | [diff] [blame] | 1784 | |
| 1785 | // Very long objectiveC method declaration. |
Daniel Jasper | cd16238 | 2013-01-07 13:26:07 +0000 | [diff] [blame] | 1786 | EXPECT_EQ( |
| 1787 | "- (NSUInteger)indexOfObject:(id)anObject inRange:(NSRange)range\n " |
| 1788 | "outRange:(NSRange)out_range outRange1:(NSRange)out_range1\n " |
| 1789 | "outRange2:(NSRange)out_range2 outRange3:(NSRange)out_range3\n " |
| 1790 | "outRange4:(NSRange)out_range4 outRange5:(NSRange)out_range5\n " |
| 1791 | "outRange6:(NSRange)out_range6 outRange7:(NSRange)out_range7\n " |
| 1792 | "outRange8:(NSRange)out_range8 outRange9:(NSRange)out_range9;", |
| 1793 | format( |
| 1794 | "- (NSUInteger)indexOfObject:(id)anObject inRange:(NSRange)range " |
| 1795 | "outRange:(NSRange) out_range outRange1:(NSRange) out_range1 " |
| 1796 | "outRange2:(NSRange) out_range2 outRange3:(NSRange) out_range3 " |
| 1797 | "outRange4:(NSRange) out_range4 outRange5:(NSRange) out_range5 " |
| 1798 | "outRange6:(NSRange) out_range6 outRange7:(NSRange) out_range7 " |
| 1799 | "outRange8:(NSRange) out_range8 outRange9:(NSRange) out_range9;")); |
Nico Weber | e0fd429 | 2013-01-10 20:18:33 +0000 | [diff] [blame] | 1800 | |
| 1801 | verifyFormat("- (int)sum:(vector<int>)numbers;"); |
Nico Weber | aab6005 | 2013-01-17 06:14:50 +0000 | [diff] [blame] | 1802 | verifyGoogleFormat("- (void)setDelegate:(id<Protocol>)delegate;"); |
Nico Weber | e0fd429 | 2013-01-10 20:18:33 +0000 | [diff] [blame] | 1803 | // FIXME: In LLVM style, there should be a space in front of a '<' for ObjC |
| 1804 | // protocol lists (but not for template classes): |
| 1805 | //verifyFormat("- (void)setDelegate:(id <Protocol>)delegate;"); |
Nico Weber | cd52bda | 2013-01-10 23:11:41 +0000 | [diff] [blame] | 1806 | |
| 1807 | verifyFormat("- (int(*)())foo:(int(*)())f;"); |
Nico Weber | aab6005 | 2013-01-17 06:14:50 +0000 | [diff] [blame] | 1808 | verifyGoogleFormat("- (int(*)())foo:(int(*)())foo;"); |
Nico Weber | cd52bda | 2013-01-10 23:11:41 +0000 | [diff] [blame] | 1809 | |
| 1810 | // If there's no return type (very rare in practice!), LLVM and Google style |
| 1811 | // agree. |
| 1812 | verifyFormat("- foo:(int)f;"); |
| 1813 | verifyGoogleFormat("- foo:(int)foo;"); |
Fariborz Jahanian | 154120c | 2012-12-20 19:54:13 +0000 | [diff] [blame] | 1814 | } |
| 1815 | |
Daniel Jasper | 886568d | 2013-01-09 08:36:49 +0000 | [diff] [blame] | 1816 | TEST_F(FormatTest, FormatObjCBlocks) { |
Daniel Jasper | 46ef852 | 2013-01-10 13:08:12 +0000 | [diff] [blame] | 1817 | verifyFormat("int (^Block)(int, int);"); |
| 1818 | verifyFormat("int (^Block1)(int, int) = ^(int i, int j)"); |
Daniel Jasper | 886568d | 2013-01-09 08:36:49 +0000 | [diff] [blame] | 1819 | } |
| 1820 | |
Nico Weber | 27d1367 | 2013-01-09 20:25:35 +0000 | [diff] [blame] | 1821 | TEST_F(FormatTest, FormatObjCInterface) { |
Nico Weber | 5f500df | 2013-01-10 20:12:55 +0000 | [diff] [blame] | 1822 | verifyFormat("@interface Foo : NSObject <NSSomeDelegate> {\n" |
Nico Weber | 27d1367 | 2013-01-09 20:25:35 +0000 | [diff] [blame] | 1823 | "@public\n" |
| 1824 | " int field1;\n" |
| 1825 | "@protected\n" |
| 1826 | " int field2;\n" |
| 1827 | "@private\n" |
| 1828 | " int field3;\n" |
| 1829 | "@package\n" |
| 1830 | " int field4;\n" |
| 1831 | "}\n" |
| 1832 | "+ (id)init;\n" |
| 1833 | "@end"); |
| 1834 | |
Nico Weber | 27d1367 | 2013-01-09 20:25:35 +0000 | [diff] [blame] | 1835 | verifyGoogleFormat("@interface Foo : NSObject<NSSomeDelegate> {\n" |
| 1836 | " @public\n" |
| 1837 | " int field1;\n" |
| 1838 | " @protected\n" |
| 1839 | " int field2;\n" |
| 1840 | " @private\n" |
| 1841 | " int field3;\n" |
| 1842 | " @package\n" |
| 1843 | " int field4;\n" |
| 1844 | "}\n" |
Nico Weber | aab6005 | 2013-01-17 06:14:50 +0000 | [diff] [blame] | 1845 | "+ (id)init;\n" |
Nico Weber | 27d1367 | 2013-01-09 20:25:35 +0000 | [diff] [blame] | 1846 | "@end"); |
| 1847 | |
Nico Weber | f5ecfa5 | 2013-01-22 16:53:59 +0000 | [diff] [blame] | 1848 | verifyFormat("@interface /* wait for it */ Foo\n" |
Nico Weber | 27d1367 | 2013-01-09 20:25:35 +0000 | [diff] [blame] | 1849 | "+ (id)init;\n" |
| 1850 | "// Look, a comment!\n" |
| 1851 | "- (int)answerWith:(int)i;\n" |
| 1852 | "@end"); |
| 1853 | |
| 1854 | verifyFormat("@interface Foo\n" |
Nico Weber | 049c447 | 2013-01-09 21:42:32 +0000 | [diff] [blame] | 1855 | "@end\n" |
| 1856 | "@interface Bar\n" |
Nico Weber | 27d1367 | 2013-01-09 20:25:35 +0000 | [diff] [blame] | 1857 | "@end"); |
| 1858 | |
| 1859 | verifyFormat("@interface Foo : Bar\n" |
| 1860 | "+ (id)init;\n" |
| 1861 | "@end"); |
| 1862 | |
Nico Weber | f5ecfa5 | 2013-01-22 16:53:59 +0000 | [diff] [blame] | 1863 | verifyFormat("@interface Foo : /**/ Bar /**/ <Baz, /**/ Quux>\n" |
Nico Weber | 27d1367 | 2013-01-09 20:25:35 +0000 | [diff] [blame] | 1864 | "+ (id)init;\n" |
| 1865 | "@end"); |
| 1866 | |
Nico Weber | 5f500df | 2013-01-10 20:12:55 +0000 | [diff] [blame] | 1867 | verifyGoogleFormat("@interface Foo : Bar<Baz, Quux>\n" |
Nico Weber | aab6005 | 2013-01-17 06:14:50 +0000 | [diff] [blame] | 1868 | "+ (id)init;\n" |
Nico Weber | 5f500df | 2013-01-10 20:12:55 +0000 | [diff] [blame] | 1869 | "@end"); |
| 1870 | |
Nico Weber | ed91bba | 2013-01-10 19:19:14 +0000 | [diff] [blame] | 1871 | verifyFormat("@interface Foo (HackStuff)\n" |
Nico Weber | 27d1367 | 2013-01-09 20:25:35 +0000 | [diff] [blame] | 1872 | "+ (id)init;\n" |
| 1873 | "@end"); |
| 1874 | |
Nico Weber | ed91bba | 2013-01-10 19:19:14 +0000 | [diff] [blame] | 1875 | verifyFormat("@interface Foo ()\n" |
Nico Weber | 27d1367 | 2013-01-09 20:25:35 +0000 | [diff] [blame] | 1876 | "+ (id)init;\n" |
| 1877 | "@end"); |
| 1878 | |
Nico Weber | 5f500df | 2013-01-10 20:12:55 +0000 | [diff] [blame] | 1879 | verifyFormat("@interface Foo (HackStuff) <MyProtocol>\n" |
Nico Weber | 27d1367 | 2013-01-09 20:25:35 +0000 | [diff] [blame] | 1880 | "+ (id)init;\n" |
| 1881 | "@end"); |
| 1882 | |
Nico Weber | 5f500df | 2013-01-10 20:12:55 +0000 | [diff] [blame] | 1883 | verifyGoogleFormat("@interface Foo (HackStuff)<MyProtocol>\n" |
Nico Weber | aab6005 | 2013-01-17 06:14:50 +0000 | [diff] [blame] | 1884 | "+ (id)init;\n" |
Nico Weber | 5f500df | 2013-01-10 20:12:55 +0000 | [diff] [blame] | 1885 | "@end"); |
| 1886 | |
Manuel Klimek | d5688cf | 2013-01-11 19:17:44 +0000 | [diff] [blame] | 1887 | verifyFormat("@interface Foo {\n" |
| 1888 | " int _i;\n" |
| 1889 | "}\n" |
Nico Weber | 27d1367 | 2013-01-09 20:25:35 +0000 | [diff] [blame] | 1890 | "+ (id)init;\n" |
| 1891 | "@end"); |
| 1892 | |
Manuel Klimek | d5688cf | 2013-01-11 19:17:44 +0000 | [diff] [blame] | 1893 | verifyFormat("@interface Foo : Bar {\n" |
| 1894 | " int _i;\n" |
| 1895 | "}\n" |
Nico Weber | 27d1367 | 2013-01-09 20:25:35 +0000 | [diff] [blame] | 1896 | "+ (id)init;\n" |
| 1897 | "@end"); |
| 1898 | |
Manuel Klimek | d5688cf | 2013-01-11 19:17:44 +0000 | [diff] [blame] | 1899 | verifyFormat("@interface Foo : Bar <Baz, Quux> {\n" |
| 1900 | " int _i;\n" |
| 1901 | "}\n" |
Nico Weber | 27d1367 | 2013-01-09 20:25:35 +0000 | [diff] [blame] | 1902 | "+ (id)init;\n" |
| 1903 | "@end"); |
| 1904 | |
Manuel Klimek | d5688cf | 2013-01-11 19:17:44 +0000 | [diff] [blame] | 1905 | verifyFormat("@interface Foo (HackStuff) {\n" |
| 1906 | " int _i;\n" |
| 1907 | "}\n" |
Nico Weber | 27d1367 | 2013-01-09 20:25:35 +0000 | [diff] [blame] | 1908 | "+ (id)init;\n" |
| 1909 | "@end"); |
| 1910 | |
Manuel Klimek | d5688cf | 2013-01-11 19:17:44 +0000 | [diff] [blame] | 1911 | verifyFormat("@interface Foo () {\n" |
| 1912 | " int _i;\n" |
| 1913 | "}\n" |
Nico Weber | 27d1367 | 2013-01-09 20:25:35 +0000 | [diff] [blame] | 1914 | "+ (id)init;\n" |
| 1915 | "@end"); |
| 1916 | |
Manuel Klimek | d5688cf | 2013-01-11 19:17:44 +0000 | [diff] [blame] | 1917 | verifyFormat("@interface Foo (HackStuff) <MyProtocol> {\n" |
| 1918 | " int _i;\n" |
| 1919 | "}\n" |
Nico Weber | 27d1367 | 2013-01-09 20:25:35 +0000 | [diff] [blame] | 1920 | "+ (id)init;\n" |
| 1921 | "@end"); |
| 1922 | } |
| 1923 | |
Nico Weber | 50767d8 | 2013-01-09 23:25:37 +0000 | [diff] [blame] | 1924 | TEST_F(FormatTest, FormatObjCImplementation) { |
| 1925 | verifyFormat("@implementation Foo : NSObject {\n" |
| 1926 | "@public\n" |
| 1927 | " int field1;\n" |
| 1928 | "@protected\n" |
| 1929 | " int field2;\n" |
| 1930 | "@private\n" |
| 1931 | " int field3;\n" |
| 1932 | "@package\n" |
| 1933 | " int field4;\n" |
| 1934 | "}\n" |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 1935 | "+ (id)init {\n}\n" |
Nico Weber | 50767d8 | 2013-01-09 23:25:37 +0000 | [diff] [blame] | 1936 | "@end"); |
| 1937 | |
| 1938 | verifyGoogleFormat("@implementation Foo : NSObject {\n" |
| 1939 | " @public\n" |
| 1940 | " int field1;\n" |
| 1941 | " @protected\n" |
| 1942 | " int field2;\n" |
| 1943 | " @private\n" |
| 1944 | " int field3;\n" |
| 1945 | " @package\n" |
| 1946 | " int field4;\n" |
| 1947 | "}\n" |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 1948 | "+ (id)init {\n}\n" |
Nico Weber | 50767d8 | 2013-01-09 23:25:37 +0000 | [diff] [blame] | 1949 | "@end"); |
| 1950 | |
| 1951 | verifyFormat("@implementation Foo\n" |
| 1952 | "+ (id)init {\n" |
Daniel Jasper | 6f5bb2c | 2013-01-14 16:24:39 +0000 | [diff] [blame] | 1953 | " if (true)\n" |
| 1954 | " return nil;\n" |
Nico Weber | 50767d8 | 2013-01-09 23:25:37 +0000 | [diff] [blame] | 1955 | "}\n" |
| 1956 | "// Look, a comment!\n" |
Manuel Klimek | d5688cf | 2013-01-11 19:17:44 +0000 | [diff] [blame] | 1957 | "- (int)answerWith:(int)i {\n" |
| 1958 | " return i;\n" |
| 1959 | "}\n" |
Nico Weber | 67015ed | 2013-01-11 21:14:08 +0000 | [diff] [blame] | 1960 | "+ (int)answerWith:(int)i {\n" |
| 1961 | " return i;\n" |
| 1962 | "}\n" |
Nico Weber | 50767d8 | 2013-01-09 23:25:37 +0000 | [diff] [blame] | 1963 | "@end"); |
| 1964 | |
| 1965 | verifyFormat("@implementation Foo\n" |
| 1966 | "@end\n" |
| 1967 | "@implementation Bar\n" |
| 1968 | "@end"); |
| 1969 | |
| 1970 | verifyFormat("@implementation Foo : Bar\n" |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 1971 | "+ (id)init {\n}\n" |
| 1972 | "- (void)foo {\n}\n" |
Nico Weber | 50767d8 | 2013-01-09 23:25:37 +0000 | [diff] [blame] | 1973 | "@end"); |
| 1974 | |
Manuel Klimek | d5688cf | 2013-01-11 19:17:44 +0000 | [diff] [blame] | 1975 | verifyFormat("@implementation Foo {\n" |
| 1976 | " int _i;\n" |
| 1977 | "}\n" |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 1978 | "+ (id)init {\n}\n" |
Nico Weber | 50767d8 | 2013-01-09 23:25:37 +0000 | [diff] [blame] | 1979 | "@end"); |
| 1980 | |
Manuel Klimek | d5688cf | 2013-01-11 19:17:44 +0000 | [diff] [blame] | 1981 | verifyFormat("@implementation Foo : Bar {\n" |
| 1982 | " int _i;\n" |
| 1983 | "}\n" |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 1984 | "+ (id)init {\n}\n" |
Nico Weber | 50767d8 | 2013-01-09 23:25:37 +0000 | [diff] [blame] | 1985 | "@end"); |
| 1986 | |
Nico Weber | ed91bba | 2013-01-10 19:19:14 +0000 | [diff] [blame] | 1987 | verifyFormat("@implementation Foo (HackStuff)\n" |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 1988 | "+ (id)init {\n}\n" |
Nico Weber | 50767d8 | 2013-01-09 23:25:37 +0000 | [diff] [blame] | 1989 | "@end"); |
| 1990 | } |
| 1991 | |
Nico Weber | 1abe6ea | 2013-01-09 21:15:03 +0000 | [diff] [blame] | 1992 | TEST_F(FormatTest, FormatObjCProtocol) { |
| 1993 | verifyFormat("@protocol Foo\n" |
| 1994 | "@property(weak) id delegate;\n" |
| 1995 | "- (NSUInteger)numberOfThings;\n" |
| 1996 | "@end"); |
| 1997 | |
Nico Weber | 5f500df | 2013-01-10 20:12:55 +0000 | [diff] [blame] | 1998 | verifyFormat("@protocol MyProtocol <NSObject>\n" |
Nico Weber | 1abe6ea | 2013-01-09 21:15:03 +0000 | [diff] [blame] | 1999 | "- (NSUInteger)numberOfThings;\n" |
| 2000 | "@end"); |
| 2001 | |
Nico Weber | 5f500df | 2013-01-10 20:12:55 +0000 | [diff] [blame] | 2002 | verifyGoogleFormat("@protocol MyProtocol<NSObject>\n" |
Nico Weber | aab6005 | 2013-01-17 06:14:50 +0000 | [diff] [blame] | 2003 | "- (NSUInteger)numberOfThings;\n" |
Nico Weber | 5f500df | 2013-01-10 20:12:55 +0000 | [diff] [blame] | 2004 | "@end"); |
| 2005 | |
Nico Weber | 1abe6ea | 2013-01-09 21:15:03 +0000 | [diff] [blame] | 2006 | verifyFormat("@protocol Foo;\n" |
| 2007 | "@protocol Bar;\n"); |
Nico Weber | 049c447 | 2013-01-09 21:42:32 +0000 | [diff] [blame] | 2008 | |
| 2009 | verifyFormat("@protocol Foo\n" |
| 2010 | "@end\n" |
| 2011 | "@protocol Bar\n" |
| 2012 | "@end"); |
Nico Weber | b530fa3 | 2013-01-10 00:25:19 +0000 | [diff] [blame] | 2013 | |
| 2014 | verifyFormat("@protocol myProtocol\n" |
| 2015 | "- (void)mandatoryWithInt:(int)i;\n" |
| 2016 | "@optional\n" |
| 2017 | "- (void)optional;\n" |
| 2018 | "@required\n" |
| 2019 | "- (void)required;\n" |
Nico Weber | 880e538 | 2013-01-10 00:42:07 +0000 | [diff] [blame] | 2020 | "@optional\n" |
| 2021 | "@property(assign) int madProp;\n" |
Nico Weber | b530fa3 | 2013-01-10 00:25:19 +0000 | [diff] [blame] | 2022 | "@end\n"); |
Nico Weber | 1abe6ea | 2013-01-09 21:15:03 +0000 | [diff] [blame] | 2023 | } |
| 2024 | |
Nico Weber | bcfdd26 | 2013-01-12 06:18:40 +0000 | [diff] [blame] | 2025 | TEST_F(FormatTest, FormatObjCMethodExpr) { |
| 2026 | verifyFormat("[foo bar:baz];"); |
| 2027 | verifyFormat("return [foo bar:baz];"); |
| 2028 | verifyFormat("f([foo bar:baz]);"); |
| 2029 | verifyFormat("f(2, [foo bar:baz]);"); |
| 2030 | verifyFormat("f(2, a ? b : c);"); |
| 2031 | verifyFormat("[[self initWithInt:4] bar:[baz quux:arrrr]];"); |
| 2032 | |
| 2033 | verifyFormat("[foo bar:baz], [foo bar:baz];"); |
| 2034 | verifyFormat("[foo bar:baz] = [foo bar:baz];"); |
| 2035 | verifyFormat("[foo bar:baz] *= [foo bar:baz];"); |
| 2036 | verifyFormat("[foo bar:baz] /= [foo bar:baz];"); |
| 2037 | verifyFormat("[foo bar:baz] %= [foo bar:baz];"); |
| 2038 | verifyFormat("[foo bar:baz] += [foo bar:baz];"); |
| 2039 | verifyFormat("[foo bar:baz] -= [foo bar:baz];"); |
| 2040 | verifyFormat("[foo bar:baz] <<= [foo bar:baz];"); |
| 2041 | verifyFormat("[foo bar:baz] >>= [foo bar:baz];"); |
| 2042 | verifyFormat("[foo bar:baz] &= [foo bar:baz];"); |
| 2043 | verifyFormat("[foo bar:baz] ^= [foo bar:baz];"); |
| 2044 | verifyFormat("[foo bar:baz] |= [foo bar:baz];"); |
| 2045 | verifyFormat("[foo bar:baz] ? [foo bar:baz] : [foo bar:baz];"); |
| 2046 | verifyFormat("[foo bar:baz] || [foo bar:baz];"); |
| 2047 | verifyFormat("[foo bar:baz] && [foo bar:baz];"); |
| 2048 | verifyFormat("[foo bar:baz] | [foo bar:baz];"); |
| 2049 | verifyFormat("[foo bar:baz] ^ [foo bar:baz];"); |
| 2050 | verifyFormat("[foo bar:baz] & [foo bar:baz];"); |
| 2051 | verifyFormat("[foo bar:baz] == [foo bar:baz];"); |
| 2052 | verifyFormat("[foo bar:baz] != [foo bar:baz];"); |
| 2053 | verifyFormat("[foo bar:baz] >= [foo bar:baz];"); |
| 2054 | verifyFormat("[foo bar:baz] <= [foo bar:baz];"); |
| 2055 | verifyFormat("[foo bar:baz] > [foo bar:baz];"); |
| 2056 | verifyFormat("[foo bar:baz] < [foo bar:baz];"); |
| 2057 | verifyFormat("[foo bar:baz] >> [foo bar:baz];"); |
| 2058 | verifyFormat("[foo bar:baz] << [foo bar:baz];"); |
| 2059 | verifyFormat("[foo bar:baz] - [foo bar:baz];"); |
| 2060 | verifyFormat("[foo bar:baz] + [foo bar:baz];"); |
| 2061 | verifyFormat("[foo bar:baz] * [foo bar:baz];"); |
| 2062 | verifyFormat("[foo bar:baz] / [foo bar:baz];"); |
| 2063 | verifyFormat("[foo bar:baz] % [foo bar:baz];"); |
| 2064 | // Whew! |
| 2065 | |
| 2066 | verifyFormat("[self stuffWithInt:(4 + 2) float:4.5];"); |
| 2067 | verifyFormat("[self stuffWithInt:a ? b : c float:4.5];"); |
| 2068 | verifyFormat("[self stuffWithInt:a ? [self foo:bar] : c];"); |
| 2069 | verifyFormat("[self stuffWithInt:a ? (e ? f : g) : c];"); |
| 2070 | verifyFormat("[cond ? obj1 : obj2 methodWithParam:param]"); |
Nico Weber | 6292dd4 | 2013-01-12 23:41:33 +0000 | [diff] [blame] | 2071 | verifyFormat("[button setAction:@selector(zoomOut:)];"); |
Nico Weber | cd45833 | 2013-01-12 23:48:49 +0000 | [diff] [blame] | 2072 | verifyFormat("[color getRed:&r green:&g blue:&b alpha:&a];"); |
Nico Weber | bcfdd26 | 2013-01-12 06:18:40 +0000 | [diff] [blame] | 2073 | |
Nico Weber | bcfdd26 | 2013-01-12 06:18:40 +0000 | [diff] [blame] | 2074 | verifyFormat("arr[[self indexForFoo:a]];"); |
| 2075 | verifyFormat("throw [self errorFor:a];"); |
| 2076 | verifyFormat("@throw [self errorFor:a];"); |
| 2077 | |
Nico Weber | e8ccc81 | 2013-01-12 22:48:47 +0000 | [diff] [blame] | 2078 | // This tests that the formatter doesn't break after "backing" but before ":", |
| 2079 | // which would be at 80 columns. |
Nico Weber | bcfdd26 | 2013-01-12 06:18:40 +0000 | [diff] [blame] | 2080 | verifyFormat( |
| 2081 | "void f() {\n" |
Nico Weber | e8ccc81 | 2013-01-12 22:48:47 +0000 | [diff] [blame] | 2082 | " if ((self = [super initWithContentRect:contentRect styleMask:styleMask\n" |
Daniel Jasper | 3499dda | 2013-01-25 15:43:32 +0000 | [diff] [blame] | 2083 | " backing:NSBackingStoreBuffered defer:YES]))"); |
Nico Weber | e8ccc81 | 2013-01-12 22:48:47 +0000 | [diff] [blame] | 2084 | |
Nico Weber | e8ccc81 | 2013-01-12 22:48:47 +0000 | [diff] [blame] | 2085 | verifyFormat("[foo checkThatBreakingAfterColonWorksOk:\n" |
| 2086 | " [bar ifItDoes:reduceOverallLineLengthLikeInThisCase]];"); |
Nico Weber | bcfdd26 | 2013-01-12 06:18:40 +0000 | [diff] [blame] | 2087 | |
| 2088 | } |
| 2089 | |
Nico Weber | 581f557 | 2013-01-07 15:56:25 +0000 | [diff] [blame] | 2090 | TEST_F(FormatTest, ObjCAt) { |
Nico Weber | 4a293a2 | 2013-01-07 16:07:07 +0000 | [diff] [blame] | 2091 | verifyFormat("@autoreleasepool"); |
Nico Weber | d0af4b4 | 2013-01-07 16:14:28 +0000 | [diff] [blame] | 2092 | verifyFormat("@catch"); |
| 2093 | verifyFormat("@class"); |
Nico Weber | 4a293a2 | 2013-01-07 16:07:07 +0000 | [diff] [blame] | 2094 | verifyFormat("@compatibility_alias"); |
| 2095 | verifyFormat("@defs"); |
Nico Weber | efcfe73 | 2013-01-07 15:17:23 +0000 | [diff] [blame] | 2096 | verifyFormat("@dynamic"); |
Nico Weber | 4a293a2 | 2013-01-07 16:07:07 +0000 | [diff] [blame] | 2097 | verifyFormat("@encode"); |
| 2098 | verifyFormat("@end"); |
| 2099 | verifyFormat("@finally"); |
| 2100 | verifyFormat("@implementation"); |
| 2101 | verifyFormat("@import"); |
| 2102 | verifyFormat("@interface"); |
| 2103 | verifyFormat("@optional"); |
| 2104 | verifyFormat("@package"); |
Nico Weber | d0af4b4 | 2013-01-07 16:14:28 +0000 | [diff] [blame] | 2105 | verifyFormat("@private"); |
Nico Weber | 4a293a2 | 2013-01-07 16:07:07 +0000 | [diff] [blame] | 2106 | verifyFormat("@property"); |
Nico Weber | d0af4b4 | 2013-01-07 16:14:28 +0000 | [diff] [blame] | 2107 | verifyFormat("@protected"); |
Nico Weber | 4a293a2 | 2013-01-07 16:07:07 +0000 | [diff] [blame] | 2108 | verifyFormat("@protocol"); |
Nico Weber | d0af4b4 | 2013-01-07 16:14:28 +0000 | [diff] [blame] | 2109 | verifyFormat("@public"); |
Nico Weber | 4a293a2 | 2013-01-07 16:07:07 +0000 | [diff] [blame] | 2110 | verifyFormat("@required"); |
| 2111 | verifyFormat("@selector"); |
| 2112 | verifyFormat("@synchronized"); |
| 2113 | verifyFormat("@synthesize"); |
Nico Weber | d0af4b4 | 2013-01-07 16:14:28 +0000 | [diff] [blame] | 2114 | verifyFormat("@throw"); |
| 2115 | verifyFormat("@try"); |
Nico Weber | 4a293a2 | 2013-01-07 16:07:07 +0000 | [diff] [blame] | 2116 | |
Nico Weber | cb4d690 | 2013-01-08 19:40:21 +0000 | [diff] [blame] | 2117 | verifyFormat("@\"String\""); |
| 2118 | verifyFormat("@1"); |
Nico Weber | 81ed2f1 | 2013-01-10 19:36:35 +0000 | [diff] [blame] | 2119 | verifyFormat("@+4.8"); |
| 2120 | verifyFormat("@-4"); |
Nico Weber | cb4d690 | 2013-01-08 19:40:21 +0000 | [diff] [blame] | 2121 | verifyFormat("@1LL"); |
| 2122 | verifyFormat("@.5"); |
| 2123 | verifyFormat("@'c'"); |
| 2124 | verifyFormat("@true"); |
| 2125 | verifyFormat("NSNumber *smallestInt = @(-INT_MAX - 1);"); |
Nico Weber | e8ccc81 | 2013-01-12 22:48:47 +0000 | [diff] [blame] | 2126 | // FIXME: Array and dictionary literals need more work. |
Nico Weber | cb4d690 | 2013-01-08 19:40:21 +0000 | [diff] [blame] | 2127 | verifyFormat("@["); |
| 2128 | verifyFormat("@{"); |
| 2129 | |
Nico Weber | 581f557 | 2013-01-07 15:56:25 +0000 | [diff] [blame] | 2130 | EXPECT_EQ("@interface", format("@ interface")); |
| 2131 | |
| 2132 | // The precise formatting of this doesn't matter, nobody writes code like |
| 2133 | // this. |
| 2134 | verifyFormat("@ /*foo*/ interface"); |
Nico Weber | efcfe73 | 2013-01-07 15:17:23 +0000 | [diff] [blame] | 2135 | } |
| 2136 | |
Nico Weber | c31689a | 2013-01-08 19:15:23 +0000 | [diff] [blame] | 2137 | TEST_F(FormatTest, ObjCSnippets) { |
Manuel Klimek | d5688cf | 2013-01-11 19:17:44 +0000 | [diff] [blame] | 2138 | verifyFormat("@autoreleasepool {\n" |
| 2139 | " foo();\n" |
| 2140 | "}"); |
Nico Weber | cf4a79c | 2013-01-08 17:56:31 +0000 | [diff] [blame] | 2141 | verifyFormat("@class Foo, Bar;"); |
Nico Weber | c31689a | 2013-01-08 19:15:23 +0000 | [diff] [blame] | 2142 | verifyFormat("@compatibility_alias AliasName ExistingClass;"); |
Nico Weber | cf4a79c | 2013-01-08 17:56:31 +0000 | [diff] [blame] | 2143 | verifyFormat("@dynamic textColor;"); |
Nico Weber | 5dfe9b4 | 2013-01-18 05:11:47 +0000 | [diff] [blame] | 2144 | verifyFormat("char *buf1 = @encode(int *);"); |
| 2145 | verifyFormat("char *buf1 = @encode(typeof(4 * 5));"); |
| 2146 | // FIXME: Enable once PR14884 is fixed: |
Nico Weber | c31689a | 2013-01-08 19:15:23 +0000 | [diff] [blame] | 2147 | //verifyFormat("char *buf1 = @encode(int **);"); |
Nico Weber | 3a2673e | 2013-01-08 20:16:23 +0000 | [diff] [blame] | 2148 | verifyFormat("Protocol *proto = @protocol(p1);"); |
Nico Weber | 6a21a55 | 2013-01-18 02:43:57 +0000 | [diff] [blame] | 2149 | verifyFormat("SEL s = @selector(foo:);"); |
Manuel Klimek | d5688cf | 2013-01-11 19:17:44 +0000 | [diff] [blame] | 2150 | verifyFormat("@synchronized(self) {\n" |
| 2151 | " f();\n" |
| 2152 | "}"); |
Nico Weber | cf4a79c | 2013-01-08 17:56:31 +0000 | [diff] [blame] | 2153 | |
Nico Weber | 7084823 | 2013-01-10 21:30:42 +0000 | [diff] [blame] | 2154 | verifyFormat("@synthesize dropArrowPosition = dropArrowPosition_;"); |
| 2155 | verifyGoogleFormat("@synthesize dropArrowPosition = dropArrowPosition_;"); |
| 2156 | |
Nico Weber | cf4a79c | 2013-01-08 17:56:31 +0000 | [diff] [blame] | 2157 | verifyFormat("@property(assign, nonatomic) CGFloat hoverAlpha;"); |
Nico Weber | 7084823 | 2013-01-10 21:30:42 +0000 | [diff] [blame] | 2158 | verifyFormat("@property(assign, getter=isEditable) BOOL editable;"); |
| 2159 | verifyGoogleFormat("@property(assign, getter=isEditable) BOOL editable;"); |
Nico Weber | cf4a79c | 2013-01-08 17:56:31 +0000 | [diff] [blame] | 2160 | } |
| 2161 | |
Daniel Jasper | cd16238 | 2013-01-07 13:26:07 +0000 | [diff] [blame] | 2162 | } // end namespace tooling |
| 2163 | } // end namespace clang |