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