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