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