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