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 | "}"); |
Daniel Jasper | b312314 | 2013-01-12 07:36:22 +0000 | [diff] [blame] | 684 | |
| 685 | // This test takes VERY long when memoization is broken. |
| 686 | verifyGoogleFormat( |
| 687 | "Constructor()\n" |
| 688 | " : aaaa(a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a," |
| 689 | " a, a, a,\n" |
| 690 | " a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a," |
| 691 | " a, a, a,\n" |
| 692 | " a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a," |
| 693 | " a, a, a,\n" |
| 694 | " a, a, a, a, a, a, a, a, a, a, a)\n" |
| 695 | " aaaa(a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a," |
| 696 | " a, a, a,\n" |
| 697 | " a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a," |
| 698 | " a, a, a,\n" |
| 699 | " a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a," |
| 700 | " a, a, a,\n" |
| 701 | " a, a, a, a, a, a, a, a, a, a, a) {}\n"); |
Daniel Jasper | 1321eb5 | 2012-12-18 21:05:13 +0000 | [diff] [blame] | 702 | } |
| 703 | |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 704 | TEST_F(FormatTest, BreaksAsHighAsPossible) { |
| 705 | verifyFormat( |
| 706 | "if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa && aaaaaaaaaaaaaaaaaaaaaaaaaa) ||\n" |
| 707 | " (bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb && bbbbbbbbbbbbbbbbbbbbbbbbbb))\n" |
| 708 | " f();"); |
| 709 | } |
| 710 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 711 | TEST_F(FormatTest, BreaksDesireably) { |
| 712 | verifyFormat("if (aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n" |
| 713 | " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n" |
Manuel Klimek | 36fab8d | 2013-01-10 13:24:24 +0000 | [diff] [blame] | 714 | " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa)) {}"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 715 | |
| 716 | verifyFormat( |
| 717 | "aaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" |
Manuel Klimek | 36fab8d | 2013-01-10 13:24:24 +0000 | [diff] [blame] | 718 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 719 | |
| 720 | verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" |
| 721 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" |
| 722 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));"); |
Daniel Jasper | a88bb45 | 2012-12-04 10:50:12 +0000 | [diff] [blame] | 723 | |
| 724 | verifyFormat( |
| 725 | "aaaaaaaa(aaaaaaaaaaaaa, aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" |
| 726 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n" |
| 727 | " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" |
| 728 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));"); |
Daniel Jasper | 33182dd | 2012-12-05 14:57:28 +0000 | [diff] [blame] | 729 | |
| 730 | verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" |
| 731 | " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); |
| 732 | |
Daniel Jasper | 723f030 | 2013-01-02 14:40:02 +0000 | [diff] [blame] | 733 | verifyFormat( |
| 734 | "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&\n" |
| 735 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); |
| 736 | |
Daniel Jasper | 33182dd | 2012-12-05 14:57:28 +0000 | [diff] [blame] | 737 | // This test case breaks on an incorrect memoization, i.e. an optimization not |
| 738 | // taking into account the StopAt value. |
| 739 | verifyFormat( |
| 740 | "return aaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n" |
Daniel Jasper | cf225b6 | 2012-12-24 13:43:52 +0000 | [diff] [blame] | 741 | " aaaaaaaaaaa(aaaaaaaaa) || aaaaaaaaaaaaaaaaaaaaaaa ||\n" |
| 742 | " aaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n" |
| 743 | " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); |
Daniel Jasper | a4974cf | 2012-12-24 16:43:00 +0000 | [diff] [blame] | 744 | |
Daniel Jasper | cd16238 | 2013-01-07 13:26:07 +0000 | [diff] [blame] | 745 | verifyFormat("{\n {\n {\n" |
| 746 | " Annotation.SpaceRequiredBefore =\n" |
| 747 | " Line.Tokens[i - 1].Tok.isNot(tok::l_paren) &&\n" |
| 748 | " Line.Tokens[i - 1].Tok.isNot(tok::l_square);\n" |
| 749 | " }\n }\n}"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 750 | } |
| 751 | |
Daniel Jasper | 7d19bc2 | 2013-01-11 14:23:32 +0000 | [diff] [blame] | 752 | TEST_F(FormatTest, DoesNotBreakTrailingAnnotation) { |
| 753 | verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" |
| 754 | " GUARDED_BY(aaaaaaaaaaaaa);"); |
| 755 | } |
| 756 | |
Daniel Jasper | e2c7acf | 2012-12-24 00:13:23 +0000 | [diff] [blame] | 757 | TEST_F(FormatTest, BreaksAccordingToOperatorPrecedence) { |
| 758 | verifyFormat( |
| 759 | "if (aaaaaaaaaaaaaaaaaaaaaaaaa ||\n" |
Manuel Klimek | 36fab8d | 2013-01-10 13:24:24 +0000 | [diff] [blame] | 760 | " bbbbbbbbbbbbbbbbbbbbbbbbb && ccccccccccccccccccccccccc) {}"); |
Daniel Jasper | cd16238 | 2013-01-07 13:26:07 +0000 | [diff] [blame] | 761 | verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa && bbbbbbbbbbbbbbbbbbbbbbbbb ||\n" |
Manuel Klimek | 36fab8d | 2013-01-10 13:24:24 +0000 | [diff] [blame] | 762 | " ccccccccccccccccccccccccc) {}"); |
Daniel Jasper | cd16238 | 2013-01-07 13:26:07 +0000 | [diff] [blame] | 763 | verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb ||\n" |
Manuel Klimek | 36fab8d | 2013-01-10 13:24:24 +0000 | [diff] [blame] | 764 | " ccccccccccccccccccccccccc) {}"); |
Daniel Jasper | e2c7acf | 2012-12-24 00:13:23 +0000 | [diff] [blame] | 765 | verifyFormat( |
| 766 | "if ((aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb) &&\n" |
Manuel Klimek | 36fab8d | 2013-01-10 13:24:24 +0000 | [diff] [blame] | 767 | " ccccccccccccccccccccccccc) {}"); |
Daniel Jasper | e2c7acf | 2012-12-24 00:13:23 +0000 | [diff] [blame] | 768 | } |
| 769 | |
Daniel Jasper | 9cda800 | 2013-01-07 13:08:40 +0000 | [diff] [blame] | 770 | TEST_F(FormatTest, PrefersNotToBreakAfterAssignments) { |
| 771 | verifyFormat( |
| 772 | "unsigned Cost = TTI.getMemoryOpCost(I->getOpcode(), VectorTy,\n" |
| 773 | " SI->getAlignment(),\n" |
| 774 | " SI->getPointerAddressSpaceee());\n"); |
| 775 | verifyFormat( |
| 776 | "CharSourceRange LineRange = CharSourceRange::getTokenRange(\n" |
| 777 | " Line.Tokens.front().Tok.getLocation(),\n" |
| 778 | " Line.Tokens.back().Tok.getLocation());"); |
| 779 | } |
| 780 | |
Daniel Jasper | cf225b6 | 2012-12-24 13:43:52 +0000 | [diff] [blame] | 781 | TEST_F(FormatTest, AlignsAfterAssignments) { |
| 782 | verifyFormat( |
| 783 | "int Result = aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" |
Daniel Jasper | 9cda800 | 2013-01-07 13:08:40 +0000 | [diff] [blame] | 784 | " aaaaaaaaaaaaaaaaaaaaaaaaa;"); |
Daniel Jasper | cf225b6 | 2012-12-24 13:43:52 +0000 | [diff] [blame] | 785 | verifyFormat( |
| 786 | "Result += aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" |
Daniel Jasper | 9cda800 | 2013-01-07 13:08:40 +0000 | [diff] [blame] | 787 | " aaaaaaaaaaaaaaaaaaaaaaaaa;"); |
Daniel Jasper | cf225b6 | 2012-12-24 13:43:52 +0000 | [diff] [blame] | 788 | verifyFormat( |
| 789 | "Result >>= aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" |
Daniel Jasper | 9cda800 | 2013-01-07 13:08:40 +0000 | [diff] [blame] | 790 | " aaaaaaaaaaaaaaaaaaaaaaaaa;"); |
Daniel Jasper | cf225b6 | 2012-12-24 13:43:52 +0000 | [diff] [blame] | 791 | verifyFormat( |
| 792 | "int Result = (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" |
Daniel Jasper | 9cda800 | 2013-01-07 13:08:40 +0000 | [diff] [blame] | 793 | " aaaaaaaaaaaaaaaaaaaaaaaaa);"); |
Daniel Jasper | cf225b6 | 2012-12-24 13:43:52 +0000 | [diff] [blame] | 794 | verifyFormat( |
Daniel Jasper | 9cda800 | 2013-01-07 13:08:40 +0000 | [diff] [blame] | 795 | "double LooooooooooooooooooooooooongResult = aaaaaaaaaaaaaaaaaaaaaaaa +\n" |
| 796 | " aaaaaaaaaaaaaaaaaaaaaaaa +\n" |
| 797 | " aaaaaaaaaaaaaaaaaaaaaaaa;"); |
Daniel Jasper | cf225b6 | 2012-12-24 13:43:52 +0000 | [diff] [blame] | 798 | } |
| 799 | |
| 800 | TEST_F(FormatTest, AlignsAfterReturn) { |
| 801 | verifyFormat( |
| 802 | "return aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" |
| 803 | " aaaaaaaaaaaaaaaaaaaaaaaaa;"); |
| 804 | verifyFormat( |
| 805 | "return (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" |
| 806 | " aaaaaaaaaaaaaaaaaaaaaaaaa);"); |
| 807 | } |
| 808 | |
Daniel Jasper | 9c837d0 | 2013-01-09 07:06:56 +0000 | [diff] [blame] | 809 | TEST_F(FormatTest, BreaksConditionalExpressions) { |
| 810 | verifyFormat( |
| 811 | "aaaa(aaaaaaaaaaaaaaaaaaaa,\n" |
| 812 | " aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" |
| 813 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); |
| 814 | verifyFormat("aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" |
| 815 | " aaaaaaaaaaaaaaaaaaaaaaa : aaaaaaaaaaaaaaaaaaaaa);"); |
| 816 | } |
| 817 | |
Nico Weber | 7d37b8b | 2013-01-12 01:28:06 +0000 | [diff] [blame] | 818 | TEST_F(FormatTest, ConditionalExpressionsInBrackets) { |
| 819 | verifyFormat("arr[foo ? bar : baz];"); |
| 820 | verifyFormat("f()[foo ? bar : baz];"); |
| 821 | verifyFormat("(a + b)[foo ? bar : baz];"); |
| 822 | verifyFormat("arr[foo ? (4 > 5 ? 4 : 5) : 5 < 5 ? 5 : 7];"); |
| 823 | } |
| 824 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 825 | TEST_F(FormatTest, AlignsStringLiterals) { |
| 826 | verifyFormat("loooooooooooooooooooooooooongFunction(\"short literal \"\n" |
| 827 | " \"short literal\");"); |
| 828 | verifyFormat( |
| 829 | "looooooooooooooooooooooooongFunction(\n" |
| 830 | " \"short literal\"\n" |
| 831 | " \"looooooooooooooooooooooooooooooooooooooooooooooooong literal\");"); |
| 832 | } |
| 833 | |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 834 | TEST_F(FormatTest, AlignsPipes) { |
| 835 | verifyFormat( |
| 836 | "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" |
| 837 | " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" |
| 838 | " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); |
| 839 | verifyFormat( |
| 840 | "aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa\n" |
| 841 | " << aaaaaaaaaaaaaaaaaaaa;"); |
| 842 | verifyFormat( |
| 843 | "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" |
| 844 | " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); |
| 845 | verifyFormat( |
| 846 | "llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n" |
| 847 | " \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\"\n" |
| 848 | " << \"ccccccccccccccccccccccccccccccccccccccccccccccccc\";"); |
| 849 | verifyFormat( |
| 850 | "aaaaaaaa << (aaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" |
| 851 | " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" |
| 852 | " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); |
| 853 | } |
| 854 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 855 | TEST_F(FormatTest, UnderstandsEquals) { |
| 856 | verifyFormat( |
| 857 | "aaaaaaaaaaaaaaaaa =\n" |
| 858 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); |
| 859 | verifyFormat( |
| 860 | "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" |
Manuel Klimek | 36fab8d | 2013-01-10 13:24:24 +0000 | [diff] [blame] | 861 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 862 | verifyFormat( |
| 863 | "if (a) {\n" |
Manuel Klimek | 36fab8d | 2013-01-10 13:24:24 +0000 | [diff] [blame] | 864 | " f();\n" |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 865 | "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" |
Manuel Klimek | 36fab8d | 2013-01-10 13:24:24 +0000 | [diff] [blame] | 866 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 867 | |
Daniel Jasper | 9cda800 | 2013-01-07 13:08:40 +0000 | [diff] [blame] | 868 | verifyFormat( |
Daniel Jasper | 3fc0bb7 | 2013-01-09 10:40:23 +0000 | [diff] [blame] | 869 | // FIXME: Does an expression like this ever make sense? If yes, fix. |
Daniel Jasper | 9cda800 | 2013-01-07 13:08:40 +0000 | [diff] [blame] | 870 | "if (int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = 100000000 +\n" |
Manuel Klimek | 36fab8d | 2013-01-10 13:24:24 +0000 | [diff] [blame] | 871 | " 10000000) {}"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 872 | } |
| 873 | |
Daniel Jasper | 9a0b494 | 2012-12-17 14:34:14 +0000 | [diff] [blame] | 874 | TEST_F(FormatTest, WrapsAtFunctionCallsIfNecessary) { |
Daniel Jasper | 1321eb5 | 2012-12-18 21:05:13 +0000 | [diff] [blame] | 875 | verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n" |
| 876 | " .looooooooooooooooooooooooooooooooooooooongFunction();"); |
Daniel Jasper | 9a0b494 | 2012-12-17 14:34:14 +0000 | [diff] [blame] | 877 | |
Daniel Jasper | 1321eb5 | 2012-12-18 21:05:13 +0000 | [diff] [blame] | 878 | verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n" |
| 879 | " ->looooooooooooooooooooooooooooooooooooooongFunction();"); |
Daniel Jasper | 9a0b494 | 2012-12-17 14:34:14 +0000 | [diff] [blame] | 880 | |
| 881 | verifyFormat( |
| 882 | "LooooooooooooooooooooooooooooooooongObject->shortFunction(Parameter1,\n" |
| 883 | " Parameter2);"); |
| 884 | |
| 885 | verifyFormat( |
| 886 | "ShortObject->shortFunction(\n" |
| 887 | " LooooooooooooooooooooooooooooooooooooooooooooooongParameter1,\n" |
| 888 | " LooooooooooooooooooooooooooooooooooooooooooooooongParameter2);"); |
| 889 | |
| 890 | verifyFormat("loooooooooooooongFunction(\n" |
| 891 | " LoooooooooooooongObject->looooooooooooooooongFunction());"); |
| 892 | |
| 893 | verifyFormat( |
| 894 | "function(LoooooooooooooooooooooooooooooooooooongObject\n" |
| 895 | " ->loooooooooooooooooooooooooooooooooooooooongFunction());"); |
| 896 | |
Daniel Jasper | 46a46a2 | 2013-01-07 07:13:20 +0000 | [diff] [blame] | 897 | // Here, it is not necessary to wrap at "." or "->". |
Daniel Jasper | 1321eb5 | 2012-12-18 21:05:13 +0000 | [diff] [blame] | 898 | verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaa) ||\n" |
Manuel Klimek | 36fab8d | 2013-01-10 13:24:24 +0000 | [diff] [blame] | 899 | " aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}"); |
Daniel Jasper | 46a46a2 | 2013-01-07 07:13:20 +0000 | [diff] [blame] | 900 | verifyFormat( |
| 901 | "aaaaaaaaaaa->aaaaaaaaa(\n" |
| 902 | " aaaaaaaaaaaaaaaaaaaaaaaaa,\n" |
| 903 | " aaaaaaaaaaaaaaaaaa->aaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa));\n"); |
Daniel Jasper | 9a0b494 | 2012-12-17 14:34:14 +0000 | [diff] [blame] | 904 | } |
| 905 | |
Daniel Jasper | 9a64fb5 | 2013-01-02 15:08:56 +0000 | [diff] [blame] | 906 | TEST_F(FormatTest, WrapsTemplateDeclarations) { |
| 907 | verifyFormat("template <typename T>\n" |
| 908 | "virtual void loooooooooooongFunction(int Param1, int Param2);"); |
| 909 | verifyFormat( |
| 910 | "template <typename T> void f(int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram1,\n" |
| 911 | " int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram2);"); |
| 912 | verifyFormat( |
| 913 | "template <typename T>\n" |
| 914 | "void looooooooooooooooooooongFunction(int Paaaaaaaaaaaaaaaaaaaaram1,\n" |
| 915 | " int Paaaaaaaaaaaaaaaaaaaaram2);"); |
Daniel Jasper | 5eda31e | 2013-01-02 18:30:06 +0000 | [diff] [blame] | 916 | verifyFormat( |
| 917 | "template <typename T>\n" |
| 918 | "aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa,\n" |
| 919 | " aaaaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaa,\n" |
| 920 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); |
Daniel Jasper | d64f738 | 2013-01-09 09:50:48 +0000 | [diff] [blame] | 921 | verifyFormat("template <typename T>\n" |
| 922 | "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" |
| 923 | " int aaaaaaaaaaaaaaaaa);"); |
Daniel Jasper | 3fc0bb7 | 2013-01-09 10:40:23 +0000 | [diff] [blame] | 924 | verifyFormat( |
| 925 | "template <typename T1, typename T2 = char, typename T3 = char,\n" |
| 926 | " typename T4 = char>\n" |
| 927 | "void f();"); |
Daniel Jasper | 9a64fb5 | 2013-01-02 15:08:56 +0000 | [diff] [blame] | 928 | } |
| 929 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 930 | TEST_F(FormatTest, UnderstandsTemplateParameters) { |
| 931 | verifyFormat("A<int> a;"); |
| 932 | verifyFormat("A<A<A<int> > > a;"); |
| 933 | verifyFormat("A<A<A<int, 2>, 3>, 4> a;"); |
| 934 | verifyFormat("bool x = a < 1 || 2 > a;"); |
| 935 | verifyFormat("bool x = 5 < f<int>();"); |
| 936 | verifyFormat("bool x = f<int>() > 5;"); |
| 937 | verifyFormat("bool x = 5 < a<int>::x;"); |
| 938 | verifyFormat("bool x = a < 4 ? a > 2 : false;"); |
| 939 | verifyFormat("bool x = f() ? a < 2 : a > 2;"); |
| 940 | |
| 941 | verifyGoogleFormat("A<A<int>> a;"); |
| 942 | verifyGoogleFormat("A<A<A<int>>> a;"); |
| 943 | verifyGoogleFormat("A<A<A<A<int>>>> a;"); |
| 944 | |
| 945 | verifyFormat("test >> a >> b;"); |
| 946 | verifyFormat("test << a >> b;"); |
| 947 | |
| 948 | verifyFormat("f<int>();"); |
Manuel Klimek | 36fab8d | 2013-01-10 13:24:24 +0000 | [diff] [blame] | 949 | verifyFormat("template <typename T> void f() {}"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 950 | } |
| 951 | |
Daniel Jasper | 98e6b4a | 2012-12-21 09:41:31 +0000 | [diff] [blame] | 952 | TEST_F(FormatTest, UnderstandsUnaryOperators) { |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 953 | verifyFormat("int a = -2;"); |
Daniel Jasper | 8822d3a | 2012-12-04 13:02:32 +0000 | [diff] [blame] | 954 | verifyFormat("f(-1, -2, -3);"); |
| 955 | verifyFormat("a[-1] = 5;"); |
| 956 | verifyFormat("int a = 5 + -2;"); |
Manuel Klimek | 36fab8d | 2013-01-10 13:24:24 +0000 | [diff] [blame] | 957 | verifyFormat("if (i == -1) {}"); |
| 958 | verifyFormat("if (i != -1) {}"); |
| 959 | verifyFormat("if (i > -1) {}"); |
| 960 | verifyFormat("if (i < -1) {}"); |
Daniel Jasper | d56a737 | 2012-12-06 13:16:39 +0000 | [diff] [blame] | 961 | verifyFormat("++(a->f());"); |
| 962 | verifyFormat("--(a->f());"); |
Manuel Klimek | 36fab8d | 2013-01-10 13:24:24 +0000 | [diff] [blame] | 963 | verifyFormat("if (!(a->f())) {}"); |
Daniel Jasper | 98e6b4a | 2012-12-21 09:41:31 +0000 | [diff] [blame] | 964 | |
| 965 | verifyFormat("a-- > b;"); |
| 966 | verifyFormat("b ? -a : c;"); |
| 967 | verifyFormat("n * sizeof char16;"); |
Daniel Jasper | ba3d307 | 2013-01-02 17:21:36 +0000 | [diff] [blame] | 968 | verifyFormat("n * alignof char16;"); |
Daniel Jasper | 98e6b4a | 2012-12-21 09:41:31 +0000 | [diff] [blame] | 969 | verifyFormat("sizeof(char);"); |
Daniel Jasper | ba3d307 | 2013-01-02 17:21:36 +0000 | [diff] [blame] | 970 | verifyFormat("alignof(char);"); |
Daniel Jasper | 1f0754b | 2013-01-02 15:26:16 +0000 | [diff] [blame] | 971 | |
| 972 | verifyFormat("return -1;"); |
| 973 | verifyFormat("switch (a) {\n" |
| 974 | "case -1:\n" |
| 975 | " break;\n" |
| 976 | "}"); |
Nico Weber | cc191d1 | 2013-01-12 05:41:23 +0000 | [diff] [blame] | 977 | |
| 978 | verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = { -5, +3 };"); |
| 979 | verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = { +5, -3 };"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 980 | } |
| 981 | |
| 982 | TEST_F(FormatTest, UndestandsOverloadedOperators) { |
Daniel Jasper | f6aef6a | 2012-12-24 10:56:04 +0000 | [diff] [blame] | 983 | verifyFormat("bool operator<();"); |
| 984 | verifyFormat("bool operator>();"); |
| 985 | verifyFormat("bool operator=();"); |
| 986 | verifyFormat("bool operator==();"); |
| 987 | verifyFormat("bool operator!=();"); |
| 988 | verifyFormat("int operator+();"); |
| 989 | verifyFormat("int operator++();"); |
| 990 | verifyFormat("bool operator();"); |
| 991 | verifyFormat("bool operator()();"); |
| 992 | verifyFormat("bool operator[]();"); |
| 993 | verifyFormat("operator bool();"); |
| 994 | verifyFormat("operator SomeType<int>();"); |
| 995 | verifyFormat("void *operator new(std::size_t size);"); |
| 996 | verifyFormat("void *operator new[](std::size_t size);"); |
| 997 | verifyFormat("void operator delete(void *ptr);"); |
| 998 | verifyFormat("void operator delete[](void *ptr);"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 999 | } |
| 1000 | |
Daniel Jasper | 088dab5 | 2013-01-11 16:09:04 +0000 | [diff] [blame] | 1001 | TEST_F(FormatTest, UnderstandsNewAndDelete) { |
| 1002 | verifyFormat("A *a = new A;"); |
| 1003 | verifyFormat("A *a = new (placement) A;"); |
| 1004 | verifyFormat("delete a;"); |
| 1005 | verifyFormat("delete (A *)a;"); |
| 1006 | } |
| 1007 | |
Daniel Jasper | 5d33440 | 2013-01-02 08:57:10 +0000 | [diff] [blame] | 1008 | TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) { |
Manuel Klimek | 36fab8d | 2013-01-10 13:24:24 +0000 | [diff] [blame] | 1009 | verifyFormat("int *f(int *a) {}"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1010 | verifyFormat("f(a, *a);"); |
| 1011 | verifyFormat("f(*a);"); |
| 1012 | verifyFormat("int a = b * 10;"); |
| 1013 | verifyFormat("int a = 10 * b;"); |
Daniel Jasper | 112fb27 | 2012-12-05 07:51:39 +0000 | [diff] [blame] | 1014 | verifyFormat("int a = b * c;"); |
Daniel Jasper | 33182dd | 2012-12-05 14:57:28 +0000 | [diff] [blame] | 1015 | verifyFormat("int a += b * c;"); |
| 1016 | verifyFormat("int a -= b * c;"); |
| 1017 | verifyFormat("int a *= b * c;"); |
| 1018 | verifyFormat("int a /= b * c;"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1019 | verifyFormat("int a = *b;"); |
Daniel Jasper | 112fb27 | 2012-12-05 07:51:39 +0000 | [diff] [blame] | 1020 | verifyFormat("int a = *b * c;"); |
| 1021 | verifyFormat("int a = b * *c;"); |
Manuel Klimek | 36fab8d | 2013-01-10 13:24:24 +0000 | [diff] [blame] | 1022 | verifyFormat("int main(int argc, char **argv) {}"); |
Nico Weber | 00d5a04 | 2012-12-23 01:07:46 +0000 | [diff] [blame] | 1023 | verifyFormat("return 10 * b;"); |
| 1024 | verifyFormat("return *b * *c;"); |
| 1025 | verifyFormat("return a & ~b;"); |
Daniel Jasper | 5d33440 | 2013-01-02 08:57:10 +0000 | [diff] [blame] | 1026 | verifyFormat("f(b ? *c : *d);"); |
| 1027 | verifyFormat("int a = b ? *c : *d;"); |
Daniel Jasper | ba3d307 | 2013-01-02 17:21:36 +0000 | [diff] [blame] | 1028 | verifyFormat("*b = a;"); |
| 1029 | verifyFormat("a * ~b;"); |
| 1030 | verifyFormat("a * !b;"); |
| 1031 | verifyFormat("a * +b;"); |
| 1032 | verifyFormat("a * -b;"); |
| 1033 | verifyFormat("a * ++b;"); |
| 1034 | verifyFormat("a * --b;"); |
Nico Weber | 2355ceb | 2013-01-12 05:47:16 +0000 | [diff] [blame] | 1035 | verifyFormat("a[4] * b;"); |
| 1036 | verifyFormat("f() * b;"); |
Nico Weber | 5e9f91c | 2013-01-12 05:50:48 +0000 | [diff] [blame] | 1037 | verifyFormat("a * [self dostuff];"); |
| 1038 | verifyFormat("a * (a + b);"); |
| 1039 | verifyFormat("(a *)(a + b);"); |
Daniel Jasper | 5cf7cf3 | 2013-01-10 11:14:08 +0000 | [diff] [blame] | 1040 | verifyFormat("int *pa = (int *)&a;"); |
Daniel Jasper | c74e279 | 2012-12-07 09:52:15 +0000 | [diff] [blame] | 1041 | |
Daniel Jasper | 9bb0d28 | 2013-01-04 20:46:38 +0000 | [diff] [blame] | 1042 | verifyFormat("InvalidRegions[*R] = 0;"); |
| 1043 | |
Daniel Jasper | 8b39c66 | 2012-12-10 18:59:13 +0000 | [diff] [blame] | 1044 | verifyFormat("A<int *> a;"); |
| 1045 | verifyFormat("A<int **> a;"); |
| 1046 | verifyFormat("A<int *, int *> a;"); |
| 1047 | verifyFormat("A<int **, int **> a;"); |
Daniel Jasper | ef5b9c3 | 2013-01-02 15:46:59 +0000 | [diff] [blame] | 1048 | verifyFormat("Type *A = static_cast<Type *>(P);"); |
Daniel Jasper | 5cf7cf3 | 2013-01-10 11:14:08 +0000 | [diff] [blame] | 1049 | verifyFormat("Type *A = (Type *)P;"); |
| 1050 | verifyFormat("Type *A = (vector<Type *, int *>)P;"); |
Daniel Jasper | 8b39c66 | 2012-12-10 18:59:13 +0000 | [diff] [blame] | 1051 | |
Daniel Jasper | 2db356d | 2013-01-08 20:03:18 +0000 | [diff] [blame] | 1052 | verifyFormat( |
| 1053 | "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" |
| 1054 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaa, *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); |
| 1055 | |
Manuel Klimek | 36fab8d | 2013-01-10 13:24:24 +0000 | [diff] [blame] | 1056 | verifyGoogleFormat("int main(int argc, char** argv) {}"); |
Daniel Jasper | 8b39c66 | 2012-12-10 18:59:13 +0000 | [diff] [blame] | 1057 | verifyGoogleFormat("A<int*> a;"); |
| 1058 | verifyGoogleFormat("A<int**> a;"); |
| 1059 | verifyGoogleFormat("A<int*, int*> a;"); |
| 1060 | verifyGoogleFormat("A<int**, int**> a;"); |
Daniel Jasper | 5d33440 | 2013-01-02 08:57:10 +0000 | [diff] [blame] | 1061 | verifyGoogleFormat("f(b ? *c : *d);"); |
| 1062 | verifyGoogleFormat("int a = b ? *c : *d;"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1063 | } |
| 1064 | |
Daniel Jasper | 46ef852 | 2013-01-10 13:08:12 +0000 | [diff] [blame] | 1065 | TEST_F(FormatTest, FormatsFunctionTypes) { |
| 1066 | // FIXME: Determine the cases that need a space after the return type and fix. |
| 1067 | verifyFormat("A<bool()> a;"); |
| 1068 | verifyFormat("A<SomeType()> a;"); |
| 1069 | verifyFormat("A<void(*)(int, std::string)> a;"); |
| 1070 | |
| 1071 | verifyFormat("int(*func)(void *);"); |
| 1072 | } |
| 1073 | |
Daniel Jasper | 4dc41de | 2013-01-02 08:44:14 +0000 | [diff] [blame] | 1074 | TEST_F(FormatTest, DoesNotBreakBeforePointerOrReference) { |
Daniel Jasper | cd16238 | 2013-01-07 13:26:07 +0000 | [diff] [blame] | 1075 | verifyFormat("int *someFunction(int LoooooooooooooooongParam1,\n" |
Manuel Klimek | 36fab8d | 2013-01-10 13:24:24 +0000 | [diff] [blame] | 1076 | " int LoooooooooooooooongParam2) {}"); |
Daniel Jasper | 4dc41de | 2013-01-02 08:44:14 +0000 | [diff] [blame] | 1077 | verifyFormat( |
| 1078 | "TypeSpecDecl *TypeSpecDecl::Create(ASTContext &C, DeclContext *DC,\n" |
| 1079 | " SourceLocation L, IdentifierIn *II,\n" |
Manuel Klimek | 36fab8d | 2013-01-10 13:24:24 +0000 | [diff] [blame] | 1080 | " Type *T) {}"); |
Daniel Jasper | 4dc41de | 2013-01-02 08:44:14 +0000 | [diff] [blame] | 1081 | } |
| 1082 | |
Daniel Jasper | 3b5943f | 2012-12-06 09:56:08 +0000 | [diff] [blame] | 1083 | TEST_F(FormatTest, LineStartsWithSpecialCharacter) { |
| 1084 | verifyFormat("(a)->b();"); |
| 1085 | verifyFormat("--a;"); |
| 1086 | } |
| 1087 | |
Daniel Jasper | 8822d3a | 2012-12-04 13:02:32 +0000 | [diff] [blame] | 1088 | TEST_F(FormatTest, HandlesIncludeDirectives) { |
| 1089 | EXPECT_EQ("#include <string>\n", format("#include <string>\n")); |
Daniel Jasper | cd1a32b | 2012-12-21 17:58:39 +0000 | [diff] [blame] | 1090 | 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] | 1091 | EXPECT_EQ("#include \"a/b/string\"\n", format("#include \"a/b/string\"\n")); |
| 1092 | EXPECT_EQ("#include \"string.h\"\n", format("#include \"string.h\"\n")); |
| 1093 | EXPECT_EQ("#include \"string.h\"\n", format("#include \"string.h\"\n")); |
Nico Weber | b23ae0c | 2012-12-21 18:21:56 +0000 | [diff] [blame] | 1094 | |
| 1095 | EXPECT_EQ("#import <string>\n", format("#import <string>\n")); |
| 1096 | EXPECT_EQ("#import <a/b/c.h>\n", format("#import <a/b/c.h>\n")); |
| 1097 | EXPECT_EQ("#import \"a/b/string\"\n", format("#import \"a/b/string\"\n")); |
| 1098 | EXPECT_EQ("#import \"string.h\"\n", format("#import \"string.h\"\n")); |
| 1099 | EXPECT_EQ("#import \"string.h\"\n", format("#import \"string.h\"\n")); |
Daniel Jasper | 8822d3a | 2012-12-04 13:02:32 +0000 | [diff] [blame] | 1100 | } |
| 1101 | |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 1102 | //===----------------------------------------------------------------------===// |
| 1103 | // Error recovery tests. |
| 1104 | //===----------------------------------------------------------------------===// |
| 1105 | |
Daniel Jasper | 700e710 | 2013-01-10 09:26:47 +0000 | [diff] [blame] | 1106 | TEST_F(FormatTest, IncorrectCodeTrailingStuff) { |
| 1107 | verifyFormat("void f() { return } 42"); |
| 1108 | } |
| 1109 | |
Daniel Jasper | ceb99ab | 2013-01-09 10:16:05 +0000 | [diff] [blame] | 1110 | TEST_F(FormatTest, IndentationWithinColumnLimitNotPossible) { |
| 1111 | verifyFormat("int aaaaaaaa =\n" |
| 1112 | " // Overly long comment\n" |
| 1113 | " b;", getLLVMStyleWithColumns(20)); |
| 1114 | verifyFormat("function(\n" |
| 1115 | " ShortArgument,\n" |
| 1116 | " LoooooooooooongArgument);\n", getLLVMStyleWithColumns(20)); |
| 1117 | } |
| 1118 | |
Alexander Kornienko | 56e49c5 | 2012-12-10 16:34:48 +0000 | [diff] [blame] | 1119 | TEST_F(FormatTest, IncorrectAccessSpecifier) { |
| 1120 | verifyFormat("public:"); |
| 1121 | verifyFormat("class A {\n" |
| 1122 | "public\n" |
Manuel Klimek | 36fab8d | 2013-01-10 13:24:24 +0000 | [diff] [blame] | 1123 | " void f() {}\n" |
Alexander Kornienko | 56e49c5 | 2012-12-10 16:34:48 +0000 | [diff] [blame] | 1124 | "};"); |
| 1125 | verifyFormat("public\n" |
| 1126 | "int qwerty;"); |
| 1127 | verifyFormat("public\n" |
Manuel Klimek | 36fab8d | 2013-01-10 13:24:24 +0000 | [diff] [blame] | 1128 | "B {}"); |
Alexander Kornienko | 56e49c5 | 2012-12-10 16:34:48 +0000 | [diff] [blame] | 1129 | verifyFormat("public\n" |
Manuel Klimek | 36fab8d | 2013-01-10 13:24:24 +0000 | [diff] [blame] | 1130 | "{}"); |
Alexander Kornienko | 56e49c5 | 2012-12-10 16:34:48 +0000 | [diff] [blame] | 1131 | verifyFormat("public\n" |
Manuel Klimek | 517e894 | 2013-01-11 17:54:10 +0000 | [diff] [blame] | 1132 | "B { int x; }"); |
Alexander Kornienko | 56e49c5 | 2012-12-10 16:34:48 +0000 | [diff] [blame] | 1133 | } |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1134 | |
Alexander Kornienko | 393b008 | 2012-12-04 15:40:36 +0000 | [diff] [blame] | 1135 | TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) { |
| 1136 | verifyFormat("{"); |
| 1137 | } |
| 1138 | |
| 1139 | TEST_F(FormatTest, IncorrectCodeDoNoWhile) { |
Manuel Klimek | 36fab8d | 2013-01-10 13:24:24 +0000 | [diff] [blame] | 1140 | verifyFormat("do {}"); |
| 1141 | verifyFormat("do {}\n" |
Alexander Kornienko | 393b008 | 2012-12-04 15:40:36 +0000 | [diff] [blame] | 1142 | "f();"); |
Manuel Klimek | 36fab8d | 2013-01-10 13:24:24 +0000 | [diff] [blame] | 1143 | verifyFormat("do {}\n" |
Alexander Kornienko | 393b008 | 2012-12-04 15:40:36 +0000 | [diff] [blame] | 1144 | "wheeee(fun);"); |
| 1145 | verifyFormat("do {\n" |
| 1146 | " f();\n" |
Manuel Klimek | de76854 | 2013-01-07 18:10:23 +0000 | [diff] [blame] | 1147 | "}"); |
Alexander Kornienko | 393b008 | 2012-12-04 15:40:36 +0000 | [diff] [blame] | 1148 | } |
| 1149 | |
Manuel Klimek | 6eca03f | 2013-01-11 19:23:05 +0000 | [diff] [blame] | 1150 | TEST_F(FormatTest, IncorrectCodeMissingParens) { |
Manuel Klimek | d465843 | 2013-01-11 18:28:36 +0000 | [diff] [blame] | 1151 | verifyFormat("if {\n foo;\n foo();\n}"); |
Manuel Klimek | 6eca03f | 2013-01-11 19:23:05 +0000 | [diff] [blame] | 1152 | verifyFormat("switch {\n foo;\n foo();\n}"); |
| 1153 | verifyFormat("for {\n foo;\n foo();\n}"); |
| 1154 | verifyFormat("while {\n foo;\n foo();\n}"); |
| 1155 | verifyFormat("do {\n foo;\n foo();\n} while;"); |
Manuel Klimek | d465843 | 2013-01-11 18:28:36 +0000 | [diff] [blame] | 1156 | } |
| 1157 | |
Daniel Jasper | 1f42f11 | 2013-01-04 18:52:56 +0000 | [diff] [blame] | 1158 | TEST_F(FormatTest, DoesNotTouchUnwrappedLinesWithErrors) { |
| 1159 | verifyFormat("namespace {\n" |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 1160 | "class Foo { Foo ( }; } // comment"); |
Daniel Jasper | 1f42f11 | 2013-01-04 18:52:56 +0000 | [diff] [blame] | 1161 | } |
| 1162 | |
Alexander Kornienko | cff563c | 2012-12-04 17:27:50 +0000 | [diff] [blame] | 1163 | TEST_F(FormatTest, IncorrectCodeErrorDetection) { |
Manuel Klimek | 36fab8d | 2013-01-10 13:24:24 +0000 | [diff] [blame] | 1164 | EXPECT_EQ("{\n{}\n", format("{\n{\n}\n")); |
| 1165 | EXPECT_EQ("{\n {}\n", format("{\n {\n}\n")); |
| 1166 | EXPECT_EQ("{\n {}\n", format("{\n {\n }\n")); |
| 1167 | EXPECT_EQ("{\n {}\n }\n}\n", format("{\n {\n }\n }\n}\n")); |
Alexander Kornienko | cff563c | 2012-12-04 17:27:50 +0000 | [diff] [blame] | 1168 | |
| 1169 | FormatStyle Style = getLLVMStyle(); |
| 1170 | Style.ColumnLimit = 10; |
| 1171 | EXPECT_EQ("{\n" |
| 1172 | " {\n" |
| 1173 | " breakme(\n" |
| 1174 | " qwe);\n" |
| 1175 | "}\n", format("{\n" |
| 1176 | " {\n" |
| 1177 | " breakme(qwe);\n" |
| 1178 | "}\n", Style)); |
| 1179 | |
| 1180 | } |
| 1181 | |
Manuel Klimek | 2851c16 | 2013-01-10 14:36:46 +0000 | [diff] [blame] | 1182 | TEST_F(FormatTest, LayoutCallsInsideBraceInitializers) { |
| 1183 | verifyFormat( |
| 1184 | "int x = {\n" |
| 1185 | " avariable,\n" |
Manuel Klimek | c8c8a47 | 2013-01-10 15:58:26 +0000 | [diff] [blame] | 1186 | " b(alongervariable)\n" |
| 1187 | "};", getLLVMStyleWithColumns(25)); |
Manuel Klimek | 2851c16 | 2013-01-10 14:36:46 +0000 | [diff] [blame] | 1188 | } |
| 1189 | |
| 1190 | TEST_F(FormatTest, LayoutTokensFollowingBlockInParentheses) { |
| 1191 | verifyFormat( |
| 1192 | "Aaa({\n" |
| 1193 | " int i;\n" |
| 1194 | "}, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n" |
| 1195 | " ccccccccccccccccc));"); |
| 1196 | } |
| 1197 | |
Manuel Klimek | 517e894 | 2013-01-11 17:54:10 +0000 | [diff] [blame] | 1198 | TEST_F(FormatTest, PullTrivialFunctionDefinitionsIntoSingleLine) { |
| 1199 | verifyFormat("void f() { return 42; }"); |
| 1200 | verifyFormat("void f() {\n" |
| 1201 | " // Comment\n" |
| 1202 | "}"); |
| 1203 | verifyFormat("{\n" |
| 1204 | "#error {\n" |
| 1205 | " int a;\n" |
| 1206 | "}"); |
| 1207 | verifyFormat("{\n" |
| 1208 | " int a;\n" |
| 1209 | "#error {\n" |
| 1210 | "}"); |
| 1211 | } |
| 1212 | |
Manuel Klimek | 606e07e | 2013-01-11 18:13:04 +0000 | [diff] [blame] | 1213 | TEST_F(FormatTest, BracedInitListWithElaboratedTypeSpecifier) { |
| 1214 | verifyFormat("struct foo a = { bar };\nint n;"); |
| 1215 | } |
| 1216 | |
Manuel Klimek | 517e894 | 2013-01-11 17:54:10 +0000 | [diff] [blame] | 1217 | // FIXME: This breaks the order of the unwrapped lines: |
| 1218 | // TEST_F(FormatTest, OrderUnwrappedLines) { |
| 1219 | // verifyFormat("{\n" |
| 1220 | // " bool a; //\n" |
| 1221 | // "#error {\n" |
| 1222 | // " int a;\n" |
| 1223 | // "}"); |
| 1224 | // } |
| 1225 | |
Nico Weber | cf4a79c | 2013-01-08 17:56:31 +0000 | [diff] [blame] | 1226 | //===----------------------------------------------------------------------===// |
| 1227 | // Objective-C tests. |
| 1228 | //===----------------------------------------------------------------------===// |
| 1229 | |
Fariborz Jahanian | 154120c | 2012-12-20 19:54:13 +0000 | [diff] [blame] | 1230 | TEST_F(FormatTest, FormatForObjectiveCMethodDecls) { |
| 1231 | verifyFormat("- (void)sendAction:(SEL)aSelector to:(BOOL)anObject;"); |
| 1232 | EXPECT_EQ("- (NSUInteger)indexOfObject:(id)anObject;", |
| 1233 | format("-(NSUInteger)indexOfObject:(id)anObject;")); |
Daniel Jasper | cd16238 | 2013-01-07 13:26:07 +0000 | [diff] [blame] | 1234 | EXPECT_EQ("- (NSInteger)Mthod1;", format("-(NSInteger)Mthod1;")); |
Fariborz Jahanian | 154120c | 2012-12-20 19:54:13 +0000 | [diff] [blame] | 1235 | EXPECT_EQ("+ (id)Mthod2;", format("+(id)Mthod2;")); |
| 1236 | EXPECT_EQ("- (NSInteger)Method3:(id)anObject;", |
| 1237 | format("-(NSInteger)Method3:(id)anObject;")); |
| 1238 | EXPECT_EQ("- (NSInteger)Method4:(id)anObject;", |
| 1239 | format("-(NSInteger)Method4:(id)anObject;")); |
| 1240 | EXPECT_EQ("- (NSInteger)Method5:(id)anObject:(id)AnotherObject;", |
| 1241 | format("-(NSInteger)Method5:(id)anObject:(id)AnotherObject;")); |
| 1242 | EXPECT_EQ("- (id)Method6:(id)A:(id)B:(id)C:(id)D;", |
| 1243 | format("- (id)Method6:(id)A:(id)B:(id)C:(id)D;")); |
Daniel Jasper | cd16238 | 2013-01-07 13:26:07 +0000 | [diff] [blame] | 1244 | EXPECT_EQ( |
| 1245 | "- (void)sendAction:(SEL)aSelector to:(id)anObject forAllCells:(BOOL)flag;", |
| 1246 | format("- (void)sendAction:(SEL)aSelector to:(id)anObject forAllCells:(BOOL)flag;")); |
Fariborz Jahanian | 9b3f02c | 2012-12-21 22:51:18 +0000 | [diff] [blame] | 1247 | |
| 1248 | // Very long objectiveC method declaration. |
Daniel Jasper | cd16238 | 2013-01-07 13:26:07 +0000 | [diff] [blame] | 1249 | EXPECT_EQ( |
| 1250 | "- (NSUInteger)indexOfObject:(id)anObject inRange:(NSRange)range\n " |
| 1251 | "outRange:(NSRange)out_range outRange1:(NSRange)out_range1\n " |
| 1252 | "outRange2:(NSRange)out_range2 outRange3:(NSRange)out_range3\n " |
| 1253 | "outRange4:(NSRange)out_range4 outRange5:(NSRange)out_range5\n " |
| 1254 | "outRange6:(NSRange)out_range6 outRange7:(NSRange)out_range7\n " |
| 1255 | "outRange8:(NSRange)out_range8 outRange9:(NSRange)out_range9;", |
| 1256 | format( |
| 1257 | "- (NSUInteger)indexOfObject:(id)anObject inRange:(NSRange)range " |
| 1258 | "outRange:(NSRange) out_range outRange1:(NSRange) out_range1 " |
| 1259 | "outRange2:(NSRange) out_range2 outRange3:(NSRange) out_range3 " |
| 1260 | "outRange4:(NSRange) out_range4 outRange5:(NSRange) out_range5 " |
| 1261 | "outRange6:(NSRange) out_range6 outRange7:(NSRange) out_range7 " |
| 1262 | "outRange8:(NSRange) out_range8 outRange9:(NSRange) out_range9;")); |
Nico Weber | e0fd429 | 2013-01-10 20:18:33 +0000 | [diff] [blame] | 1263 | |
| 1264 | verifyFormat("- (int)sum:(vector<int>)numbers;"); |
Nico Weber | cd52bda | 2013-01-10 23:11:41 +0000 | [diff] [blame] | 1265 | verifyGoogleFormat("-(void) setDelegate:(id<Protocol>)delegate;"); |
Nico Weber | e0fd429 | 2013-01-10 20:18:33 +0000 | [diff] [blame] | 1266 | // FIXME: In LLVM style, there should be a space in front of a '<' for ObjC |
| 1267 | // protocol lists (but not for template classes): |
| 1268 | //verifyFormat("- (void)setDelegate:(id <Protocol>)delegate;"); |
Nico Weber | cd52bda | 2013-01-10 23:11:41 +0000 | [diff] [blame] | 1269 | |
| 1270 | verifyFormat("- (int(*)())foo:(int(*)())f;"); |
| 1271 | verifyGoogleFormat("-(int(*)()) foo:(int(*)())foo;"); |
| 1272 | |
| 1273 | // If there's no return type (very rare in practice!), LLVM and Google style |
| 1274 | // agree. |
| 1275 | verifyFormat("- foo:(int)f;"); |
| 1276 | verifyGoogleFormat("- foo:(int)foo;"); |
Fariborz Jahanian | 154120c | 2012-12-20 19:54:13 +0000 | [diff] [blame] | 1277 | } |
| 1278 | |
Daniel Jasper | 886568d | 2013-01-09 08:36:49 +0000 | [diff] [blame] | 1279 | TEST_F(FormatTest, FormatObjCBlocks) { |
Daniel Jasper | 46ef852 | 2013-01-10 13:08:12 +0000 | [diff] [blame] | 1280 | verifyFormat("int (^Block)(int, int);"); |
| 1281 | verifyFormat("int (^Block1)(int, int) = ^(int i, int j)"); |
Daniel Jasper | 886568d | 2013-01-09 08:36:49 +0000 | [diff] [blame] | 1282 | } |
| 1283 | |
Nico Weber | 27d1367 | 2013-01-09 20:25:35 +0000 | [diff] [blame] | 1284 | TEST_F(FormatTest, FormatObjCInterface) { |
| 1285 | // FIXME: Handle comments like in "@interface /* wait for it */ Foo", PR14875 |
Nico Weber | 5f500df | 2013-01-10 20:12:55 +0000 | [diff] [blame] | 1286 | verifyFormat("@interface Foo : NSObject <NSSomeDelegate> {\n" |
Nico Weber | 27d1367 | 2013-01-09 20:25:35 +0000 | [diff] [blame] | 1287 | "@public\n" |
| 1288 | " int field1;\n" |
| 1289 | "@protected\n" |
| 1290 | " int field2;\n" |
| 1291 | "@private\n" |
| 1292 | " int field3;\n" |
| 1293 | "@package\n" |
| 1294 | " int field4;\n" |
| 1295 | "}\n" |
| 1296 | "+ (id)init;\n" |
| 1297 | "@end"); |
| 1298 | |
Nico Weber | 27d1367 | 2013-01-09 20:25:35 +0000 | [diff] [blame] | 1299 | verifyGoogleFormat("@interface Foo : NSObject<NSSomeDelegate> {\n" |
| 1300 | " @public\n" |
| 1301 | " int field1;\n" |
| 1302 | " @protected\n" |
| 1303 | " int field2;\n" |
| 1304 | " @private\n" |
| 1305 | " int field3;\n" |
| 1306 | " @package\n" |
| 1307 | " int field4;\n" |
| 1308 | "}\n" |
Nico Weber | cd52bda | 2013-01-10 23:11:41 +0000 | [diff] [blame] | 1309 | "+(id) init;\n" |
Nico Weber | 27d1367 | 2013-01-09 20:25:35 +0000 | [diff] [blame] | 1310 | "@end"); |
| 1311 | |
| 1312 | verifyFormat("@interface Foo\n" |
| 1313 | "+ (id)init;\n" |
| 1314 | "// Look, a comment!\n" |
| 1315 | "- (int)answerWith:(int)i;\n" |
| 1316 | "@end"); |
| 1317 | |
| 1318 | verifyFormat("@interface Foo\n" |
Nico Weber | 049c447 | 2013-01-09 21:42:32 +0000 | [diff] [blame] | 1319 | "@end\n" |
| 1320 | "@interface Bar\n" |
Nico Weber | 27d1367 | 2013-01-09 20:25:35 +0000 | [diff] [blame] | 1321 | "@end"); |
| 1322 | |
| 1323 | verifyFormat("@interface Foo : Bar\n" |
| 1324 | "+ (id)init;\n" |
| 1325 | "@end"); |
| 1326 | |
Nico Weber | 5f500df | 2013-01-10 20:12:55 +0000 | [diff] [blame] | 1327 | verifyFormat("@interface Foo : Bar <Baz, Quux>\n" |
Nico Weber | 27d1367 | 2013-01-09 20:25:35 +0000 | [diff] [blame] | 1328 | "+ (id)init;\n" |
| 1329 | "@end"); |
| 1330 | |
Nico Weber | 5f500df | 2013-01-10 20:12:55 +0000 | [diff] [blame] | 1331 | verifyGoogleFormat("@interface Foo : Bar<Baz, Quux>\n" |
Nico Weber | cd52bda | 2013-01-10 23:11:41 +0000 | [diff] [blame] | 1332 | "+(id) init;\n" |
Nico Weber | 5f500df | 2013-01-10 20:12:55 +0000 | [diff] [blame] | 1333 | "@end"); |
| 1334 | |
Nico Weber | ed91bba | 2013-01-10 19:19:14 +0000 | [diff] [blame] | 1335 | verifyFormat("@interface Foo (HackStuff)\n" |
Nico Weber | 27d1367 | 2013-01-09 20:25:35 +0000 | [diff] [blame] | 1336 | "+ (id)init;\n" |
| 1337 | "@end"); |
| 1338 | |
Nico Weber | ed91bba | 2013-01-10 19:19:14 +0000 | [diff] [blame] | 1339 | verifyFormat("@interface Foo ()\n" |
Nico Weber | 27d1367 | 2013-01-09 20:25:35 +0000 | [diff] [blame] | 1340 | "+ (id)init;\n" |
| 1341 | "@end"); |
| 1342 | |
Nico Weber | 5f500df | 2013-01-10 20:12:55 +0000 | [diff] [blame] | 1343 | verifyFormat("@interface Foo (HackStuff) <MyProtocol>\n" |
Nico Weber | 27d1367 | 2013-01-09 20:25:35 +0000 | [diff] [blame] | 1344 | "+ (id)init;\n" |
| 1345 | "@end"); |
| 1346 | |
Nico Weber | 5f500df | 2013-01-10 20:12:55 +0000 | [diff] [blame] | 1347 | verifyGoogleFormat("@interface Foo (HackStuff)<MyProtocol>\n" |
Nico Weber | cd52bda | 2013-01-10 23:11:41 +0000 | [diff] [blame] | 1348 | "+(id) init;\n" |
Nico Weber | 5f500df | 2013-01-10 20:12:55 +0000 | [diff] [blame] | 1349 | "@end"); |
| 1350 | |
Manuel Klimek | d5688cf | 2013-01-11 19:17:44 +0000 | [diff] [blame] | 1351 | verifyFormat("@interface Foo {\n" |
| 1352 | " int _i;\n" |
| 1353 | "}\n" |
Nico Weber | 27d1367 | 2013-01-09 20:25:35 +0000 | [diff] [blame] | 1354 | "+ (id)init;\n" |
| 1355 | "@end"); |
| 1356 | |
Manuel Klimek | d5688cf | 2013-01-11 19:17:44 +0000 | [diff] [blame] | 1357 | verifyFormat("@interface Foo : Bar {\n" |
| 1358 | " int _i;\n" |
| 1359 | "}\n" |
Nico Weber | 27d1367 | 2013-01-09 20:25:35 +0000 | [diff] [blame] | 1360 | "+ (id)init;\n" |
| 1361 | "@end"); |
| 1362 | |
Manuel Klimek | d5688cf | 2013-01-11 19:17:44 +0000 | [diff] [blame] | 1363 | verifyFormat("@interface Foo : Bar <Baz, Quux> {\n" |
| 1364 | " int _i;\n" |
| 1365 | "}\n" |
Nico Weber | 27d1367 | 2013-01-09 20:25:35 +0000 | [diff] [blame] | 1366 | "+ (id)init;\n" |
| 1367 | "@end"); |
| 1368 | |
Manuel Klimek | d5688cf | 2013-01-11 19:17:44 +0000 | [diff] [blame] | 1369 | verifyFormat("@interface Foo (HackStuff) {\n" |
| 1370 | " int _i;\n" |
| 1371 | "}\n" |
Nico Weber | 27d1367 | 2013-01-09 20:25:35 +0000 | [diff] [blame] | 1372 | "+ (id)init;\n" |
| 1373 | "@end"); |
| 1374 | |
Manuel Klimek | d5688cf | 2013-01-11 19:17:44 +0000 | [diff] [blame] | 1375 | verifyFormat("@interface Foo () {\n" |
| 1376 | " int _i;\n" |
| 1377 | "}\n" |
Nico Weber | 27d1367 | 2013-01-09 20:25:35 +0000 | [diff] [blame] | 1378 | "+ (id)init;\n" |
| 1379 | "@end"); |
| 1380 | |
Manuel Klimek | d5688cf | 2013-01-11 19:17:44 +0000 | [diff] [blame] | 1381 | verifyFormat("@interface Foo (HackStuff) <MyProtocol> {\n" |
| 1382 | " int _i;\n" |
| 1383 | "}\n" |
Nico Weber | 27d1367 | 2013-01-09 20:25:35 +0000 | [diff] [blame] | 1384 | "+ (id)init;\n" |
| 1385 | "@end"); |
| 1386 | } |
| 1387 | |
Nico Weber | 50767d8 | 2013-01-09 23:25:37 +0000 | [diff] [blame] | 1388 | TEST_F(FormatTest, FormatObjCImplementation) { |
| 1389 | verifyFormat("@implementation Foo : NSObject {\n" |
| 1390 | "@public\n" |
| 1391 | " int field1;\n" |
| 1392 | "@protected\n" |
| 1393 | " int field2;\n" |
| 1394 | "@private\n" |
| 1395 | " int field3;\n" |
| 1396 | "@package\n" |
| 1397 | " int field4;\n" |
| 1398 | "}\n" |
Manuel Klimek | 36fab8d | 2013-01-10 13:24:24 +0000 | [diff] [blame] | 1399 | "+ (id)init {}\n" |
Nico Weber | 50767d8 | 2013-01-09 23:25:37 +0000 | [diff] [blame] | 1400 | "@end"); |
| 1401 | |
| 1402 | verifyGoogleFormat("@implementation Foo : NSObject {\n" |
| 1403 | " @public\n" |
| 1404 | " int field1;\n" |
| 1405 | " @protected\n" |
| 1406 | " int field2;\n" |
| 1407 | " @private\n" |
| 1408 | " int field3;\n" |
| 1409 | " @package\n" |
| 1410 | " int field4;\n" |
| 1411 | "}\n" |
Nico Weber | cd52bda | 2013-01-10 23:11:41 +0000 | [diff] [blame] | 1412 | "+(id) init {}\n" |
Nico Weber | 50767d8 | 2013-01-09 23:25:37 +0000 | [diff] [blame] | 1413 | "@end"); |
| 1414 | |
| 1415 | verifyFormat("@implementation Foo\n" |
| 1416 | "+ (id)init {\n" |
| 1417 | " if (true)\n" |
| 1418 | " return nil;\n" |
| 1419 | "}\n" |
| 1420 | "// Look, a comment!\n" |
Manuel Klimek | d5688cf | 2013-01-11 19:17:44 +0000 | [diff] [blame] | 1421 | "- (int)answerWith:(int)i {\n" |
| 1422 | " return i;\n" |
| 1423 | "}\n" |
Nico Weber | 67015ed | 2013-01-11 21:14:08 +0000 | [diff] [blame] | 1424 | "+ (int)answerWith:(int)i {\n" |
| 1425 | " return i;\n" |
| 1426 | "}\n" |
Nico Weber | 50767d8 | 2013-01-09 23:25:37 +0000 | [diff] [blame] | 1427 | "@end"); |
| 1428 | |
| 1429 | verifyFormat("@implementation Foo\n" |
| 1430 | "@end\n" |
| 1431 | "@implementation Bar\n" |
| 1432 | "@end"); |
| 1433 | |
| 1434 | verifyFormat("@implementation Foo : Bar\n" |
Manuel Klimek | 36fab8d | 2013-01-10 13:24:24 +0000 | [diff] [blame] | 1435 | "+ (id)init {}\n" |
Nico Weber | 67015ed | 2013-01-11 21:14:08 +0000 | [diff] [blame] | 1436 | "- (void)foo {}\n" |
Nico Weber | 50767d8 | 2013-01-09 23:25:37 +0000 | [diff] [blame] | 1437 | "@end"); |
| 1438 | |
Manuel Klimek | d5688cf | 2013-01-11 19:17:44 +0000 | [diff] [blame] | 1439 | verifyFormat("@implementation Foo {\n" |
| 1440 | " int _i;\n" |
| 1441 | "}\n" |
Manuel Klimek | 36fab8d | 2013-01-10 13:24:24 +0000 | [diff] [blame] | 1442 | "+ (id)init {}\n" |
Nico Weber | 50767d8 | 2013-01-09 23:25:37 +0000 | [diff] [blame] | 1443 | "@end"); |
| 1444 | |
Manuel Klimek | d5688cf | 2013-01-11 19:17:44 +0000 | [diff] [blame] | 1445 | verifyFormat("@implementation Foo : Bar {\n" |
| 1446 | " int _i;\n" |
| 1447 | "}\n" |
Manuel Klimek | 36fab8d | 2013-01-10 13:24:24 +0000 | [diff] [blame] | 1448 | "+ (id)init {}\n" |
Nico Weber | 50767d8 | 2013-01-09 23:25:37 +0000 | [diff] [blame] | 1449 | "@end"); |
| 1450 | |
Nico Weber | ed91bba | 2013-01-10 19:19:14 +0000 | [diff] [blame] | 1451 | verifyFormat("@implementation Foo (HackStuff)\n" |
Manuel Klimek | 36fab8d | 2013-01-10 13:24:24 +0000 | [diff] [blame] | 1452 | "+ (id)init {}\n" |
Nico Weber | 50767d8 | 2013-01-09 23:25:37 +0000 | [diff] [blame] | 1453 | "@end"); |
| 1454 | } |
| 1455 | |
Nico Weber | 1abe6ea | 2013-01-09 21:15:03 +0000 | [diff] [blame] | 1456 | TEST_F(FormatTest, FormatObjCProtocol) { |
| 1457 | verifyFormat("@protocol Foo\n" |
| 1458 | "@property(weak) id delegate;\n" |
| 1459 | "- (NSUInteger)numberOfThings;\n" |
| 1460 | "@end"); |
| 1461 | |
Nico Weber | 5f500df | 2013-01-10 20:12:55 +0000 | [diff] [blame] | 1462 | verifyFormat("@protocol MyProtocol <NSObject>\n" |
Nico Weber | 1abe6ea | 2013-01-09 21:15:03 +0000 | [diff] [blame] | 1463 | "- (NSUInteger)numberOfThings;\n" |
| 1464 | "@end"); |
| 1465 | |
Nico Weber | 5f500df | 2013-01-10 20:12:55 +0000 | [diff] [blame] | 1466 | verifyGoogleFormat("@protocol MyProtocol<NSObject>\n" |
Nico Weber | cd52bda | 2013-01-10 23:11:41 +0000 | [diff] [blame] | 1467 | "-(NSUInteger) numberOfThings;\n" |
Nico Weber | 5f500df | 2013-01-10 20:12:55 +0000 | [diff] [blame] | 1468 | "@end"); |
| 1469 | |
Nico Weber | 1abe6ea | 2013-01-09 21:15:03 +0000 | [diff] [blame] | 1470 | verifyFormat("@protocol Foo;\n" |
| 1471 | "@protocol Bar;\n"); |
Nico Weber | 049c447 | 2013-01-09 21:42:32 +0000 | [diff] [blame] | 1472 | |
| 1473 | verifyFormat("@protocol Foo\n" |
| 1474 | "@end\n" |
| 1475 | "@protocol Bar\n" |
| 1476 | "@end"); |
Nico Weber | b530fa3 | 2013-01-10 00:25:19 +0000 | [diff] [blame] | 1477 | |
| 1478 | verifyFormat("@protocol myProtocol\n" |
| 1479 | "- (void)mandatoryWithInt:(int)i;\n" |
| 1480 | "@optional\n" |
| 1481 | "- (void)optional;\n" |
| 1482 | "@required\n" |
| 1483 | "- (void)required;\n" |
Nico Weber | 880e538 | 2013-01-10 00:42:07 +0000 | [diff] [blame] | 1484 | "@optional\n" |
| 1485 | "@property(assign) int madProp;\n" |
Nico Weber | b530fa3 | 2013-01-10 00:25:19 +0000 | [diff] [blame] | 1486 | "@end\n"); |
Nico Weber | 1abe6ea | 2013-01-09 21:15:03 +0000 | [diff] [blame] | 1487 | } |
| 1488 | |
Nico Weber | bcfdd26 | 2013-01-12 06:18:40 +0000 | [diff] [blame] | 1489 | TEST_F(FormatTest, FormatObjCMethodExpr) { |
| 1490 | verifyFormat("[foo bar:baz];"); |
| 1491 | verifyFormat("return [foo bar:baz];"); |
| 1492 | verifyFormat("f([foo bar:baz]);"); |
| 1493 | verifyFormat("f(2, [foo bar:baz]);"); |
| 1494 | verifyFormat("f(2, a ? b : c);"); |
| 1495 | verifyFormat("[[self initWithInt:4] bar:[baz quux:arrrr]];"); |
| 1496 | |
| 1497 | verifyFormat("[foo bar:baz], [foo bar:baz];"); |
| 1498 | verifyFormat("[foo bar:baz] = [foo bar:baz];"); |
| 1499 | verifyFormat("[foo bar:baz] *= [foo bar:baz];"); |
| 1500 | verifyFormat("[foo bar:baz] /= [foo bar:baz];"); |
| 1501 | verifyFormat("[foo bar:baz] %= [foo bar:baz];"); |
| 1502 | verifyFormat("[foo bar:baz] += [foo bar:baz];"); |
| 1503 | verifyFormat("[foo bar:baz] -= [foo bar:baz];"); |
| 1504 | verifyFormat("[foo bar:baz] <<= [foo bar:baz];"); |
| 1505 | verifyFormat("[foo bar:baz] >>= [foo bar:baz];"); |
| 1506 | verifyFormat("[foo bar:baz] &= [foo bar:baz];"); |
| 1507 | verifyFormat("[foo bar:baz] ^= [foo bar:baz];"); |
| 1508 | verifyFormat("[foo bar:baz] |= [foo bar:baz];"); |
| 1509 | verifyFormat("[foo bar:baz] ? [foo bar:baz] : [foo bar:baz];"); |
| 1510 | verifyFormat("[foo bar:baz] || [foo bar:baz];"); |
| 1511 | verifyFormat("[foo bar:baz] && [foo bar:baz];"); |
| 1512 | verifyFormat("[foo bar:baz] | [foo bar:baz];"); |
| 1513 | verifyFormat("[foo bar:baz] ^ [foo bar:baz];"); |
| 1514 | verifyFormat("[foo bar:baz] & [foo bar:baz];"); |
| 1515 | verifyFormat("[foo bar:baz] == [foo bar:baz];"); |
| 1516 | verifyFormat("[foo bar:baz] != [foo bar:baz];"); |
| 1517 | verifyFormat("[foo bar:baz] >= [foo bar:baz];"); |
| 1518 | verifyFormat("[foo bar:baz] <= [foo bar:baz];"); |
| 1519 | verifyFormat("[foo bar:baz] > [foo bar:baz];"); |
| 1520 | verifyFormat("[foo bar:baz] < [foo bar:baz];"); |
| 1521 | verifyFormat("[foo bar:baz] >> [foo bar:baz];"); |
| 1522 | verifyFormat("[foo bar:baz] << [foo bar:baz];"); |
| 1523 | verifyFormat("[foo bar:baz] - [foo bar:baz];"); |
| 1524 | verifyFormat("[foo bar:baz] + [foo bar:baz];"); |
| 1525 | verifyFormat("[foo bar:baz] * [foo bar:baz];"); |
| 1526 | verifyFormat("[foo bar:baz] / [foo bar:baz];"); |
| 1527 | verifyFormat("[foo bar:baz] % [foo bar:baz];"); |
| 1528 | // Whew! |
| 1529 | |
| 1530 | verifyFormat("[self stuffWithInt:(4 + 2) float:4.5];"); |
| 1531 | verifyFormat("[self stuffWithInt:a ? b : c float:4.5];"); |
| 1532 | verifyFormat("[self stuffWithInt:a ? [self foo:bar] : c];"); |
| 1533 | verifyFormat("[self stuffWithInt:a ? (e ? f : g) : c];"); |
| 1534 | verifyFormat("[cond ? obj1 : obj2 methodWithParam:param]"); |
| 1535 | |
Nico Weber | bcfdd26 | 2013-01-12 06:18:40 +0000 | [diff] [blame] | 1536 | verifyFormat("arr[[self indexForFoo:a]];"); |
| 1537 | verifyFormat("throw [self errorFor:a];"); |
| 1538 | verifyFormat("@throw [self errorFor:a];"); |
| 1539 | |
| 1540 | // The formatting of this isn't ideal yet. It tests that the formatter doesn't |
| 1541 | // break after "backing" but before ":", which would be at 80 columns. |
| 1542 | verifyFormat( |
| 1543 | "void f() {\n" |
| 1544 | " if ((self = [super initWithContentRect:contentRect styleMask:\n" |
| 1545 | " styleMask backing:NSBackingStoreBuffered defer:YES]))"); |
| 1546 | |
| 1547 | } |
| 1548 | |
Nico Weber | 581f557 | 2013-01-07 15:56:25 +0000 | [diff] [blame] | 1549 | TEST_F(FormatTest, ObjCAt) { |
Nico Weber | 4a293a2 | 2013-01-07 16:07:07 +0000 | [diff] [blame] | 1550 | verifyFormat("@autoreleasepool"); |
Nico Weber | d0af4b4 | 2013-01-07 16:14:28 +0000 | [diff] [blame] | 1551 | verifyFormat("@catch"); |
| 1552 | verifyFormat("@class"); |
Nico Weber | 4a293a2 | 2013-01-07 16:07:07 +0000 | [diff] [blame] | 1553 | verifyFormat("@compatibility_alias"); |
| 1554 | verifyFormat("@defs"); |
Nico Weber | efcfe73 | 2013-01-07 15:17:23 +0000 | [diff] [blame] | 1555 | verifyFormat("@dynamic"); |
Nico Weber | 4a293a2 | 2013-01-07 16:07:07 +0000 | [diff] [blame] | 1556 | verifyFormat("@encode"); |
| 1557 | verifyFormat("@end"); |
| 1558 | verifyFormat("@finally"); |
| 1559 | verifyFormat("@implementation"); |
| 1560 | verifyFormat("@import"); |
| 1561 | verifyFormat("@interface"); |
| 1562 | verifyFormat("@optional"); |
| 1563 | verifyFormat("@package"); |
Nico Weber | d0af4b4 | 2013-01-07 16:14:28 +0000 | [diff] [blame] | 1564 | verifyFormat("@private"); |
Nico Weber | 4a293a2 | 2013-01-07 16:07:07 +0000 | [diff] [blame] | 1565 | verifyFormat("@property"); |
Nico Weber | d0af4b4 | 2013-01-07 16:14:28 +0000 | [diff] [blame] | 1566 | verifyFormat("@protected"); |
Nico Weber | 4a293a2 | 2013-01-07 16:07:07 +0000 | [diff] [blame] | 1567 | verifyFormat("@protocol"); |
Nico Weber | d0af4b4 | 2013-01-07 16:14:28 +0000 | [diff] [blame] | 1568 | verifyFormat("@public"); |
Nico Weber | 4a293a2 | 2013-01-07 16:07:07 +0000 | [diff] [blame] | 1569 | verifyFormat("@required"); |
| 1570 | verifyFormat("@selector"); |
| 1571 | verifyFormat("@synchronized"); |
| 1572 | verifyFormat("@synthesize"); |
Nico Weber | d0af4b4 | 2013-01-07 16:14:28 +0000 | [diff] [blame] | 1573 | verifyFormat("@throw"); |
| 1574 | verifyFormat("@try"); |
Nico Weber | 4a293a2 | 2013-01-07 16:07:07 +0000 | [diff] [blame] | 1575 | |
Nico Weber | cb4d690 | 2013-01-08 19:40:21 +0000 | [diff] [blame] | 1576 | verifyFormat("@\"String\""); |
| 1577 | verifyFormat("@1"); |
Nico Weber | 81ed2f1 | 2013-01-10 19:36:35 +0000 | [diff] [blame] | 1578 | verifyFormat("@+4.8"); |
| 1579 | verifyFormat("@-4"); |
Nico Weber | cb4d690 | 2013-01-08 19:40:21 +0000 | [diff] [blame] | 1580 | verifyFormat("@1LL"); |
| 1581 | verifyFormat("@.5"); |
| 1582 | verifyFormat("@'c'"); |
| 1583 | verifyFormat("@true"); |
| 1584 | verifyFormat("NSNumber *smallestInt = @(-INT_MAX - 1);"); |
| 1585 | verifyFormat("@["); |
| 1586 | verifyFormat("@{"); |
| 1587 | |
Nico Weber | 581f557 | 2013-01-07 15:56:25 +0000 | [diff] [blame] | 1588 | EXPECT_EQ("@interface", format("@ interface")); |
| 1589 | |
| 1590 | // The precise formatting of this doesn't matter, nobody writes code like |
| 1591 | // this. |
| 1592 | verifyFormat("@ /*foo*/ interface"); |
Nico Weber | efcfe73 | 2013-01-07 15:17:23 +0000 | [diff] [blame] | 1593 | } |
| 1594 | |
Nico Weber | c31689a | 2013-01-08 19:15:23 +0000 | [diff] [blame] | 1595 | TEST_F(FormatTest, ObjCSnippets) { |
| 1596 | // FIXME: Make the uncommented lines below pass. |
Manuel Klimek | d5688cf | 2013-01-11 19:17:44 +0000 | [diff] [blame] | 1597 | verifyFormat("@autoreleasepool {\n" |
| 1598 | " foo();\n" |
| 1599 | "}"); |
Nico Weber | cf4a79c | 2013-01-08 17:56:31 +0000 | [diff] [blame] | 1600 | verifyFormat("@class Foo, Bar;"); |
Nico Weber | c31689a | 2013-01-08 19:15:23 +0000 | [diff] [blame] | 1601 | verifyFormat("@compatibility_alias AliasName ExistingClass;"); |
Nico Weber | cf4a79c | 2013-01-08 17:56:31 +0000 | [diff] [blame] | 1602 | verifyFormat("@dynamic textColor;"); |
Nico Weber | c31689a | 2013-01-08 19:15:23 +0000 | [diff] [blame] | 1603 | //verifyFormat("char *buf1 = @encode(int **);"); |
Nico Weber | 3a2673e | 2013-01-08 20:16:23 +0000 | [diff] [blame] | 1604 | verifyFormat("Protocol *proto = @protocol(p1);"); |
Nico Weber | c31689a | 2013-01-08 19:15:23 +0000 | [diff] [blame] | 1605 | //verifyFormat("SEL s = @selector(foo:);"); |
Manuel Klimek | d5688cf | 2013-01-11 19:17:44 +0000 | [diff] [blame] | 1606 | verifyFormat("@synchronized(self) {\n" |
| 1607 | " f();\n" |
| 1608 | "}"); |
Nico Weber | cf4a79c | 2013-01-08 17:56:31 +0000 | [diff] [blame] | 1609 | |
Nico Weber | 7084823 | 2013-01-10 21:30:42 +0000 | [diff] [blame] | 1610 | verifyFormat("@synthesize dropArrowPosition = dropArrowPosition_;"); |
| 1611 | verifyGoogleFormat("@synthesize dropArrowPosition = dropArrowPosition_;"); |
| 1612 | |
Nico Weber | cf4a79c | 2013-01-08 17:56:31 +0000 | [diff] [blame] | 1613 | verifyFormat("@property(assign, nonatomic) CGFloat hoverAlpha;"); |
Nico Weber | 7084823 | 2013-01-10 21:30:42 +0000 | [diff] [blame] | 1614 | verifyFormat("@property(assign, getter=isEditable) BOOL editable;"); |
| 1615 | verifyGoogleFormat("@property(assign, getter=isEditable) BOOL editable;"); |
Nico Weber | cf4a79c | 2013-01-08 17:56:31 +0000 | [diff] [blame] | 1616 | } |
| 1617 | |
Daniel Jasper | cd16238 | 2013-01-07 13:26:07 +0000 | [diff] [blame] | 1618 | } // end namespace tooling |
| 1619 | } // end namespace clang |