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