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