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