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