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 | 8ed9f2b | 2013-04-03 13:36:17 +0000 | [diff] [blame] | 314 | verifyFormat("for (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaa =\n" |
| 315 | " 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" |
Daniel Jasper | f9955d3 | 2013-03-20 12:37:50 +0000 | [diff] [blame] | 326 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" |
| 327 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" |
Daniel Jasper | 7522318 | 2013-01-28 17:30:17 +0000 | [diff] [blame] | 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 | 8ed9f2b | 2013-04-03 13:36:17 +0000 | [diff] [blame] | 332 | verifyFormat("for (some_namespace::SomeIterator iter( // force break\n" |
| 333 | " aaaaaaaaaa);\n" |
| 334 | " iter; ++iter) {\n" |
| 335 | "}"); |
Daniel Jasper | 8159d2f | 2013-02-04 07:30:30 +0000 | [diff] [blame] | 336 | |
Daniel Jasper | faab0d3 | 2013-02-27 09:47:53 +0000 | [diff] [blame] | 337 | FormatStyle NoBinPacking = getLLVMStyle(); |
| 338 | NoBinPacking.BinPackParameters = false; |
| 339 | verifyFormat("for (int aaaaaaaaaaa = 1;\n" |
| 340 | " aaaaaaaaaaa <= aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa,\n" |
| 341 | " aaaaaaaaaaaaaaaa,\n" |
| 342 | " aaaaaaaaaaaaaaaa,\n" |
| 343 | " aaaaaaaaaaaaaaaa);\n" |
| 344 | " aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n" |
| 345 | "}", |
| 346 | NoBinPacking); |
| 347 | verifyFormat( |
Daniel Jasper | 237d4c1 | 2013-02-23 21:01:55 +0000 | [diff] [blame] | 348 | "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n" |
| 349 | " E = UnwrappedLines.end();\n" |
| 350 | " I != E;\n" |
Daniel Jasper | faab0d3 | 2013-02-27 09:47:53 +0000 | [diff] [blame] | 351 | " ++I) {\n}", |
| 352 | NoBinPacking); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 353 | } |
| 354 | |
Daniel Jasper | 3b9a8fc | 2013-01-28 13:21:16 +0000 | [diff] [blame] | 355 | TEST_F(FormatTest, RangeBasedForLoops) { |
| 356 | verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" |
| 357 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); |
| 358 | verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaa :\n" |
| 359 | " aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa, aaaaaaaaaaaaa)) {\n}"); |
Daniel Jasper | 84a1a63 | 2013-02-26 13:18:08 +0000 | [diff] [blame] | 360 | verifyFormat("for (const aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaa :\n" |
| 361 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); |
Daniel Jasper | 3b9a8fc | 2013-01-28 13:21:16 +0000 | [diff] [blame] | 362 | } |
| 363 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 364 | TEST_F(FormatTest, FormatsWhileLoop) { |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 365 | verifyFormat("while (true) {\n}"); |
Alexander Kornienko | 2e97cfc | 2012-12-05 15:06:06 +0000 | [diff] [blame] | 366 | verifyFormat("while (true)\n" |
| 367 | " f();"); |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 368 | verifyFormat("while () {\n}"); |
Alexander Kornienko | 2e97cfc | 2012-12-05 15:06:06 +0000 | [diff] [blame] | 369 | verifyFormat("while () {\n" |
| 370 | " f();\n" |
| 371 | "}"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 372 | } |
| 373 | |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 374 | TEST_F(FormatTest, FormatsDoWhile) { |
| 375 | verifyFormat("do {\n" |
| 376 | " do_something();\n" |
| 377 | "} while (something());"); |
| 378 | verifyFormat("do\n" |
| 379 | " do_something();\n" |
| 380 | "while (something());"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 381 | } |
| 382 | |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 383 | TEST_F(FormatTest, FormatsSwitchStatement) { |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 384 | verifyFormat("switch (x) {\n" |
| 385 | "case 1:\n" |
| 386 | " f();\n" |
| 387 | " break;\n" |
| 388 | "case kFoo:\n" |
| 389 | "case ns::kBar:\n" |
| 390 | "case kBaz:\n" |
| 391 | " break;\n" |
| 392 | "default:\n" |
| 393 | " g();\n" |
| 394 | " break;\n" |
| 395 | "}"); |
| 396 | verifyFormat("switch (x) {\n" |
| 397 | "case 1: {\n" |
| 398 | " f();\n" |
| 399 | " break;\n" |
| 400 | "}\n" |
| 401 | "}"); |
Nico Weber | 94fb729 | 2013-01-18 05:50:57 +0000 | [diff] [blame] | 402 | verifyFormat("switch (x) {\n" |
| 403 | "case 1: {\n" |
| 404 | " f();\n" |
| 405 | " {\n" |
| 406 | " g();\n" |
| 407 | " h();\n" |
| 408 | " }\n" |
| 409 | " break;\n" |
| 410 | "}\n" |
| 411 | "}"); |
| 412 | verifyFormat("switch (x) {\n" |
| 413 | "case 1: {\n" |
| 414 | " f();\n" |
| 415 | " if (foo) {\n" |
| 416 | " g();\n" |
| 417 | " h();\n" |
| 418 | " }\n" |
| 419 | " break;\n" |
| 420 | "}\n" |
| 421 | "}"); |
| 422 | verifyFormat("switch (x) {\n" |
| 423 | "case 1: {\n" |
| 424 | " f();\n" |
| 425 | " g();\n" |
| 426 | "} break;\n" |
| 427 | "}"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 428 | verifyFormat("switch (test)\n" |
| 429 | " ;"); |
Daniel Jasper | 4ba9bfd | 2013-03-12 12:26:55 +0000 | [diff] [blame] | 430 | verifyFormat("switch (x) {\n" |
| 431 | "default: {\n" |
| 432 | " // Do nothing.\n" |
Manuel Klimek | 67d080d | 2013-04-12 14:13:36 +0000 | [diff] [blame] | 433 | "}\n" |
Daniel Jasper | 4ba9bfd | 2013-03-12 12:26:55 +0000 | [diff] [blame] | 434 | "}"); |
Daniel Jasper | 6050a1e | 2013-03-13 15:53:12 +0000 | [diff] [blame] | 435 | verifyFormat("switch (x) {\n" |
Daniel Jasper | 5999f76 | 2013-04-09 17:46:55 +0000 | [diff] [blame] | 436 | "// comment\n" |
Daniel Jasper | 6050a1e | 2013-03-13 15:53:12 +0000 | [diff] [blame] | 437 | "// if 1, do f()\n" |
| 438 | "case 1:\n" |
| 439 | " f();\n" |
| 440 | "}"); |
Daniel Jasper | c30eb51 | 2013-03-19 18:33:58 +0000 | [diff] [blame] | 441 | verifyFormat("switch (x) {\n" |
| 442 | "case 1:\n" |
| 443 | " // Do amazing stuff\n" |
| 444 | " {\n" |
| 445 | " f();\n" |
| 446 | " g();\n" |
| 447 | " }\n" |
| 448 | " break;\n" |
| 449 | "}"); |
Daniel Jasper | bcca7e4 | 2013-03-20 10:23:53 +0000 | [diff] [blame] | 450 | verifyFormat("#define A \\\n" |
| 451 | " switch (x) { \\\n" |
| 452 | " case a: \\\n" |
| 453 | " foo = b; \\\n" |
| 454 | " }", getLLVMStyleWithColumns(20)); |
Daniel Jasper | 66d19bd | 2013-02-18 11:59:17 +0000 | [diff] [blame] | 455 | |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 456 | verifyGoogleFormat("switch (x) {\n" |
| 457 | " case 1:\n" |
| 458 | " f();\n" |
| 459 | " break;\n" |
| 460 | " case kFoo:\n" |
| 461 | " case ns::kBar:\n" |
| 462 | " case kBaz:\n" |
| 463 | " break;\n" |
| 464 | " default:\n" |
| 465 | " g();\n" |
| 466 | " break;\n" |
| 467 | "}"); |
| 468 | verifyGoogleFormat("switch (x) {\n" |
| 469 | " case 1: {\n" |
| 470 | " f();\n" |
| 471 | " break;\n" |
| 472 | " }\n" |
| 473 | "}"); |
| 474 | verifyGoogleFormat("switch (test)\n" |
| 475 | " ;"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 476 | } |
| 477 | |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 478 | TEST_F(FormatTest, FormatsLabels) { |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 479 | verifyFormat("void f() {\n" |
| 480 | " some_code();\n" |
| 481 | "test_label:\n" |
| 482 | " some_other_code();\n" |
| 483 | " {\n" |
| 484 | " some_more_code();\n" |
| 485 | " another_label:\n" |
| 486 | " some_more_code();\n" |
| 487 | " }\n" |
| 488 | "}"); |
| 489 | verifyFormat("some_code();\n" |
| 490 | "test_label:\n" |
| 491 | "some_other_code();"); |
| 492 | } |
| 493 | |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 494 | //===----------------------------------------------------------------------===// |
| 495 | // Tests for comments. |
| 496 | //===----------------------------------------------------------------------===// |
| 497 | |
| 498 | TEST_F(FormatTest, UnderstandsSingleLineComments) { |
Daniel Jasper | b64eca0 | 2013-03-22 10:01:29 +0000 | [diff] [blame] | 499 | verifyFormat("//* */"); |
Daniel Jasper | e35ec2b | 2012-12-18 19:56:56 +0000 | [diff] [blame] | 500 | verifyFormat("// line 1\n" |
| 501 | "// line 2\n" |
Manuel Klimek | 36fab8d | 2013-01-10 13:24:24 +0000 | [diff] [blame] | 502 | "void f() {}\n"); |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 503 | |
Daniel Jasper | e35ec2b | 2012-12-18 19:56:56 +0000 | [diff] [blame] | 504 | verifyFormat("void f() {\n" |
| 505 | " // Doesn't do anything\n" |
| 506 | "}"); |
Daniel Jasper | dcc2a62 | 2013-01-18 08:44:07 +0000 | [diff] [blame] | 507 | verifyFormat("void f(int i, // some comment (probably for i)\n" |
| 508 | " int j, // some comment (probably for j)\n" |
Daniel Jasper | 487f64b | 2013-01-13 16:10:20 +0000 | [diff] [blame] | 509 | " int k); // some comment (probably for k)"); |
| 510 | verifyFormat("void f(int i,\n" |
| 511 | " // some comment (probably for j)\n" |
| 512 | " int j,\n" |
| 513 | " // some comment (probably for k)\n" |
| 514 | " int k);"); |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 515 | |
Daniel Jasper | dcc2a62 | 2013-01-18 08:44:07 +0000 | [diff] [blame] | 516 | verifyFormat("int i // This is a fancy variable\n" |
| 517 | " = 5; // with nicely aligned comment."); |
| 518 | |
| 519 | verifyFormat("// Leading comment.\n" |
| 520 | "int a; // Trailing comment."); |
| 521 | verifyFormat("int a; // Trailing comment\n" |
| 522 | " // on 2\n" |
| 523 | " // or 3 lines.\n" |
| 524 | "int b;"); |
| 525 | verifyFormat("int a; // Trailing comment\n" |
| 526 | "\n" |
| 527 | "// Leading comment.\n" |
| 528 | "int b;"); |
| 529 | verifyFormat("int a; // Comment.\n" |
| 530 | " // More details.\n" |
| 531 | "int bbbb; // Another comment."); |
| 532 | verifyFormat( |
| 533 | "int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; // comment\n" |
| 534 | "int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; // comment\n" |
| 535 | "int cccccccccccccccccccccccccccccc; // comment\n" |
| 536 | "int ddd; // looooooooooooooooooooooooong comment\n" |
| 537 | "int aaaaaaaaaaaaaaaaaaaaaaa; // comment\n" |
| 538 | "int bbbbbbbbbbbbbbbbbbbbb; // comment\n" |
| 539 | "int ccccccccccccccccccc; // comment"); |
| 540 | |
Daniel Jasper | 7d1185d | 2013-01-18 09:19:33 +0000 | [diff] [blame] | 541 | verifyFormat("#include \"a\" // comment\n" |
| 542 | "#include \"a/b/c\" // comment"); |
| 543 | verifyFormat("#include <a> // comment\n" |
| 544 | "#include <a/b/c> // comment"); |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 545 | |
Daniel Jasper | e35ec2b | 2012-12-18 19:56:56 +0000 | [diff] [blame] | 546 | verifyFormat("enum E {\n" |
| 547 | " // comment\n" |
Daniel Jasper | 7ad4eff | 2013-01-07 11:09:06 +0000 | [diff] [blame] | 548 | " VAL_A, // comment\n" |
Daniel Jasper | e35ec2b | 2012-12-18 19:56:56 +0000 | [diff] [blame] | 549 | " VAL_B\n" |
| 550 | "};"); |
Daniel Jasper | 05b1ac8 | 2012-12-17 11:29:41 +0000 | [diff] [blame] | 551 | |
| 552 | verifyFormat( |
| 553 | "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" |
Daniel Jasper | 7ad4eff | 2013-01-07 11:09:06 +0000 | [diff] [blame] | 554 | " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; // Trailing comment"); |
Daniel Jasper | 043835a | 2013-01-09 09:33:39 +0000 | [diff] [blame] | 555 | verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" |
| 556 | " // Comment inside a statement.\n" |
| 557 | " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;"); |
Daniel Jasper | 36ba0ff | 2013-02-06 15:23:09 +0000 | [diff] [blame] | 558 | verifyFormat( |
| 559 | "bool aaaaaaaaaaaaa = // comment\n" |
| 560 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" |
| 561 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); |
Manuel Klimek | 6cf5814 | 2013-01-07 08:54:53 +0000 | [diff] [blame] | 562 | |
Daniel Jasper | 42f458d | 2013-02-13 19:25:54 +0000 | [diff] [blame] | 563 | verifyFormat("int aaaa; // aaaaa\n" |
Daniel Jasper | 66d19bd | 2013-02-18 11:59:17 +0000 | [diff] [blame] | 564 | "int aa; // aaaaaaa", |
| 565 | getLLVMStyleWithColumns(20)); |
Daniel Jasper | 42f458d | 2013-02-13 19:25:54 +0000 | [diff] [blame] | 566 | |
Daniel Jasper | 821627e | 2013-01-21 22:49:20 +0000 | [diff] [blame] | 567 | EXPECT_EQ("void f() { // This does something ..\n" |
| 568 | "}\n" |
| 569 | "int a; // This is unrelated", |
| 570 | format("void f() { // This does something ..\n" |
| 571 | " }\n" |
| 572 | "int a; // This is unrelated")); |
| 573 | EXPECT_EQ("void f() { // This does something ..\n" |
| 574 | "} // awesome..\n" |
| 575 | "\n" |
| 576 | "int a; // This is unrelated", |
| 577 | format("void f() { // This does something ..\n" |
| 578 | " } // awesome..\n" |
| 579 | " \n" |
| 580 | "int a; // This is unrelated")); |
| 581 | |
Daniel Jasper | 7ad4eff | 2013-01-07 11:09:06 +0000 | [diff] [blame] | 582 | EXPECT_EQ("int i; // single line trailing comment", |
Manuel Klimek | 6cf5814 | 2013-01-07 08:54:53 +0000 | [diff] [blame] | 583 | format("int i;\\\n// single line trailing comment")); |
Daniel Jasper | 7ad4eff | 2013-01-07 11:09:06 +0000 | [diff] [blame] | 584 | |
| 585 | verifyGoogleFormat("int a; // Trailing comment."); |
Daniel Jasper | d4f2c2e | 2013-01-29 19:41:55 +0000 | [diff] [blame] | 586 | |
| 587 | verifyFormat("someFunction(anotherFunction( // Force break.\n" |
| 588 | " parameter));"); |
Daniel Jasper | 5b7e7b0 | 2013-02-05 09:34:14 +0000 | [diff] [blame] | 589 | |
| 590 | verifyGoogleFormat("#endif // HEADER_GUARD"); |
Daniel Jasper | 474e462 | 2013-02-06 22:04:05 +0000 | [diff] [blame] | 591 | |
| 592 | verifyFormat("const char *test[] = {\n" |
| 593 | " // A\n" |
| 594 | " \"aaaa\",\n" |
| 595 | " // B\n" |
| 596 | " \"aaaaa\",\n" |
| 597 | "};"); |
Daniel Jasper | 729a743 | 2013-02-11 12:36:37 +0000 | [diff] [blame] | 598 | verifyGoogleFormat( |
| 599 | "aaaaaaaaaaaaaaaaaaaaaaaaaa(\n" |
| 600 | " aaaaaaaaaaaaaaaaaaaaaa); // 81 cols with this comment"); |
Alexander Kornienko | 4128e19 | 2013-04-03 12:38:53 +0000 | [diff] [blame] | 601 | EXPECT_EQ("D(a, {\n" |
| 602 | " // test\n" |
| 603 | " int a;\n" |
| 604 | "});", |
| 605 | format("D(a, {\n" |
| 606 | "// test\n" |
| 607 | "int a;\n" |
| 608 | "});")); |
Daniel Jasper | 05b1ac8 | 2012-12-17 11:29:41 +0000 | [diff] [blame] | 609 | } |
| 610 | |
Daniel Jasper | c363dbb | 2013-03-22 16:25:51 +0000 | [diff] [blame] | 611 | TEST_F(FormatTest, CanFormatCommentsLocally) { |
| 612 | EXPECT_EQ("int a; // comment\n" |
| 613 | "int b; // comment", |
| 614 | format("int a; // comment\n" |
| 615 | "int b; // comment", |
| 616 | 0, 0, getLLVMStyle())); |
| 617 | EXPECT_EQ("int a; // comment\n" |
| 618 | " // line 2\n" |
| 619 | "int b;", |
| 620 | format("int a; // comment\n" |
| 621 | " // line 2\n" |
| 622 | "int b;", |
| 623 | 28, 0, getLLVMStyle())); |
Daniel Jasper | af84976 | 2013-04-24 06:33:59 +0000 | [diff] [blame] | 624 | EXPECT_EQ("int aaaaaa; // comment\n" |
| 625 | "int b;\n" |
| 626 | "int c; // unrelated comment", |
| 627 | format("int aaaaaa; // comment\n" |
| 628 | "int b;\n" |
| 629 | "int c; // unrelated comment", |
| 630 | 31, 0, getLLVMStyle())); |
Daniel Jasper | c363dbb | 2013-03-22 16:25:51 +0000 | [diff] [blame] | 631 | } |
| 632 | |
Daniel Jasper | 812c045 | 2013-03-01 16:45:59 +0000 | [diff] [blame] | 633 | TEST_F(FormatTest, RemovesTrailingWhitespaceOfComments) { |
| 634 | EXPECT_EQ("// comment", format("// comment ")); |
| 635 | EXPECT_EQ("int aaaaaaa, bbbbbbb; // comment", |
| 636 | format("int aaaaaaa, bbbbbbb; // comment ", |
| 637 | getLLVMStyleWithColumns(33))); |
| 638 | } |
| 639 | |
Daniel Jasper | 05b1ac8 | 2012-12-17 11:29:41 +0000 | [diff] [blame] | 640 | TEST_F(FormatTest, UnderstandsMultiLineComments) { |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 641 | verifyFormat("f(/*test=*/ true);"); |
Daniel Jasper | 2c6cc48 | 2013-01-17 12:53:34 +0000 | [diff] [blame] | 642 | EXPECT_EQ( |
| 643 | "f(aaaaaaaaaaaaaaaaaaaaaaaaa, /* Trailing comment for aa... */\n" |
| 644 | " bbbbbbbbbbbbbbbbbbbbbbbbb);", |
Alexander Kornienko | 919398b | 2013-04-17 17:34:05 +0000 | [diff] [blame] | 645 | format("f(aaaaaaaaaaaaaaaaaaaaaaaaa , \\\n/* Trailing comment for aa... */\n" |
Daniel Jasper | 2c6cc48 | 2013-01-17 12:53:34 +0000 | [diff] [blame] | 646 | " bbbbbbbbbbbbbbbbbbbbbbbbb);")); |
| 647 | EXPECT_EQ( |
| 648 | "f(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" |
| 649 | " /* Leading comment for bb... */ bbbbbbbbbbbbbbbbbbbbbbbbb);", |
| 650 | format("f(aaaaaaaaaaaaaaaaaaaaaaaaa , \n" |
| 651 | "/* Leading comment for bb... */ bbbbbbbbbbbbbbbbbbbbbbbbb);")); |
Daniel Jasper | d4f2c2e | 2013-01-29 19:41:55 +0000 | [diff] [blame] | 652 | |
Daniel Jasper | faab0d3 | 2013-02-27 09:47:53 +0000 | [diff] [blame] | 653 | FormatStyle NoBinPacking = getLLVMStyle(); |
| 654 | NoBinPacking.BinPackParameters = false; |
| 655 | verifyFormat("aaaaaaaa(/* parameter 1 */ aaaaaa,\n" |
| 656 | " /* parameter 2 */ aaaaaa,\n" |
| 657 | " /* parameter 3 */ aaaaaa,\n" |
| 658 | " /* parameter 4 */ aaaaaa);", |
| 659 | NoBinPacking); |
Daniel Jasper | 2c6cc48 | 2013-01-17 12:53:34 +0000 | [diff] [blame] | 660 | } |
| 661 | |
Alexander Kornienko | f753615 | 2013-03-14 16:10:54 +0000 | [diff] [blame] | 662 | TEST_F(FormatTest, AlignsMultiLineComments) { |
| 663 | EXPECT_EQ("/*\n" |
| 664 | " * Really multi-line\n" |
| 665 | " * comment.\n" |
| 666 | " */\n" |
| 667 | "void f() {}", |
| 668 | format(" /*\n" |
| 669 | " * Really multi-line\n" |
| 670 | " * comment.\n" |
| 671 | " */\n" |
| 672 | " void f() {}")); |
Alexander Kornienko | f753615 | 2013-03-14 16:10:54 +0000 | [diff] [blame] | 673 | EXPECT_EQ("class C {\n" |
| 674 | " /*\n" |
| 675 | " * Another multi-line\n" |
| 676 | " * comment.\n" |
| 677 | " */\n" |
| 678 | " void f() {}\n" |
| 679 | "};", |
| 680 | format("class C {\n" |
| 681 | "/*\n" |
| 682 | " * Another multi-line\n" |
| 683 | " * comment.\n" |
| 684 | " */\n" |
| 685 | "void f() {}\n" |
| 686 | "};")); |
Alexander Kornienko | 1fdd8b3 | 2013-03-15 13:42:02 +0000 | [diff] [blame] | 687 | EXPECT_EQ("/*\n" |
| 688 | " 1. This is a comment with non-trivial formatting.\n" |
| 689 | " 1.1. We have to indent/outdent all lines equally\n" |
| 690 | " 1.1.1. to keep the formatting.\n" |
| 691 | " */", |
| 692 | format(" /*\n" |
| 693 | " 1. This is a comment with non-trivial formatting.\n" |
| 694 | " 1.1. We have to indent/outdent all lines equally\n" |
| 695 | " 1.1.1. to keep the formatting.\n" |
| 696 | " */")); |
| 697 | EXPECT_EQ("/*\n" |
| 698 | " Don't try to outdent if there's not enough inentation.\n" |
| 699 | " */", |
| 700 | format(" /*\n" |
| 701 | " Don't try to outdent if there's not enough inentation.\n" |
| 702 | " */")); |
Alexander Kornienko | f753615 | 2013-03-14 16:10:54 +0000 | [diff] [blame] | 703 | } |
| 704 | |
Alexander Kornienko | 5262dd9 | 2013-03-27 11:52:18 +0000 | [diff] [blame] | 705 | TEST_F(FormatTest, SplitsLongCxxComments) { |
| 706 | EXPECT_EQ("// A comment that\n" |
| 707 | "// doesn't fit on\n" |
| 708 | "// one line", |
| 709 | format("// A comment that doesn't fit on one line", |
| 710 | getLLVMStyleWithColumns(20))); |
Alexander Kornienko | 919398b | 2013-04-17 17:34:05 +0000 | [diff] [blame] | 711 | EXPECT_EQ("// a b c d\n" |
| 712 | "// e f g\n" |
| 713 | "// h i j k", |
| 714 | format("// a b c d e f g h i j k", |
| 715 | getLLVMStyleWithColumns(10))); |
| 716 | EXPECT_EQ("// a b c d\n" |
| 717 | "// e f g\n" |
| 718 | "// h i j k", |
| 719 | format("\\\n// a b c d e f g h i j k", |
| 720 | getLLVMStyleWithColumns(10))); |
Alexander Kornienko | 5262dd9 | 2013-03-27 11:52:18 +0000 | [diff] [blame] | 721 | EXPECT_EQ("if (true) // A comment that\n" |
| 722 | " // doesn't fit on\n" |
| 723 | " // one line", |
| 724 | format("if (true) // A comment that doesn't fit on one line ", |
| 725 | getLLVMStyleWithColumns(30))); |
| 726 | EXPECT_EQ("// Don't_touch_leading_whitespace", |
| 727 | format("// Don't_touch_leading_whitespace", |
| 728 | getLLVMStyleWithColumns(20))); |
| 729 | EXPECT_EQ( |
| 730 | "//Don't add leading\n" |
| 731 | "//whitespace", |
| 732 | format("//Don't add leading whitespace", getLLVMStyleWithColumns(20))); |
Alexander Kornienko | 9fdc00a | 2013-03-28 18:40:55 +0000 | [diff] [blame] | 733 | EXPECT_EQ("// A comment before\n" |
| 734 | "// a macro\n" |
| 735 | "// definition\n" |
| 736 | "#define a b", |
| 737 | format("// A comment before a macro definition\n" |
| 738 | "#define a b", |
| 739 | getLLVMStyleWithColumns(20))); |
Alexander Kornienko | 5262dd9 | 2013-03-27 11:52:18 +0000 | [diff] [blame] | 740 | } |
| 741 | |
Alexander Kornienko | f52d527 | 2013-04-02 13:04:06 +0000 | [diff] [blame] | 742 | TEST_F(FormatTest, ParsesCommentsAdjacentToPPDirectives) { |
| 743 | EXPECT_EQ("namespace {}\n// Test\n#define A", |
| 744 | format("namespace {}\n // Test\n#define A")); |
| 745 | EXPECT_EQ("namespace {}\n/* Test */\n#define A", |
| 746 | format("namespace {}\n /* Test */\n#define A")); |
| 747 | EXPECT_EQ("namespace {}\n/* Test */ #define A", |
| 748 | format("namespace {}\n /* Test */ #define A")); |
| 749 | } |
| 750 | |
Alexander Kornienko | 052685c | 2013-03-19 17:41:36 +0000 | [diff] [blame] | 751 | TEST_F(FormatTest, SplitsLongLinesInComments) { |
| 752 | EXPECT_EQ("/* This is a long\n" |
Alexander Kornienko | 7c22cf3 | 2013-03-21 12:28:10 +0000 | [diff] [blame] | 753 | " * comment that\n" |
| 754 | " * doesn't\n" |
| 755 | " * fit on one line.\n" |
| 756 | " */", |
Alexander Kornienko | 052685c | 2013-03-19 17:41:36 +0000 | [diff] [blame] | 757 | format("/* " |
| 758 | "This is a long " |
Alexander Kornienko | 7c22cf3 | 2013-03-21 12:28:10 +0000 | [diff] [blame] | 759 | "comment that " |
| 760 | "doesn't " |
Alexander Kornienko | 052685c | 2013-03-19 17:41:36 +0000 | [diff] [blame] | 761 | "fit on one line. */", |
| 762 | getLLVMStyleWithColumns(20))); |
Alexander Kornienko | 919398b | 2013-04-17 17:34:05 +0000 | [diff] [blame] | 763 | EXPECT_EQ("/* a b c d\n" |
| 764 | " * e f g\n" |
| 765 | " * h i j k\n" |
| 766 | " */", |
| 767 | format("/* a b c d e f g h i j k */", |
| 768 | getLLVMStyleWithColumns(10))); |
| 769 | EXPECT_EQ("/* a b c d\n" |
| 770 | " * e f g\n" |
| 771 | " * h i j k\n" |
| 772 | " */", |
| 773 | format("\\\n/* a b c d e f g h i j k */", |
| 774 | getLLVMStyleWithColumns(10))); |
Alexander Kornienko | 052685c | 2013-03-19 17:41:36 +0000 | [diff] [blame] | 775 | EXPECT_EQ("/*\n" |
| 776 | "This is a long\n" |
| 777 | "comment that doesn't\n" |
| 778 | "fit on one line.\n" |
| 779 | "*/", |
| 780 | format("/*\n" |
| 781 | "This is a long " |
| 782 | "comment that doesn't " |
| 783 | "fit on one line. \n" |
| 784 | "*/", getLLVMStyleWithColumns(20))); |
| 785 | EXPECT_EQ("/*\n" |
| 786 | " * This is a long\n" |
| 787 | " * comment that\n" |
| 788 | " * doesn't fit on\n" |
| 789 | " * one line.\n" |
| 790 | " */", |
Alexander Kornienko | 7c22cf3 | 2013-03-21 12:28:10 +0000 | [diff] [blame] | 791 | format("/* \n" |
Alexander Kornienko | 052685c | 2013-03-19 17:41:36 +0000 | [diff] [blame] | 792 | " * This is a long " |
| 793 | " comment that " |
| 794 | " doesn't fit on " |
| 795 | " one line. \n" |
| 796 | " */", getLLVMStyleWithColumns(20))); |
| 797 | EXPECT_EQ("/*\n" |
| 798 | " * This_is_a_comment_with_words_that_dont_fit_on_one_line\n" |
| 799 | " * so_it_should_be_broken\n" |
| 800 | " * wherever_a_space_occurs\n" |
| 801 | " */", |
| 802 | format("/*\n" |
| 803 | " * This_is_a_comment_with_words_that_dont_fit_on_one_line " |
| 804 | " so_it_should_be_broken " |
| 805 | " wherever_a_space_occurs \n" |
| 806 | " */", |
| 807 | getLLVMStyleWithColumns(20))); |
| 808 | EXPECT_EQ("/*\n" |
Alexander Kornienko | 5262dd9 | 2013-03-27 11:52:18 +0000 | [diff] [blame] | 809 | " * This_comment_can_not_be_broken_into_lines\n" |
Alexander Kornienko | 052685c | 2013-03-19 17:41:36 +0000 | [diff] [blame] | 810 | " */", |
| 811 | format("/*\n" |
Alexander Kornienko | 5262dd9 | 2013-03-27 11:52:18 +0000 | [diff] [blame] | 812 | " * This_comment_can_not_be_broken_into_lines\n" |
Alexander Kornienko | 052685c | 2013-03-19 17:41:36 +0000 | [diff] [blame] | 813 | " */", |
| 814 | getLLVMStyleWithColumns(20))); |
| 815 | EXPECT_EQ("{\n" |
| 816 | " /*\n" |
| 817 | " This is another\n" |
| 818 | " long comment that\n" |
| 819 | " doesn't fit on one\n" |
| 820 | " line 1234567890\n" |
| 821 | " */\n" |
| 822 | "}", |
| 823 | format("{\n" |
| 824 | "/*\n" |
| 825 | "This is another " |
| 826 | " long comment that " |
| 827 | " doesn't fit on one" |
| 828 | " line 1234567890\n" |
| 829 | "*/\n" |
| 830 | "}", getLLVMStyleWithColumns(20))); |
| 831 | EXPECT_EQ("{\n" |
| 832 | " /*\n" |
| 833 | " * This i s\n" |
| 834 | " * another comment\n" |
| 835 | " * t hat doesn' t\n" |
| 836 | " * fit on one l i\n" |
| 837 | " * n e\n" |
| 838 | " */\n" |
| 839 | "}", |
| 840 | format("{\n" |
| 841 | "/*\n" |
| 842 | " * This i s" |
| 843 | " another comment" |
| 844 | " t hat doesn' t" |
| 845 | " fit on one l i" |
| 846 | " n e\n" |
| 847 | " */\n" |
| 848 | "}", getLLVMStyleWithColumns(20))); |
| 849 | EXPECT_EQ("/*\n" |
| 850 | " * This is a long\n" |
| 851 | " * comment that\n" |
| 852 | " * doesn't fit on\n" |
| 853 | " * one line\n" |
| 854 | " */", |
| 855 | format(" /*\n" |
| 856 | " * This is a long comment that doesn't fit on one line\n" |
| 857 | " */", getLLVMStyleWithColumns(20))); |
Alexander Kornienko | 7c22cf3 | 2013-03-21 12:28:10 +0000 | [diff] [blame] | 858 | EXPECT_EQ("{\n" |
| 859 | " if (something) /* This is a\n" |
| 860 | "long comment */\n" |
| 861 | " ;\n" |
| 862 | "}", |
| 863 | format("{\n" |
| 864 | " if (something) /* This is a long comment */\n" |
| 865 | " ;\n" |
| 866 | "}", |
| 867 | getLLVMStyleWithColumns(30))); |
Alexander Kornienko | 052685c | 2013-03-19 17:41:36 +0000 | [diff] [blame] | 868 | } |
| 869 | |
| 870 | TEST_F(FormatTest, SplitsLongLinesInCommentsInPreprocessor) { |
| 871 | EXPECT_EQ("#define X \\\n" |
Alexander Kornienko | 7c22cf3 | 2013-03-21 12:28:10 +0000 | [diff] [blame] | 872 | " /* \\\n" |
| 873 | " Test \\\n" |
Alexander Kornienko | 052685c | 2013-03-19 17:41:36 +0000 | [diff] [blame] | 874 | " Macro comment \\\n" |
| 875 | " with a long \\\n" |
| 876 | " line \\\n" |
Alexander Kornienko | 70ce788 | 2013-04-15 14:28:00 +0000 | [diff] [blame] | 877 | " */ \\\n" |
Alexander Kornienko | 052685c | 2013-03-19 17:41:36 +0000 | [diff] [blame] | 878 | " A + B", |
| 879 | format("#define X \\\n" |
| 880 | " /*\n" |
Alexander Kornienko | 7c22cf3 | 2013-03-21 12:28:10 +0000 | [diff] [blame] | 881 | " Test\n" |
Alexander Kornienko | 052685c | 2013-03-19 17:41:36 +0000 | [diff] [blame] | 882 | " Macro comment with a long line\n" |
| 883 | " */ \\\n" |
| 884 | " A + B", |
| 885 | getLLVMStyleWithColumns(20))); |
| 886 | EXPECT_EQ("#define X \\\n" |
| 887 | " /* Macro comment \\\n" |
Alexander Kornienko | 7c22cf3 | 2013-03-21 12:28:10 +0000 | [diff] [blame] | 888 | " with a long \\\n" |
Alexander Kornienko | 70ce788 | 2013-04-15 14:28:00 +0000 | [diff] [blame] | 889 | " line */ \\\n" |
Alexander Kornienko | 7c22cf3 | 2013-03-21 12:28:10 +0000 | [diff] [blame] | 890 | " A + B", |
| 891 | format("#define X \\\n" |
| 892 | " /* Macro comment with a long\n" |
| 893 | " line */ \\\n" |
| 894 | " A + B", |
| 895 | getLLVMStyleWithColumns(20))); |
| 896 | EXPECT_EQ("#define X \\\n" |
| 897 | " /* Macro comment \\\n" |
| 898 | " * with a long \\\n" |
Alexander Kornienko | 70ce788 | 2013-04-15 14:28:00 +0000 | [diff] [blame] | 899 | " * line */ \\\n" |
Alexander Kornienko | 052685c | 2013-03-19 17:41:36 +0000 | [diff] [blame] | 900 | " A + B", |
| 901 | format("#define X \\\n" |
| 902 | " /* Macro comment with a long line */ \\\n" |
| 903 | " A + B", |
| 904 | getLLVMStyleWithColumns(20))); |
| 905 | } |
| 906 | |
Daniel Jasper | 2c6cc48 | 2013-01-17 12:53:34 +0000 | [diff] [blame] | 907 | TEST_F(FormatTest, CommentsInStaticInitializers) { |
| 908 | EXPECT_EQ( |
| 909 | "static SomeType type = { aaaaaaaaaaaaaaaaaaaa, /* comment */\n" |
| 910 | " aaaaaaaaaaaaaaaaaaaa /* comment */,\n" |
| 911 | " /* comment */ aaaaaaaaaaaaaaaaaaaa,\n" |
| 912 | " aaaaaaaaaaaaaaaaaaaa, // comment\n" |
| 913 | " aaaaaaaaaaaaaaaaaaaa };", |
| 914 | format("static SomeType type = { aaaaaaaaaaaaaaaaaaaa , /* comment */\n" |
| 915 | " aaaaaaaaaaaaaaaaaaaa /* comment */ ,\n" |
| 916 | " /* comment */ aaaaaaaaaaaaaaaaaaaa ,\n" |
| 917 | " aaaaaaaaaaaaaaaaaaaa , // comment\n" |
| 918 | " aaaaaaaaaaaaaaaaaaaa };")); |
| 919 | verifyFormat("static SomeType type = { aaaaaaaaaaa, // comment for aa...\n" |
| 920 | " bbbbbbbbbbb, ccccccccccc };"); |
| 921 | verifyFormat("static SomeType type = { aaaaaaaaaaa,\n" |
| 922 | " // comment for bb....\n" |
| 923 | " bbbbbbbbbbb, ccccccccccc };"); |
| 924 | verifyGoogleFormat( |
| 925 | "static SomeType type = { aaaaaaaaaaa, // comment for aa...\n" |
Daniel Jasper | f343cab | 2013-01-31 14:59:26 +0000 | [diff] [blame] | 926 | " bbbbbbbbbbb, ccccccccccc };"); |
Daniel Jasper | 2c6cc48 | 2013-01-17 12:53:34 +0000 | [diff] [blame] | 927 | verifyGoogleFormat("static SomeType type = { aaaaaaaaaaa,\n" |
| 928 | " // comment for bb....\n" |
Daniel Jasper | f343cab | 2013-01-31 14:59:26 +0000 | [diff] [blame] | 929 | " bbbbbbbbbbb, ccccccccccc };"); |
Daniel Jasper | 2c6cc48 | 2013-01-17 12:53:34 +0000 | [diff] [blame] | 930 | |
Daniel Jasper | f343cab | 2013-01-31 14:59:26 +0000 | [diff] [blame] | 931 | verifyFormat("S s = { { a, b, c }, // Group #1\n" |
| 932 | " { d, e, f }, // Group #2\n" |
| 933 | " { g, h, i } }; // Group #3"); |
| 934 | verifyFormat("S s = { { // Group #1\n" |
| 935 | " a, b, c },\n" |
| 936 | " { // Group #2\n" |
| 937 | " d, e, f },\n" |
| 938 | " { // Group #3\n" |
| 939 | " g, h, i } };"); |
Daniel Jasper | c4615b7 | 2013-02-20 12:56:39 +0000 | [diff] [blame] | 940 | |
| 941 | EXPECT_EQ("S s = {\n" |
| 942 | " // Some comment\n" |
Daniel Jasper | 1ef81d5 | 2013-02-26 13:10:34 +0000 | [diff] [blame] | 943 | " a,\n" |
Daniel Jasper | c4615b7 | 2013-02-20 12:56:39 +0000 | [diff] [blame] | 944 | "\n" |
| 945 | " // Comment after empty line\n" |
| 946 | " b\n" |
Daniel Jasper | 1ef81d5 | 2013-02-26 13:10:34 +0000 | [diff] [blame] | 947 | "}", |
| 948 | format("S s = {\n" |
| 949 | " // Some comment\n" |
| 950 | " a,\n" |
| 951 | " \n" |
| 952 | " // Comment after empty line\n" |
| 953 | " b\n" |
| 954 | "}")); |
| 955 | EXPECT_EQ("S s = { a, b };", format("S s = {\n" |
Daniel Jasper | 84a1a63 | 2013-02-26 13:18:08 +0000 | [diff] [blame] | 956 | " a,\n" |
| 957 | "\n" |
| 958 | " b\n" |
| 959 | "};")); |
Daniel Jasper | 25e81b2 | 2013-02-28 15:04:12 +0000 | [diff] [blame] | 960 | verifyFormat("const uint8_t aaaaaaaaaaaaaaaaaaaaaa[0] = {\n" |
| 961 | " 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // comment\n" |
| 962 | " 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // comment\n" |
| 963 | " 0x00, 0x00, 0x00, 0x00 // comment\n" |
| 964 | "};"); |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 965 | } |
| 966 | |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 967 | //===----------------------------------------------------------------------===// |
| 968 | // Tests for classes, namespaces, etc. |
| 969 | //===----------------------------------------------------------------------===// |
| 970 | |
| 971 | TEST_F(FormatTest, DoesNotBreakSemiAfterClassDecl) { |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 972 | verifyFormat("class A {\n};"); |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 973 | } |
| 974 | |
| 975 | TEST_F(FormatTest, UnderstandsAccessSpecifiers) { |
| 976 | verifyFormat("class A {\n" |
| 977 | "public:\n" |
Daniel Jasper | ac3223e | 2013-04-10 09:49:49 +0000 | [diff] [blame] | 978 | "public: // comment\n" |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 979 | "protected:\n" |
| 980 | "private:\n" |
Manuel Klimek | 36fab8d | 2013-01-10 13:24:24 +0000 | [diff] [blame] | 981 | " void f() {}\n" |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 982 | "};"); |
| 983 | verifyGoogleFormat("class A {\n" |
| 984 | " public:\n" |
| 985 | " protected:\n" |
| 986 | " private:\n" |
Manuel Klimek | 36fab8d | 2013-01-10 13:24:24 +0000 | [diff] [blame] | 987 | " void f() {}\n" |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 988 | "};"); |
| 989 | } |
| 990 | |
Alexander Kornienko | 94b748f | 2013-03-27 17:08:02 +0000 | [diff] [blame] | 991 | TEST_F(FormatTest, SeparatesLogicalBlocks) { |
| 992 | EXPECT_EQ("class A {\n" |
| 993 | "public:\n" |
| 994 | " void f();\n" |
| 995 | "\n" |
| 996 | "private:\n" |
| 997 | " void g() {}\n" |
| 998 | " // test\n" |
| 999 | "protected:\n" |
| 1000 | " int h;\n" |
| 1001 | "};", |
| 1002 | format("class A {\n" |
| 1003 | "public:\n" |
| 1004 | "void f();\n" |
| 1005 | "private:\n" |
| 1006 | "void g() {}\n" |
| 1007 | "// test\n" |
| 1008 | "protected:\n" |
| 1009 | "int h;\n" |
| 1010 | "};")); |
| 1011 | } |
| 1012 | |
Daniel Jasper | 6b119d6 | 2013-04-05 17:22:09 +0000 | [diff] [blame] | 1013 | TEST_F(FormatTest, FormatsClasses) { |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 1014 | verifyFormat("class A : public B {\n};"); |
| 1015 | verifyFormat("class A : public ::B {\n};"); |
Daniel Jasper | 6cabab4 | 2013-02-14 08:42:54 +0000 | [diff] [blame] | 1016 | |
| 1017 | verifyFormat( |
| 1018 | "class AAAAAAAAAAAAAAAAAAAA : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n" |
| 1019 | " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {\n" |
| 1020 | "};\n"); |
| 1021 | verifyFormat("class AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA :\n" |
| 1022 | " public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n" |
| 1023 | " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {\n" |
| 1024 | "};\n"); |
| 1025 | verifyFormat( |
| 1026 | "class A : public B, public C, public D, public E, public F, public G {\n" |
| 1027 | "};"); |
| 1028 | verifyFormat("class AAAAAAAAAAAA : public B,\n" |
| 1029 | " public C,\n" |
| 1030 | " public D,\n" |
| 1031 | " public E,\n" |
| 1032 | " public F,\n" |
| 1033 | " public G {\n" |
| 1034 | "};"); |
Daniel Jasper | 6b119d6 | 2013-04-05 17:22:09 +0000 | [diff] [blame] | 1035 | |
| 1036 | verifyFormat("class\n" |
| 1037 | " ReallyReallyLongClassName {\n};", |
| 1038 | getLLVMStyleWithColumns(32)); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1039 | } |
| 1040 | |
Manuel Klimek | de76854 | 2013-01-07 18:10:23 +0000 | [diff] [blame] | 1041 | TEST_F(FormatTest, FormatsVariableDeclarationsAfterStructOrClass) { |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 1042 | verifyFormat("class A {\n} a, b;"); |
| 1043 | verifyFormat("struct A {\n} a, b;"); |
| 1044 | verifyFormat("union A {\n} a;"); |
Manuel Klimek | de76854 | 2013-01-07 18:10:23 +0000 | [diff] [blame] | 1045 | } |
| 1046 | |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 1047 | TEST_F(FormatTest, FormatsEnum) { |
Alexander Kornienko | a166e73 | 2012-12-04 14:46:19 +0000 | [diff] [blame] | 1048 | verifyFormat("enum {\n" |
| 1049 | " Zero,\n" |
| 1050 | " One = 1,\n" |
| 1051 | " Two = One + 1,\n" |
| 1052 | " Three = (One + Two),\n" |
| 1053 | " Four = (Zero && (One ^ Two)) | (One << Two),\n" |
| 1054 | " Five = (One, Two, Three, Four, 5)\n" |
| 1055 | "};"); |
| 1056 | verifyFormat("enum Enum {\n" |
| 1057 | "};"); |
| 1058 | verifyFormat("enum {\n" |
| 1059 | "};"); |
Manuel Klimek | 308232c | 2013-01-21 19:17:52 +0000 | [diff] [blame] | 1060 | verifyFormat("enum X E {\n} d;"); |
| 1061 | verifyFormat("enum __attribute__((...)) E {\n} d;"); |
| 1062 | verifyFormat("enum __declspec__((...)) E {\n} d;"); |
| 1063 | verifyFormat("enum X f() {\n a();\n return 42;\n}"); |
Alexander Kornienko | a166e73 | 2012-12-04 14:46:19 +0000 | [diff] [blame] | 1064 | } |
| 1065 | |
Nico Weber | efaddc0 | 2013-01-14 05:49:49 +0000 | [diff] [blame] | 1066 | TEST_F(FormatTest, FormatsBitfields) { |
| 1067 | verifyFormat("struct Bitfields {\n" |
| 1068 | " unsigned sClass : 8;\n" |
| 1069 | " unsigned ValueKind : 2;\n" |
| 1070 | "};"); |
| 1071 | } |
| 1072 | |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 1073 | TEST_F(FormatTest, FormatsNamespaces) { |
| 1074 | verifyFormat("namespace some_namespace {\n" |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 1075 | "class A {\n};\n" |
Manuel Klimek | 517e894 | 2013-01-11 17:54:10 +0000 | [diff] [blame] | 1076 | "void f() { f(); }\n" |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 1077 | "}"); |
| 1078 | verifyFormat("namespace {\n" |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 1079 | "class A {\n};\n" |
Manuel Klimek | 517e894 | 2013-01-11 17:54:10 +0000 | [diff] [blame] | 1080 | "void f() { f(); }\n" |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 1081 | "}"); |
Dmitri Gribenko | 1f94f2b | 2012-12-30 21:27:25 +0000 | [diff] [blame] | 1082 | verifyFormat("inline namespace X {\n" |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 1083 | "class A {\n};\n" |
Manuel Klimek | 517e894 | 2013-01-11 17:54:10 +0000 | [diff] [blame] | 1084 | "void f() { f(); }\n" |
Dmitri Gribenko | 1f94f2b | 2012-12-30 21:27:25 +0000 | [diff] [blame] | 1085 | "}"); |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 1086 | verifyFormat("using namespace some_namespace;\n" |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 1087 | "class A {\n};\n" |
Manuel Klimek | 517e894 | 2013-01-11 17:54:10 +0000 | [diff] [blame] | 1088 | "void f() { f(); }"); |
Manuel Klimek | 7fc2db0 | 2013-02-06 16:08:09 +0000 | [diff] [blame] | 1089 | |
| 1090 | // This code is more common than we thought; if we |
| 1091 | // layout this correctly the semicolon will go into |
| 1092 | // its own line, which is undesireable. |
| 1093 | verifyFormat("namespace {\n};"); |
| 1094 | verifyFormat("namespace {\n" |
| 1095 | "class A {\n" |
| 1096 | "};\n" |
| 1097 | "};"); |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 1098 | } |
| 1099 | |
Daniel Jasper | 66d19bd | 2013-02-18 11:59:17 +0000 | [diff] [blame] | 1100 | TEST_F(FormatTest, FormatsExternC) { verifyFormat("extern \"C\" {\nint a;"); } |
Manuel Klimek | d19dc2d | 2013-01-21 14:32:05 +0000 | [diff] [blame] | 1101 | |
Daniel Jasper | 923ebef | 2013-03-14 13:45:21 +0000 | [diff] [blame] | 1102 | TEST_F(FormatTest, FormatsInlineASM) { |
| 1103 | verifyFormat("asm(\"xyz\" : \"=a\"(a), \"=d\"(b) : \"a\"(data));"); |
| 1104 | verifyFormat( |
| 1105 | "asm(\"movq\\t%%rbx, %%rsi\\n\\t\"\n" |
| 1106 | " \"cpuid\\n\\t\"\n" |
| 1107 | " \"xchgq\\t%%rbx, %%rsi\\n\\t\"\n" |
| 1108 | " : \"=a\" (*rEAX), \"=S\" (*rEBX), \"=c\" (*rECX), \"=d\" (*rEDX)\n" |
| 1109 | " : \"a\"(value));"); |
| 1110 | } |
| 1111 | |
Nico Weber | a9ccdd1 | 2013-01-07 16:36:17 +0000 | [diff] [blame] | 1112 | TEST_F(FormatTest, FormatTryCatch) { |
Manuel Klimek | 517e894 | 2013-01-11 17:54:10 +0000 | [diff] [blame] | 1113 | // FIXME: Handle try-catch explicitly in the UnwrappedLineParser, then we'll |
| 1114 | // also not create single-line-blocks. |
Nico Weber | a9ccdd1 | 2013-01-07 16:36:17 +0000 | [diff] [blame] | 1115 | verifyFormat("try {\n" |
| 1116 | " throw a * b;\n" |
| 1117 | "}\n" |
| 1118 | "catch (int a) {\n" |
| 1119 | " // Do nothing.\n" |
| 1120 | "}\n" |
| 1121 | "catch (...) {\n" |
| 1122 | " exit(42);\n" |
| 1123 | "}"); |
| 1124 | |
| 1125 | // Function-level try statements. |
Manuel Klimek | 517e894 | 2013-01-11 17:54:10 +0000 | [diff] [blame] | 1126 | verifyFormat("int f() try { return 4; }\n" |
Nico Weber | a9ccdd1 | 2013-01-07 16:36:17 +0000 | [diff] [blame] | 1127 | "catch (...) {\n" |
| 1128 | " return 5;\n" |
| 1129 | "}"); |
| 1130 | verifyFormat("class A {\n" |
| 1131 | " int a;\n" |
Manuel Klimek | 36fab8d | 2013-01-10 13:24:24 +0000 | [diff] [blame] | 1132 | " A() try : a(0) {}\n" |
Nico Weber | a9ccdd1 | 2013-01-07 16:36:17 +0000 | [diff] [blame] | 1133 | " catch (...) {\n" |
| 1134 | " throw;\n" |
| 1135 | " }\n" |
| 1136 | "};\n"); |
| 1137 | } |
| 1138 | |
| 1139 | TEST_F(FormatTest, FormatObjCTryCatch) { |
Manuel Klimek | d5688cf | 2013-01-11 19:17:44 +0000 | [diff] [blame] | 1140 | verifyFormat("@try {\n" |
| 1141 | " f();\n" |
| 1142 | "}\n" |
| 1143 | "@catch (NSException e) {\n" |
| 1144 | " @throw;\n" |
| 1145 | "}\n" |
| 1146 | "@finally {\n" |
| 1147 | " exit(42);\n" |
| 1148 | "}"); |
Nico Weber | a9ccdd1 | 2013-01-07 16:36:17 +0000 | [diff] [blame] | 1149 | } |
| 1150 | |
Daniel Jasper | 05b1ac8 | 2012-12-17 11:29:41 +0000 | [diff] [blame] | 1151 | TEST_F(FormatTest, StaticInitializers) { |
| 1152 | verifyFormat("static SomeClass SC = { 1, 'a' };"); |
| 1153 | |
| 1154 | // FIXME: Format like enums if the static initializer does not fit on a line. |
| 1155 | verifyFormat( |
Daniel Jasper | 9cda800 | 2013-01-07 13:08:40 +0000 | [diff] [blame] | 1156 | "static SomeClass WithALoooooooooooooooooooongName = {\n" |
Manuel Klimek | c8c8a47 | 2013-01-10 15:58:26 +0000 | [diff] [blame] | 1157 | " 100000000, \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n" |
| 1158 | "};"); |
| 1159 | |
| 1160 | verifyFormat( |
| 1161 | "static SomeClass = { a, b, c, d, e, f, g, h, i, j,\n" |
| 1162 | " looooooooooooooooooooooooooooooooooongname,\n" |
| 1163 | " looooooooooooooooooooooooooooooong };"); |
Daniel Jasper | f343cab | 2013-01-31 14:59:26 +0000 | [diff] [blame] | 1164 | // Allow bin-packing in static initializers as this would often lead to |
| 1165 | // terrible results, e.g.: |
| 1166 | verifyGoogleFormat( |
| 1167 | "static SomeClass = { a, b, c, d, e, f, g, h, i, j,\n" |
| 1168 | " looooooooooooooooooooooooooooooooooongname,\n" |
| 1169 | " looooooooooooooooooooooooooooooong };"); |
Daniel Jasper | 05b1ac8 | 2012-12-17 11:29:41 +0000 | [diff] [blame] | 1170 | } |
| 1171 | |
Manuel Klimek | 2c7739e | 2013-01-14 16:41:43 +0000 | [diff] [blame] | 1172 | TEST_F(FormatTest, NestedStaticInitializers) { |
| 1173 | verifyFormat("static A x = { { {} } };\n"); |
Daniel Jasper | 66d19bd | 2013-02-18 11:59:17 +0000 | [diff] [blame] | 1174 | verifyFormat("static A x = { { { init1, init2, init3, init4 },\n" |
| 1175 | " { init1, init2, init3, init4 } } };"); |
Daniel Jasper | 0df6acd | 2013-01-16 14:59:02 +0000 | [diff] [blame] | 1176 | |
Daniel Jasper | 66d19bd | 2013-02-18 11:59:17 +0000 | [diff] [blame] | 1177 | verifyFormat("somes Status::global_reps[3] = {\n" |
| 1178 | " { kGlobalRef, OK_CODE, NULL, NULL, NULL },\n" |
| 1179 | " { kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL },\n" |
| 1180 | " { kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL }\n" |
| 1181 | "};"); |
| 1182 | verifyGoogleFormat("somes Status::global_reps[3] = {\n" |
| 1183 | " { kGlobalRef, OK_CODE, NULL, NULL, NULL },\n" |
| 1184 | " { kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL },\n" |
| 1185 | " { kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL }\n" |
| 1186 | "};"); |
Manuel Klimek | 2c7739e | 2013-01-14 16:41:43 +0000 | [diff] [blame] | 1187 | verifyFormat( |
| 1188 | "CGRect cg_rect = { { rect.fLeft, rect.fTop },\n" |
| 1189 | " { rect.fRight - rect.fLeft, rect.fBottom - rect.fTop" |
| 1190 | " } };"); |
| 1191 | |
Daniel Jasper | f343cab | 2013-01-31 14:59:26 +0000 | [diff] [blame] | 1192 | verifyFormat( |
| 1193 | "SomeArrayOfSomeType a = { { { 1, 2, 3 }, { 1, 2, 3 },\n" |
| 1194 | " { 111111111111111111111111111111,\n" |
| 1195 | " 222222222222222222222222222222,\n" |
| 1196 | " 333333333333333333333333333333 },\n" |
| 1197 | " { 1, 2, 3 }, { 1, 2, 3 } } };"); |
Daniel Jasper | 6bd0232 | 2013-02-03 18:07:15 +0000 | [diff] [blame] | 1198 | verifyFormat( |
| 1199 | "SomeArrayOfSomeType a = { { { 1, 2, 3 } }, { { 1, 2, 3 } },\n" |
| 1200 | " { { 111111111111111111111111111111,\n" |
| 1201 | " 222222222222222222222222222222,\n" |
| 1202 | " 333333333333333333333333333333 } },\n" |
| 1203 | " { { 1, 2, 3 } }, { { 1, 2, 3 } } };"); |
Daniel Jasper | f343cab | 2013-01-31 14:59:26 +0000 | [diff] [blame] | 1204 | |
Nico Weber | 6a21a55 | 2013-01-18 02:43:57 +0000 | [diff] [blame] | 1205 | // 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] | 1206 | // 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] | 1207 | verifyFormat( |
| 1208 | "struct {\n" |
| 1209 | " unsigned bit;\n" |
| 1210 | " const char *const name;\n" |
| 1211 | "} kBitsToOs[] = { { kOsMac, \"Mac\" }, { kOsWin, \"Windows\" },\n" |
| 1212 | " { kOsLinux, \"Linux\" }, { kOsCrOS, \"Chrome OS\" } };"); |
Manuel Klimek | 2c7739e | 2013-01-14 16:41:43 +0000 | [diff] [blame] | 1213 | } |
| 1214 | |
Manuel Klimek | a080a18 | 2013-01-02 16:30:12 +0000 | [diff] [blame] | 1215 | TEST_F(FormatTest, FormatsSmallMacroDefinitionsInSingleLine) { |
| 1216 | verifyFormat("#define ALooooooooooooooooooooooooooooooooooooooongMacro(" |
| 1217 | " \\\n" |
| 1218 | " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)"); |
| 1219 | } |
| 1220 | |
Daniel Jasper | 7160751 | 2013-01-07 10:48:50 +0000 | [diff] [blame] | 1221 | TEST_F(FormatTest, DoesNotBreakPureVirtualFunctionDefinition) { |
Daniel Jasper | 8ed9f2b | 2013-04-03 13:36:17 +0000 | [diff] [blame] | 1222 | verifyFormat( |
Daniel Jasper | 1407bee | 2013-04-11 14:29:13 +0000 | [diff] [blame] | 1223 | "virtual void write(ELFWriter *writerrr,\n" |
| 1224 | " OwningPtr<FileOutputBuffer> &buffer) = 0;"); |
Daniel Jasper | 7160751 | 2013-01-07 10:48:50 +0000 | [diff] [blame] | 1225 | } |
| 1226 | |
Manuel Klimek | bd04f2a | 2013-01-31 15:58:48 +0000 | [diff] [blame] | 1227 | TEST_F(FormatTest, LayoutUnknownPPDirective) { |
| 1228 | EXPECT_EQ("#123 \"A string literal\"", |
| 1229 | format(" # 123 \"A string literal\"")); |
| 1230 | EXPECT_EQ("#;", format("#;")); |
Manuel Klimek | dd5b101 | 2013-01-07 10:03:37 +0000 | [diff] [blame] | 1231 | verifyFormat("#\n;\n;\n;"); |
Manuel Klimek | a080a18 | 2013-01-02 16:30:12 +0000 | [diff] [blame] | 1232 | } |
| 1233 | |
| 1234 | TEST_F(FormatTest, UnescapedEndOfLineEndsPPDirective) { |
| 1235 | EXPECT_EQ("#line 42 \"test\"\n", |
| 1236 | format("# \\\n line \\\n 42 \\\n \"test\"\n")); |
Daniel Jasper | 66d19bd | 2013-02-18 11:59:17 +0000 | [diff] [blame] | 1237 | EXPECT_EQ("#define A B\n", format("# \\\n define \\\n A \\\n B\n", |
| 1238 | getLLVMStyleWithColumns(12))); |
Manuel Klimek | a080a18 | 2013-01-02 16:30:12 +0000 | [diff] [blame] | 1239 | } |
| 1240 | |
| 1241 | TEST_F(FormatTest, EndOfFileEndsPPDirective) { |
| 1242 | EXPECT_EQ("#line 42 \"test\"", |
| 1243 | format("# \\\n line \\\n 42 \\\n \"test\"")); |
Daniel Jasper | 66d19bd | 2013-02-18 11:59:17 +0000 | [diff] [blame] | 1244 | EXPECT_EQ("#define A B", format("# \\\n define \\\n A \\\n B")); |
Manuel Klimek | a080a18 | 2013-01-02 16:30:12 +0000 | [diff] [blame] | 1245 | } |
| 1246 | |
Manuel Klimek | 060143e | 2013-01-02 18:33:23 +0000 | [diff] [blame] | 1247 | TEST_F(FormatTest, IndentsPPDirectiveInReducedSpace) { |
Daniel Jasper | a4d4621 | 2013-02-28 11:05:57 +0000 | [diff] [blame] | 1248 | verifyFormat("#define A(BB)", getLLVMStyleWithColumns(13)); |
| 1249 | verifyFormat("#define A( \\\n BB)", getLLVMStyleWithColumns(12)); |
Manuel Klimek | 060143e | 2013-01-02 18:33:23 +0000 | [diff] [blame] | 1250 | verifyFormat("#define A( \\\n A, B)", getLLVMStyleWithColumns(12)); |
Daniel Jasper | a4d4621 | 2013-02-28 11:05:57 +0000 | [diff] [blame] | 1251 | // FIXME: We never break before the macro name. |
| 1252 | verifyFormat("#define AA(\\\n B)", getLLVMStyleWithColumns(12)); |
Daniel Jasper | e0b15ea | 2013-01-14 15:40:57 +0000 | [diff] [blame] | 1253 | |
| 1254 | verifyFormat("#define A A\n#define A A"); |
| 1255 | verifyFormat("#define A(X) A\n#define A A"); |
| 1256 | |
Daniel Jasper | a4d4621 | 2013-02-28 11:05:57 +0000 | [diff] [blame] | 1257 | verifyFormat("#define Something Other", getLLVMStyleWithColumns(23)); |
| 1258 | verifyFormat("#define Something \\\n Other", getLLVMStyleWithColumns(22)); |
Manuel Klimek | 060143e | 2013-01-02 18:33:23 +0000 | [diff] [blame] | 1259 | } |
| 1260 | |
| 1261 | TEST_F(FormatTest, HandlePreprocessorDirectiveContext) { |
Alexander Kornienko | 9fdc00a | 2013-03-28 18:40:55 +0000 | [diff] [blame] | 1262 | EXPECT_EQ("// somecomment\n" |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 1263 | "#include \"a.h\"\n" |
Daniel Jasper | a4d4621 | 2013-02-28 11:05:57 +0000 | [diff] [blame] | 1264 | "#define A( \\\n" |
| 1265 | " A, B)\n" |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 1266 | "#include \"b.h\"\n" |
Alexander Kornienko | 5262dd9 | 2013-03-27 11:52:18 +0000 | [diff] [blame] | 1267 | "// somecomment\n", |
Alexander Kornienko | 9fdc00a | 2013-03-28 18:40:55 +0000 | [diff] [blame] | 1268 | format(" // somecomment\n" |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 1269 | " #include \"a.h\"\n" |
| 1270 | "#define A(A,\\\n" |
| 1271 | " B)\n" |
| 1272 | " #include \"b.h\"\n" |
Alexander Kornienko | 5262dd9 | 2013-03-27 11:52:18 +0000 | [diff] [blame] | 1273 | " // somecomment\n", |
Daniel Jasper | 66d19bd | 2013-02-18 11:59:17 +0000 | [diff] [blame] | 1274 | getLLVMStyleWithColumns(13))); |
Manuel Klimek | 060143e | 2013-01-02 18:33:23 +0000 | [diff] [blame] | 1275 | } |
| 1276 | |
Daniel Jasper | 66d19bd | 2013-02-18 11:59:17 +0000 | [diff] [blame] | 1277 | TEST_F(FormatTest, LayoutSingleHash) { EXPECT_EQ("#\na;", format("#\na;")); } |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 1278 | |
| 1279 | TEST_F(FormatTest, LayoutCodeInMacroDefinitions) { |
| 1280 | EXPECT_EQ("#define A \\\n" |
| 1281 | " c; \\\n" |
| 1282 | " e;\n" |
Daniel Jasper | 66d19bd | 2013-02-18 11:59:17 +0000 | [diff] [blame] | 1283 | "f;", |
| 1284 | format("#define A c; e;\n" |
| 1285 | "f;", |
| 1286 | getLLVMStyleWithColumns(14))); |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 1287 | } |
| 1288 | |
Daniel Jasper | 66d19bd | 2013-02-18 11:59:17 +0000 | [diff] [blame] | 1289 | TEST_F(FormatTest, LayoutRemainingTokens) { EXPECT_EQ("{}", format("{}")); } |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 1290 | |
| 1291 | TEST_F(FormatTest, LayoutSingleUnwrappedLineInMacro) { |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 1292 | EXPECT_EQ("# define A\\\n b;", |
| 1293 | format("# define A b;", 11, 2, getLLVMStyleWithColumns(11))); |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 1294 | } |
| 1295 | |
| 1296 | TEST_F(FormatTest, MacroDefinitionInsideStatement) { |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 1297 | EXPECT_EQ("int x,\n" |
| 1298 | "#define A\n" |
Daniel Jasper | 66d19bd | 2013-02-18 11:59:17 +0000 | [diff] [blame] | 1299 | " y;", |
| 1300 | format("int x,\n#define A\ny;")); |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 1301 | } |
| 1302 | |
Manuel Klimek | 6f8424b | 2013-01-05 21:34:55 +0000 | [diff] [blame] | 1303 | TEST_F(FormatTest, HashInMacroDefinition) { |
| 1304 | verifyFormat("#define A \\\n b #c;", getLLVMStyleWithColumns(11)); |
| 1305 | verifyFormat("#define A \\\n" |
| 1306 | " { \\\n" |
| 1307 | " f(#c);\\\n" |
Daniel Jasper | 66d19bd | 2013-02-18 11:59:17 +0000 | [diff] [blame] | 1308 | " }", |
| 1309 | getLLVMStyleWithColumns(11)); |
Daniel Jasper | 765561f | 2013-01-08 16:17:54 +0000 | [diff] [blame] | 1310 | |
| 1311 | verifyFormat("#define A(X) \\\n" |
Daniel Jasper | 66d19bd | 2013-02-18 11:59:17 +0000 | [diff] [blame] | 1312 | " void function##X()", |
| 1313 | getLLVMStyleWithColumns(22)); |
Daniel Jasper | 765561f | 2013-01-08 16:17:54 +0000 | [diff] [blame] | 1314 | |
| 1315 | verifyFormat("#define A(a, b, c) \\\n" |
Daniel Jasper | 66d19bd | 2013-02-18 11:59:17 +0000 | [diff] [blame] | 1316 | " void a##b##c()", |
| 1317 | getLLVMStyleWithColumns(22)); |
Daniel Jasper | 765561f | 2013-01-08 16:17:54 +0000 | [diff] [blame] | 1318 | |
Daniel Jasper | e0b15ea | 2013-01-14 15:40:57 +0000 | [diff] [blame] | 1319 | verifyFormat("#define A void # ## #", getLLVMStyleWithColumns(22)); |
Manuel Klimek | 6f8424b | 2013-01-05 21:34:55 +0000 | [diff] [blame] | 1320 | } |
| 1321 | |
Manuel Klimek | 7ccbc21 | 2013-01-23 14:37:36 +0000 | [diff] [blame] | 1322 | TEST_F(FormatTest, RespectWhitespaceInMacroDefinitions) { |
| 1323 | verifyFormat("#define A (1)"); |
| 1324 | } |
| 1325 | |
Manuel Klimek | a28fc06 | 2013-02-11 12:33:24 +0000 | [diff] [blame] | 1326 | TEST_F(FormatTest, EmptyLinesInMacroDefinitions) { |
| 1327 | EXPECT_EQ("#define A b;", format("#define A \\\n" |
| 1328 | " \\\n" |
Daniel Jasper | 66d19bd | 2013-02-18 11:59:17 +0000 | [diff] [blame] | 1329 | " b;", |
| 1330 | getLLVMStyleWithColumns(25))); |
Manuel Klimek | a28fc06 | 2013-02-11 12:33:24 +0000 | [diff] [blame] | 1331 | EXPECT_EQ("#define A \\\n" |
| 1332 | " \\\n" |
| 1333 | " a; \\\n" |
Daniel Jasper | 66d19bd | 2013-02-18 11:59:17 +0000 | [diff] [blame] | 1334 | " b;", |
| 1335 | format("#define A \\\n" |
| 1336 | " \\\n" |
| 1337 | " a; \\\n" |
| 1338 | " b;", |
| 1339 | getLLVMStyleWithColumns(11))); |
Manuel Klimek | a28fc06 | 2013-02-11 12:33:24 +0000 | [diff] [blame] | 1340 | EXPECT_EQ("#define A \\\n" |
| 1341 | " a; \\\n" |
| 1342 | " \\\n" |
Daniel Jasper | 66d19bd | 2013-02-18 11:59:17 +0000 | [diff] [blame] | 1343 | " b;", |
| 1344 | format("#define A \\\n" |
| 1345 | " a; \\\n" |
| 1346 | " \\\n" |
| 1347 | " b;", |
| 1348 | getLLVMStyleWithColumns(11))); |
Manuel Klimek | a28fc06 | 2013-02-11 12:33:24 +0000 | [diff] [blame] | 1349 | } |
| 1350 | |
Daniel Jasper | 34511fb | 2013-02-19 17:14:38 +0000 | [diff] [blame] | 1351 | TEST_F(FormatTest, MacroDefinitionsWithIncompleteCode) { |
Daniel Jasper | cf6d76a | 2013-03-18 12:50:26 +0000 | [diff] [blame] | 1352 | verifyFormat("#define A :"); |
| 1353 | |
Daniel Jasper | 34511fb | 2013-02-19 17:14:38 +0000 | [diff] [blame] | 1354 | // FIXME: Improve formatting of case labels in macros. |
| 1355 | verifyFormat("#define SOMECASES \\\n" |
Daniel Jasper | bcca7e4 | 2013-03-20 10:23:53 +0000 | [diff] [blame] | 1356 | " case 1: \\\n" |
Daniel Jasper | 34511fb | 2013-02-19 17:14:38 +0000 | [diff] [blame] | 1357 | " case 2\n", |
| 1358 | getLLVMStyleWithColumns(20)); |
| 1359 | |
| 1360 | verifyFormat("#define A template <typename T>"); |
Daniel Jasper | 03628b8 | 2013-02-19 20:05:41 +0000 | [diff] [blame] | 1361 | verifyFormat("#define STR(x) #x\n" |
| 1362 | "f(STR(this_is_a_string_literal{));"); |
Daniel Jasper | 34511fb | 2013-02-19 17:14:38 +0000 | [diff] [blame] | 1363 | } |
| 1364 | |
Alexander Kornienko | 3d713a7 | 2013-04-08 22:16:06 +0000 | [diff] [blame] | 1365 | TEST_F(FormatTest, MacroCallsWithoutTrailingSemicolon) { |
| 1366 | EXPECT_EQ("INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n" |
| 1367 | "INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n" |
| 1368 | "INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n" |
| 1369 | "class X {\n" |
| 1370 | "};\n" |
| 1371 | "INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n" |
| 1372 | "int *createScopDetectionPass() { return 0; }", |
| 1373 | format(" INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n" |
| 1374 | " INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n" |
| 1375 | " INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n" |
| 1376 | " class X {};\n" |
| 1377 | " INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n" |
| 1378 | " int *createScopDetectionPass() { return 0; }")); |
Alexander Kornienko | 99b0e14 | 2013-04-09 16:15:19 +0000 | [diff] [blame] | 1379 | // FIXME: We could probably treat IPC_BEGIN_MESSAGE_MAP/IPC_END_MESSAGE_MAP as |
| 1380 | // braces, so that inner block is indented one level more. |
| 1381 | EXPECT_EQ("int q() {\n" |
| 1382 | " IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n" |
| 1383 | " IPC_MESSAGE_HANDLER(xxx, qqq)\n" |
| 1384 | " IPC_END_MESSAGE_MAP()\n" |
| 1385 | "}", |
| 1386 | format("int q() {\n" |
| 1387 | " IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n" |
| 1388 | " IPC_MESSAGE_HANDLER(xxx, qqq)\n" |
| 1389 | " IPC_END_MESSAGE_MAP()\n" |
| 1390 | "}")); |
| 1391 | EXPECT_EQ("int q() {\n" |
| 1392 | " f(x);\n" |
| 1393 | " f(x) {}\n" |
| 1394 | " f(x)->g();\n" |
| 1395 | " f(x)->*g();\n" |
| 1396 | " f(x).g();\n" |
| 1397 | " f(x) = x;\n" |
| 1398 | " f(x) += x;\n" |
| 1399 | " f(x) -= x;\n" |
| 1400 | " f(x) *= x;\n" |
| 1401 | " f(x) /= x;\n" |
| 1402 | " f(x) %= x;\n" |
| 1403 | " f(x) &= x;\n" |
| 1404 | " f(x) |= x;\n" |
| 1405 | " f(x) ^= x;\n" |
| 1406 | " f(x) >>= x;\n" |
| 1407 | " f(x) <<= x;\n" |
| 1408 | " f(x)[y].z();\n" |
| 1409 | " LOG(INFO) << x;\n" |
| 1410 | " ifstream(x) >> x;\n" |
| 1411 | "}\n", |
| 1412 | format("int q() {\n" |
| 1413 | " f(x)\n;\n" |
| 1414 | " f(x)\n {}\n" |
| 1415 | " f(x)\n->g();\n" |
| 1416 | " f(x)\n->*g();\n" |
| 1417 | " f(x)\n.g();\n" |
| 1418 | " f(x)\n = x;\n" |
| 1419 | " f(x)\n += x;\n" |
| 1420 | " f(x)\n -= x;\n" |
| 1421 | " f(x)\n *= x;\n" |
| 1422 | " f(x)\n /= x;\n" |
| 1423 | " f(x)\n %= x;\n" |
| 1424 | " f(x)\n &= x;\n" |
| 1425 | " f(x)\n |= x;\n" |
| 1426 | " f(x)\n ^= x;\n" |
| 1427 | " f(x)\n >>= x;\n" |
| 1428 | " f(x)\n <<= x;\n" |
| 1429 | " f(x)\n[y].z();\n" |
| 1430 | " LOG(INFO)\n << x;\n" |
| 1431 | " ifstream(x)\n >> x;\n" |
| 1432 | "}\n")); |
| 1433 | EXPECT_EQ("int q() {\n" |
| 1434 | " f(x)\n" |
| 1435 | " if (1) {\n" |
| 1436 | " }\n" |
| 1437 | " f(x)\n" |
| 1438 | " while (1) {\n" |
| 1439 | " }\n" |
| 1440 | " f(x)\n" |
| 1441 | " g(x);\n" |
| 1442 | " f(x)\n" |
| 1443 | " try {\n" |
| 1444 | " q();\n" |
| 1445 | " }\n" |
| 1446 | " catch (...) {\n" |
| 1447 | " }\n" |
| 1448 | "}\n", |
| 1449 | format("int q() {\n" |
| 1450 | "f(x)\n" |
| 1451 | "if (1) {}\n" |
| 1452 | "f(x)\n" |
| 1453 | "while (1) {}\n" |
| 1454 | "f(x)\n" |
| 1455 | "g(x);\n" |
| 1456 | "f(x)\n" |
| 1457 | "try { q(); } catch (...) {}\n" |
| 1458 | "}\n")); |
| 1459 | EXPECT_EQ("class A {\n" |
| 1460 | " A() : t(0) {}\n" |
| 1461 | " A(X x)\n" // FIXME: function-level try blocks are broken. |
| 1462 | " try : t(0) {\n" |
| 1463 | " }\n" |
| 1464 | " catch (...) {\n" |
| 1465 | " }\n" |
| 1466 | "};", |
| 1467 | format("class A {\n" |
| 1468 | " A()\n : t(0) {}\n" |
| 1469 | " A(X x)\n" |
| 1470 | " try : t(0) {} catch (...) {}\n" |
| 1471 | "};")); |
Alexander Kornienko | 3d713a7 | 2013-04-08 22:16:06 +0000 | [diff] [blame] | 1472 | } |
| 1473 | |
Manuel Klimek | c37b4d6 | 2013-01-05 22:14:16 +0000 | [diff] [blame] | 1474 | TEST_F(FormatTest, IndentPreprocessorDirectivesAtZero) { |
| 1475 | EXPECT_EQ("{\n {\n#define A\n }\n}", format("{{\n#define A\n}}")); |
| 1476 | } |
| 1477 | |
Manuel Klimek | f6fd00b | 2013-01-05 22:56:06 +0000 | [diff] [blame] | 1478 | TEST_F(FormatTest, FormatHashIfNotAtStartOfLine) { |
Manuel Klimek | 517e894 | 2013-01-11 17:54:10 +0000 | [diff] [blame] | 1479 | verifyFormat("{\n { a #c; }\n}"); |
Manuel Klimek | f6fd00b | 2013-01-05 22:56:06 +0000 | [diff] [blame] | 1480 | } |
| 1481 | |
Manuel Klimek | a5342db | 2013-01-06 20:07:31 +0000 | [diff] [blame] | 1482 | TEST_F(FormatTest, FormatUnbalancedStructuralElements) { |
| 1483 | EXPECT_EQ("#define A \\\n { \\\n {\nint i;", |
| 1484 | format("#define A { {\nint i;", getLLVMStyleWithColumns(11))); |
| 1485 | EXPECT_EQ("#define A \\\n } \\\n }\nint i;", |
| 1486 | format("#define A } }\nint i;", getLLVMStyleWithColumns(11))); |
| 1487 | } |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 1488 | |
Manuel Klimek | 9541938 | 2013-01-07 07:56:50 +0000 | [diff] [blame] | 1489 | TEST_F(FormatTest, EscapedNewlineAtStartOfTokenInMacroDefinition) { |
Daniel Jasper | cd16238 | 2013-01-07 13:26:07 +0000 | [diff] [blame] | 1490 | EXPECT_EQ( |
| 1491 | "#define A \\\n int i; \\\n int j;", |
| 1492 | format("#define A \\\nint i;\\\n int j;", getLLVMStyleWithColumns(11))); |
Manuel Klimek | 9541938 | 2013-01-07 07:56:50 +0000 | [diff] [blame] | 1493 | } |
| 1494 | |
Manuel Klimek | d544c57 | 2013-01-07 09:24:17 +0000 | [diff] [blame] | 1495 | TEST_F(FormatTest, CalculateSpaceOnConsecutiveLinesInMacro) { |
| 1496 | verifyFormat("#define A \\\n" |
| 1497 | " int v( \\\n" |
| 1498 | " a); \\\n" |
Daniel Jasper | 66d19bd | 2013-02-18 11:59:17 +0000 | [diff] [blame] | 1499 | " int i;", |
| 1500 | getLLVMStyleWithColumns(11)); |
Manuel Klimek | d544c57 | 2013-01-07 09:24:17 +0000 | [diff] [blame] | 1501 | } |
| 1502 | |
Manuel Klimek | a080a18 | 2013-01-02 16:30:12 +0000 | [diff] [blame] | 1503 | TEST_F(FormatTest, MixingPreprocessorDirectivesAndNormalCode) { |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 1504 | EXPECT_EQ( |
| 1505 | "#define ALooooooooooooooooooooooooooooooooooooooongMacro(" |
| 1506 | " \\\n" |
| 1507 | " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n" |
| 1508 | "\n" |
| 1509 | "AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n" |
| 1510 | " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n", |
| 1511 | format(" #define ALooooooooooooooooooooooooooooooooooooooongMacro(" |
| 1512 | "\\\n" |
| 1513 | "aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n" |
| 1514 | " \n" |
| 1515 | " AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n" |
| 1516 | " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n")); |
Manuel Klimek | a080a18 | 2013-01-02 16:30:12 +0000 | [diff] [blame] | 1517 | } |
| 1518 | |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 1519 | TEST_F(FormatTest, LayoutStatementsAroundPreprocessorDirectives) { |
| 1520 | EXPECT_EQ("int\n" |
| 1521 | "#define A\n" |
Daniel Jasper | 8ed9f2b | 2013-04-03 13:36:17 +0000 | [diff] [blame] | 1522 | "a;", |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 1523 | format("int\n#define A\na;")); |
Daniel Jasper | 66d19bd | 2013-02-18 11:59:17 +0000 | [diff] [blame] | 1524 | verifyFormat("functionCallTo(\n" |
| 1525 | " someOtherFunction(\n" |
| 1526 | " withSomeParameters, whichInSequence,\n" |
| 1527 | " areLongerThanALine(andAnotherCall,\n" |
| 1528 | "#define A B\n" |
| 1529 | " withMoreParamters,\n" |
| 1530 | " whichStronglyInfluenceTheLayout),\n" |
Daniel Jasper | 7d81281 | 2013-02-21 15:00:29 +0000 | [diff] [blame] | 1531 | " andMoreParameters),\n" |
| 1532 | " trailing);", |
Daniel Jasper | 66d19bd | 2013-02-18 11:59:17 +0000 | [diff] [blame] | 1533 | getLLVMStyleWithColumns(69)); |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 1534 | } |
| 1535 | |
Manuel Klimek | bb42bf1 | 2013-01-10 11:52:21 +0000 | [diff] [blame] | 1536 | TEST_F(FormatTest, LayoutBlockInsideParens) { |
| 1537 | EXPECT_EQ("functionCall({\n" |
| 1538 | " int i;\n" |
Daniel Jasper | 66d19bd | 2013-02-18 11:59:17 +0000 | [diff] [blame] | 1539 | "});", |
| 1540 | format(" functionCall ( {int i;} );")); |
Manuel Klimek | bb42bf1 | 2013-01-10 11:52:21 +0000 | [diff] [blame] | 1541 | } |
| 1542 | |
| 1543 | TEST_F(FormatTest, LayoutBlockInsideStatement) { |
Manuel Klimek | 517e894 | 2013-01-11 17:54:10 +0000 | [diff] [blame] | 1544 | EXPECT_EQ("SOME_MACRO { int i; }\n" |
Daniel Jasper | 66d19bd | 2013-02-18 11:59:17 +0000 | [diff] [blame] | 1545 | "int i;", |
| 1546 | format(" SOME_MACRO {int i;} int i;")); |
Manuel Klimek | bb42bf1 | 2013-01-10 11:52:21 +0000 | [diff] [blame] | 1547 | } |
| 1548 | |
| 1549 | TEST_F(FormatTest, LayoutNestedBlocks) { |
| 1550 | verifyFormat("void AddOsStrings(unsigned bitmask) {\n" |
| 1551 | " struct s {\n" |
| 1552 | " int i;\n" |
| 1553 | " };\n" |
| 1554 | " s kBitsToOs[] = { { 10 } };\n" |
| 1555 | " for (int i = 0; i < 10; ++i)\n" |
| 1556 | " return;\n" |
| 1557 | "}"); |
| 1558 | } |
| 1559 | |
Manuel Klimek | 36fab8d | 2013-01-10 13:24:24 +0000 | [diff] [blame] | 1560 | TEST_F(FormatTest, PutEmptyBlocksIntoOneLine) { |
| 1561 | EXPECT_EQ("{}", format("{}")); |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 1562 | |
| 1563 | // Negative test for enum. |
| 1564 | verifyFormat("enum E {\n};"); |
| 1565 | |
| 1566 | // Note that when there's a missing ';', we still join... |
| 1567 | verifyFormat("enum E {}"); |
Manuel Klimek | 36fab8d | 2013-01-10 13:24:24 +0000 | [diff] [blame] | 1568 | } |
| 1569 | |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 1570 | //===----------------------------------------------------------------------===// |
| 1571 | // Line break tests. |
| 1572 | //===----------------------------------------------------------------------===// |
| 1573 | |
| 1574 | TEST_F(FormatTest, FormatsFunctionDefinition) { |
| 1575 | verifyFormat("void f(int a, int b, int c, int d, int e, int f, int g," |
| 1576 | " int h, int j, int f,\n" |
Daniel Jasper | f11a705 | 2013-02-21 21:33:55 +0000 | [diff] [blame] | 1577 | " int c, int ddddddddddddd) {}"); |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 1578 | } |
| 1579 | |
| 1580 | TEST_F(FormatTest, FormatsAwesomeMethodCall) { |
| 1581 | verifyFormat( |
Daniel Jasper | 986e17f | 2013-01-28 07:35:34 +0000 | [diff] [blame] | 1582 | "SomeLongMethodName(SomeReallyLongMethod(CallOtherReallyLongMethod(\n" |
| 1583 | " parameter, parameter, parameter)),\n" |
Daniel Jasper | a4974cf | 2012-12-24 16:43:00 +0000 | [diff] [blame] | 1584 | " SecondLongCall(parameter));"); |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 1585 | } |
| 1586 | |
Daniel Jasper | ae8699b | 2013-01-28 09:35:24 +0000 | [diff] [blame] | 1587 | TEST_F(FormatTest, PreventConfusingIndents) { |
Daniel Jasper | 986e17f | 2013-01-28 07:35:34 +0000 | [diff] [blame] | 1588 | verifyFormat( |
| 1589 | "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" |
| 1590 | " aaaaaaaaaaaaaaaaaaaaaaaa(\n" |
| 1591 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" |
| 1592 | " aaaaaaaaaaaaaaaaaaaaaaaa);"); |
| 1593 | verifyFormat( |
| 1594 | "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[\n" |
| 1595 | " aaaaaaaaaaaaaaaaaaaaaaaa[\n" |
| 1596 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa],\n" |
| 1597 | " aaaaaaaaaaaaaaaaaaaaaaaa];"); |
| 1598 | verifyFormat( |
| 1599 | "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n" |
| 1600 | " aaaaaaaaaaaaaaaaaaaaaaaa<\n" |
| 1601 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>,\n" |
| 1602 | " aaaaaaaaaaaaaaaaaaaaaaaa>;"); |
Daniel Jasper | ae8699b | 2013-01-28 09:35:24 +0000 | [diff] [blame] | 1603 | verifyFormat("int a = bbbb && ccc && fffff(\n" |
| 1604 | "#define A Just forcing a new line\n" |
| 1605 | " ddd);"); |
Daniel Jasper | 986e17f | 2013-01-28 07:35:34 +0000 | [diff] [blame] | 1606 | } |
| 1607 | |
Daniel Jasper | bf71ba2 | 2013-04-08 20:33:42 +0000 | [diff] [blame] | 1608 | TEST_F(FormatTest, ExpressionIndentation) { |
| 1609 | verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" |
| 1610 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" |
| 1611 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" |
| 1612 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" |
| 1613 | " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb +\n" |
| 1614 | " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb &&\n" |
| 1615 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" |
| 1616 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >\n" |
| 1617 | " ccccccccccccccccccccccccccccccccccccccccc;"); |
| 1618 | verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" |
| 1619 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" |
| 1620 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" |
| 1621 | " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}"); |
| 1622 | verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" |
| 1623 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" |
| 1624 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" |
| 1625 | " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}"); |
| 1626 | verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n" |
| 1627 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" |
| 1628 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n" |
| 1629 | " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}"); |
| 1630 | } |
| 1631 | |
Daniel Jasper | 1321eb5 | 2012-12-18 21:05:13 +0000 | [diff] [blame] | 1632 | TEST_F(FormatTest, ConstructorInitializers) { |
Manuel Klimek | 36fab8d | 2013-01-10 13:24:24 +0000 | [diff] [blame] | 1633 | verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}"); |
Daniel Jasper | 7e9bf8c | 2013-01-11 11:37:55 +0000 | [diff] [blame] | 1634 | verifyFormat("Constructor() : Inttializer(FitsOnTheLine) {}", |
| 1635 | getLLVMStyleWithColumns(45)); |
Daniel Jasper | f11a705 | 2013-02-21 21:33:55 +0000 | [diff] [blame] | 1636 | verifyFormat("Constructor()\n" |
| 1637 | " : Inttializer(FitsOnTheLine) {}", |
Daniel Jasper | 7e9bf8c | 2013-01-11 11:37:55 +0000 | [diff] [blame] | 1638 | getLLVMStyleWithColumns(44)); |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 1639 | verifyFormat("Constructor()\n" |
Daniel Jasper | f11a705 | 2013-02-21 21:33:55 +0000 | [diff] [blame] | 1640 | " : Inttializer(FitsOnTheLine) {}", |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 1641 | getLLVMStyleWithColumns(43)); |
Daniel Jasper | 1321eb5 | 2012-12-18 21:05:13 +0000 | [diff] [blame] | 1642 | |
| 1643 | verifyFormat( |
| 1644 | "SomeClass::Constructor()\n" |
Daniel Jasper | f11a705 | 2013-02-21 21:33:55 +0000 | [diff] [blame] | 1645 | " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}"); |
Daniel Jasper | 1321eb5 | 2012-12-18 21:05:13 +0000 | [diff] [blame] | 1646 | |
| 1647 | verifyFormat( |
| 1648 | "SomeClass::Constructor()\n" |
Daniel Jasper | 7e9bf8c | 2013-01-11 11:37:55 +0000 | [diff] [blame] | 1649 | " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" |
Daniel Jasper | f11a705 | 2013-02-21 21:33:55 +0000 | [diff] [blame] | 1650 | " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}"); |
Daniel Jasper | 7e9bf8c | 2013-01-11 11:37:55 +0000 | [diff] [blame] | 1651 | verifyFormat( |
| 1652 | "SomeClass::Constructor()\n" |
Daniel Jasper | 1321eb5 | 2012-12-18 21:05:13 +0000 | [diff] [blame] | 1653 | " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" |
Daniel Jasper | f11a705 | 2013-02-21 21:33:55 +0000 | [diff] [blame] | 1654 | " aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}"); |
Daniel Jasper | 1321eb5 | 2012-12-18 21:05:13 +0000 | [diff] [blame] | 1655 | |
| 1656 | verifyFormat("Constructor()\n" |
| 1657 | " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" |
| 1658 | " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" |
| 1659 | " aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" |
Daniel Jasper | f11a705 | 2013-02-21 21:33:55 +0000 | [diff] [blame] | 1660 | " aaaaaaaaaaaaaaaaaaaaaaa() {}"); |
Daniel Jasper | 1321eb5 | 2012-12-18 21:05:13 +0000 | [diff] [blame] | 1661 | |
Daniel Jasper | 02b771e | 2013-01-28 13:31:35 +0000 | [diff] [blame] | 1662 | verifyFormat("Constructor()\n" |
| 1663 | " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" |
Daniel Jasper | f11a705 | 2013-02-21 21:33:55 +0000 | [diff] [blame] | 1664 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}"); |
Daniel Jasper | 02b771e | 2013-01-28 13:31:35 +0000 | [diff] [blame] | 1665 | |
Daniel Jasper | 9c3e71a | 2013-02-25 15:59:54 +0000 | [diff] [blame] | 1666 | verifyFormat("Constructor(int Parameter = 0)\n" |
| 1667 | " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa),\n" |
| 1668 | " aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {}"); |
| 1669 | |
Daniel Jasper | 1321eb5 | 2012-12-18 21:05:13 +0000 | [diff] [blame] | 1670 | // Here a line could be saved by splitting the second initializer onto two |
| 1671 | // lines, but that is not desireable. |
Daniel Jasper | faab0d3 | 2013-02-27 09:47:53 +0000 | [diff] [blame] | 1672 | verifyFormat("Constructor()\n" |
| 1673 | " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n" |
| 1674 | " aaaaaaaaaaa(aaaaaaaaaaa),\n" |
| 1675 | " aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}"); |
Daniel Jasper | 1321eb5 | 2012-12-18 21:05:13 +0000 | [diff] [blame] | 1676 | |
Daniel Jasper | b130a54 | 2013-02-15 16:49:44 +0000 | [diff] [blame] | 1677 | FormatStyle OnePerLine = getLLVMStyle(); |
| 1678 | OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true; |
| 1679 | verifyFormat("SomeClass::Constructor()\n" |
| 1680 | " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" |
| 1681 | " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" |
Daniel Jasper | f11a705 | 2013-02-21 21:33:55 +0000 | [diff] [blame] | 1682 | " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", |
Daniel Jasper | 66d19bd | 2013-02-18 11:59:17 +0000 | [diff] [blame] | 1683 | OnePerLine); |
Daniel Jasper | b130a54 | 2013-02-15 16:49:44 +0000 | [diff] [blame] | 1684 | verifyFormat("SomeClass::Constructor()\n" |
| 1685 | " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n" |
| 1686 | " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" |
Daniel Jasper | f11a705 | 2013-02-21 21:33:55 +0000 | [diff] [blame] | 1687 | " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", |
Daniel Jasper | 66d19bd | 2013-02-18 11:59:17 +0000 | [diff] [blame] | 1688 | OnePerLine); |
Daniel Jasper | b130a54 | 2013-02-15 16:49:44 +0000 | [diff] [blame] | 1689 | verifyFormat("MyClass::MyClass(int var)\n" |
| 1690 | " : some_var_(var), // 4 space indent\n" |
| 1691 | " some_other_var_(var + 1) { // lined up\n" |
Daniel Jasper | 66d19bd | 2013-02-18 11:59:17 +0000 | [diff] [blame] | 1692 | "}", |
| 1693 | OnePerLine); |
Daniel Jasper | 3a39ac7 | 2013-02-28 09:39:12 +0000 | [diff] [blame] | 1694 | verifyFormat("Constructor()\n" |
| 1695 | " : aaaaa(aaaaaa),\n" |
| 1696 | " aaaaa(aaaaaa),\n" |
| 1697 | " aaaaa(aaaaaa),\n" |
| 1698 | " aaaaa(aaaaaa),\n" |
| 1699 | " aaaaa(aaaaaa) {}", |
| 1700 | OnePerLine); |
Daniel Jasper | 001bf4e | 2013-04-22 07:59:53 +0000 | [diff] [blame] | 1701 | verifyFormat("Constructor()\n" |
| 1702 | " : aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n" |
| 1703 | " aaaaaaaaaaaaaaaaaaaaaa) {}", |
| 1704 | OnePerLine); |
Daniel Jasper | fca24bc | 2013-04-25 13:31:51 +0000 | [diff] [blame] | 1705 | } |
| 1706 | |
| 1707 | TEST_F(FormatTest, MemoizationTests) { |
| 1708 | // This breaks if the memoization lookup does not take \c Indent and |
| 1709 | // \c LastSpace into account. |
| 1710 | verifyFormat( |
| 1711 | "extern CFRunLoopTimerRef\n" |
| 1712 | "CFRunLoopTimerCreate(CFAllocatorRef allocato, CFAbsoluteTime fireDate,\n" |
| 1713 | " CFTimeInterval interval, CFOptionFlags flags,\n" |
| 1714 | " CFIndex order, CFRunLoopTimerCallBack callout,\n" |
| 1715 | " CFRunLoopTimerContext *context);"); |
| 1716 | |
| 1717 | // Deep nesting somewhat works around our memoization. |
| 1718 | verifyFormat( |
| 1719 | "aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n" |
| 1720 | " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n" |
| 1721 | " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n" |
| 1722 | " aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n" |
| 1723 | " aaaaa())))))))))))))))))))))))))))))))))))))));", |
| 1724 | getLLVMStyleWithColumns(65)); |
Daniel Jasper | b312314 | 2013-01-12 07:36:22 +0000 | [diff] [blame] | 1725 | |
| 1726 | // This test takes VERY long when memoization is broken. |
Daniel Jasper | fca24bc | 2013-04-25 13:31:51 +0000 | [diff] [blame] | 1727 | FormatStyle OnePerLine = getLLVMStyle(); |
| 1728 | OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true; |
Daniel Jasper | faab0d3 | 2013-02-27 09:47:53 +0000 | [diff] [blame] | 1729 | OnePerLine.BinPackParameters = false; |
Daniel Jasper | 0df6acd | 2013-01-16 14:59:02 +0000 | [diff] [blame] | 1730 | std::string input = "Constructor()\n" |
Daniel Jasper | 3499dda | 2013-01-25 15:43:32 +0000 | [diff] [blame] | 1731 | " : aaaa(a,\n"; |
Daniel Jasper | 0df6acd | 2013-01-16 14:59:02 +0000 | [diff] [blame] | 1732 | for (unsigned i = 0, e = 80; i != e; ++i) { |
| 1733 | input += " a,\n"; |
| 1734 | } |
Daniel Jasper | f11a705 | 2013-02-21 21:33:55 +0000 | [diff] [blame] | 1735 | input += " a) {}"; |
Daniel Jasper | faab0d3 | 2013-02-27 09:47:53 +0000 | [diff] [blame] | 1736 | verifyFormat(input, OnePerLine); |
Daniel Jasper | 1321eb5 | 2012-12-18 21:05:13 +0000 | [diff] [blame] | 1737 | } |
| 1738 | |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 1739 | TEST_F(FormatTest, BreaksAsHighAsPossible) { |
| 1740 | verifyFormat( |
Alexander Kornienko | 3fd9ccd | 2013-03-12 16:28:18 +0000 | [diff] [blame] | 1741 | "void f() {\n" |
| 1742 | " if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaa && aaaaaaaaaaaaaaaaaaaaaaaaaa) ||\n" |
| 1743 | " (bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb && bbbbbbbbbbbbbbbbbbbbbbbbbb))\n" |
| 1744 | " f();\n" |
| 1745 | "}"); |
Daniel Jasper | 518ee34 | 2013-02-26 13:59:14 +0000 | [diff] [blame] | 1746 | verifyFormat("if (Intervals[i].getRange().getFirst() <\n" |
Daniel Jasper | bf71ba2 | 2013-04-08 20:33:42 +0000 | [diff] [blame] | 1747 | " Intervals[i - 1].getRange().getLast()) {\n}"); |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 1748 | } |
| 1749 | |
Daniel Jasper | 1407bee | 2013-04-11 14:29:13 +0000 | [diff] [blame] | 1750 | TEST_F(FormatTest, BreaksFunctionDeclarations) { |
| 1751 | // Principially, we break function declarations in a certain order: |
| 1752 | // 1) break amongst arguments. |
| 1753 | verifyFormat("Aaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccc,\n" |
| 1754 | " Cccccccccccccc cccccccccccccc);"); |
| 1755 | |
| 1756 | // 2) break after return type. |
| 1757 | verifyFormat("Aaaaaaaaaaaaaaaaaaaaaaaa\n" |
| 1758 | "bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccccccccccccccc);"); |
| 1759 | |
| 1760 | // 3) break after (. |
| 1761 | verifyFormat( |
| 1762 | "Aaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbb(\n" |
| 1763 | " Cccccccccccccccccccccccccccccc cccccccccccccccccccccccccccccccc);"); |
| 1764 | |
| 1765 | // 4) break before after nested name specifiers. |
| 1766 | verifyFormat( |
| 1767 | "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" |
| 1768 | "SomeClasssssssssssssssssssssssssssssssssssssss::\n" |
| 1769 | " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc);"); |
| 1770 | |
| 1771 | // However, there are exceptions, if a sufficient amount of lines can be |
| 1772 | // saved. |
| 1773 | // FIXME: The precise cut-offs wrt. the number of saved lines might need some |
| 1774 | // more adjusting. |
| 1775 | verifyFormat("Aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n" |
| 1776 | " Cccccccccccccc cccccccccc,\n" |
| 1777 | " Cccccccccccccc cccccccccc,\n" |
| 1778 | " Cccccccccccccc cccccccccc,\n" |
| 1779 | " Cccccccccccccc cccccccccc);"); |
| 1780 | verifyFormat( |
| 1781 | "Aaaaaaaaaaaaaaaaaa\n" |
| 1782 | "bbbbbbbbbbbbbb(Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" |
| 1783 | " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" |
| 1784 | " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);"); |
| 1785 | verifyFormat( |
| 1786 | "Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n" |
| 1787 | " Cccccccccccccc cccccccccc,\n" |
| 1788 | " Cccccccccccccc cccccccccc,\n" |
| 1789 | " Cccccccccccccc cccccccccc,\n" |
| 1790 | " Cccccccccccccc cccccccccc,\n" |
| 1791 | " Cccccccccccccc cccccccccc,\n" |
| 1792 | " Cccccccccccccc cccccccccc);"); |
| 1793 | verifyFormat("Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n" |
| 1794 | " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" |
| 1795 | " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" |
| 1796 | " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" |
| 1797 | " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);"); |
Daniel Jasper | 01218ff | 2013-04-15 22:36:37 +0000 | [diff] [blame] | 1798 | |
| 1799 | // Break after multi-line parameters. |
| 1800 | verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" |
| 1801 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" |
| 1802 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" |
| 1803 | " bbbb bbbb);"); |
Daniel Jasper | 1407bee | 2013-04-11 14:29:13 +0000 | [diff] [blame] | 1804 | } |
| 1805 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1806 | TEST_F(FormatTest, BreaksDesireably) { |
| 1807 | verifyFormat("if (aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n" |
| 1808 | " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n" |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 1809 | " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa)) {\n}"); |
Daniel Jasper | e438bac | 2013-01-23 20:41:06 +0000 | [diff] [blame] | 1810 | verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" |
| 1811 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n" |
| 1812 | "}"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1813 | |
| 1814 | verifyFormat( |
| 1815 | "aaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" |
Daniel Jasper | f11a705 | 2013-02-21 21:33:55 +0000 | [diff] [blame] | 1816 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1817 | |
| 1818 | verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" |
| 1819 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" |
| 1820 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));"); |
Daniel Jasper | a88bb45 | 2012-12-04 10:50:12 +0000 | [diff] [blame] | 1821 | |
| 1822 | verifyFormat( |
| 1823 | "aaaaaaaa(aaaaaaaaaaaaa, aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" |
| 1824 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n" |
| 1825 | " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" |
| 1826 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));"); |
Daniel Jasper | 33182dd | 2012-12-05 14:57:28 +0000 | [diff] [blame] | 1827 | |
| 1828 | verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" |
| 1829 | " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); |
| 1830 | |
Daniel Jasper | 723f030 | 2013-01-02 14:40:02 +0000 | [diff] [blame] | 1831 | verifyFormat( |
Alexander Kornienko | 3fd9ccd | 2013-03-12 16:28:18 +0000 | [diff] [blame] | 1832 | "void f() {\n" |
| 1833 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&\n" |
| 1834 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" |
| 1835 | "}"); |
Daniel Jasper | 3499dda | 2013-01-25 15:43:32 +0000 | [diff] [blame] | 1836 | verifyFormat( |
| 1837 | "aaaaaa(new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" |
| 1838 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));"); |
| 1839 | verifyFormat( |
| 1840 | "aaaaaa(aaa, new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" |
| 1841 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));"); |
Daniel Jasper | 64f0928 | 2013-03-20 13:53:11 +0000 | [diff] [blame] | 1842 | verifyFormat( |
| 1843 | "aaaaaaaaaaaaaaaaa(\n" |
| 1844 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" |
| 1845 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); |
Daniel Jasper | 723f030 | 2013-01-02 14:40:02 +0000 | [diff] [blame] | 1846 | |
Daniel Jasper | 33182dd | 2012-12-05 14:57:28 +0000 | [diff] [blame] | 1847 | // This test case breaks on an incorrect memoization, i.e. an optimization not |
| 1848 | // taking into account the StopAt value. |
| 1849 | verifyFormat( |
| 1850 | "return aaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n" |
Daniel Jasper | cf225b6 | 2012-12-24 13:43:52 +0000 | [diff] [blame] | 1851 | " aaaaaaaaaaa(aaaaaaaaa) || aaaaaaaaaaaaaaaaaaaaaaa ||\n" |
| 1852 | " aaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n" |
| 1853 | " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); |
Daniel Jasper | a4974cf | 2012-12-24 16:43:00 +0000 | [diff] [blame] | 1854 | |
Daniel Jasper | cd16238 | 2013-01-07 13:26:07 +0000 | [diff] [blame] | 1855 | verifyFormat("{\n {\n {\n" |
| 1856 | " Annotation.SpaceRequiredBefore =\n" |
| 1857 | " Line.Tokens[i - 1].Tok.isNot(tok::l_paren) &&\n" |
| 1858 | " Line.Tokens[i - 1].Tok.isNot(tok::l_square);\n" |
| 1859 | " }\n }\n}"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1860 | } |
| 1861 | |
Daniel Jasper | 0df6acd | 2013-01-16 14:59:02 +0000 | [diff] [blame] | 1862 | TEST_F(FormatTest, FormatsOneParameterPerLineIfNecessary) { |
Daniel Jasper | 8ed9f2b | 2013-04-03 13:36:17 +0000 | [diff] [blame] | 1863 | FormatStyle NoBinPacking = getGoogleStyle(); |
Daniel Jasper | faab0d3 | 2013-02-27 09:47:53 +0000 | [diff] [blame] | 1864 | NoBinPacking.BinPackParameters = false; |
| 1865 | verifyFormat("f(aaaaaaaaaaaaaaaaaaaa,\n" |
| 1866 | " aaaaaaaaaaaaaaaaaaaa,\n" |
| 1867 | " aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaa);", |
| 1868 | NoBinPacking); |
| 1869 | verifyFormat("aaaaaaa(aaaaaaaaaaaaa,\n" |
| 1870 | " aaaaaaaaaaaaa,\n" |
| 1871 | " aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));", |
| 1872 | NoBinPacking); |
| 1873 | verifyFormat( |
Daniel Jasper | 0df6acd | 2013-01-16 14:59:02 +0000 | [diff] [blame] | 1874 | "aaaaaaaa(aaaaaaaaaaaaa,\n" |
| 1875 | " aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" |
| 1876 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n" |
| 1877 | " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" |
Daniel Jasper | faab0d3 | 2013-02-27 09:47:53 +0000 | [diff] [blame] | 1878 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));", |
| 1879 | NoBinPacking); |
| 1880 | verifyFormat("aaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n" |
| 1881 | " .aaaaaaaaaaaaaaaaaa();", |
| 1882 | NoBinPacking); |
| 1883 | verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" |
| 1884 | " aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaa);", |
| 1885 | NoBinPacking); |
Daniel Jasper | 66d19bd | 2013-02-18 11:59:17 +0000 | [diff] [blame] | 1886 | |
Daniel Jasper | faab0d3 | 2013-02-27 09:47:53 +0000 | [diff] [blame] | 1887 | verifyFormat( |
Daniel Jasper | 8f4bd7a | 2013-01-23 10:08:28 +0000 | [diff] [blame] | 1888 | "aaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa,\n" |
| 1889 | " aaaaaaaaaaaa,\n" |
Daniel Jasper | faab0d3 | 2013-02-27 09:47:53 +0000 | [diff] [blame] | 1890 | " aaaaaaaaaaaa);", |
| 1891 | NoBinPacking); |
| 1892 | verifyFormat( |
Daniel Jasper | 0df6acd | 2013-01-16 14:59:02 +0000 | [diff] [blame] | 1893 | "somefunction(someotherFunction(ddddddddddddddddddddddddddddddddddd,\n" |
| 1894 | " ddddddddddddddddddddddddddddd),\n" |
Daniel Jasper | faab0d3 | 2013-02-27 09:47:53 +0000 | [diff] [blame] | 1895 | " test);", |
| 1896 | NoBinPacking); |
Daniel Jasper | 0df6acd | 2013-01-16 14:59:02 +0000 | [diff] [blame] | 1897 | |
Daniel Jasper | faab0d3 | 2013-02-27 09:47:53 +0000 | [diff] [blame] | 1898 | verifyFormat("std::vector<aaaaaaaaaaaaaaaaaaaaaaa,\n" |
| 1899 | " aaaaaaaaaaaaaaaaaaaaaaa,\n" |
| 1900 | " aaaaaaaaaaaaaaaaaaaaaaa> aaaaaaaaaaaaaaaaaa;", |
| 1901 | NoBinPacking); |
| 1902 | verifyFormat("a(\"a\"\n" |
| 1903 | " \"a\",\n" |
| 1904 | " a);"); |
Daniel Jasper | 8f4bd7a | 2013-01-23 10:08:28 +0000 | [diff] [blame] | 1905 | |
Daniel Jasper | faab0d3 | 2013-02-27 09:47:53 +0000 | [diff] [blame] | 1906 | NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false; |
Daniel Jasper | f157960 | 2013-01-29 16:03:49 +0000 | [diff] [blame] | 1907 | verifyFormat("void aaaaaaaaaa(aaaaaaaaa,\n" |
Daniel Jasper | 8f4bd7a | 2013-01-23 10:08:28 +0000 | [diff] [blame] | 1908 | " aaaaaaaaa,\n" |
Daniel Jasper | f157960 | 2013-01-29 16:03:49 +0000 | [diff] [blame] | 1909 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", |
Daniel Jasper | faab0d3 | 2013-02-27 09:47:53 +0000 | [diff] [blame] | 1910 | NoBinPacking); |
Daniel Jasper | fc75908 | 2013-02-14 14:26:07 +0000 | [diff] [blame] | 1911 | verifyFormat( |
| 1912 | "void f() {\n" |
| 1913 | " aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n" |
| 1914 | " .aaaaaaa();\n" |
Daniel Jasper | 66d19bd | 2013-02-18 11:59:17 +0000 | [diff] [blame] | 1915 | "}", |
Daniel Jasper | faab0d3 | 2013-02-27 09:47:53 +0000 | [diff] [blame] | 1916 | NoBinPacking); |
Daniel Jasper | 0df6acd | 2013-01-16 14:59:02 +0000 | [diff] [blame] | 1917 | } |
| 1918 | |
Daniel Jasper | c79afda | 2013-01-18 10:56:38 +0000 | [diff] [blame] | 1919 | TEST_F(FormatTest, FormatsBuilderPattern) { |
| 1920 | verifyFormat( |
| 1921 | "return llvm::StringSwitch<Reference::Kind>(name)\n" |
Daniel Jasper | 65622aa | 2013-04-08 10:45:44 +0000 | [diff] [blame] | 1922 | " .StartsWith(\".eh_frame_hdr\", ORDER_EH_FRAMEHDR)\n" |
| 1923 | " .StartsWith(\".eh_frame\", ORDER_EH_FRAME).StartsWith(\".init\", ORDER_INIT)\n" |
| 1924 | " .StartsWith(\".fini\", ORDER_FINI).StartsWith(\".hash\", ORDER_HASH)\n" |
| 1925 | " .Default(ORDER_TEXT);\n"); |
Daniel Jasper | bf71ba2 | 2013-04-08 20:33:42 +0000 | [diff] [blame] | 1926 | |
Daniel Jasper | 4a544e5 | 2013-02-15 20:33:06 +0000 | [diff] [blame] | 1927 | verifyFormat("return aaaaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa() <\n" |
Daniel Jasper | bf71ba2 | 2013-04-08 20:33:42 +0000 | [diff] [blame] | 1928 | " aaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa();"); |
Daniel Jasper | 515f65d | 2013-02-18 13:24:21 +0000 | [diff] [blame] | 1929 | verifyFormat( |
| 1930 | "aaaaaaa->aaaaaaa\n" |
| 1931 | " ->aaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" |
| 1932 | " ->aaaaaaaa(aaaaaaaaaaaaaaa);"); |
Daniel Jasper | 82282dc | 2013-02-18 13:52:06 +0000 | [diff] [blame] | 1933 | verifyFormat( |
| 1934 | "aaaaaaaaaaaaaaaaaaa()->aaaaaa(bbbbb)->aaaaaaaaaaaaaaaaaaa( // break\n" |
Daniel Jasper | 2484971 | 2013-03-01 16:48:32 +0000 | [diff] [blame] | 1935 | " aaaaaaaaaaaaaa);"); |
Daniel Jasper | 82282dc | 2013-02-18 13:52:06 +0000 | [diff] [blame] | 1936 | verifyFormat( |
| 1937 | "aaaaaaaaaaaaaaaaaaaaaaa *aaaaaaaaa = aaaaaa->aaaaaaaaaaaa()\n" |
| 1938 | " ->aaaaaaaaaaaaaaaa(\n" |
| 1939 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" |
| 1940 | " ->aaaaaaaaaaaaaaaaa();"); |
Daniel Jasper | c79afda | 2013-01-18 10:56:38 +0000 | [diff] [blame] | 1941 | } |
| 1942 | |
Daniel Jasper | 7d19bc2 | 2013-01-11 14:23:32 +0000 | [diff] [blame] | 1943 | TEST_F(FormatTest, DoesNotBreakTrailingAnnotation) { |
| 1944 | verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" |
Daniel Jasper | 8ef19a2 | 2013-03-14 09:50:46 +0000 | [diff] [blame] | 1945 | " LOCKS_EXCLUDED(aaaaaaaaaaaaa);"); |
Daniel Jasper | 60ca75d | 2013-01-17 13:31:52 +0000 | [diff] [blame] | 1946 | verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n" |
Daniel Jasper | 8ef19a2 | 2013-03-14 09:50:46 +0000 | [diff] [blame] | 1947 | " LOCKS_EXCLUDED(aaaaaaaaaaaaa);"); |
Daniel Jasper | 60ca75d | 2013-01-17 13:31:52 +0000 | [diff] [blame] | 1948 | verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n" |
Daniel Jasper | 8ef19a2 | 2013-03-14 09:50:46 +0000 | [diff] [blame] | 1949 | " LOCKS_EXCLUDED(aaaaaaaaaaaaa) {}"); |
Daniel Jasper | 8ed4100 | 2013-02-28 14:44:25 +0000 | [diff] [blame] | 1950 | verifyFormat( |
| 1951 | "void aaaaaaaaaaaaaaaaaa()\n" |
| 1952 | " __attribute__((aaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaa,\n" |
| 1953 | " aaaaaaaaaaaaaaaaaaaaaaaaa));"); |
Daniel Jasper | 8ef19a2 | 2013-03-14 09:50:46 +0000 | [diff] [blame] | 1954 | verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" |
| 1955 | " __attribute__((unused));"); |
Daniel Jasper | 1407bee | 2013-04-11 14:29:13 +0000 | [diff] [blame] | 1956 | |
Daniel Jasper | 8ef19a2 | 2013-03-14 09:50:46 +0000 | [diff] [blame] | 1957 | // FIXME: This is bad indentation, but generally hard to distinguish from a |
| 1958 | // function declaration. |
| 1959 | verifyFormat( |
| 1960 | "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" |
| 1961 | "GUARDED_BY(aaaaaaaaaaaa);"); |
Daniel Jasper | 7d19bc2 | 2013-01-11 14:23:32 +0000 | [diff] [blame] | 1962 | } |
| 1963 | |
Daniel Jasper | e2c7acf | 2012-12-24 00:13:23 +0000 | [diff] [blame] | 1964 | TEST_F(FormatTest, BreaksAccordingToOperatorPrecedence) { |
| 1965 | verifyFormat( |
| 1966 | "if (aaaaaaaaaaaaaaaaaaaaaaaaa ||\n" |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 1967 | " bbbbbbbbbbbbbbbbbbbbbbbbb && ccccccccccccccccccccccccc) {\n}"); |
Daniel Jasper | cd16238 | 2013-01-07 13:26:07 +0000 | [diff] [blame] | 1968 | verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa && bbbbbbbbbbbbbbbbbbbbbbbbb ||\n" |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 1969 | " ccccccccccccccccccccccccc) {\n}"); |
Daniel Jasper | cd16238 | 2013-01-07 13:26:07 +0000 | [diff] [blame] | 1970 | verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb ||\n" |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 1971 | " ccccccccccccccccccccccccc) {\n}"); |
Daniel Jasper | e2c7acf | 2012-12-24 00:13:23 +0000 | [diff] [blame] | 1972 | verifyFormat( |
| 1973 | "if ((aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb) &&\n" |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 1974 | " ccccccccccccccccccccccccc) {\n}"); |
Daniel Jasper | 29f123b | 2013-02-08 15:28:42 +0000 | [diff] [blame] | 1975 | verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA ||\n" |
| 1976 | " bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB ||\n" |
| 1977 | " cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC ||\n" |
| 1978 | " dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;"); |
| 1979 | verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa ||\n" |
| 1980 | " aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) &&\n" |
| 1981 | " aaaaaaaaaaaaaaa != aa) {\n}"); |
Daniel Jasper | e2c7acf | 2012-12-24 00:13:23 +0000 | [diff] [blame] | 1982 | } |
| 1983 | |
Daniel Jasper | 7006e7e | 2013-01-23 12:27:43 +0000 | [diff] [blame] | 1984 | TEST_F(FormatTest, BreaksAfterAssignments) { |
Daniel Jasper | 9cda800 | 2013-01-07 13:08:40 +0000 | [diff] [blame] | 1985 | verifyFormat( |
Daniel Jasper | 7006e7e | 2013-01-23 12:27:43 +0000 | [diff] [blame] | 1986 | "unsigned Cost =\n" |
| 1987 | " TTI.getMemoryOpCost(I->getOpcode(), VectorTy, SI->getAlignment(),\n" |
| 1988 | " SI->getPointerAddressSpaceee());\n"); |
Daniel Jasper | 9cda800 | 2013-01-07 13:08:40 +0000 | [diff] [blame] | 1989 | verifyFormat( |
Daniel Jasper | 13cb7c2 | 2013-01-23 15:55:19 +0000 | [diff] [blame] | 1990 | "CharSourceRange LineRange = CharSourceRange::getTokenRange(\n" |
| 1991 | " Line.Tokens.front().Tok.getLo(), Line.Tokens.back().Tok.getLoc());"); |
Daniel Jasper | f39c885 | 2013-01-23 16:58:21 +0000 | [diff] [blame] | 1992 | |
| 1993 | verifyFormat( |
| 1994 | "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa = aaaaaaaaaaaaaa(0).aaaa()\n" |
| 1995 | " .aaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaa);"); |
Daniel Jasper | 9cda800 | 2013-01-07 13:08:40 +0000 | [diff] [blame] | 1996 | } |
| 1997 | |
Daniel Jasper | cf225b6 | 2012-12-24 13:43:52 +0000 | [diff] [blame] | 1998 | TEST_F(FormatTest, AlignsAfterAssignments) { |
| 1999 | verifyFormat( |
| 2000 | "int Result = aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" |
Daniel Jasper | 9cda800 | 2013-01-07 13:08:40 +0000 | [diff] [blame] | 2001 | " aaaaaaaaaaaaaaaaaaaaaaaaa;"); |
Daniel Jasper | cf225b6 | 2012-12-24 13:43:52 +0000 | [diff] [blame] | 2002 | verifyFormat( |
| 2003 | "Result += aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" |
Daniel Jasper | 9cda800 | 2013-01-07 13:08:40 +0000 | [diff] [blame] | 2004 | " aaaaaaaaaaaaaaaaaaaaaaaaa;"); |
Daniel Jasper | cf225b6 | 2012-12-24 13:43:52 +0000 | [diff] [blame] | 2005 | verifyFormat( |
| 2006 | "Result >>= aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" |
Daniel Jasper | 9cda800 | 2013-01-07 13:08:40 +0000 | [diff] [blame] | 2007 | " aaaaaaaaaaaaaaaaaaaaaaaaa;"); |
Daniel Jasper | cf225b6 | 2012-12-24 13:43:52 +0000 | [diff] [blame] | 2008 | verifyFormat( |
| 2009 | "int Result = (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" |
Daniel Jasper | 9cda800 | 2013-01-07 13:08:40 +0000 | [diff] [blame] | 2010 | " aaaaaaaaaaaaaaaaaaaaaaaaa);"); |
Daniel Jasper | 66d19bd | 2013-02-18 11:59:17 +0000 | [diff] [blame] | 2011 | verifyFormat("double LooooooooooooooooooooooooongResult =\n" |
| 2012 | " aaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaa +\n" |
| 2013 | " aaaaaaaaaaaaaaaaaaaaaaaa;"); |
Daniel Jasper | cf225b6 | 2012-12-24 13:43:52 +0000 | [diff] [blame] | 2014 | } |
| 2015 | |
| 2016 | TEST_F(FormatTest, AlignsAfterReturn) { |
| 2017 | verifyFormat( |
| 2018 | "return aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" |
| 2019 | " aaaaaaaaaaaaaaaaaaaaaaaaa;"); |
| 2020 | verifyFormat( |
| 2021 | "return (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" |
| 2022 | " aaaaaaaaaaaaaaaaaaaaaaaaa);"); |
Daniel Jasper | 3791130 | 2013-04-02 14:33:13 +0000 | [diff] [blame] | 2023 | verifyFormat( |
| 2024 | "return aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n" |
Daniel Jasper | bf71ba2 | 2013-04-08 20:33:42 +0000 | [diff] [blame] | 2025 | " aaaaaaaaaaaaaaaaaaaaaa();"); |
Daniel Jasper | 3791130 | 2013-04-02 14:33:13 +0000 | [diff] [blame] | 2026 | verifyFormat( |
| 2027 | "return (aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n" |
Daniel Jasper | bf71ba2 | 2013-04-08 20:33:42 +0000 | [diff] [blame] | 2028 | " aaaaaaaaaaaaaaaaaaaaaa());"); |
| 2029 | verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" |
| 2030 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); |
| 2031 | verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" |
| 2032 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) &&\n" |
| 2033 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); |
Daniel Jasper | cf225b6 | 2012-12-24 13:43:52 +0000 | [diff] [blame] | 2034 | } |
| 2035 | |
Daniel Jasper | 9c837d0 | 2013-01-09 07:06:56 +0000 | [diff] [blame] | 2036 | TEST_F(FormatTest, BreaksConditionalExpressions) { |
| 2037 | verifyFormat( |
| 2038 | "aaaa(aaaaaaaaaaaaaaaaaaaa,\n" |
Daniel Jasper | bfe6fd4 | 2013-01-28 12:45:14 +0000 | [diff] [blame] | 2039 | " aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" |
| 2040 | " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); |
Daniel Jasper | 1f2b078 | 2013-01-16 16:23:19 +0000 | [diff] [blame] | 2041 | verifyFormat( |
Daniel Jasper | bfe6fd4 | 2013-01-28 12:45:14 +0000 | [diff] [blame] | 2042 | "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" |
| 2043 | " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); |
| 2044 | verifyFormat( |
| 2045 | "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa)\n" |
| 2046 | " : aaaaaaaaaaaaa);"); |
| 2047 | verifyFormat( |
| 2048 | "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" |
Daniel Jasper | 64f0928 | 2013-03-20 13:53:11 +0000 | [diff] [blame] | 2049 | " aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" |
Daniel Jasper | bfe6fd4 | 2013-01-28 12:45:14 +0000 | [diff] [blame] | 2050 | " : aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" |
| 2051 | " aaaaaaaaaaaaa);"); |
| 2052 | verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" |
| 2053 | " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" |
| 2054 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" |
| 2055 | " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" |
| 2056 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); |
| 2057 | verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" |
| 2058 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" |
| 2059 | " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" |
| 2060 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" |
| 2061 | " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" |
| 2062 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" |
| 2063 | " aaaaaaaaaaaaaaaaaaaaaaaaaaa);"); |
Daniel Jasper | 7878a7b | 2013-02-15 11:07:25 +0000 | [diff] [blame] | 2064 | |
| 2065 | verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" |
| 2066 | " ? aaaaaaaaaaaaaaaaaaaaaaaaaaa\n" |
| 2067 | " : aaaaaaaaaaaaaaaaaaaaaaaaaaa;"); |
Daniel Jasper | 3791130 | 2013-04-02 14:33:13 +0000 | [diff] [blame] | 2068 | verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n" |
| 2069 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" |
| 2070 | " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" |
| 2071 | " : aaaaaaaaaaaaaaaa;"); |
Daniel Jasper | 237d4c1 | 2013-02-23 21:01:55 +0000 | [diff] [blame] | 2072 | verifyFormat( |
| 2073 | "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" |
| 2074 | " ? aaaaaaaaaaaaaaa\n" |
| 2075 | " : aaaaaaaaaaaaaaa;"); |
| 2076 | verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n" |
Daniel Jasper | bf71ba2 | 2013-04-08 20:33:42 +0000 | [diff] [blame] | 2077 | " aaaaaaaaa\n" |
Daniel Jasper | 65622aa | 2013-04-08 10:45:44 +0000 | [diff] [blame] | 2078 | " ? b\n" |
| 2079 | " : c);"); |
Daniel Jasper | 087387a | 2013-02-08 16:49:27 +0000 | [diff] [blame] | 2080 | verifyFormat( |
Daniel Jasper | 7878a7b | 2013-02-15 11:07:25 +0000 | [diff] [blame] | 2081 | "unsigned Indent =\n" |
| 2082 | " format(TheLine.First, IndentForLevel[TheLine.Level] >= 0\n" |
| 2083 | " ? IndentForLevel[TheLine.Level]\n" |
Daniel Jasper | 7d81281 | 2013-02-21 15:00:29 +0000 | [diff] [blame] | 2084 | " : TheLine * 2,\n" |
| 2085 | " TheLine.InPPDirective, PreviousEndOfLineColumn);", |
Daniel Jasper | 66d19bd | 2013-02-18 11:59:17 +0000 | [diff] [blame] | 2086 | getLLVMStyleWithColumns(70)); |
Daniel Jasper | 7878a7b | 2013-02-15 11:07:25 +0000 | [diff] [blame] | 2087 | |
Daniel Jasper | faab0d3 | 2013-02-27 09:47:53 +0000 | [diff] [blame] | 2088 | FormatStyle NoBinPacking = getLLVMStyle(); |
| 2089 | NoBinPacking.BinPackParameters = false; |
| 2090 | verifyFormat( |
Daniel Jasper | 237d4c1 | 2013-02-23 21:01:55 +0000 | [diff] [blame] | 2091 | "void f() {\n" |
| 2092 | " g(aaa,\n" |
| 2093 | " aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n" |
| 2094 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" |
| 2095 | " ? aaaaaaaaaaaaaaa\n" |
| 2096 | " : aaaaaaaaaaaaaaa);\n" |
Daniel Jasper | faab0d3 | 2013-02-27 09:47:53 +0000 | [diff] [blame] | 2097 | "}", |
| 2098 | NoBinPacking); |
Daniel Jasper | 9c837d0 | 2013-01-09 07:06:56 +0000 | [diff] [blame] | 2099 | } |
| 2100 | |
Daniel Jasper | 2e60377 | 2013-01-29 11:21:01 +0000 | [diff] [blame] | 2101 | TEST_F(FormatTest, DeclarationsOfMultipleVariables) { |
| 2102 | verifyFormat("bool aaaaaaaaaaaaaaaaa = aaaaaa->aaaaaaaaaaaaaaaaa(),\n" |
| 2103 | " aaaaaaaaaaa = aaaaaa->aaaaaaaaaaa();"); |
| 2104 | verifyFormat("bool a = true, b = false;"); |
| 2105 | |
Daniel Jasper | 2e60377 | 2013-01-29 11:21:01 +0000 | [diff] [blame] | 2106 | verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaa =\n" |
Daniel Jasper | 8ed9f2b | 2013-04-03 13:36:17 +0000 | [diff] [blame] | 2107 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa),\n" |
Daniel Jasper | 2e60377 | 2013-01-29 11:21:01 +0000 | [diff] [blame] | 2108 | " bbbbbbbbbbbbbbbbbbbbbbbbb =\n" |
Daniel Jasper | 3791130 | 2013-04-02 14:33:13 +0000 | [diff] [blame] | 2109 | " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(bbbbbbbbbbbbbbbb);"); |
Daniel Jasper | 2e60377 | 2013-01-29 11:21:01 +0000 | [diff] [blame] | 2110 | verifyFormat( |
Daniel Jasper | 7d81281 | 2013-02-21 15:00:29 +0000 | [diff] [blame] | 2111 | "bool aaaaaaaaaaaaaaaaaaaaa =\n" |
Daniel Jasper | 8ed9f2b | 2013-04-03 13:36:17 +0000 | [diff] [blame] | 2112 | " bbbbbbbbbbbbbbbbbbbbbbbbbbbb && cccccccccccccccccccccccccccc,\n" |
Daniel Jasper | 7d81281 | 2013-02-21 15:00:29 +0000 | [diff] [blame] | 2113 | " d = e && f;"); |
Daniel Jasper | adc0f09 | 2013-04-05 09:38:50 +0000 | [diff] [blame] | 2114 | verifyFormat("aaaaaaaaa a = aaaaaaaaaaaaaaaaaaaa, b = bbbbbbbbbbbbbbbbbbbb,\n" |
| 2115 | " c = cccccccccccccccccccc, d = dddddddddddddddddddd;"); |
| 2116 | verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n" |
| 2117 | " *c = ccccccccccccccccccc, *d = ddddddddddddddddddd;"); |
| 2118 | verifyFormat("aaaaaaaaa ***a = aaaaaaaaaaaaaaaaaaa, ***b = bbbbbbbbbbbbbbb,\n" |
| 2119 | " ***c = ccccccccccccccccccc, ***d = ddddddddddddddd;"); |
| 2120 | // FIXME: If multiple variables are defined, the "*" needs to move to the new |
| 2121 | // line. Also fix indent for breaking after the type, this looks bad. |
| 2122 | verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" |
| 2123 | "aaaaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaa,\n" |
| 2124 | " *b = bbbbbbbbbbbbbbbbbbb;"); |
Daniel Jasper | 2e60377 | 2013-01-29 11:21:01 +0000 | [diff] [blame] | 2125 | |
Daniel Jasper | adc0f09 | 2013-04-05 09:38:50 +0000 | [diff] [blame] | 2126 | // Not ideal, but pointer-with-type does not allow much here. |
| 2127 | verifyGoogleFormat( |
| 2128 | "aaaaaaaaa* a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n" |
| 2129 | " *b = bbbbbbbbbbbbbbbbbbb, *d = ddddddddddddddddddd;"); |
Daniel Jasper | 2e60377 | 2013-01-29 11:21:01 +0000 | [diff] [blame] | 2130 | } |
| 2131 | |
Nico Weber | 7d37b8b | 2013-01-12 01:28:06 +0000 | [diff] [blame] | 2132 | TEST_F(FormatTest, ConditionalExpressionsInBrackets) { |
| 2133 | verifyFormat("arr[foo ? bar : baz];"); |
| 2134 | verifyFormat("f()[foo ? bar : baz];"); |
| 2135 | verifyFormat("(a + b)[foo ? bar : baz];"); |
| 2136 | verifyFormat("arr[foo ? (4 > 5 ? 4 : 5) : 5 < 5 ? 5 : 7];"); |
| 2137 | } |
| 2138 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 2139 | TEST_F(FormatTest, AlignsStringLiterals) { |
| 2140 | verifyFormat("loooooooooooooooooooooooooongFunction(\"short literal \"\n" |
| 2141 | " \"short literal\");"); |
| 2142 | verifyFormat( |
| 2143 | "looooooooooooooooooooooooongFunction(\n" |
| 2144 | " \"short literal\"\n" |
| 2145 | " \"looooooooooooooooooooooooooooooooooooooooooooooooong literal\");"); |
Daniel Jasper | 66d19bd | 2013-02-18 11:59:17 +0000 | [diff] [blame] | 2146 | verifyFormat("someFunction(\"Always break between multi-line\"\n" |
| 2147 | " \" string literals\",\n" |
| 2148 | " and, other, parameters);"); |
Manuel Klimek | b56b6d1 | 2013-02-20 15:25:48 +0000 | [diff] [blame] | 2149 | EXPECT_EQ("fun + \"1243\" /* comment */\n" |
| 2150 | " \"5678\";", |
| 2151 | format("fun + \"1243\" /* comment */\n" |
| 2152 | " \"5678\";", |
| 2153 | getLLVMStyleWithColumns(28))); |
| 2154 | EXPECT_EQ( |
| 2155 | "aaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" |
| 2156 | " \"aaaaaaaaaaaaaaaaaaaaa\"\n" |
| 2157 | " \"aaaaaaaaaaaaaaaa\";", |
| 2158 | format("aaaaaa =" |
| 2159 | "\"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa " |
| 2160 | "aaaaaaaaaaaaaaaaaaaaa\" " |
| 2161 | "\"aaaaaaaaaaaaaaaa\";")); |
Manuel Klimek | e9a6226 | 2013-02-20 15:32:58 +0000 | [diff] [blame] | 2162 | verifyFormat("a = a + \"a\"\n" |
| 2163 | " \"a\"\n" |
| 2164 | " \"a\";"); |
Daniel Jasper | 3a20441 | 2013-02-23 07:46:38 +0000 | [diff] [blame] | 2165 | |
| 2166 | verifyFormat( |
| 2167 | "#define LL_FORMAT \"ll\"\n" |
| 2168 | "printf(\"aaaaa: %d, bbbbbb: %\" LL_FORMAT \"d, cccccccc: %\" LL_FORMAT\n" |
| 2169 | " \"d, ddddddddd: %\" LL_FORMAT \"d\");"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 2170 | } |
| 2171 | |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 2172 | TEST_F(FormatTest, AlignsPipes) { |
| 2173 | verifyFormat( |
| 2174 | "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" |
| 2175 | " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" |
| 2176 | " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); |
| 2177 | verifyFormat( |
| 2178 | "aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa\n" |
| 2179 | " << aaaaaaaaaaaaaaaaaaaa;"); |
| 2180 | verifyFormat( |
| 2181 | "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" |
| 2182 | " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); |
| 2183 | verifyFormat( |
| 2184 | "llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n" |
| 2185 | " \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\"\n" |
| 2186 | " << \"ccccccccccccccccccccccccccccccccccccccccccccccccc\";"); |
| 2187 | verifyFormat( |
| 2188 | "aaaaaaaa << (aaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" |
| 2189 | " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" |
| 2190 | " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); |
Daniel Jasper | fa543ac | 2013-02-04 07:34:48 +0000 | [diff] [blame] | 2191 | |
| 2192 | verifyFormat("return out << \"somepacket = {\\n\"\n" |
| 2193 | " << \" aaaaaa = \" << pkt.aaaaaa << \"\\n\"\n" |
| 2194 | " << \" bbbb = \" << pkt.bbbb << \"\\n\"\n" |
| 2195 | " << \" cccccc = \" << pkt.cccccc << \"\\n\"\n" |
| 2196 | " << \" ddd = [\" << pkt.ddd << \"]\\n\"\n" |
| 2197 | " << \"}\";"); |
Daniel Jasper | 4e8a7b4 | 2013-02-06 21:04:05 +0000 | [diff] [blame] | 2198 | |
| 2199 | verifyFormat( |
| 2200 | "llvm::outs() << \"aaaaaaaaaaaaaaaaa = \" << aaaaaaaaaaaaaaaaa\n" |
| 2201 | " << \"bbbbbbbbbbbbbbbbb = \" << bbbbbbbbbbbbbbbbb\n" |
| 2202 | " << \"ccccccccccccccccc = \" << ccccccccccccccccc\n" |
| 2203 | " << \"ddddddddddddddddd = \" << ddddddddddddddddd\n" |
| 2204 | " << \"eeeeeeeeeeeeeeeee = \" << eeeeeeeeeeeeeeeee;"); |
Daniel Jasper | bfa1edd | 2013-03-14 14:00:17 +0000 | [diff] [blame] | 2205 | verifyFormat("llvm::outs() << aaaaaaaaaaaaaaaaaaaaaaaa << \"=\"\n" |
| 2206 | " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;"); |
Daniel Jasper | 3791130 | 2013-04-02 14:33:13 +0000 | [diff] [blame] | 2207 | |
| 2208 | verifyFormat( |
| 2209 | "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" |
| 2210 | " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 2211 | } |
| 2212 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 2213 | TEST_F(FormatTest, UnderstandsEquals) { |
| 2214 | verifyFormat( |
| 2215 | "aaaaaaaaaaaaaaaaa =\n" |
| 2216 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); |
| 2217 | verifyFormat( |
| 2218 | "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 2219 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 2220 | verifyFormat( |
| 2221 | "if (a) {\n" |
Manuel Klimek | 36fab8d | 2013-01-10 13:24:24 +0000 | [diff] [blame] | 2222 | " f();\n" |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 2223 | "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 2224 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" |
| 2225 | "}"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 2226 | |
Daniel Jasper | 66d19bd | 2013-02-18 11:59:17 +0000 | [diff] [blame] | 2227 | verifyFormat("if (int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" |
| 2228 | " 100000000 + 10000000) {\n}"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 2229 | } |
| 2230 | |
Daniel Jasper | 9a0b494 | 2012-12-17 14:34:14 +0000 | [diff] [blame] | 2231 | TEST_F(FormatTest, WrapsAtFunctionCallsIfNecessary) { |
Daniel Jasper | 1321eb5 | 2012-12-18 21:05:13 +0000 | [diff] [blame] | 2232 | verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n" |
| 2233 | " .looooooooooooooooooooooooooooooooooooooongFunction();"); |
Daniel Jasper | 9a0b494 | 2012-12-17 14:34:14 +0000 | [diff] [blame] | 2234 | |
Daniel Jasper | 1321eb5 | 2012-12-18 21:05:13 +0000 | [diff] [blame] | 2235 | verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n" |
| 2236 | " ->looooooooooooooooooooooooooooooooooooooongFunction();"); |
Daniel Jasper | 9a0b494 | 2012-12-17 14:34:14 +0000 | [diff] [blame] | 2237 | |
| 2238 | verifyFormat( |
| 2239 | "LooooooooooooooooooooooooooooooooongObject->shortFunction(Parameter1,\n" |
| 2240 | " Parameter2);"); |
| 2241 | |
| 2242 | verifyFormat( |
| 2243 | "ShortObject->shortFunction(\n" |
| 2244 | " LooooooooooooooooooooooooooooooooooooooooooooooongParameter1,\n" |
| 2245 | " LooooooooooooooooooooooooooooooooooooooooooooooongParameter2);"); |
| 2246 | |
| 2247 | verifyFormat("loooooooooooooongFunction(\n" |
| 2248 | " LoooooooooooooongObject->looooooooooooooooongFunction());"); |
| 2249 | |
| 2250 | verifyFormat( |
| 2251 | "function(LoooooooooooooooooooooooooooooooooooongObject\n" |
| 2252 | " ->loooooooooooooooooooooooooooooooooooooooongFunction());"); |
| 2253 | |
Daniel Jasper | fc75908 | 2013-02-14 14:26:07 +0000 | [diff] [blame] | 2254 | verifyFormat("EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n" |
| 2255 | " .WillRepeatedly(Return(SomeValue));"); |
| 2256 | verifyFormat("SomeMap[std::pair(aaaaaaaaaaaa, bbbbbbbbbbbbbbb)]\n" |
| 2257 | " .insert(ccccccccccccccccccccccc);"); |
Daniel Jasper | 3af59ce | 2013-03-15 14:57:30 +0000 | [diff] [blame] | 2258 | verifyFormat( |
| 2259 | "aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" |
| 2260 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" |
| 2261 | " .aaaaaaaaaaaaaaa(\n" |
| 2262 | " aa(aaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" |
| 2263 | " aaaaaaaaaaaaaaaaaaaaaaaaaaa));"); |
Daniel Jasper | 3791130 | 2013-04-02 14:33:13 +0000 | [diff] [blame] | 2264 | verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" |
| 2265 | " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" |
| 2266 | " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" |
| 2267 | " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()) {\n" |
| 2268 | "}"); |
Daniel Jasper | fc75908 | 2013-02-14 14:26:07 +0000 | [diff] [blame] | 2269 | |
Daniel Jasper | 46a46a2 | 2013-01-07 07:13:20 +0000 | [diff] [blame] | 2270 | // Here, it is not necessary to wrap at "." or "->". |
Daniel Jasper | 1321eb5 | 2012-12-18 21:05:13 +0000 | [diff] [blame] | 2271 | verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaa) ||\n" |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 2272 | " aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); |
Daniel Jasper | 46a46a2 | 2013-01-07 07:13:20 +0000 | [diff] [blame] | 2273 | verifyFormat( |
| 2274 | "aaaaaaaaaaa->aaaaaaaaa(\n" |
| 2275 | " aaaaaaaaaaaaaaaaaaaaaaaaa,\n" |
| 2276 | " aaaaaaaaaaaaaaaaaa->aaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa));\n"); |
Daniel Jasper | 9fc56f2 | 2013-02-14 15:01:34 +0000 | [diff] [blame] | 2277 | |
| 2278 | verifyFormat( |
| 2279 | "aaaaaaaaaaaaaaaaaaaaaaaaa(\n" |
| 2280 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa().aaaaaaaaaaaaaaaaa());"); |
Daniel Jasper | 8d0d19a | 2013-03-13 15:37:48 +0000 | [diff] [blame] | 2281 | verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() *\n" |
| 2282 | " aaaaaaaaa()->aaaaaa()->aaaaa());"); |
| 2283 | verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() ||\n" |
| 2284 | " aaaaaaaaa()->aaaaaa()->aaaaa());"); |
Daniel Jasper | faab0d3 | 2013-02-27 09:47:53 +0000 | [diff] [blame] | 2285 | |
Daniel Jasper | 3af59ce | 2013-03-15 14:57:30 +0000 | [diff] [blame] | 2286 | // FIXME: Should we break before .a()? |
| 2287 | verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" |
| 2288 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa).a();"); |
| 2289 | |
Daniel Jasper | faab0d3 | 2013-02-27 09:47:53 +0000 | [diff] [blame] | 2290 | FormatStyle NoBinPacking = getLLVMStyle(); |
| 2291 | NoBinPacking.BinPackParameters = false; |
| 2292 | verifyFormat("aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n" |
| 2293 | " .aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n" |
| 2294 | " .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa,\n" |
| 2295 | " aaaaaaaaaaaaaaaaaaa,\n" |
| 2296 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", |
| 2297 | NoBinPacking); |
Daniel Jasper | 9a0b494 | 2012-12-17 14:34:14 +0000 | [diff] [blame] | 2298 | } |
| 2299 | |
Daniel Jasper | 9a64fb5 | 2013-01-02 15:08:56 +0000 | [diff] [blame] | 2300 | TEST_F(FormatTest, WrapsTemplateDeclarations) { |
| 2301 | verifyFormat("template <typename T>\n" |
| 2302 | "virtual void loooooooooooongFunction(int Param1, int Param2);"); |
| 2303 | verifyFormat( |
Daniel Jasper | c79afda | 2013-01-18 10:56:38 +0000 | [diff] [blame] | 2304 | "template <typename T>\n" |
Daniel Jasper | 29f123b | 2013-02-08 15:28:42 +0000 | [diff] [blame] | 2305 | "using comment_to_xml_conversion = comment_to_xml_conversion<T, int>;"); |
Daniel Jasper | 66d19bd | 2013-02-18 11:59:17 +0000 | [diff] [blame] | 2306 | verifyFormat("template <typename T>\n" |
| 2307 | "void f(int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram1,\n" |
| 2308 | " int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram2);"); |
Daniel Jasper | 9a64fb5 | 2013-01-02 15:08:56 +0000 | [diff] [blame] | 2309 | verifyFormat( |
| 2310 | "template <typename T>\n" |
| 2311 | "void looooooooooooooooooooongFunction(int Paaaaaaaaaaaaaaaaaaaaram1,\n" |
| 2312 | " int Paaaaaaaaaaaaaaaaaaaaram2);"); |
Daniel Jasper | 5eda31e | 2013-01-02 18:30:06 +0000 | [diff] [blame] | 2313 | verifyFormat( |
| 2314 | "template <typename T>\n" |
| 2315 | "aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa,\n" |
| 2316 | " aaaaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaa,\n" |
| 2317 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); |
Daniel Jasper | d64f738 | 2013-01-09 09:50:48 +0000 | [diff] [blame] | 2318 | verifyFormat("template <typename T>\n" |
| 2319 | "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" |
Daniel Jasper | 8ed9f2b | 2013-04-03 13:36:17 +0000 | [diff] [blame] | 2320 | " int aaaaaaaaaaaaaaaaaaaaaa);"); |
Daniel Jasper | 3fc0bb7 | 2013-01-09 10:40:23 +0000 | [diff] [blame] | 2321 | verifyFormat( |
| 2322 | "template <typename T1, typename T2 = char, typename T3 = char,\n" |
| 2323 | " typename T4 = char>\n" |
| 2324 | "void f();"); |
Daniel Jasper | 3499dda | 2013-01-25 15:43:32 +0000 | [diff] [blame] | 2325 | verifyFormat( |
| 2326 | "aaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>(\n" |
| 2327 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); |
Daniel Jasper | 0178673 | 2013-02-04 07:21:18 +0000 | [diff] [blame] | 2328 | |
Daniel Jasper | 66d19bd | 2013-02-18 11:59:17 +0000 | [diff] [blame] | 2329 | verifyFormat("a<aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaa>(\n" |
| 2330 | " a(aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));"); |
Daniel Jasper | 9a64fb5 | 2013-01-02 15:08:56 +0000 | [diff] [blame] | 2331 | } |
| 2332 | |
Daniel Jasper | 63f0036 | 2013-01-25 10:57:27 +0000 | [diff] [blame] | 2333 | TEST_F(FormatTest, WrapsAtNestedNameSpecifiers) { |
| 2334 | verifyFormat( |
| 2335 | "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" |
| 2336 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); |
| 2337 | verifyFormat( |
| 2338 | "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" |
| 2339 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" |
| 2340 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());"); |
| 2341 | |
| 2342 | // FIXME: Should we have an extra indent after the second break? |
| 2343 | verifyFormat( |
| 2344 | "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" |
| 2345 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" |
| 2346 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); |
| 2347 | |
| 2348 | // FIXME: Look into whether we should indent 4 from the start or 4 from |
| 2349 | // "bbbbb..." here instead of what we are doing now. |
| 2350 | verifyFormat( |
| 2351 | "aaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb::\n" |
Daniel Jasper | 5f2173e | 2013-01-28 07:43:15 +0000 | [diff] [blame] | 2352 | " cccccccccccccccccccccccccccccccccccccccccccccc());"); |
Daniel Jasper | 63f0036 | 2013-01-25 10:57:27 +0000 | [diff] [blame] | 2353 | |
| 2354 | // Breaking at nested name specifiers is generally not desirable. |
| 2355 | verifyFormat( |
| 2356 | "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" |
| 2357 | " aaaaaaaaaaaaaaaaaaaaaaa);"); |
Daniel Jasper | bfe6fd4 | 2013-01-28 12:45:14 +0000 | [diff] [blame] | 2358 | |
| 2359 | verifyFormat( |
| 2360 | "aaaaaaaaaaaaaaaaaa(aaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" |
| 2361 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" |
| 2362 | " aaaaaaaaaaaaaaaaaaaaa);", |
| 2363 | getLLVMStyleWithColumns(74)); |
Daniel Jasper | 3791130 | 2013-04-02 14:33:13 +0000 | [diff] [blame] | 2364 | |
| 2365 | verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" |
| 2366 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" |
| 2367 | " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); |
Daniel Jasper | 63f0036 | 2013-01-25 10:57:27 +0000 | [diff] [blame] | 2368 | } |
| 2369 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 2370 | TEST_F(FormatTest, UnderstandsTemplateParameters) { |
| 2371 | verifyFormat("A<int> a;"); |
| 2372 | verifyFormat("A<A<A<int> > > a;"); |
| 2373 | verifyFormat("A<A<A<int, 2>, 3>, 4> a;"); |
| 2374 | verifyFormat("bool x = a < 1 || 2 > a;"); |
| 2375 | verifyFormat("bool x = 5 < f<int>();"); |
| 2376 | verifyFormat("bool x = f<int>() > 5;"); |
| 2377 | verifyFormat("bool x = 5 < a<int>::x;"); |
| 2378 | verifyFormat("bool x = a < 4 ? a > 2 : false;"); |
| 2379 | verifyFormat("bool x = f() ? a < 2 : a > 2;"); |
| 2380 | |
| 2381 | verifyGoogleFormat("A<A<int>> a;"); |
| 2382 | verifyGoogleFormat("A<A<A<int>>> a;"); |
| 2383 | verifyGoogleFormat("A<A<A<A<int>>>> a;"); |
Daniel Jasper | 8ff690a | 2013-02-06 14:22:40 +0000 | [diff] [blame] | 2384 | verifyGoogleFormat("A<A<int> > a;"); |
| 2385 | verifyGoogleFormat("A<A<A<int> > > a;"); |
| 2386 | verifyGoogleFormat("A<A<A<A<int> > > > a;"); |
| 2387 | EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A> >> a;", getGoogleStyle())); |
| 2388 | 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] | 2389 | |
| 2390 | verifyFormat("test >> a >> b;"); |
| 2391 | verifyFormat("test << a >> b;"); |
| 2392 | |
| 2393 | verifyFormat("f<int>();"); |
Manuel Klimek | 36fab8d | 2013-01-10 13:24:24 +0000 | [diff] [blame] | 2394 | verifyFormat("template <typename T> void f() {}"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 2395 | } |
| 2396 | |
Daniel Jasper | 807809c | 2013-02-28 09:21:10 +0000 | [diff] [blame] | 2397 | TEST_F(FormatTest, UnderstandsBinaryOperators) { |
| 2398 | verifyFormat("COMPARE(a, ==, b);"); |
Alexander Kornienko | 54a38bd | 2013-03-20 16:41:56 +0000 | [diff] [blame] | 2399 | } |
| 2400 | |
| 2401 | TEST_F(FormatTest, UnderstandsPointersToMembers) { |
| 2402 | verifyFormat("int A::*x;"); |
| 2403 | // FIXME: Recognize pointers to member functions. |
| 2404 | //verifyFormat("int (S::*func)(void *);"); |
| 2405 | verifyFormat("int(S::*func)(void *);"); |
| 2406 | verifyFormat("(a->*f)();"); |
| 2407 | verifyFormat("a->*x;"); |
| 2408 | verifyFormat("(a.*f)();"); |
| 2409 | verifyFormat("((*a).*f)();"); |
| 2410 | verifyFormat("a.*x;"); |
Daniel Jasper | 807809c | 2013-02-28 09:21:10 +0000 | [diff] [blame] | 2411 | } |
| 2412 | |
Daniel Jasper | 98e6b4a | 2012-12-21 09:41:31 +0000 | [diff] [blame] | 2413 | TEST_F(FormatTest, UnderstandsUnaryOperators) { |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 2414 | verifyFormat("int a = -2;"); |
Daniel Jasper | 8822d3a | 2012-12-04 13:02:32 +0000 | [diff] [blame] | 2415 | verifyFormat("f(-1, -2, -3);"); |
| 2416 | verifyFormat("a[-1] = 5;"); |
| 2417 | verifyFormat("int a = 5 + -2;"); |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 2418 | verifyFormat("if (i == -1) {\n}"); |
| 2419 | verifyFormat("if (i != -1) {\n}"); |
| 2420 | verifyFormat("if (i > -1) {\n}"); |
| 2421 | verifyFormat("if (i < -1) {\n}"); |
Daniel Jasper | d56a737 | 2012-12-06 13:16:39 +0000 | [diff] [blame] | 2422 | verifyFormat("++(a->f());"); |
| 2423 | verifyFormat("--(a->f());"); |
Daniel Jasper | 4abbb53 | 2013-01-14 12:18:19 +0000 | [diff] [blame] | 2424 | verifyFormat("(a->f())++;"); |
| 2425 | verifyFormat("a[42]++;"); |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 2426 | verifyFormat("if (!(a->f())) {\n}"); |
Daniel Jasper | 98e6b4a | 2012-12-21 09:41:31 +0000 | [diff] [blame] | 2427 | |
| 2428 | verifyFormat("a-- > b;"); |
| 2429 | verifyFormat("b ? -a : c;"); |
| 2430 | verifyFormat("n * sizeof char16;"); |
Daniel Jasper | ba3d307 | 2013-01-02 17:21:36 +0000 | [diff] [blame] | 2431 | verifyFormat("n * alignof char16;"); |
Daniel Jasper | 98e6b4a | 2012-12-21 09:41:31 +0000 | [diff] [blame] | 2432 | verifyFormat("sizeof(char);"); |
Daniel Jasper | ba3d307 | 2013-01-02 17:21:36 +0000 | [diff] [blame] | 2433 | verifyFormat("alignof(char);"); |
Daniel Jasper | 1f0754b | 2013-01-02 15:26:16 +0000 | [diff] [blame] | 2434 | |
| 2435 | verifyFormat("return -1;"); |
| 2436 | verifyFormat("switch (a) {\n" |
| 2437 | "case -1:\n" |
| 2438 | " break;\n" |
| 2439 | "}"); |
Daniel Jasper | b598542 | 2013-03-22 10:44:43 +0000 | [diff] [blame] | 2440 | verifyFormat("#define X -1"); |
| 2441 | verifyFormat("#define X -kConstant"); |
Nico Weber | cc191d1 | 2013-01-12 05:41:23 +0000 | [diff] [blame] | 2442 | |
| 2443 | verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = { -5, +3 };"); |
| 2444 | verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = { +5, -3 };"); |
Daniel Jasper | b369c2c | 2013-01-15 14:27:39 +0000 | [diff] [blame] | 2445 | |
| 2446 | verifyFormat("int a = /* confusing comment */ -1;"); |
| 2447 | // FIXME: The space after 'i' is wrong, but hopefully, this is a rare case. |
| 2448 | verifyFormat("int a = i /* confusing comment */++;"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 2449 | } |
| 2450 | |
| 2451 | TEST_F(FormatTest, UndestandsOverloadedOperators) { |
Daniel Jasper | f6aef6a | 2012-12-24 10:56:04 +0000 | [diff] [blame] | 2452 | verifyFormat("bool operator<();"); |
| 2453 | verifyFormat("bool operator>();"); |
| 2454 | verifyFormat("bool operator=();"); |
| 2455 | verifyFormat("bool operator==();"); |
| 2456 | verifyFormat("bool operator!=();"); |
| 2457 | verifyFormat("int operator+();"); |
| 2458 | verifyFormat("int operator++();"); |
| 2459 | verifyFormat("bool operator();"); |
| 2460 | verifyFormat("bool operator()();"); |
| 2461 | verifyFormat("bool operator[]();"); |
| 2462 | verifyFormat("operator bool();"); |
Daniel Jasper | 2b4c924 | 2013-02-11 08:01:18 +0000 | [diff] [blame] | 2463 | verifyFormat("operator int();"); |
| 2464 | verifyFormat("operator void *();"); |
Daniel Jasper | f6aef6a | 2012-12-24 10:56:04 +0000 | [diff] [blame] | 2465 | verifyFormat("operator SomeType<int>();"); |
Daniel Jasper | 2b4c924 | 2013-02-11 08:01:18 +0000 | [diff] [blame] | 2466 | verifyFormat("operator SomeType<int, int>();"); |
| 2467 | verifyFormat("operator SomeType<SomeType<int> >();"); |
Daniel Jasper | f6aef6a | 2012-12-24 10:56:04 +0000 | [diff] [blame] | 2468 | verifyFormat("void *operator new(std::size_t size);"); |
| 2469 | verifyFormat("void *operator new[](std::size_t size);"); |
| 2470 | verifyFormat("void operator delete(void *ptr);"); |
| 2471 | verifyFormat("void operator delete[](void *ptr);"); |
Daniel Jasper | 2b4c924 | 2013-02-11 08:01:18 +0000 | [diff] [blame] | 2472 | |
Daniel Jasper | dc2efa1 | 2013-02-15 19:24:08 +0000 | [diff] [blame] | 2473 | verifyFormat( |
| 2474 | "ostream &operator<<(ostream &OutputStream,\n" |
| 2475 | " SomeReallyLongType WithSomeReallyLongValue);"); |
Daniel Jasper | 27b91cc | 2013-04-05 17:21:59 +0000 | [diff] [blame] | 2476 | verifyFormat("bool operator<(const aaaaaaaaaaaaaaaaaaaaa &left,\n" |
| 2477 | " const aaaaaaaaaaaaaaaaaaaaa &right) {\n" |
| 2478 | " return left.group < right.group;\n" |
| 2479 | "}"); |
Daniel Jasper | 2eb23b7 | 2013-04-11 08:48:20 +0000 | [diff] [blame] | 2480 | verifyFormat("SomeType &operator=(const SomeType &S);"); |
Daniel Jasper | dc2efa1 | 2013-02-15 19:24:08 +0000 | [diff] [blame] | 2481 | |
Daniel Jasper | 2b4c924 | 2013-02-11 08:01:18 +0000 | [diff] [blame] | 2482 | verifyGoogleFormat("operator void*();"); |
| 2483 | verifyGoogleFormat("operator SomeType<SomeType<int>>();"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 2484 | } |
| 2485 | |
Daniel Jasper | 088dab5 | 2013-01-11 16:09:04 +0000 | [diff] [blame] | 2486 | TEST_F(FormatTest, UnderstandsNewAndDelete) { |
Daniel Jasper | 37eff83 | 2013-02-23 08:07:18 +0000 | [diff] [blame] | 2487 | verifyFormat("void f() {\n" |
| 2488 | " A *a = new A;\n" |
| 2489 | " A *a = new (placement) A;\n" |
| 2490 | " delete a;\n" |
| 2491 | " delete (A *)a;\n" |
| 2492 | "}"); |
Daniel Jasper | 088dab5 | 2013-01-11 16:09:04 +0000 | [diff] [blame] | 2493 | } |
| 2494 | |
Daniel Jasper | 5d33440 | 2013-01-02 08:57:10 +0000 | [diff] [blame] | 2495 | TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) { |
Manuel Klimek | 36fab8d | 2013-01-10 13:24:24 +0000 | [diff] [blame] | 2496 | verifyFormat("int *f(int *a) {}"); |
Manuel Klimek | 36fab8d | 2013-01-10 13:24:24 +0000 | [diff] [blame] | 2497 | verifyFormat("int main(int argc, char **argv) {}"); |
Daniel Jasper | d0f349b | 2013-02-18 12:44:35 +0000 | [diff] [blame] | 2498 | verifyFormat("Test::Test(int b) : a(b * b) {}"); |
Daniel Jasper | 4bfc65a | 2013-01-23 12:10:53 +0000 | [diff] [blame] | 2499 | verifyIndependentOfContext("f(a, *a);"); |
Daniel Jasper | 8a5d7cd | 2013-03-01 17:13:29 +0000 | [diff] [blame] | 2500 | verifyFormat("void g() { f(*a); }"); |
Daniel Jasper | 4bfc65a | 2013-01-23 12:10:53 +0000 | [diff] [blame] | 2501 | verifyIndependentOfContext("int a = b * 10;"); |
| 2502 | verifyIndependentOfContext("int a = 10 * b;"); |
| 2503 | verifyIndependentOfContext("int a = b * c;"); |
| 2504 | verifyIndependentOfContext("int a += b * c;"); |
| 2505 | verifyIndependentOfContext("int a -= b * c;"); |
| 2506 | verifyIndependentOfContext("int a *= b * c;"); |
| 2507 | verifyIndependentOfContext("int a /= b * c;"); |
| 2508 | verifyIndependentOfContext("int a = *b;"); |
| 2509 | verifyIndependentOfContext("int a = *b * c;"); |
| 2510 | verifyIndependentOfContext("int a = b * *c;"); |
| 2511 | verifyIndependentOfContext("return 10 * b;"); |
| 2512 | verifyIndependentOfContext("return *b * *c;"); |
| 2513 | verifyIndependentOfContext("return a & ~b;"); |
| 2514 | verifyIndependentOfContext("f(b ? *c : *d);"); |
| 2515 | verifyIndependentOfContext("int a = b ? *c : *d;"); |
| 2516 | verifyIndependentOfContext("*b = a;"); |
| 2517 | verifyIndependentOfContext("a * ~b;"); |
| 2518 | verifyIndependentOfContext("a * !b;"); |
| 2519 | verifyIndependentOfContext("a * +b;"); |
| 2520 | verifyIndependentOfContext("a * -b;"); |
| 2521 | verifyIndependentOfContext("a * ++b;"); |
| 2522 | verifyIndependentOfContext("a * --b;"); |
| 2523 | verifyIndependentOfContext("a[4] * b;"); |
Daniel Jasper | 9c65b06 | 2013-02-27 11:43:50 +0000 | [diff] [blame] | 2524 | verifyIndependentOfContext("a[a * a] = 1;"); |
Daniel Jasper | 4bfc65a | 2013-01-23 12:10:53 +0000 | [diff] [blame] | 2525 | verifyIndependentOfContext("f() * b;"); |
| 2526 | verifyIndependentOfContext("a * [self dostuff];"); |
Daniel Jasper | 81d2d38 | 2013-04-01 17:13:26 +0000 | [diff] [blame] | 2527 | verifyIndependentOfContext("int x = a * (a + b);"); |
Daniel Jasper | 4bfc65a | 2013-01-23 12:10:53 +0000 | [diff] [blame] | 2528 | verifyIndependentOfContext("(a *)(a + b);"); |
| 2529 | verifyIndependentOfContext("int *pa = (int *)&a;"); |
Nico Weber | 95e8e46 | 2013-02-12 16:17:07 +0000 | [diff] [blame] | 2530 | verifyIndependentOfContext("return sizeof(int **);"); |
| 2531 | verifyIndependentOfContext("return sizeof(int ******);"); |
| 2532 | verifyIndependentOfContext("return (int **&)a;"); |
Daniel Jasper | 8a5d7cd | 2013-03-01 17:13:29 +0000 | [diff] [blame] | 2533 | verifyFormat("void f(Type (*parameter)[10]) {}"); |
Nico Weber | 95e8e46 | 2013-02-12 16:17:07 +0000 | [diff] [blame] | 2534 | verifyGoogleFormat("return sizeof(int**);"); |
| 2535 | verifyIndependentOfContext("Type **A = static_cast<Type **>(P);"); |
| 2536 | verifyGoogleFormat("Type** A = static_cast<Type**>(P);"); |
| 2537 | // FIXME: The newline is wrong. |
| 2538 | verifyFormat("auto a = [](int **&, int ***) {}\n;"); |
Daniel Jasper | c74e279 | 2012-12-07 09:52:15 +0000 | [diff] [blame] | 2539 | |
Daniel Jasper | 4bfc65a | 2013-01-23 12:10:53 +0000 | [diff] [blame] | 2540 | verifyIndependentOfContext("InvalidRegions[*R] = 0;"); |
Daniel Jasper | 9bb0d28 | 2013-01-04 20:46:38 +0000 | [diff] [blame] | 2541 | |
Daniel Jasper | 4bfc65a | 2013-01-23 12:10:53 +0000 | [diff] [blame] | 2542 | verifyIndependentOfContext("A<int *> a;"); |
| 2543 | verifyIndependentOfContext("A<int **> a;"); |
| 2544 | verifyIndependentOfContext("A<int *, int *> a;"); |
Daniel Jasper | 4e77809 | 2013-02-06 10:05:46 +0000 | [diff] [blame] | 2545 | verifyIndependentOfContext( |
| 2546 | "const char *const p = reinterpret_cast<const char *const>(q);"); |
Daniel Jasper | 4bfc65a | 2013-01-23 12:10:53 +0000 | [diff] [blame] | 2547 | verifyIndependentOfContext("A<int **, int **> a;"); |
Daniel Jasper | 6b5ba8b | 2013-02-06 10:57:42 +0000 | [diff] [blame] | 2548 | verifyIndependentOfContext("void f(int *a = d * e, int *b = c * d);"); |
Daniel Jasper | d3cf17b | 2013-03-14 10:50:25 +0000 | [diff] [blame] | 2549 | verifyFormat("for (char **a = b; *a; ++a) {\n}"); |
Daniel Jasper | 8b39c66 | 2012-12-10 18:59:13 +0000 | [diff] [blame] | 2550 | |
Daniel Jasper | 2db356d | 2013-01-08 20:03:18 +0000 | [diff] [blame] | 2551 | verifyFormat( |
| 2552 | "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" |
| 2553 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaa, *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); |
| 2554 | |
Manuel Klimek | 36fab8d | 2013-01-10 13:24:24 +0000 | [diff] [blame] | 2555 | verifyGoogleFormat("int main(int argc, char** argv) {}"); |
Daniel Jasper | 8b39c66 | 2012-12-10 18:59:13 +0000 | [diff] [blame] | 2556 | verifyGoogleFormat("A<int*> a;"); |
| 2557 | verifyGoogleFormat("A<int**> a;"); |
| 2558 | verifyGoogleFormat("A<int*, int*> a;"); |
| 2559 | verifyGoogleFormat("A<int**, int**> a;"); |
Daniel Jasper | 5d33440 | 2013-01-02 08:57:10 +0000 | [diff] [blame] | 2560 | verifyGoogleFormat("f(b ? *c : *d);"); |
| 2561 | verifyGoogleFormat("int a = b ? *c : *d;"); |
Daniel Jasper | 48bd7b7 | 2013-01-16 16:04:06 +0000 | [diff] [blame] | 2562 | verifyGoogleFormat("Type* t = **x;"); |
| 2563 | verifyGoogleFormat("Type* t = *++*x;"); |
| 2564 | verifyGoogleFormat("*++*x;"); |
| 2565 | verifyGoogleFormat("Type* t = const_cast<T*>(&*x);"); |
| 2566 | verifyGoogleFormat("Type* t = x++ * y;"); |
Daniel Jasper | 4e77809 | 2013-02-06 10:05:46 +0000 | [diff] [blame] | 2567 | verifyGoogleFormat( |
| 2568 | "const char* const p = reinterpret_cast<const char* const>(q);"); |
Manuel Klimek | 1b6f4bd | 2013-01-14 10:58:01 +0000 | [diff] [blame] | 2569 | |
Daniel Jasper | 4bfc65a | 2013-01-23 12:10:53 +0000 | [diff] [blame] | 2570 | verifyIndependentOfContext("a = *(x + y);"); |
| 2571 | verifyIndependentOfContext("a = &(x + y);"); |
| 2572 | verifyIndependentOfContext("*(x + y).call();"); |
| 2573 | verifyIndependentOfContext("&(x + y)->call();"); |
Daniel Jasper | 8a5d7cd | 2013-03-01 17:13:29 +0000 | [diff] [blame] | 2574 | verifyFormat("void f() { &(*I).first; }"); |
Daniel Jasper | b369c2c | 2013-01-15 14:27:39 +0000 | [diff] [blame] | 2575 | |
Daniel Jasper | 4bfc65a | 2013-01-23 12:10:53 +0000 | [diff] [blame] | 2576 | verifyIndependentOfContext("f(b * /* confusing comment */ ++c);"); |
Daniel Jasper | b369c2c | 2013-01-15 14:27:39 +0000 | [diff] [blame] | 2577 | verifyFormat( |
| 2578 | "int *MyValues = {\n" |
| 2579 | " *A, // Operator detection might be confused by the '{'\n" |
| 2580 | " *BB // Operator detection might be confused by previous comment\n" |
| 2581 | "};"); |
Nico Weber | 5096a44 | 2013-01-17 17:17:19 +0000 | [diff] [blame] | 2582 | |
Daniel Jasper | 4bfc65a | 2013-01-23 12:10:53 +0000 | [diff] [blame] | 2583 | verifyIndependentOfContext("if (int *a = &b)"); |
| 2584 | verifyIndependentOfContext("if (int &a = *b)"); |
| 2585 | verifyIndependentOfContext("if (a & b[i])"); |
| 2586 | verifyIndependentOfContext("if (a::b::c::d & b[i])"); |
| 2587 | verifyIndependentOfContext("if (*b[i])"); |
| 2588 | verifyIndependentOfContext("if (int *a = (&b))"); |
| 2589 | verifyIndependentOfContext("while (int *a = &b)"); |
Daniel Jasper | 20d3583 | 2013-01-23 12:58:14 +0000 | [diff] [blame] | 2590 | verifyFormat("void f() {\n" |
| 2591 | " for (const int &v : Values) {\n" |
| 2592 | " }\n" |
| 2593 | "}"); |
Daniel Jasper | d0f349b | 2013-02-18 12:44:35 +0000 | [diff] [blame] | 2594 | verifyFormat("for (int i = a * a; i < 10; ++i) {\n}"); |
| 2595 | verifyFormat("for (int i = 0; i < a * a; ++i) {\n}"); |
Daniel Jasper | ffee171 | 2013-01-22 11:46:26 +0000 | [diff] [blame] | 2596 | |
Daniel Jasper | 6f21a98 | 2013-03-13 07:49:51 +0000 | [diff] [blame] | 2597 | verifyIndependentOfContext("A = new SomeType *[Length];"); |
Daniel Jasper | 4bfc65a | 2013-01-23 12:10:53 +0000 | [diff] [blame] | 2598 | verifyIndependentOfContext("A = new SomeType *[Length]();"); |
Daniel Jasper | ffee171 | 2013-01-22 11:46:26 +0000 | [diff] [blame] | 2599 | verifyGoogleFormat("A = new SomeType* [Length]();"); |
Daniel Jasper | 6f21a98 | 2013-03-13 07:49:51 +0000 | [diff] [blame] | 2600 | verifyGoogleFormat("A = new SomeType* [Length];"); |
Daniel Jasper | 8a5d7cd | 2013-03-01 17:13:29 +0000 | [diff] [blame] | 2601 | } |
Daniel Jasper | 8ff690a | 2013-02-06 14:22:40 +0000 | [diff] [blame] | 2602 | |
Daniel Jasper | 8a5d7cd | 2013-03-01 17:13:29 +0000 | [diff] [blame] | 2603 | TEST_F(FormatTest, AdaptivelyFormatsPointersAndReferences) { |
Daniel Jasper | 8ff690a | 2013-02-06 14:22:40 +0000 | [diff] [blame] | 2604 | EXPECT_EQ("int *a;\n" |
| 2605 | "int *a;\n" |
Daniel Jasper | 66d19bd | 2013-02-18 11:59:17 +0000 | [diff] [blame] | 2606 | "int *a;", |
| 2607 | format("int *a;\n" |
| 2608 | "int* a;\n" |
| 2609 | "int *a;", |
| 2610 | getGoogleStyle())); |
Daniel Jasper | 8ff690a | 2013-02-06 14:22:40 +0000 | [diff] [blame] | 2611 | EXPECT_EQ("int* a;\n" |
| 2612 | "int* a;\n" |
Daniel Jasper | 66d19bd | 2013-02-18 11:59:17 +0000 | [diff] [blame] | 2613 | "int* a;", |
| 2614 | format("int* a;\n" |
| 2615 | "int* a;\n" |
| 2616 | "int *a;", |
| 2617 | getGoogleStyle())); |
Daniel Jasper | 8ff690a | 2013-02-06 14:22:40 +0000 | [diff] [blame] | 2618 | EXPECT_EQ("int *a;\n" |
| 2619 | "int *a;\n" |
Daniel Jasper | 66d19bd | 2013-02-18 11:59:17 +0000 | [diff] [blame] | 2620 | "int *a;", |
| 2621 | format("int *a;\n" |
| 2622 | "int * a;\n" |
| 2623 | "int * a;", |
| 2624 | getGoogleStyle())); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 2625 | } |
| 2626 | |
Alexander Kornienko | 3fd9ccd | 2013-03-12 16:28:18 +0000 | [diff] [blame] | 2627 | TEST_F(FormatTest, UnderstandsRvalueReferences) { |
| 2628 | verifyFormat("int f(int &&a) {}"); |
| 2629 | verifyFormat("int f(int a, char &&b) {}"); |
| 2630 | verifyFormat("void f() { int &&a = b; }"); |
| 2631 | verifyGoogleFormat("int f(int a, char&& b) {}"); |
| 2632 | verifyGoogleFormat("void f() { int&& a = b; }"); |
| 2633 | |
| 2634 | // FIXME: These require somewhat deeper changes in template arguments |
| 2635 | // formatting. |
| 2636 | // verifyIndependentOfContext("A<int &&> a;"); |
| 2637 | // verifyIndependentOfContext("A<int &&, int &&> a;"); |
| 2638 | // verifyGoogleFormat("A<int&&> a;"); |
| 2639 | // verifyGoogleFormat("A<int&&, int&&> a;"); |
| 2640 | } |
| 2641 | |
Manuel Klimek | a32a7fd | 2013-01-23 14:08:21 +0000 | [diff] [blame] | 2642 | TEST_F(FormatTest, FormatsBinaryOperatorsPrecedingEquals) { |
| 2643 | verifyFormat("void f() {\n" |
| 2644 | " x[aaaaaaaaa -\n" |
Daniel Jasper | bf71ba2 | 2013-04-08 20:33:42 +0000 | [diff] [blame] | 2645 | " b] = 23;\n" |
Daniel Jasper | 66d19bd | 2013-02-18 11:59:17 +0000 | [diff] [blame] | 2646 | "}", |
| 2647 | getLLVMStyleWithColumns(15)); |
Manuel Klimek | a32a7fd | 2013-01-23 14:08:21 +0000 | [diff] [blame] | 2648 | } |
| 2649 | |
Daniel Jasper | 4981bd0 | 2013-01-13 08:01:36 +0000 | [diff] [blame] | 2650 | TEST_F(FormatTest, FormatsCasts) { |
| 2651 | verifyFormat("Type *A = static_cast<Type *>(P);"); |
| 2652 | verifyFormat("Type *A = (Type *)P;"); |
| 2653 | verifyFormat("Type *A = (vector<Type *, int *>)P;"); |
| 2654 | verifyFormat("int a = (int)(2.0f);"); |
| 2655 | |
| 2656 | // FIXME: These also need to be identified. |
| 2657 | verifyFormat("int a = (int) 2.0f;"); |
| 2658 | verifyFormat("int a = (int) * b;"); |
| 2659 | |
| 2660 | // These are not casts. |
| 2661 | verifyFormat("void f(int *) {}"); |
Nico Weber | 6fa7e74 | 2013-02-13 04:32:57 +0000 | [diff] [blame] | 2662 | verifyFormat("f(foo)->b;"); |
| 2663 | verifyFormat("f(foo).b;"); |
| 2664 | verifyFormat("f(foo)(b);"); |
| 2665 | verifyFormat("f(foo)[b];"); |
| 2666 | verifyFormat("[](foo) { return 4; }(bar)];"); |
| 2667 | verifyFormat("(*funptr)(foo)[4];"); |
| 2668 | verifyFormat("funptrs[4](foo)[4];"); |
Daniel Jasper | 4981bd0 | 2013-01-13 08:01:36 +0000 | [diff] [blame] | 2669 | verifyFormat("void f(int *);"); |
| 2670 | verifyFormat("void f(int *) = 0;"); |
| 2671 | verifyFormat("void f(SmallVector<int>) {}"); |
| 2672 | verifyFormat("void f(SmallVector<int>);"); |
| 2673 | verifyFormat("void f(SmallVector<int>) = 0;"); |
Nico Weber | 7728057 | 2013-02-09 18:02:07 +0000 | [diff] [blame] | 2674 | verifyFormat("void f(int i = (kValue) * kMask) {}"); |
| 2675 | verifyFormat("void f(int i = (kA * kB) & kMask) {}"); |
Nico Weber | 6fa7e74 | 2013-02-13 04:32:57 +0000 | [diff] [blame] | 2676 | verifyFormat("int a = sizeof(int) * b;"); |
| 2677 | verifyFormat("int a = alignof(int) * b;"); |
Daniel Jasper | faab0d3 | 2013-02-27 09:47:53 +0000 | [diff] [blame] | 2678 | |
Daniel Jasper | 37eff83 | 2013-02-23 08:07:18 +0000 | [diff] [blame] | 2679 | // These are not casts, but at some point were confused with casts. |
| 2680 | verifyFormat("virtual void foo(int *) override;"); |
| 2681 | verifyFormat("virtual void foo(char &) const;"); |
| 2682 | verifyFormat("virtual void foo(int *a, char *) const;"); |
Daniel Jasper | 6a365aa | 2013-03-13 17:13:53 +0000 | [diff] [blame] | 2683 | verifyFormat("int a = sizeof(int *) + b;"); |
| 2684 | verifyFormat("int a = alignof(int *) + b;"); |
Daniel Jasper | 4981bd0 | 2013-01-13 08:01:36 +0000 | [diff] [blame] | 2685 | } |
| 2686 | |
Daniel Jasper | 46ef852 | 2013-01-10 13:08:12 +0000 | [diff] [blame] | 2687 | TEST_F(FormatTest, FormatsFunctionTypes) { |
Daniel Jasper | 46ef852 | 2013-01-10 13:08:12 +0000 | [diff] [blame] | 2688 | verifyFormat("A<bool()> a;"); |
| 2689 | verifyFormat("A<SomeType()> a;"); |
| 2690 | verifyFormat("A<void(*)(int, std::string)> a;"); |
Daniel Jasper | 9322aae | 2013-03-20 09:53:18 +0000 | [diff] [blame] | 2691 | verifyFormat("A<void *(int)>;"); |
Daniel Jasper | 81d2d38 | 2013-04-01 17:13:26 +0000 | [diff] [blame] | 2692 | verifyFormat("void *(*a)(int *, SomeType *);"); |
Daniel Jasper | 46ef852 | 2013-01-10 13:08:12 +0000 | [diff] [blame] | 2693 | |
Daniel Jasper | 8a5d7cd | 2013-03-01 17:13:29 +0000 | [diff] [blame] | 2694 | // FIXME: Inconsistent. |
| 2695 | verifyFormat("int (*func)(void *);"); |
| 2696 | verifyFormat("void f() { int(*func)(void *); }"); |
Daniel Jasper | 9322aae | 2013-03-20 09:53:18 +0000 | [diff] [blame] | 2697 | |
Daniel Jasper | 81d2d38 | 2013-04-01 17:13:26 +0000 | [diff] [blame] | 2698 | verifyGoogleFormat("A<void*(int*, SomeType*)>;"); |
| 2699 | verifyGoogleFormat("void* (*a)(int);"); |
Daniel Jasper | 46ef852 | 2013-01-10 13:08:12 +0000 | [diff] [blame] | 2700 | } |
| 2701 | |
Daniel Jasper | 3c08a81 | 2013-02-24 18:54:32 +0000 | [diff] [blame] | 2702 | TEST_F(FormatTest, BreaksLongDeclarations) { |
Daniel Jasper | 83f25ba | 2013-01-28 15:16:31 +0000 | [diff] [blame] | 2703 | verifyFormat("int *someFunction(int LoooooooooooooooooooongParam1,\n" |
Daniel Jasper | f11a705 | 2013-02-21 21:33:55 +0000 | [diff] [blame] | 2704 | " int LoooooooooooooooooooongParam2) {}"); |
Daniel Jasper | 4dc41de | 2013-01-02 08:44:14 +0000 | [diff] [blame] | 2705 | verifyFormat( |
Daniel Jasper | 1407bee | 2013-04-11 14:29:13 +0000 | [diff] [blame] | 2706 | "TypeSpecDecl *TypeSpecDecl::Create(ASTContext &C, DeclContext *DC,\n" |
| 2707 | " SourceLocation L, IdentifierIn *II,\n" |
| 2708 | " Type *T) {}"); |
Daniel Jasper | 3c08a81 | 2013-02-24 18:54:32 +0000 | [diff] [blame] | 2709 | verifyFormat("ReallyLongReturnType<TemplateParam1, TemplateParam2>\n" |
| 2710 | "ReallyReallyLongFunctionName(\n" |
| 2711 | " const std::string &SomeParameter,\n" |
| 2712 | " const SomeType<string, SomeOtherTemplateParameter> &\n" |
| 2713 | " ReallyReallyLongParameterName,\n" |
| 2714 | " const SomeType<string, SomeOtherTemplateParameter> &\n" |
| 2715 | " AnotherLongParameterName) {}"); |
| 2716 | verifyFormat( |
| 2717 | "aaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaa<aaaaaaaaaaaaa, aaaaaaaaaaaa>\n" |
| 2718 | "aaaaaaaaaaaaaaaaaaaaaaa;"); |
| 2719 | |
Daniel Jasper | d75ff64 | 2013-01-28 15:40:20 +0000 | [diff] [blame] | 2720 | verifyGoogleFormat( |
Daniel Jasper | faab0d3 | 2013-02-27 09:47:53 +0000 | [diff] [blame] | 2721 | "TypeSpecDecl* TypeSpecDecl::Create(ASTContext& C, DeclContext* DC,\n" |
| 2722 | " SourceLocation L) {}"); |
Daniel Jasper | a03ab10 | 2013-02-13 20:33:44 +0000 | [diff] [blame] | 2723 | verifyGoogleFormat( |
| 2724 | "some_namespace::LongReturnType\n" |
| 2725 | "long_namespace::SomeVeryLongClass::SomeVeryLongFunction(\n" |
Daniel Jasper | f11a705 | 2013-02-21 21:33:55 +0000 | [diff] [blame] | 2726 | " int first_long_parameter, int second_parameter) {}"); |
Daniel Jasper | a03ab10 | 2013-02-13 20:33:44 +0000 | [diff] [blame] | 2727 | |
| 2728 | verifyGoogleFormat("template <typename T>\n" |
| 2729 | "aaaaaaaa::aaaaa::aaaaaa<T, aaaaaaaaaaaaaaaaaaaaaaaaa>\n" |
Daniel Jasper | f11a705 | 2013-02-21 21:33:55 +0000 | [diff] [blame] | 2730 | "aaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaa() {}"); |
Daniel Jasper | b6f02f3 | 2013-02-28 10:06:05 +0000 | [diff] [blame] | 2731 | verifyGoogleFormat("A<A<A>> aaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" |
| 2732 | " int aaaaaaaaaaaaaaaaaaaaaaa);"); |
Daniel Jasper | 4dc41de | 2013-01-02 08:44:14 +0000 | [diff] [blame] | 2733 | } |
| 2734 | |
Daniel Jasper | 3b5943f | 2012-12-06 09:56:08 +0000 | [diff] [blame] | 2735 | TEST_F(FormatTest, LineStartsWithSpecialCharacter) { |
| 2736 | verifyFormat("(a)->b();"); |
| 2737 | verifyFormat("--a;"); |
| 2738 | } |
| 2739 | |
Daniel Jasper | 8822d3a | 2012-12-04 13:02:32 +0000 | [diff] [blame] | 2740 | TEST_F(FormatTest, HandlesIncludeDirectives) { |
Daniel Jasper | 2b9c10b | 2013-01-14 15:52:06 +0000 | [diff] [blame] | 2741 | verifyFormat("#include <string>\n" |
| 2742 | "#include <a/b/c.h>\n" |
| 2743 | "#include \"a/b/string\"\n" |
| 2744 | "#include \"string.h\"\n" |
| 2745 | "#include \"string.h\"\n" |
Manuel Klimek | 407a31a | 2013-01-15 15:50:27 +0000 | [diff] [blame] | 2746 | "#include <a-a>\n" |
Daniel Jasper | 3a20441 | 2013-02-23 07:46:38 +0000 | [diff] [blame] | 2747 | "#include < path with space >\n" |
| 2748 | "#include \"some very long include paaaaaaaaaaaaaaaaaaaaaaath\"", |
| 2749 | getLLVMStyleWithColumns(35)); |
Nico Weber | b23ae0c | 2012-12-21 18:21:56 +0000 | [diff] [blame] | 2750 | |
Daniel Jasper | 8134e1e | 2013-01-13 08:12:18 +0000 | [diff] [blame] | 2751 | verifyFormat("#import <string>"); |
| 2752 | verifyFormat("#import <a/b/c.h>"); |
| 2753 | verifyFormat("#import \"a/b/string\""); |
| 2754 | verifyFormat("#import \"string.h\""); |
| 2755 | verifyFormat("#import \"string.h\""); |
Daniel Jasper | 8822d3a | 2012-12-04 13:02:32 +0000 | [diff] [blame] | 2756 | } |
| 2757 | |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 2758 | //===----------------------------------------------------------------------===// |
| 2759 | // Error recovery tests. |
| 2760 | //===----------------------------------------------------------------------===// |
| 2761 | |
Daniel Jasper | c5cfa49 | 2013-02-14 09:19:04 +0000 | [diff] [blame] | 2762 | TEST_F(FormatTest, IncompleteParameterLists) { |
Daniel Jasper | faab0d3 | 2013-02-27 09:47:53 +0000 | [diff] [blame] | 2763 | FormatStyle NoBinPacking = getLLVMStyle(); |
| 2764 | NoBinPacking.BinPackParameters = false; |
| 2765 | verifyFormat("void aaaaaaaaaaaaaaaaaa(int level,\n" |
| 2766 | " double *min_x,\n" |
| 2767 | " double *max_x,\n" |
| 2768 | " double *min_y,\n" |
| 2769 | " double *max_y,\n" |
| 2770 | " double *min_z,\n" |
| 2771 | " double *max_z, ) {}", |
| 2772 | NoBinPacking); |
Daniel Jasper | c5cfa49 | 2013-02-14 09:19:04 +0000 | [diff] [blame] | 2773 | } |
| 2774 | |
Daniel Jasper | 700e710 | 2013-01-10 09:26:47 +0000 | [diff] [blame] | 2775 | TEST_F(FormatTest, IncorrectCodeTrailingStuff) { |
Alexander Kornienko | 51c23fa | 2013-01-16 11:45:16 +0000 | [diff] [blame] | 2776 | verifyFormat("void f() { return; }\n42"); |
| 2777 | verifyFormat("void f() {\n" |
| 2778 | " if (0)\n" |
| 2779 | " return;\n" |
| 2780 | "}\n" |
| 2781 | "42"); |
Alexander Kornienko | d881875 | 2013-01-16 11:43:46 +0000 | [diff] [blame] | 2782 | verifyFormat("void f() { return }\n42"); |
| 2783 | verifyFormat("void f() {\n" |
| 2784 | " if (0)\n" |
| 2785 | " return\n" |
| 2786 | "}\n" |
| 2787 | "42"); |
| 2788 | } |
| 2789 | |
| 2790 | TEST_F(FormatTest, IncorrectCodeMissingSemicolon) { |
| 2791 | EXPECT_EQ("void f() { return }", format("void f ( ) { return }")); |
| 2792 | EXPECT_EQ("void f() {\n" |
| 2793 | " if (a)\n" |
| 2794 | " return\n" |
Daniel Jasper | 66d19bd | 2013-02-18 11:59:17 +0000 | [diff] [blame] | 2795 | "}", |
| 2796 | format("void f ( ) { if ( a ) return }")); |
Alexander Kornienko | d881875 | 2013-01-16 11:43:46 +0000 | [diff] [blame] | 2797 | EXPECT_EQ("namespace N { void f() }", format("namespace N { void f() }")); |
| 2798 | EXPECT_EQ("namespace N {\n" |
| 2799 | "void f() {}\n" |
| 2800 | "void g()\n" |
Daniel Jasper | 66d19bd | 2013-02-18 11:59:17 +0000 | [diff] [blame] | 2801 | "}", |
| 2802 | format("namespace N { void f( ) { } void g( ) }")); |
Daniel Jasper | 700e710 | 2013-01-10 09:26:47 +0000 | [diff] [blame] | 2803 | } |
| 2804 | |
Daniel Jasper | ceb99ab | 2013-01-09 10:16:05 +0000 | [diff] [blame] | 2805 | TEST_F(FormatTest, IndentationWithinColumnLimitNotPossible) { |
| 2806 | verifyFormat("int aaaaaaaa =\n" |
Alexander Kornienko | 5262dd9 | 2013-03-27 11:52:18 +0000 | [diff] [blame] | 2807 | " // Overlylongcomment\n" |
Daniel Jasper | 66d19bd | 2013-02-18 11:59:17 +0000 | [diff] [blame] | 2808 | " b;", |
| 2809 | getLLVMStyleWithColumns(20)); |
Daniel Jasper | ceb99ab | 2013-01-09 10:16:05 +0000 | [diff] [blame] | 2810 | verifyFormat("function(\n" |
| 2811 | " ShortArgument,\n" |
Daniel Jasper | 66d19bd | 2013-02-18 11:59:17 +0000 | [diff] [blame] | 2812 | " LoooooooooooongArgument);\n", |
| 2813 | getLLVMStyleWithColumns(20)); |
Daniel Jasper | ceb99ab | 2013-01-09 10:16:05 +0000 | [diff] [blame] | 2814 | } |
| 2815 | |
Alexander Kornienko | 56e49c5 | 2012-12-10 16:34:48 +0000 | [diff] [blame] | 2816 | TEST_F(FormatTest, IncorrectAccessSpecifier) { |
| 2817 | verifyFormat("public:"); |
| 2818 | verifyFormat("class A {\n" |
| 2819 | "public\n" |
Manuel Klimek | 36fab8d | 2013-01-10 13:24:24 +0000 | [diff] [blame] | 2820 | " void f() {}\n" |
Alexander Kornienko | 56e49c5 | 2012-12-10 16:34:48 +0000 | [diff] [blame] | 2821 | "};"); |
| 2822 | verifyFormat("public\n" |
| 2823 | "int qwerty;"); |
| 2824 | verifyFormat("public\n" |
Manuel Klimek | 36fab8d | 2013-01-10 13:24:24 +0000 | [diff] [blame] | 2825 | "B {}"); |
Alexander Kornienko | 56e49c5 | 2012-12-10 16:34:48 +0000 | [diff] [blame] | 2826 | verifyFormat("public\n" |
Manuel Klimek | 36fab8d | 2013-01-10 13:24:24 +0000 | [diff] [blame] | 2827 | "{}"); |
Alexander Kornienko | 56e49c5 | 2012-12-10 16:34:48 +0000 | [diff] [blame] | 2828 | verifyFormat("public\n" |
Manuel Klimek | 517e894 | 2013-01-11 17:54:10 +0000 | [diff] [blame] | 2829 | "B { int x; }"); |
Alexander Kornienko | 56e49c5 | 2012-12-10 16:34:48 +0000 | [diff] [blame] | 2830 | } |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 2831 | |
Daniel Jasper | 92f9faf | 2013-03-20 15:58:10 +0000 | [diff] [blame] | 2832 | TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) { |
| 2833 | verifyFormat("{"); |
| 2834 | verifyFormat("#})"); |
| 2835 | } |
Alexander Kornienko | 393b008 | 2012-12-04 15:40:36 +0000 | [diff] [blame] | 2836 | |
| 2837 | TEST_F(FormatTest, IncorrectCodeDoNoWhile) { |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 2838 | verifyFormat("do {\n}"); |
| 2839 | verifyFormat("do {\n}\n" |
Alexander Kornienko | 393b008 | 2012-12-04 15:40:36 +0000 | [diff] [blame] | 2840 | "f();"); |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 2841 | verifyFormat("do {\n}\n" |
Alexander Kornienko | 393b008 | 2012-12-04 15:40:36 +0000 | [diff] [blame] | 2842 | "wheeee(fun);"); |
| 2843 | verifyFormat("do {\n" |
| 2844 | " f();\n" |
Manuel Klimek | de76854 | 2013-01-07 18:10:23 +0000 | [diff] [blame] | 2845 | "}"); |
Alexander Kornienko | 393b008 | 2012-12-04 15:40:36 +0000 | [diff] [blame] | 2846 | } |
| 2847 | |
Manuel Klimek | 6eca03f | 2013-01-11 19:23:05 +0000 | [diff] [blame] | 2848 | TEST_F(FormatTest, IncorrectCodeMissingParens) { |
Manuel Klimek | d465843 | 2013-01-11 18:28:36 +0000 | [diff] [blame] | 2849 | verifyFormat("if {\n foo;\n foo();\n}"); |
Manuel Klimek | 6eca03f | 2013-01-11 19:23:05 +0000 | [diff] [blame] | 2850 | verifyFormat("switch {\n foo;\n foo();\n}"); |
| 2851 | verifyFormat("for {\n foo;\n foo();\n}"); |
| 2852 | verifyFormat("while {\n foo;\n foo();\n}"); |
| 2853 | verifyFormat("do {\n foo;\n foo();\n} while;"); |
Manuel Klimek | d465843 | 2013-01-11 18:28:36 +0000 | [diff] [blame] | 2854 | } |
| 2855 | |
Daniel Jasper | 1f42f11 | 2013-01-04 18:52:56 +0000 | [diff] [blame] | 2856 | TEST_F(FormatTest, DoesNotTouchUnwrappedLinesWithErrors) { |
| 2857 | verifyFormat("namespace {\n" |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 2858 | "class Foo { Foo ( }; } // comment"); |
Daniel Jasper | 1f42f11 | 2013-01-04 18:52:56 +0000 | [diff] [blame] | 2859 | } |
| 2860 | |
Alexander Kornienko | cff563c | 2012-12-04 17:27:50 +0000 | [diff] [blame] | 2861 | TEST_F(FormatTest, IncorrectCodeErrorDetection) { |
Manuel Klimek | 36fab8d | 2013-01-10 13:24:24 +0000 | [diff] [blame] | 2862 | EXPECT_EQ("{\n{}\n", format("{\n{\n}\n")); |
| 2863 | EXPECT_EQ("{\n {}\n", format("{\n {\n}\n")); |
| 2864 | EXPECT_EQ("{\n {}\n", format("{\n {\n }\n")); |
| 2865 | EXPECT_EQ("{\n {}\n }\n}\n", format("{\n {\n }\n }\n}\n")); |
Alexander Kornienko | cff563c | 2012-12-04 17:27:50 +0000 | [diff] [blame] | 2866 | |
Alexander Kornienko | cff563c | 2012-12-04 17:27:50 +0000 | [diff] [blame] | 2867 | EXPECT_EQ("{\n" |
| 2868 | " {\n" |
| 2869 | " breakme(\n" |
| 2870 | " qwe);\n" |
Daniel Jasper | 66d19bd | 2013-02-18 11:59:17 +0000 | [diff] [blame] | 2871 | "}\n", |
| 2872 | format("{\n" |
| 2873 | " {\n" |
| 2874 | " breakme(qwe);\n" |
| 2875 | "}\n", |
| 2876 | getLLVMStyleWithColumns(10))); |
Alexander Kornienko | cff563c | 2012-12-04 17:27:50 +0000 | [diff] [blame] | 2877 | } |
| 2878 | |
Manuel Klimek | 2851c16 | 2013-01-10 14:36:46 +0000 | [diff] [blame] | 2879 | TEST_F(FormatTest, LayoutCallsInsideBraceInitializers) { |
Daniel Jasper | 66d19bd | 2013-02-18 11:59:17 +0000 | [diff] [blame] | 2880 | verifyFormat("int x = {\n" |
| 2881 | " avariable,\n" |
| 2882 | " b(alongervariable)\n" |
| 2883 | "};", |
| 2884 | getLLVMStyleWithColumns(25)); |
Manuel Klimek | 2851c16 | 2013-01-10 14:36:46 +0000 | [diff] [blame] | 2885 | } |
| 2886 | |
Manuel Klimek | c44ee89 | 2013-01-21 10:07:49 +0000 | [diff] [blame] | 2887 | TEST_F(FormatTest, LayoutBraceInitializersInReturnStatement) { |
| 2888 | verifyFormat("return (a)(b) { 1, 2, 3 };"); |
| 2889 | } |
| 2890 | |
Manuel Klimek | 2851c16 | 2013-01-10 14:36:46 +0000 | [diff] [blame] | 2891 | TEST_F(FormatTest, LayoutTokensFollowingBlockInParentheses) { |
Daniel Jasper | 7d81281 | 2013-02-21 15:00:29 +0000 | [diff] [blame] | 2892 | // FIXME: This is bad, find a better and more generic solution. |
Manuel Klimek | 2851c16 | 2013-01-10 14:36:46 +0000 | [diff] [blame] | 2893 | verifyFormat( |
| 2894 | "Aaa({\n" |
| 2895 | " int i;\n" |
Daniel Jasper | 7d81281 | 2013-02-21 15:00:29 +0000 | [diff] [blame] | 2896 | "},\n" |
| 2897 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n" |
| 2898 | " ccccccccccccccccc));"); |
Manuel Klimek | 2851c16 | 2013-01-10 14:36:46 +0000 | [diff] [blame] | 2899 | } |
| 2900 | |
Manuel Klimek | 517e894 | 2013-01-11 17:54:10 +0000 | [diff] [blame] | 2901 | TEST_F(FormatTest, PullTrivialFunctionDefinitionsIntoSingleLine) { |
| 2902 | verifyFormat("void f() { return 42; }"); |
| 2903 | verifyFormat("void f() {\n" |
| 2904 | " // Comment\n" |
| 2905 | "}"); |
| 2906 | verifyFormat("{\n" |
| 2907 | "#error {\n" |
| 2908 | " int a;\n" |
| 2909 | "}"); |
| 2910 | verifyFormat("{\n" |
| 2911 | " int a;\n" |
| 2912 | "#error {\n" |
| 2913 | "}"); |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 2914 | |
| 2915 | verifyFormat("void f() { return 42; }", getLLVMStyleWithColumns(23)); |
| 2916 | verifyFormat("void f() {\n return 42;\n}", getLLVMStyleWithColumns(22)); |
| 2917 | |
| 2918 | verifyFormat("void f() {}", getLLVMStyleWithColumns(11)); |
| 2919 | verifyFormat("void f() {\n}", getLLVMStyleWithColumns(10)); |
Manuel Klimek | 517e894 | 2013-01-11 17:54:10 +0000 | [diff] [blame] | 2920 | } |
| 2921 | |
Manuel Klimek | 47ea7f6 | 2013-01-15 13:38:33 +0000 | [diff] [blame] | 2922 | TEST_F(FormatTest, UnderstandContextOfRecordTypeKeywords) { |
| 2923 | // Elaborate type variable declarations. |
Manuel Klimek | 606e07e | 2013-01-11 18:13:04 +0000 | [diff] [blame] | 2924 | verifyFormat("struct foo a = { bar };\nint n;"); |
Manuel Klimek | 47ea7f6 | 2013-01-15 13:38:33 +0000 | [diff] [blame] | 2925 | verifyFormat("class foo a = { bar };\nint n;"); |
| 2926 | verifyFormat("union foo a = { bar };\nint n;"); |
| 2927 | |
| 2928 | // Elaborate types inside function definitions. |
| 2929 | verifyFormat("struct foo f() {}\nint n;"); |
| 2930 | verifyFormat("class foo f() {}\nint n;"); |
| 2931 | verifyFormat("union foo f() {}\nint n;"); |
| 2932 | |
| 2933 | // Templates. |
| 2934 | verifyFormat("template <class X> void f() {}\nint n;"); |
| 2935 | verifyFormat("template <struct X> void f() {}\nint n;"); |
| 2936 | verifyFormat("template <union X> void f() {}\nint n;"); |
| 2937 | |
| 2938 | // Actual definitions... |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 2939 | verifyFormat("struct {\n} n;"); |
| 2940 | verifyFormat( |
| 2941 | "template <template <class T, class Y>, class Z> class X {\n} n;"); |
Manuel Klimek | 47ea7f6 | 2013-01-15 13:38:33 +0000 | [diff] [blame] | 2942 | verifyFormat("union Z {\n int n;\n} x;"); |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 2943 | verifyFormat("class MACRO Z {\n} n;"); |
| 2944 | verifyFormat("class MACRO(X) Z {\n} n;"); |
| 2945 | verifyFormat("class __attribute__(X) Z {\n} n;"); |
| 2946 | verifyFormat("class __declspec(X) Z {\n} n;"); |
Manuel Klimek | b8b1ce1 | 2013-02-06 15:57:54 +0000 | [diff] [blame] | 2947 | verifyFormat("class A##B##C {\n} n;"); |
Manuel Klimek | 47ea7f6 | 2013-01-15 13:38:33 +0000 | [diff] [blame] | 2948 | |
Manuel Klimek | 7f5b025 | 2013-01-21 10:17:14 +0000 | [diff] [blame] | 2949 | // Redefinition from nested context: |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 2950 | verifyFormat("class A::B::C {\n} n;"); |
Manuel Klimek | 7f5b025 | 2013-01-21 10:17:14 +0000 | [diff] [blame] | 2951 | |
Manuel Klimek | 3a3408c | 2013-01-21 13:58:54 +0000 | [diff] [blame] | 2952 | // Template definitions. |
| 2953 | // FIXME: This is still incorrectly handled at the formatter side. |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 2954 | verifyFormat("template <> struct X < 15, i < 3 && 42 < 50 && 33<28> {\n};"); |
Manuel Klimek | 3a3408c | 2013-01-21 13:58:54 +0000 | [diff] [blame] | 2955 | |
| 2956 | // FIXME: |
| 2957 | // This now gets parsed incorrectly as class definition. |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 2958 | // verifyFormat("class A<int> f() {\n}\nint n;"); |
Manuel Klimek | 3a3408c | 2013-01-21 13:58:54 +0000 | [diff] [blame] | 2959 | |
Manuel Klimek | 47ea7f6 | 2013-01-15 13:38:33 +0000 | [diff] [blame] | 2960 | // Elaborate types where incorrectly parsing the structural element would |
| 2961 | // break the indent. |
| 2962 | verifyFormat("if (true)\n" |
| 2963 | " class X x;\n" |
| 2964 | "else\n" |
| 2965 | " f();\n"); |
Daniel Jasper | 6fe554e | 2013-03-20 15:12:38 +0000 | [diff] [blame] | 2966 | |
| 2967 | // This is simply incomplete. Formatting is not important, but must not crash. |
| 2968 | verifyFormat("class A:"); |
Manuel Klimek | 606e07e | 2013-01-11 18:13:04 +0000 | [diff] [blame] | 2969 | } |
| 2970 | |
Manuel Klimek | 407a31a | 2013-01-15 15:50:27 +0000 | [diff] [blame] | 2971 | TEST_F(FormatTest, DoNotInterfereWithErrorAndWarning) { |
| 2972 | verifyFormat("#error Leave all white!!!!! space* alone!\n"); |
| 2973 | verifyFormat("#warning Leave all white!!!!! space* alone!\n"); |
| 2974 | EXPECT_EQ("#error 1", format(" # error 1")); |
| 2975 | EXPECT_EQ("#warning 1", format(" # warning 1")); |
| 2976 | } |
| 2977 | |
Daniel Jasper | aae7bad | 2013-04-23 13:54:04 +0000 | [diff] [blame] | 2978 | TEST_F(FormatTest, FormatHashIfExpressions) { |
| 2979 | // FIXME: Come up with a better indentation for #elif. |
| 2980 | verifyFormat( |
| 2981 | "#if !defined(AAAAAAA) && (defined CCCCCC || defined DDDDDD) && \\\n" |
| 2982 | " defined(BBBBBBBB)\n" |
| 2983 | "#elif !defined(AAAAAA) && (defined CCCCC || defined DDDDDD) && \\\n" |
| 2984 | " defined(BBBBBBBB)\n" |
| 2985 | "#endif", |
| 2986 | getLLVMStyleWithColumns(65)); |
| 2987 | } |
| 2988 | |
Manuel Klimek | 525fe16 | 2013-01-18 14:04:34 +0000 | [diff] [blame] | 2989 | TEST_F(FormatTest, MergeHandlingInTheFaceOfPreprocessorDirectives) { |
| 2990 | FormatStyle AllowsMergedIf = getGoogleStyle(); |
| 2991 | AllowsMergedIf.AllowShortIfStatementsOnASingleLine = true; |
| 2992 | verifyFormat("void f() { f(); }\n#error E", AllowsMergedIf); |
| 2993 | verifyFormat("if (true) return 42;\n#error E", AllowsMergedIf); |
Manuel Klimek | 4c12812 | 2013-01-18 14:46:43 +0000 | [diff] [blame] | 2994 | verifyFormat("if (true)\n#error E\n return 42;", AllowsMergedIf); |
| 2995 | EXPECT_EQ("if (true) return 42;", |
| 2996 | format("if (true)\nreturn 42;", AllowsMergedIf)); |
| 2997 | FormatStyle ShortMergedIf = AllowsMergedIf; |
| 2998 | ShortMergedIf.ColumnLimit = 25; |
Daniel Jasper | 2972d04 | 2013-04-25 08:56:26 +0000 | [diff] [blame] | 2999 | verifyFormat("#define A \\\n" |
Daniel Jasper | 66d19bd | 2013-02-18 11:59:17 +0000 | [diff] [blame] | 3000 | " if (true) return 42;", |
| 3001 | ShortMergedIf); |
Daniel Jasper | 2972d04 | 2013-04-25 08:56:26 +0000 | [diff] [blame] | 3002 | verifyFormat("#define A \\\n" |
| 3003 | " f(); \\\n" |
Manuel Klimek | 4c12812 | 2013-01-18 14:46:43 +0000 | [diff] [blame] | 3004 | " if (true)\n" |
Daniel Jasper | 66d19bd | 2013-02-18 11:59:17 +0000 | [diff] [blame] | 3005 | "#define B", |
| 3006 | ShortMergedIf); |
Daniel Jasper | 2972d04 | 2013-04-25 08:56:26 +0000 | [diff] [blame] | 3007 | verifyFormat("#define A \\\n" |
| 3008 | " f(); \\\n" |
Manuel Klimek | 4c12812 | 2013-01-18 14:46:43 +0000 | [diff] [blame] | 3009 | " if (true)\n" |
Daniel Jasper | 66d19bd | 2013-02-18 11:59:17 +0000 | [diff] [blame] | 3010 | "g();", |
| 3011 | ShortMergedIf); |
Manuel Klimek | 0fbe008 | 2013-01-21 14:16:56 +0000 | [diff] [blame] | 3012 | verifyFormat("{\n" |
| 3013 | "#ifdef A\n" |
| 3014 | " // Comment\n" |
| 3015 | " if (true) continue;\n" |
| 3016 | "#endif\n" |
| 3017 | " // Comment\n" |
Daniel Jasper | 66d19bd | 2013-02-18 11:59:17 +0000 | [diff] [blame] | 3018 | " if (true) continue;", |
| 3019 | ShortMergedIf); |
Manuel Klimek | 525fe16 | 2013-01-18 14:04:34 +0000 | [diff] [blame] | 3020 | } |
| 3021 | |
Manuel Klimek | 86721d2 | 2013-01-22 16:31:55 +0000 | [diff] [blame] | 3022 | TEST_F(FormatTest, BlockCommentsInControlLoops) { |
| 3023 | verifyFormat("if (0) /* a comment in a strange place */ {\n" |
| 3024 | " f();\n" |
| 3025 | "}"); |
| 3026 | verifyFormat("if (0) /* a comment in a strange place */ {\n" |
| 3027 | " f();\n" |
| 3028 | "} /* another comment */ else /* comment #3 */ {\n" |
| 3029 | " g();\n" |
| 3030 | "}"); |
| 3031 | verifyFormat("while (0) /* a comment in a strange place */ {\n" |
| 3032 | " f();\n" |
| 3033 | "}"); |
| 3034 | verifyFormat("for (;;) /* a comment in a strange place */ {\n" |
| 3035 | " f();\n" |
| 3036 | "}"); |
| 3037 | verifyFormat("do /* a comment in a strange place */ {\n" |
| 3038 | " f();\n" |
| 3039 | "} /* another comment */ while (0);"); |
| 3040 | } |
| 3041 | |
| 3042 | TEST_F(FormatTest, BlockComments) { |
| 3043 | EXPECT_EQ("/* */ /* */ /* */\n/* */ /* */ /* */", |
| 3044 | format("/* *//* */ /* */\n/* *//* */ /* */")); |
Daniel Jasper | 66d19bd | 2013-02-18 11:59:17 +0000 | [diff] [blame] | 3045 | EXPECT_EQ("/* */ a /* */ b;", format(" /* */ a/* */ b;")); |
Alexander Kornienko | 7c22cf3 | 2013-03-21 12:28:10 +0000 | [diff] [blame] | 3046 | EXPECT_EQ("#define A /*123*/\\\n" |
Manuel Klimek | 86721d2 | 2013-01-22 16:31:55 +0000 | [diff] [blame] | 3047 | " b\n" |
| 3048 | "/* */\n" |
| 3049 | "someCall(\n" |
| 3050 | " parameter);", |
Alexander Kornienko | 7c22cf3 | 2013-03-21 12:28:10 +0000 | [diff] [blame] | 3051 | format("#define A /*123*/ b\n" |
Manuel Klimek | 86721d2 | 2013-01-22 16:31:55 +0000 | [diff] [blame] | 3052 | "/* */\n" |
Daniel Jasper | 66d19bd | 2013-02-18 11:59:17 +0000 | [diff] [blame] | 3053 | "someCall(parameter);", |
| 3054 | getLLVMStyleWithColumns(15))); |
Manuel Klimek | 86721d2 | 2013-01-22 16:31:55 +0000 | [diff] [blame] | 3055 | |
| 3056 | EXPECT_EQ("#define A\n" |
| 3057 | "/* */ someCall(\n" |
| 3058 | " parameter);", |
| 3059 | format("#define A\n" |
Daniel Jasper | 66d19bd | 2013-02-18 11:59:17 +0000 | [diff] [blame] | 3060 | "/* */someCall(parameter);", |
| 3061 | getLLVMStyleWithColumns(15))); |
Daniel Jasper | 2752ff3 | 2013-02-04 07:32:14 +0000 | [diff] [blame] | 3062 | |
Daniel Jasper | faab0d3 | 2013-02-27 09:47:53 +0000 | [diff] [blame] | 3063 | FormatStyle NoBinPacking = getLLVMStyle(); |
| 3064 | NoBinPacking.BinPackParameters = false; |
Daniel Jasper | 2752ff3 | 2013-02-04 07:32:14 +0000 | [diff] [blame] | 3065 | EXPECT_EQ("someFunction(1, /* comment 1 */\n" |
| 3066 | " 2, /* comment 2 */\n" |
| 3067 | " 3, /* comment 3 */\n" |
Daniel Jasper | cda1650 | 2013-02-04 08:34:57 +0000 | [diff] [blame] | 3068 | " aaaa,\n" |
| 3069 | " bbbb);", |
Daniel Jasper | 2752ff3 | 2013-02-04 07:32:14 +0000 | [diff] [blame] | 3070 | format("someFunction (1, /* comment 1 */\n" |
| 3071 | " 2, /* comment 2 */ \n" |
| 3072 | " 3, /* comment 3 */\n" |
Daniel Jasper | 66d19bd | 2013-02-18 11:59:17 +0000 | [diff] [blame] | 3073 | "aaaa, bbbb );", |
Daniel Jasper | faab0d3 | 2013-02-27 09:47:53 +0000 | [diff] [blame] | 3074 | NoBinPacking)); |
Daniel Jasper | 36ba0ff | 2013-02-06 15:23:09 +0000 | [diff] [blame] | 3075 | verifyFormat( |
| 3076 | "bool aaaaaaaaaaaaa = /* comment: */ aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" |
| 3077 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); |
| 3078 | EXPECT_EQ( |
| 3079 | "bool aaaaaaaaaaaaa = /* trailing comment */\n" |
| 3080 | " aaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaa ||\n" |
| 3081 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaa;", |
| 3082 | format( |
| 3083 | "bool aaaaaaaaaaaaa = /* trailing comment */\n" |
| 3084 | " aaaaaaaaaaaaaaaaaaaaaaaaaaa||aaaaaaaaaaaaaaaaaaaaaaaaa ||\n" |
| 3085 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaa;")); |
Daniel Jasper | 15417ef | 2013-02-06 20:07:35 +0000 | [diff] [blame] | 3086 | EXPECT_EQ( |
| 3087 | "int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; /* comment */\n" |
| 3088 | "int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; /* comment */\n" |
| 3089 | "int cccccccccccccccccccccccccccccc; /* comment */\n", |
| 3090 | format("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; /* comment */\n" |
| 3091 | "int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; /* comment */\n" |
| 3092 | "int cccccccccccccccccccccccccccccc; /* comment */\n")); |
Manuel Klimek | 86721d2 | 2013-01-22 16:31:55 +0000 | [diff] [blame] | 3093 | } |
| 3094 | |
Manuel Klimek | b3507cd | 2013-02-06 16:40:56 +0000 | [diff] [blame] | 3095 | TEST_F(FormatTest, BlockCommentsInMacros) { |
| 3096 | EXPECT_EQ("#define A \\\n" |
| 3097 | " { \\\n" |
| 3098 | " /* one line */ \\\n" |
| 3099 | " someCall();", |
| 3100 | format("#define A { \\\n" |
| 3101 | " /* one line */ \\\n" |
Daniel Jasper | 66d19bd | 2013-02-18 11:59:17 +0000 | [diff] [blame] | 3102 | " someCall();", |
| 3103 | getLLVMStyleWithColumns(20))); |
Manuel Klimek | b3507cd | 2013-02-06 16:40:56 +0000 | [diff] [blame] | 3104 | EXPECT_EQ("#define A \\\n" |
| 3105 | " { \\\n" |
| 3106 | " /* previous */ \\\n" |
| 3107 | " /* one line */ \\\n" |
| 3108 | " someCall();", |
| 3109 | format("#define A { \\\n" |
| 3110 | " /* previous */ \\\n" |
| 3111 | " /* one line */ \\\n" |
Daniel Jasper | 66d19bd | 2013-02-18 11:59:17 +0000 | [diff] [blame] | 3112 | " someCall();", |
| 3113 | getLLVMStyleWithColumns(20))); |
Manuel Klimek | b3507cd | 2013-02-06 16:40:56 +0000 | [diff] [blame] | 3114 | } |
| 3115 | |
| 3116 | TEST_F(FormatTest, IndentLineCommentsInStartOfBlockAtEndOfFile) { |
| 3117 | // FIXME: This is not what we want... |
| 3118 | verifyFormat("{\n" |
| 3119 | "// a" |
| 3120 | "// b"); |
| 3121 | } |
| 3122 | |
Manuel Klimek | 092a2c7 | 2013-01-23 10:09:28 +0000 | [diff] [blame] | 3123 | TEST_F(FormatTest, FormatStarDependingOnContext) { |
Manuel Klimek | 70b03f4 | 2013-01-23 09:32:48 +0000 | [diff] [blame] | 3124 | verifyFormat("void f(int *a);"); |
| 3125 | verifyFormat("void f() { f(fint * b); }"); |
Manuel Klimek | 836b58f | 2013-01-23 11:03:04 +0000 | [diff] [blame] | 3126 | verifyFormat("class A {\n void f(int *a);\n};"); |
| 3127 | verifyFormat("class A {\n int *a;\n};"); |
| 3128 | verifyFormat("namespace a {\n" |
| 3129 | "namespace b {\n" |
| 3130 | "class A {\n" |
| 3131 | " void f() {}\n" |
| 3132 | " int *a;\n" |
| 3133 | "};\n" |
| 3134 | "}\n" |
| 3135 | "}"); |
Manuel Klimek | 70b03f4 | 2013-01-23 09:32:48 +0000 | [diff] [blame] | 3136 | } |
| 3137 | |
Manuel Klimek | 092a2c7 | 2013-01-23 10:09:28 +0000 | [diff] [blame] | 3138 | TEST_F(FormatTest, SpecialTokensAtEndOfLine) { |
| 3139 | verifyFormat("while"); |
| 3140 | verifyFormat("operator"); |
| 3141 | } |
| 3142 | |
Nico Weber | cf4a79c | 2013-01-08 17:56:31 +0000 | [diff] [blame] | 3143 | //===----------------------------------------------------------------------===// |
| 3144 | // Objective-C tests. |
| 3145 | //===----------------------------------------------------------------------===// |
| 3146 | |
Fariborz Jahanian | 154120c | 2012-12-20 19:54:13 +0000 | [diff] [blame] | 3147 | TEST_F(FormatTest, FormatForObjectiveCMethodDecls) { |
| 3148 | verifyFormat("- (void)sendAction:(SEL)aSelector to:(BOOL)anObject;"); |
| 3149 | EXPECT_EQ("- (NSUInteger)indexOfObject:(id)anObject;", |
| 3150 | format("-(NSUInteger)indexOfObject:(id)anObject;")); |
Daniel Jasper | cd16238 | 2013-01-07 13:26:07 +0000 | [diff] [blame] | 3151 | EXPECT_EQ("- (NSInteger)Mthod1;", format("-(NSInteger)Mthod1;")); |
Fariborz Jahanian | 154120c | 2012-12-20 19:54:13 +0000 | [diff] [blame] | 3152 | EXPECT_EQ("+ (id)Mthod2;", format("+(id)Mthod2;")); |
| 3153 | EXPECT_EQ("- (NSInteger)Method3:(id)anObject;", |
| 3154 | format("-(NSInteger)Method3:(id)anObject;")); |
| 3155 | EXPECT_EQ("- (NSInteger)Method4:(id)anObject;", |
| 3156 | format("-(NSInteger)Method4:(id)anObject;")); |
| 3157 | EXPECT_EQ("- (NSInteger)Method5:(id)anObject:(id)AnotherObject;", |
| 3158 | format("-(NSInteger)Method5:(id)anObject:(id)AnotherObject;")); |
| 3159 | EXPECT_EQ("- (id)Method6:(id)A:(id)B:(id)C:(id)D;", |
| 3160 | format("- (id)Method6:(id)A:(id)B:(id)C:(id)D;")); |
Daniel Jasper | cd16238 | 2013-01-07 13:26:07 +0000 | [diff] [blame] | 3161 | EXPECT_EQ( |
| 3162 | "- (void)sendAction:(SEL)aSelector to:(id)anObject forAllCells:(BOOL)flag;", |
Daniel Jasper | 66d19bd | 2013-02-18 11:59:17 +0000 | [diff] [blame] | 3163 | format( |
| 3164 | "- (void)sendAction:(SEL)aSelector to:(id)anObject forAllCells:(BOOL)flag;")); |
Fariborz Jahanian | 9b3f02c | 2012-12-21 22:51:18 +0000 | [diff] [blame] | 3165 | |
| 3166 | // Very long objectiveC method declaration. |
Daniel Jasper | 63d7ced | 2013-02-05 10:07:47 +0000 | [diff] [blame] | 3167 | verifyFormat("- (NSUInteger)indexOfObject:(id)anObject\n" |
| 3168 | " inRange:(NSRange)range\n" |
| 3169 | " outRange:(NSRange)out_range\n" |
| 3170 | " outRange1:(NSRange)out_range1\n" |
| 3171 | " outRange2:(NSRange)out_range2\n" |
| 3172 | " outRange3:(NSRange)out_range3\n" |
| 3173 | " outRange4:(NSRange)out_range4\n" |
| 3174 | " outRange5:(NSRange)out_range5\n" |
| 3175 | " outRange6:(NSRange)out_range6\n" |
| 3176 | " outRange7:(NSRange)out_range7\n" |
| 3177 | " outRange8:(NSRange)out_range8\n" |
| 3178 | " outRange9:(NSRange)out_range9;"); |
Nico Weber | e0fd429 | 2013-01-10 20:18:33 +0000 | [diff] [blame] | 3179 | |
| 3180 | verifyFormat("- (int)sum:(vector<int>)numbers;"); |
Nico Weber | aab6005 | 2013-01-17 06:14:50 +0000 | [diff] [blame] | 3181 | verifyGoogleFormat("- (void)setDelegate:(id<Protocol>)delegate;"); |
Nico Weber | e0fd429 | 2013-01-10 20:18:33 +0000 | [diff] [blame] | 3182 | // FIXME: In LLVM style, there should be a space in front of a '<' for ObjC |
| 3183 | // protocol lists (but not for template classes): |
| 3184 | //verifyFormat("- (void)setDelegate:(id <Protocol>)delegate;"); |
Nico Weber | cd52bda | 2013-01-10 23:11:41 +0000 | [diff] [blame] | 3185 | |
| 3186 | verifyFormat("- (int(*)())foo:(int(*)())f;"); |
Nico Weber | aab6005 | 2013-01-17 06:14:50 +0000 | [diff] [blame] | 3187 | verifyGoogleFormat("- (int(*)())foo:(int(*)())foo;"); |
Nico Weber | cd52bda | 2013-01-10 23:11:41 +0000 | [diff] [blame] | 3188 | |
| 3189 | // If there's no return type (very rare in practice!), LLVM and Google style |
| 3190 | // agree. |
Daniel Jasper | 627707b | 2013-03-22 16:55:40 +0000 | [diff] [blame] | 3191 | verifyFormat("- foo;"); |
Nico Weber | cd52bda | 2013-01-10 23:11:41 +0000 | [diff] [blame] | 3192 | verifyFormat("- foo:(int)f;"); |
| 3193 | verifyGoogleFormat("- foo:(int)foo;"); |
Fariborz Jahanian | 154120c | 2012-12-20 19:54:13 +0000 | [diff] [blame] | 3194 | } |
| 3195 | |
Daniel Jasper | 886568d | 2013-01-09 08:36:49 +0000 | [diff] [blame] | 3196 | TEST_F(FormatTest, FormatObjCBlocks) { |
Daniel Jasper | 46ef852 | 2013-01-10 13:08:12 +0000 | [diff] [blame] | 3197 | verifyFormat("int (^Block)(int, int);"); |
| 3198 | verifyFormat("int (^Block1)(int, int) = ^(int i, int j)"); |
Daniel Jasper | 886568d | 2013-01-09 08:36:49 +0000 | [diff] [blame] | 3199 | } |
| 3200 | |
Nico Weber | 27d1367 | 2013-01-09 20:25:35 +0000 | [diff] [blame] | 3201 | TEST_F(FormatTest, FormatObjCInterface) { |
Nico Weber | 5f500df | 2013-01-10 20:12:55 +0000 | [diff] [blame] | 3202 | verifyFormat("@interface Foo : NSObject <NSSomeDelegate> {\n" |
Nico Weber | 27d1367 | 2013-01-09 20:25:35 +0000 | [diff] [blame] | 3203 | "@public\n" |
| 3204 | " int field1;\n" |
| 3205 | "@protected\n" |
| 3206 | " int field2;\n" |
| 3207 | "@private\n" |
| 3208 | " int field3;\n" |
| 3209 | "@package\n" |
| 3210 | " int field4;\n" |
| 3211 | "}\n" |
| 3212 | "+ (id)init;\n" |
| 3213 | "@end"); |
| 3214 | |
Nico Weber | 27d1367 | 2013-01-09 20:25:35 +0000 | [diff] [blame] | 3215 | verifyGoogleFormat("@interface Foo : NSObject<NSSomeDelegate> {\n" |
| 3216 | " @public\n" |
| 3217 | " int field1;\n" |
| 3218 | " @protected\n" |
| 3219 | " int field2;\n" |
| 3220 | " @private\n" |
| 3221 | " int field3;\n" |
| 3222 | " @package\n" |
| 3223 | " int field4;\n" |
| 3224 | "}\n" |
Nico Weber | aab6005 | 2013-01-17 06:14:50 +0000 | [diff] [blame] | 3225 | "+ (id)init;\n" |
Nico Weber | 27d1367 | 2013-01-09 20:25:35 +0000 | [diff] [blame] | 3226 | "@end"); |
| 3227 | |
Nico Weber | f5ecfa5 | 2013-01-22 16:53:59 +0000 | [diff] [blame] | 3228 | verifyFormat("@interface /* wait for it */ Foo\n" |
Nico Weber | 27d1367 | 2013-01-09 20:25:35 +0000 | [diff] [blame] | 3229 | "+ (id)init;\n" |
| 3230 | "// Look, a comment!\n" |
| 3231 | "- (int)answerWith:(int)i;\n" |
| 3232 | "@end"); |
| 3233 | |
| 3234 | verifyFormat("@interface Foo\n" |
Nico Weber | 049c447 | 2013-01-09 21:42:32 +0000 | [diff] [blame] | 3235 | "@end\n" |
| 3236 | "@interface Bar\n" |
Nico Weber | 27d1367 | 2013-01-09 20:25:35 +0000 | [diff] [blame] | 3237 | "@end"); |
| 3238 | |
| 3239 | verifyFormat("@interface Foo : Bar\n" |
| 3240 | "+ (id)init;\n" |
| 3241 | "@end"); |
| 3242 | |
Nico Weber | f5ecfa5 | 2013-01-22 16:53:59 +0000 | [diff] [blame] | 3243 | verifyFormat("@interface Foo : /**/ Bar /**/ <Baz, /**/ Quux>\n" |
Nico Weber | 27d1367 | 2013-01-09 20:25:35 +0000 | [diff] [blame] | 3244 | "+ (id)init;\n" |
| 3245 | "@end"); |
| 3246 | |
Nico Weber | 5f500df | 2013-01-10 20:12:55 +0000 | [diff] [blame] | 3247 | verifyGoogleFormat("@interface Foo : Bar<Baz, Quux>\n" |
Nico Weber | aab6005 | 2013-01-17 06:14:50 +0000 | [diff] [blame] | 3248 | "+ (id)init;\n" |
Nico Weber | 5f500df | 2013-01-10 20:12:55 +0000 | [diff] [blame] | 3249 | "@end"); |
| 3250 | |
Nico Weber | ed91bba | 2013-01-10 19:19:14 +0000 | [diff] [blame] | 3251 | verifyFormat("@interface Foo (HackStuff)\n" |
Nico Weber | 27d1367 | 2013-01-09 20:25:35 +0000 | [diff] [blame] | 3252 | "+ (id)init;\n" |
| 3253 | "@end"); |
| 3254 | |
Nico Weber | ed91bba | 2013-01-10 19:19:14 +0000 | [diff] [blame] | 3255 | verifyFormat("@interface Foo ()\n" |
Nico Weber | 27d1367 | 2013-01-09 20:25:35 +0000 | [diff] [blame] | 3256 | "+ (id)init;\n" |
| 3257 | "@end"); |
| 3258 | |
Nico Weber | 5f500df | 2013-01-10 20:12:55 +0000 | [diff] [blame] | 3259 | verifyFormat("@interface Foo (HackStuff) <MyProtocol>\n" |
Nico Weber | 27d1367 | 2013-01-09 20:25:35 +0000 | [diff] [blame] | 3260 | "+ (id)init;\n" |
| 3261 | "@end"); |
| 3262 | |
Nico Weber | 5f500df | 2013-01-10 20:12:55 +0000 | [diff] [blame] | 3263 | verifyGoogleFormat("@interface Foo (HackStuff)<MyProtocol>\n" |
Nico Weber | aab6005 | 2013-01-17 06:14:50 +0000 | [diff] [blame] | 3264 | "+ (id)init;\n" |
Nico Weber | 5f500df | 2013-01-10 20:12:55 +0000 | [diff] [blame] | 3265 | "@end"); |
| 3266 | |
Manuel Klimek | d5688cf | 2013-01-11 19:17:44 +0000 | [diff] [blame] | 3267 | verifyFormat("@interface Foo {\n" |
| 3268 | " int _i;\n" |
| 3269 | "}\n" |
Nico Weber | 27d1367 | 2013-01-09 20:25:35 +0000 | [diff] [blame] | 3270 | "+ (id)init;\n" |
| 3271 | "@end"); |
| 3272 | |
Manuel Klimek | d5688cf | 2013-01-11 19:17:44 +0000 | [diff] [blame] | 3273 | verifyFormat("@interface Foo : Bar {\n" |
| 3274 | " int _i;\n" |
| 3275 | "}\n" |
Nico Weber | 27d1367 | 2013-01-09 20:25:35 +0000 | [diff] [blame] | 3276 | "+ (id)init;\n" |
| 3277 | "@end"); |
| 3278 | |
Manuel Klimek | d5688cf | 2013-01-11 19:17:44 +0000 | [diff] [blame] | 3279 | verifyFormat("@interface Foo : Bar <Baz, Quux> {\n" |
| 3280 | " int _i;\n" |
| 3281 | "}\n" |
Nico Weber | 27d1367 | 2013-01-09 20:25:35 +0000 | [diff] [blame] | 3282 | "+ (id)init;\n" |
| 3283 | "@end"); |
| 3284 | |
Manuel Klimek | d5688cf | 2013-01-11 19:17:44 +0000 | [diff] [blame] | 3285 | verifyFormat("@interface Foo (HackStuff) {\n" |
| 3286 | " int _i;\n" |
| 3287 | "}\n" |
Nico Weber | 27d1367 | 2013-01-09 20:25:35 +0000 | [diff] [blame] | 3288 | "+ (id)init;\n" |
| 3289 | "@end"); |
| 3290 | |
Manuel Klimek | d5688cf | 2013-01-11 19:17:44 +0000 | [diff] [blame] | 3291 | verifyFormat("@interface Foo () {\n" |
| 3292 | " int _i;\n" |
| 3293 | "}\n" |
Nico Weber | 27d1367 | 2013-01-09 20:25:35 +0000 | [diff] [blame] | 3294 | "+ (id)init;\n" |
| 3295 | "@end"); |
| 3296 | |
Manuel Klimek | d5688cf | 2013-01-11 19:17:44 +0000 | [diff] [blame] | 3297 | verifyFormat("@interface Foo (HackStuff) <MyProtocol> {\n" |
| 3298 | " int _i;\n" |
| 3299 | "}\n" |
Nico Weber | 27d1367 | 2013-01-09 20:25:35 +0000 | [diff] [blame] | 3300 | "+ (id)init;\n" |
| 3301 | "@end"); |
| 3302 | } |
| 3303 | |
Nico Weber | 50767d8 | 2013-01-09 23:25:37 +0000 | [diff] [blame] | 3304 | TEST_F(FormatTest, FormatObjCImplementation) { |
| 3305 | verifyFormat("@implementation Foo : NSObject {\n" |
| 3306 | "@public\n" |
| 3307 | " int field1;\n" |
| 3308 | "@protected\n" |
| 3309 | " int field2;\n" |
| 3310 | "@private\n" |
| 3311 | " int field3;\n" |
| 3312 | "@package\n" |
| 3313 | " int field4;\n" |
| 3314 | "}\n" |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 3315 | "+ (id)init {\n}\n" |
Nico Weber | 50767d8 | 2013-01-09 23:25:37 +0000 | [diff] [blame] | 3316 | "@end"); |
| 3317 | |
| 3318 | verifyGoogleFormat("@implementation Foo : NSObject {\n" |
| 3319 | " @public\n" |
| 3320 | " int field1;\n" |
| 3321 | " @protected\n" |
| 3322 | " int field2;\n" |
| 3323 | " @private\n" |
| 3324 | " int field3;\n" |
| 3325 | " @package\n" |
| 3326 | " int field4;\n" |
| 3327 | "}\n" |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 3328 | "+ (id)init {\n}\n" |
Nico Weber | 50767d8 | 2013-01-09 23:25:37 +0000 | [diff] [blame] | 3329 | "@end"); |
| 3330 | |
| 3331 | verifyFormat("@implementation Foo\n" |
| 3332 | "+ (id)init {\n" |
Daniel Jasper | 6f5bb2c | 2013-01-14 16:24:39 +0000 | [diff] [blame] | 3333 | " if (true)\n" |
| 3334 | " return nil;\n" |
Nico Weber | 50767d8 | 2013-01-09 23:25:37 +0000 | [diff] [blame] | 3335 | "}\n" |
| 3336 | "// Look, a comment!\n" |
Manuel Klimek | d5688cf | 2013-01-11 19:17:44 +0000 | [diff] [blame] | 3337 | "- (int)answerWith:(int)i {\n" |
| 3338 | " return i;\n" |
| 3339 | "}\n" |
Nico Weber | 67015ed | 2013-01-11 21:14:08 +0000 | [diff] [blame] | 3340 | "+ (int)answerWith:(int)i {\n" |
| 3341 | " return i;\n" |
| 3342 | "}\n" |
Nico Weber | 50767d8 | 2013-01-09 23:25:37 +0000 | [diff] [blame] | 3343 | "@end"); |
| 3344 | |
| 3345 | verifyFormat("@implementation Foo\n" |
| 3346 | "@end\n" |
| 3347 | "@implementation Bar\n" |
| 3348 | "@end"); |
| 3349 | |
| 3350 | verifyFormat("@implementation Foo : Bar\n" |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 3351 | "+ (id)init {\n}\n" |
| 3352 | "- (void)foo {\n}\n" |
Nico Weber | 50767d8 | 2013-01-09 23:25:37 +0000 | [diff] [blame] | 3353 | "@end"); |
| 3354 | |
Manuel Klimek | d5688cf | 2013-01-11 19:17:44 +0000 | [diff] [blame] | 3355 | verifyFormat("@implementation Foo {\n" |
| 3356 | " int _i;\n" |
| 3357 | "}\n" |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 3358 | "+ (id)init {\n}\n" |
Nico Weber | 50767d8 | 2013-01-09 23:25:37 +0000 | [diff] [blame] | 3359 | "@end"); |
| 3360 | |
Manuel Klimek | d5688cf | 2013-01-11 19:17:44 +0000 | [diff] [blame] | 3361 | verifyFormat("@implementation Foo : Bar {\n" |
| 3362 | " int _i;\n" |
| 3363 | "}\n" |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 3364 | "+ (id)init {\n}\n" |
Nico Weber | 50767d8 | 2013-01-09 23:25:37 +0000 | [diff] [blame] | 3365 | "@end"); |
| 3366 | |
Nico Weber | ed91bba | 2013-01-10 19:19:14 +0000 | [diff] [blame] | 3367 | verifyFormat("@implementation Foo (HackStuff)\n" |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 3368 | "+ (id)init {\n}\n" |
Nico Weber | 50767d8 | 2013-01-09 23:25:37 +0000 | [diff] [blame] | 3369 | "@end"); |
| 3370 | } |
| 3371 | |
Nico Weber | 1abe6ea | 2013-01-09 21:15:03 +0000 | [diff] [blame] | 3372 | TEST_F(FormatTest, FormatObjCProtocol) { |
| 3373 | verifyFormat("@protocol Foo\n" |
| 3374 | "@property(weak) id delegate;\n" |
| 3375 | "- (NSUInteger)numberOfThings;\n" |
| 3376 | "@end"); |
| 3377 | |
Nico Weber | 5f500df | 2013-01-10 20:12:55 +0000 | [diff] [blame] | 3378 | verifyFormat("@protocol MyProtocol <NSObject>\n" |
Nico Weber | 1abe6ea | 2013-01-09 21:15:03 +0000 | [diff] [blame] | 3379 | "- (NSUInteger)numberOfThings;\n" |
| 3380 | "@end"); |
| 3381 | |
Nico Weber | 5f500df | 2013-01-10 20:12:55 +0000 | [diff] [blame] | 3382 | verifyGoogleFormat("@protocol MyProtocol<NSObject>\n" |
Nico Weber | aab6005 | 2013-01-17 06:14:50 +0000 | [diff] [blame] | 3383 | "- (NSUInteger)numberOfThings;\n" |
Nico Weber | 5f500df | 2013-01-10 20:12:55 +0000 | [diff] [blame] | 3384 | "@end"); |
| 3385 | |
Nico Weber | 1abe6ea | 2013-01-09 21:15:03 +0000 | [diff] [blame] | 3386 | verifyFormat("@protocol Foo;\n" |
| 3387 | "@protocol Bar;\n"); |
Nico Weber | 049c447 | 2013-01-09 21:42:32 +0000 | [diff] [blame] | 3388 | |
| 3389 | verifyFormat("@protocol Foo\n" |
| 3390 | "@end\n" |
| 3391 | "@protocol Bar\n" |
| 3392 | "@end"); |
Nico Weber | b530fa3 | 2013-01-10 00:25:19 +0000 | [diff] [blame] | 3393 | |
| 3394 | verifyFormat("@protocol myProtocol\n" |
| 3395 | "- (void)mandatoryWithInt:(int)i;\n" |
| 3396 | "@optional\n" |
| 3397 | "- (void)optional;\n" |
| 3398 | "@required\n" |
| 3399 | "- (void)required;\n" |
Nico Weber | 880e538 | 2013-01-10 00:42:07 +0000 | [diff] [blame] | 3400 | "@optional\n" |
| 3401 | "@property(assign) int madProp;\n" |
Nico Weber | b530fa3 | 2013-01-10 00:25:19 +0000 | [diff] [blame] | 3402 | "@end\n"); |
Nico Weber | 1abe6ea | 2013-01-09 21:15:03 +0000 | [diff] [blame] | 3403 | } |
| 3404 | |
Daniel Jasper | 63d7ced | 2013-02-05 10:07:47 +0000 | [diff] [blame] | 3405 | TEST_F(FormatTest, FormatObjCMethodDeclarations) { |
| 3406 | verifyFormat("- (void)doSomethingWith:(GTMFoo *)theFoo\n" |
| 3407 | " rect:(NSRect)theRect\n" |
| 3408 | " interval:(float)theInterval {\n" |
| 3409 | "}"); |
| 3410 | verifyFormat("- (void)shortf:(GTMFoo *)theFoo\n" |
| 3411 | " longKeyword:(NSRect)theRect\n" |
| 3412 | " evenLongerKeyword:(float)theInterval\n" |
| 3413 | " error:(NSError **)theError {\n" |
| 3414 | "}"); |
| 3415 | } |
| 3416 | |
Nico Weber | bcfdd26 | 2013-01-12 06:18:40 +0000 | [diff] [blame] | 3417 | TEST_F(FormatTest, FormatObjCMethodExpr) { |
| 3418 | verifyFormat("[foo bar:baz];"); |
| 3419 | verifyFormat("return [foo bar:baz];"); |
| 3420 | verifyFormat("f([foo bar:baz]);"); |
| 3421 | verifyFormat("f(2, [foo bar:baz]);"); |
| 3422 | verifyFormat("f(2, a ? b : c);"); |
| 3423 | verifyFormat("[[self initWithInt:4] bar:[baz quux:arrrr]];"); |
| 3424 | |
Nico Weber | ee0feec | 2013-02-05 16:21:00 +0000 | [diff] [blame] | 3425 | // Unary operators. |
| 3426 | verifyFormat("int a = +[foo bar:baz];"); |
| 3427 | verifyFormat("int a = -[foo bar:baz];"); |
| 3428 | verifyFormat("int a = ![foo bar:baz];"); |
| 3429 | verifyFormat("int a = ~[foo bar:baz];"); |
| 3430 | verifyFormat("int a = ++[foo bar:baz];"); |
| 3431 | verifyFormat("int a = --[foo bar:baz];"); |
| 3432 | verifyFormat("int a = sizeof [foo bar:baz];"); |
| 3433 | verifyFormat("int a = alignof [foo bar:baz];"); |
Nico Weber | e8a9798 | 2013-02-06 06:20:11 +0000 | [diff] [blame] | 3434 | verifyFormat("int a = &[foo bar:baz];"); |
| 3435 | verifyFormat("int a = *[foo bar:baz];"); |
Nico Weber | ee0feec | 2013-02-05 16:21:00 +0000 | [diff] [blame] | 3436 | // FIXME: Make casts work, without breaking f()[4]. |
Nico Weber | 4c2cc60 | 2013-02-13 03:48:27 +0000 | [diff] [blame] | 3437 | //verifyFormat("int a = (int)[foo bar:baz];"); |
Nico Weber | e0dc7c7 | 2013-02-13 04:40:03 +0000 | [diff] [blame] | 3438 | //verifyFormat("return (int)[foo bar:baz];"); |
| 3439 | //verifyFormat("(void)[foo bar:baz];"); |
Nico Weber | 4c2cc60 | 2013-02-13 03:48:27 +0000 | [diff] [blame] | 3440 | verifyFormat("return (MyType *)[self.tableView cellForRowAtIndexPath:cell];"); |
Nico Weber | ee0feec | 2013-02-05 16:21:00 +0000 | [diff] [blame] | 3441 | |
| 3442 | // Binary operators. |
Nico Weber | bcfdd26 | 2013-01-12 06:18:40 +0000 | [diff] [blame] | 3443 | verifyFormat("[foo bar:baz], [foo bar:baz];"); |
| 3444 | verifyFormat("[foo bar:baz] = [foo bar:baz];"); |
| 3445 | verifyFormat("[foo bar:baz] *= [foo bar:baz];"); |
| 3446 | verifyFormat("[foo bar:baz] /= [foo bar:baz];"); |
| 3447 | verifyFormat("[foo bar:baz] %= [foo bar:baz];"); |
| 3448 | verifyFormat("[foo bar:baz] += [foo bar:baz];"); |
| 3449 | verifyFormat("[foo bar:baz] -= [foo bar:baz];"); |
| 3450 | verifyFormat("[foo bar:baz] <<= [foo bar:baz];"); |
| 3451 | verifyFormat("[foo bar:baz] >>= [foo bar:baz];"); |
| 3452 | verifyFormat("[foo bar:baz] &= [foo bar:baz];"); |
| 3453 | verifyFormat("[foo bar:baz] ^= [foo bar:baz];"); |
| 3454 | verifyFormat("[foo bar:baz] |= [foo bar:baz];"); |
| 3455 | verifyFormat("[foo bar:baz] ? [foo bar:baz] : [foo bar:baz];"); |
| 3456 | verifyFormat("[foo bar:baz] || [foo bar:baz];"); |
| 3457 | verifyFormat("[foo bar:baz] && [foo bar:baz];"); |
| 3458 | verifyFormat("[foo bar:baz] | [foo bar:baz];"); |
| 3459 | verifyFormat("[foo bar:baz] ^ [foo bar:baz];"); |
| 3460 | verifyFormat("[foo bar:baz] & [foo bar:baz];"); |
| 3461 | verifyFormat("[foo bar:baz] == [foo bar:baz];"); |
| 3462 | verifyFormat("[foo bar:baz] != [foo bar:baz];"); |
| 3463 | verifyFormat("[foo bar:baz] >= [foo bar:baz];"); |
| 3464 | verifyFormat("[foo bar:baz] <= [foo bar:baz];"); |
| 3465 | verifyFormat("[foo bar:baz] > [foo bar:baz];"); |
| 3466 | verifyFormat("[foo bar:baz] < [foo bar:baz];"); |
| 3467 | verifyFormat("[foo bar:baz] >> [foo bar:baz];"); |
| 3468 | verifyFormat("[foo bar:baz] << [foo bar:baz];"); |
| 3469 | verifyFormat("[foo bar:baz] - [foo bar:baz];"); |
| 3470 | verifyFormat("[foo bar:baz] + [foo bar:baz];"); |
| 3471 | verifyFormat("[foo bar:baz] * [foo bar:baz];"); |
| 3472 | verifyFormat("[foo bar:baz] / [foo bar:baz];"); |
| 3473 | verifyFormat("[foo bar:baz] % [foo bar:baz];"); |
| 3474 | // Whew! |
| 3475 | |
Nico Weber | c2e6d2a | 2013-02-11 15:32:15 +0000 | [diff] [blame] | 3476 | verifyFormat("return in[42];"); |
| 3477 | verifyFormat("for (id foo in [self getStuffFor:bla]) {\n" |
| 3478 | "}"); |
| 3479 | |
Nico Weber | bcfdd26 | 2013-01-12 06:18:40 +0000 | [diff] [blame] | 3480 | verifyFormat("[self stuffWithInt:(4 + 2) float:4.5];"); |
| 3481 | verifyFormat("[self stuffWithInt:a ? b : c float:4.5];"); |
| 3482 | verifyFormat("[self stuffWithInt:a ? [self foo:bar] : c];"); |
| 3483 | verifyFormat("[self stuffWithInt:a ? (e ? f : g) : c];"); |
| 3484 | verifyFormat("[cond ? obj1 : obj2 methodWithParam:param]"); |
Nico Weber | 6292dd4 | 2013-01-12 23:41:33 +0000 | [diff] [blame] | 3485 | verifyFormat("[button setAction:@selector(zoomOut:)];"); |
Nico Weber | cd45833 | 2013-01-12 23:48:49 +0000 | [diff] [blame] | 3486 | verifyFormat("[color getRed:&r green:&g blue:&b alpha:&a];"); |
Daniel Jasper | 63d7ced | 2013-02-05 10:07:47 +0000 | [diff] [blame] | 3487 | |
Nico Weber | bcfdd26 | 2013-01-12 06:18:40 +0000 | [diff] [blame] | 3488 | verifyFormat("arr[[self indexForFoo:a]];"); |
| 3489 | verifyFormat("throw [self errorFor:a];"); |
| 3490 | verifyFormat("@throw [self errorFor:a];"); |
| 3491 | |
Nico Weber | e8ccc81 | 2013-01-12 22:48:47 +0000 | [diff] [blame] | 3492 | // This tests that the formatter doesn't break after "backing" but before ":", |
| 3493 | // which would be at 80 columns. |
Nico Weber | bcfdd26 | 2013-01-12 06:18:40 +0000 | [diff] [blame] | 3494 | verifyFormat( |
| 3495 | "void f() {\n" |
Daniel Jasper | 63d7ced | 2013-02-05 10:07:47 +0000 | [diff] [blame] | 3496 | " if ((self = [super initWithContentRect:contentRect\n" |
| 3497 | " styleMask:styleMask\n" |
| 3498 | " backing:NSBackingStoreBuffered\n" |
| 3499 | " defer:YES]))"); |
| 3500 | |
Daniel Jasper | 4e77809 | 2013-02-06 10:05:46 +0000 | [diff] [blame] | 3501 | verifyFormat( |
| 3502 | "[foo checkThatBreakingAfterColonWorksOk:\n" |
| 3503 | " [bar ifItDoes:reduceOverallLineLengthLikeInThisCase]];"); |
Daniel Jasper | 63d7ced | 2013-02-05 10:07:47 +0000 | [diff] [blame] | 3504 | |
| 3505 | verifyFormat("[myObj short:arg1 // Force line break\n" |
| 3506 | " longKeyword:arg2\n" |
| 3507 | " evenLongerKeyword:arg3\n" |
| 3508 | " error:arg4];"); |
| 3509 | verifyFormat( |
| 3510 | "void f() {\n" |
| 3511 | " popup_window_.reset([[RenderWidgetPopupWindow alloc]\n" |
| 3512 | " initWithContentRect:NSMakeRect(origin_global.x, origin_global.y,\n" |
| 3513 | " pos.width(), pos.height())\n" |
| 3514 | " styleMask:NSBorderlessWindowMask\n" |
| 3515 | " backing:NSBackingStoreBuffered\n" |
| 3516 | " defer:NO]);\n" |
| 3517 | "}"); |
Daniel Jasper | 4e77809 | 2013-02-06 10:05:46 +0000 | [diff] [blame] | 3518 | verifyFormat("[contentsContainer replaceSubview:[subviews objectAtIndex:0]\n" |
| 3519 | " with:contentsNativeView];"); |
| 3520 | |
| 3521 | verifyFormat( |
| 3522 | "[pboard addTypes:[NSArray arrayWithObject:kBookmarkButtonDragType]\n" |
| 3523 | " owner:nillllll];"); |
| 3524 | |
Daniel Jasper | 4e77809 | 2013-02-06 10:05:46 +0000 | [diff] [blame] | 3525 | verifyFormat( |
Daniel Jasper | ce3d1a6 | 2013-02-08 08:22:00 +0000 | [diff] [blame] | 3526 | "[pboard setData:[NSData dataWithBytes:&button length:sizeof(button)]\n" |
Daniel Jasper | 4e77809 | 2013-02-06 10:05:46 +0000 | [diff] [blame] | 3527 | " forType:kBookmarkButtonDragType];"); |
| 3528 | |
| 3529 | verifyFormat("[defaultCenter addObserver:self\n" |
| 3530 | " selector:@selector(willEnterFullscreen)\n" |
| 3531 | " name:kWillEnterFullscreenNotification\n" |
| 3532 | " object:nil];"); |
Daniel Jasper | 9e9e6e0 | 2013-02-06 16:00:26 +0000 | [diff] [blame] | 3533 | verifyFormat("[image_rep drawInRect:drawRect\n" |
| 3534 | " fromRect:NSZeroRect\n" |
| 3535 | " operation:NSCompositeCopy\n" |
| 3536 | " fraction:1.0\n" |
| 3537 | " respectFlipped:NO\n" |
| 3538 | " hints:nil];"); |
Nico Weber | 4ed7f3e | 2013-02-06 16:54:35 +0000 | [diff] [blame] | 3539 | |
| 3540 | verifyFormat( |
| 3541 | "scoped_nsobject<NSTextField> message(\n" |
| 3542 | " // The frame will be fixed up when |-setMessageText:| is called.\n" |
| 3543 | " [[NSTextField alloc] initWithFrame:NSMakeRect(0, 0, 0, 0)]);"); |
Nico Weber | bcfdd26 | 2013-01-12 06:18:40 +0000 | [diff] [blame] | 3544 | } |
| 3545 | |
Nico Weber | 581f557 | 2013-01-07 15:56:25 +0000 | [diff] [blame] | 3546 | TEST_F(FormatTest, ObjCAt) { |
Nico Weber | 4a293a2 | 2013-01-07 16:07:07 +0000 | [diff] [blame] | 3547 | verifyFormat("@autoreleasepool"); |
Nico Weber | d0af4b4 | 2013-01-07 16:14:28 +0000 | [diff] [blame] | 3548 | verifyFormat("@catch"); |
| 3549 | verifyFormat("@class"); |
Nico Weber | 4a293a2 | 2013-01-07 16:07:07 +0000 | [diff] [blame] | 3550 | verifyFormat("@compatibility_alias"); |
| 3551 | verifyFormat("@defs"); |
Nico Weber | efcfe73 | 2013-01-07 15:17:23 +0000 | [diff] [blame] | 3552 | verifyFormat("@dynamic"); |
Nico Weber | 4a293a2 | 2013-01-07 16:07:07 +0000 | [diff] [blame] | 3553 | verifyFormat("@encode"); |
| 3554 | verifyFormat("@end"); |
| 3555 | verifyFormat("@finally"); |
| 3556 | verifyFormat("@implementation"); |
| 3557 | verifyFormat("@import"); |
| 3558 | verifyFormat("@interface"); |
| 3559 | verifyFormat("@optional"); |
| 3560 | verifyFormat("@package"); |
Nico Weber | d0af4b4 | 2013-01-07 16:14:28 +0000 | [diff] [blame] | 3561 | verifyFormat("@private"); |
Nico Weber | 4a293a2 | 2013-01-07 16:07:07 +0000 | [diff] [blame] | 3562 | verifyFormat("@property"); |
Nico Weber | d0af4b4 | 2013-01-07 16:14:28 +0000 | [diff] [blame] | 3563 | verifyFormat("@protected"); |
Nico Weber | 4a293a2 | 2013-01-07 16:07:07 +0000 | [diff] [blame] | 3564 | verifyFormat("@protocol"); |
Nico Weber | d0af4b4 | 2013-01-07 16:14:28 +0000 | [diff] [blame] | 3565 | verifyFormat("@public"); |
Nico Weber | 4a293a2 | 2013-01-07 16:07:07 +0000 | [diff] [blame] | 3566 | verifyFormat("@required"); |
| 3567 | verifyFormat("@selector"); |
| 3568 | verifyFormat("@synchronized"); |
| 3569 | verifyFormat("@synthesize"); |
Nico Weber | d0af4b4 | 2013-01-07 16:14:28 +0000 | [diff] [blame] | 3570 | verifyFormat("@throw"); |
| 3571 | verifyFormat("@try"); |
Nico Weber | 4a293a2 | 2013-01-07 16:07:07 +0000 | [diff] [blame] | 3572 | |
Nico Weber | 581f557 | 2013-01-07 15:56:25 +0000 | [diff] [blame] | 3573 | EXPECT_EQ("@interface", format("@ interface")); |
| 3574 | |
| 3575 | // The precise formatting of this doesn't matter, nobody writes code like |
| 3576 | // this. |
| 3577 | verifyFormat("@ /*foo*/ interface"); |
Nico Weber | efcfe73 | 2013-01-07 15:17:23 +0000 | [diff] [blame] | 3578 | } |
| 3579 | |
Nico Weber | c31689a | 2013-01-08 19:15:23 +0000 | [diff] [blame] | 3580 | TEST_F(FormatTest, ObjCSnippets) { |
Manuel Klimek | d5688cf | 2013-01-11 19:17:44 +0000 | [diff] [blame] | 3581 | verifyFormat("@autoreleasepool {\n" |
| 3582 | " foo();\n" |
| 3583 | "}"); |
Nico Weber | cf4a79c | 2013-01-08 17:56:31 +0000 | [diff] [blame] | 3584 | verifyFormat("@class Foo, Bar;"); |
Nico Weber | c31689a | 2013-01-08 19:15:23 +0000 | [diff] [blame] | 3585 | verifyFormat("@compatibility_alias AliasName ExistingClass;"); |
Nico Weber | cf4a79c | 2013-01-08 17:56:31 +0000 | [diff] [blame] | 3586 | verifyFormat("@dynamic textColor;"); |
Nico Weber | 5dfe9b4 | 2013-01-18 05:11:47 +0000 | [diff] [blame] | 3587 | verifyFormat("char *buf1 = @encode(int *);"); |
| 3588 | verifyFormat("char *buf1 = @encode(typeof(4 * 5));"); |
Nico Weber | 95e8e46 | 2013-02-12 16:17:07 +0000 | [diff] [blame] | 3589 | verifyFormat("char *buf1 = @encode(int **);"); |
Nico Weber | 3a2673e | 2013-01-08 20:16:23 +0000 | [diff] [blame] | 3590 | verifyFormat("Protocol *proto = @protocol(p1);"); |
Nico Weber | 6a21a55 | 2013-01-18 02:43:57 +0000 | [diff] [blame] | 3591 | verifyFormat("SEL s = @selector(foo:);"); |
Manuel Klimek | d5688cf | 2013-01-11 19:17:44 +0000 | [diff] [blame] | 3592 | verifyFormat("@synchronized(self) {\n" |
| 3593 | " f();\n" |
| 3594 | "}"); |
Nico Weber | cf4a79c | 2013-01-08 17:56:31 +0000 | [diff] [blame] | 3595 | |
Nico Weber | 7084823 | 2013-01-10 21:30:42 +0000 | [diff] [blame] | 3596 | verifyFormat("@synthesize dropArrowPosition = dropArrowPosition_;"); |
| 3597 | verifyGoogleFormat("@synthesize dropArrowPosition = dropArrowPosition_;"); |
| 3598 | |
Nico Weber | cf4a79c | 2013-01-08 17:56:31 +0000 | [diff] [blame] | 3599 | verifyFormat("@property(assign, nonatomic) CGFloat hoverAlpha;"); |
Nico Weber | 7084823 | 2013-01-10 21:30:42 +0000 | [diff] [blame] | 3600 | verifyFormat("@property(assign, getter=isEditable) BOOL editable;"); |
| 3601 | verifyGoogleFormat("@property(assign, getter=isEditable) BOOL editable;"); |
Nico Weber | cf4a79c | 2013-01-08 17:56:31 +0000 | [diff] [blame] | 3602 | } |
| 3603 | |
Nico Weber | 712f5b3 | 2013-02-07 00:19:29 +0000 | [diff] [blame] | 3604 | TEST_F(FormatTest, ObjCLiterals) { |
| 3605 | verifyFormat("@\"String\""); |
| 3606 | verifyFormat("@1"); |
| 3607 | verifyFormat("@+4.8"); |
| 3608 | verifyFormat("@-4"); |
| 3609 | verifyFormat("@1LL"); |
| 3610 | verifyFormat("@.5"); |
| 3611 | verifyFormat("@'c'"); |
| 3612 | verifyFormat("@true"); |
| 3613 | |
| 3614 | verifyFormat("NSNumber *smallestInt = @(-INT_MAX - 1);"); |
| 3615 | verifyFormat("NSNumber *piOverTwo = @(M_PI / 2);"); |
| 3616 | verifyFormat("NSNumber *favoriteColor = @(Green);"); |
| 3617 | verifyFormat("NSString *path = @(getenv(\"PATH\"));"); |
| 3618 | |
Nico Weber | 712f5b3 | 2013-02-07 00:19:29 +0000 | [diff] [blame] | 3619 | verifyFormat("@["); |
Nico Weber | 051860e | 2013-02-10 02:08:05 +0000 | [diff] [blame] | 3620 | verifyFormat("@[]"); |
| 3621 | verifyFormat( |
| 3622 | "NSArray *array = @[ @\" Hey \", NSApp, [NSNumber numberWithInt:42] ];"); |
| 3623 | verifyFormat("return @[ @3, @[], @[ @4, @5 ] ];"); |
| 3624 | |
Nico Weber | 712f5b3 | 2013-02-07 00:19:29 +0000 | [diff] [blame] | 3625 | verifyFormat("@{"); |
Nico Weber | d74fcdb | 2013-02-10 20:35:35 +0000 | [diff] [blame] | 3626 | verifyFormat("@{}"); |
| 3627 | verifyFormat("@{ @\"one\" : @1 }"); |
| 3628 | verifyFormat("return @{ @\"one\" : @1 };"); |
| 3629 | verifyFormat("@{ @\"one\" : @1, }"); |
Manuel Klimek | 67d080d | 2013-04-12 14:13:36 +0000 | [diff] [blame] | 3630 | |
| 3631 | // FIXME: Breaking in cases where we think there's a structural error |
| 3632 | // showed that we're incorrectly parsing this code. We need to fix the |
| 3633 | // parsing here. |
| 3634 | verifyFormat("@{ @\"one\" : @\n" |
| 3635 | "{ @2 : @1 }\n" |
| 3636 | "}"); |
| 3637 | verifyFormat("@{ @\"one\" : @\n" |
| 3638 | "{ @2 : @1 }\n" |
| 3639 | ",\n" |
| 3640 | "}"); |
| 3641 | |
Nico Weber | d74fcdb | 2013-02-10 20:35:35 +0000 | [diff] [blame] | 3642 | verifyFormat("@{ 1 > 2 ? @\"one\" : @\"two\" : 1 > 2 ? @1 : @2 }"); |
| 3643 | verifyFormat("[self setDict:@{}"); |
| 3644 | verifyFormat("[self setDict:@{ @1 : @2 }"); |
| 3645 | verifyFormat("NSLog(@\"%@\", @{ @1 : @2, @2 : @3 }[@1]);"); |
| 3646 | verifyFormat( |
| 3647 | "NSDictionary *masses = @{ @\"H\" : @1.0078, @\"He\" : @4.0026 };"); |
Nico Weber | e626200 | 2013-02-10 20:39:05 +0000 | [diff] [blame] | 3648 | verifyFormat( |
| 3649 | "NSDictionary *settings = @{ AVEncoderKey : @(AVAudioQualityMax) };"); |
Daniel Jasper | 66d19bd | 2013-02-18 11:59:17 +0000 | [diff] [blame] | 3650 | |
Nico Weber | d74fcdb | 2013-02-10 20:35:35 +0000 | [diff] [blame] | 3651 | // FIXME: Nested and multi-line array and dictionary literals need more work. |
Nico Weber | 0ac717b | 2013-02-10 20:48:24 +0000 | [diff] [blame] | 3652 | verifyFormat( |
| 3653 | "NSDictionary *d = @{ @\"nam\" : NSUserNam(), @\"dte\" : [NSDate date],\n" |
| 3654 | " @\"processInfo\" : [NSProcessInfo processInfo] };"); |
Nico Weber | 712f5b3 | 2013-02-07 00:19:29 +0000 | [diff] [blame] | 3655 | } |
| 3656 | |
Manuel Klimek | 547d5db | 2013-02-08 17:38:27 +0000 | [diff] [blame] | 3657 | TEST_F(FormatTest, ReformatRegionAdjustsIndent) { |
| 3658 | EXPECT_EQ("{\n" |
| 3659 | "{\n" |
| 3660 | "a;\n" |
| 3661 | "b;\n" |
| 3662 | "}\n" |
Daniel Jasper | 66d19bd | 2013-02-18 11:59:17 +0000 | [diff] [blame] | 3663 | "}", |
| 3664 | format("{\n" |
| 3665 | "{\n" |
| 3666 | "a;\n" |
| 3667 | " b;\n" |
| 3668 | "}\n" |
| 3669 | "}", |
| 3670 | 13, 2, getLLVMStyle())); |
Manuel Klimek | 547d5db | 2013-02-08 17:38:27 +0000 | [diff] [blame] | 3671 | EXPECT_EQ("{\n" |
| 3672 | "{\n" |
| 3673 | " a;\n" |
| 3674 | "b;\n" |
| 3675 | "}\n" |
Daniel Jasper | 66d19bd | 2013-02-18 11:59:17 +0000 | [diff] [blame] | 3676 | "}", |
| 3677 | format("{\n" |
| 3678 | "{\n" |
| 3679 | " a;\n" |
| 3680 | "b;\n" |
| 3681 | "}\n" |
| 3682 | "}", |
| 3683 | 9, 2, getLLVMStyle())); |
Manuel Klimek | 547d5db | 2013-02-08 17:38:27 +0000 | [diff] [blame] | 3684 | EXPECT_EQ("{\n" |
| 3685 | "{\n" |
| 3686 | "public:\n" |
| 3687 | " b;\n" |
| 3688 | "}\n" |
Daniel Jasper | 66d19bd | 2013-02-18 11:59:17 +0000 | [diff] [blame] | 3689 | "}", |
| 3690 | format("{\n" |
| 3691 | "{\n" |
| 3692 | "public:\n" |
| 3693 | " b;\n" |
| 3694 | "}\n" |
| 3695 | "}", |
| 3696 | 17, 2, getLLVMStyle())); |
Manuel Klimek | 547d5db | 2013-02-08 17:38:27 +0000 | [diff] [blame] | 3697 | EXPECT_EQ("{\n" |
| 3698 | "{\n" |
| 3699 | "a;\n" |
| 3700 | "}\n" |
| 3701 | "{\n" |
| 3702 | " b;\n" |
| 3703 | "}\n" |
Daniel Jasper | 66d19bd | 2013-02-18 11:59:17 +0000 | [diff] [blame] | 3704 | "}", |
| 3705 | format("{\n" |
| 3706 | "{\n" |
| 3707 | "a;\n" |
| 3708 | "}\n" |
| 3709 | "{\n" |
| 3710 | " b;\n" |
| 3711 | "}\n" |
| 3712 | "}", |
| 3713 | 22, 2, getLLVMStyle())); |
Manuel Klimek | 52635ff | 2013-02-08 19:53:32 +0000 | [diff] [blame] | 3714 | EXPECT_EQ(" {\n" |
| 3715 | " a;\n" |
Daniel Jasper | 66d19bd | 2013-02-18 11:59:17 +0000 | [diff] [blame] | 3716 | " }", |
| 3717 | format(" {\n" |
| 3718 | "a;\n" |
| 3719 | " }", |
| 3720 | 4, 2, getLLVMStyle())); |
Daniel Jasper | 9ece2bb | 2013-02-12 16:51:23 +0000 | [diff] [blame] | 3721 | EXPECT_EQ("void f() {}\n" |
Daniel Jasper | 66d19bd | 2013-02-18 11:59:17 +0000 | [diff] [blame] | 3722 | "void g() {}", |
| 3723 | format("void f() {}\n" |
| 3724 | "void g() {}", |
| 3725 | 13, 0, getLLVMStyle())); |
Daniel Jasper | 83a90e5 | 2013-03-20 14:31:47 +0000 | [diff] [blame] | 3726 | EXPECT_EQ("int a; // comment\n" |
| 3727 | " // line 2\n" |
| 3728 | "int b;", |
| 3729 | format("int a; // comment\n" |
| 3730 | " // line 2\n" |
| 3731 | " int b;", |
| 3732 | 35, 0, getLLVMStyle())); |
Manuel Klimek | 52635ff | 2013-02-08 19:53:32 +0000 | [diff] [blame] | 3733 | } |
Manuel Klimek | 547d5db | 2013-02-08 17:38:27 +0000 | [diff] [blame] | 3734 | |
Manuel Klimek | 8092a94 | 2013-02-20 10:15:13 +0000 | [diff] [blame] | 3735 | TEST_F(FormatTest, BreakStringLiterals) { |
| 3736 | EXPECT_EQ("\"some text \"\n" |
| 3737 | "\"other\";", |
| 3738 | format("\"some text other\";", getLLVMStyleWithColumns(12))); |
Alexander Kornienko | 919398b | 2013-04-17 17:34:05 +0000 | [diff] [blame] | 3739 | EXPECT_EQ("\"some text \"\n" |
| 3740 | "\"other\";", |
| 3741 | format("\\\n\"some text other\";", getLLVMStyleWithColumns(12))); |
Manuel Klimek | 8092a94 | 2013-02-20 10:15:13 +0000 | [diff] [blame] | 3742 | EXPECT_EQ( |
| 3743 | "#define A \\\n" |
| 3744 | " \"some \" \\\n" |
| 3745 | " \"text \" \\\n" |
| 3746 | " \"other\";", |
| 3747 | format("#define A \"some text other\";", getLLVMStyleWithColumns(12))); |
| 3748 | EXPECT_EQ( |
| 3749 | "#define A \\\n" |
| 3750 | " \"so \" \\\n" |
| 3751 | " \"text \" \\\n" |
| 3752 | " \"other\";", |
| 3753 | format("#define A \"so text other\";", getLLVMStyleWithColumns(12))); |
| 3754 | |
| 3755 | EXPECT_EQ("\"some text\"", |
| 3756 | format("\"some text\"", getLLVMStyleWithColumns(1))); |
| 3757 | EXPECT_EQ("\"some text\"", |
| 3758 | format("\"some text\"", getLLVMStyleWithColumns(11))); |
| 3759 | EXPECT_EQ("\"some \"\n" |
| 3760 | "\"text\"", |
| 3761 | format("\"some text\"", getLLVMStyleWithColumns(10))); |
| 3762 | EXPECT_EQ("\"some \"\n" |
| 3763 | "\"text\"", |
| 3764 | format("\"some text\"", getLLVMStyleWithColumns(7))); |
Manuel Klimek | af31fd7 | 2013-03-01 13:14:08 +0000 | [diff] [blame] | 3765 | EXPECT_EQ("\"some\"\n" |
Alexander Kornienko | 70ce788 | 2013-04-15 14:28:00 +0000 | [diff] [blame] | 3766 | "\" tex\"\n" |
| 3767 | "\"t\"", |
Manuel Klimek | 8092a94 | 2013-02-20 10:15:13 +0000 | [diff] [blame] | 3768 | format("\"some text\"", getLLVMStyleWithColumns(6))); |
Manuel Klimek | 0090591 | 2013-03-04 20:03:38 +0000 | [diff] [blame] | 3769 | EXPECT_EQ("\"some\"\n" |
| 3770 | "\" tex\"\n" |
| 3771 | "\" and\"", |
| 3772 | format("\"some tex and\"", getLLVMStyleWithColumns(6))); |
| 3773 | EXPECT_EQ("\"some\"\n" |
| 3774 | "\"/tex\"\n" |
| 3775 | "\"/and\"", |
| 3776 | format("\"some/tex/and\"", getLLVMStyleWithColumns(6))); |
Manuel Klimek | 8092a94 | 2013-02-20 10:15:13 +0000 | [diff] [blame] | 3777 | |
| 3778 | EXPECT_EQ("variable =\n" |
| 3779 | " \"long string \"\n" |
| 3780 | " \"literal\";", |
| 3781 | format("variable = \"long string literal\";", |
| 3782 | getLLVMStyleWithColumns(20))); |
| 3783 | |
| 3784 | EXPECT_EQ("variable = f(\n" |
| 3785 | " \"long string \"\n" |
Daniel Jasper | faab0d3 | 2013-02-27 09:47:53 +0000 | [diff] [blame] | 3786 | " \"literal\",\n" |
| 3787 | " short,\n" |
Manuel Klimek | 8092a94 | 2013-02-20 10:15:13 +0000 | [diff] [blame] | 3788 | " loooooooooooooooooooong);", |
| 3789 | format("variable = f(\"long string literal\", short, " |
| 3790 | "loooooooooooooooooooong);", |
| 3791 | getLLVMStyleWithColumns(20))); |
| 3792 | EXPECT_EQ( |
| 3793 | "f(\"one two\".split(\n" |
| 3794 | " variable));", |
| 3795 | format("f(\"one two\".split(variable));", getLLVMStyleWithColumns(20))); |
| 3796 | EXPECT_EQ("f(\"one two three four five six \"\n" |
| 3797 | " \"seven\".split(\n" |
| 3798 | " really_looooong_variable));", |
| 3799 | format("f(\"one two three four five six seven\"." |
| 3800 | "split(really_looooong_variable));", |
| 3801 | getLLVMStyleWithColumns(33))); |
| 3802 | |
| 3803 | EXPECT_EQ("f(\"some \"\n" |
| 3804 | " \"text\",\n" |
| 3805 | " other);", |
| 3806 | format("f(\"some text\", other);", getLLVMStyleWithColumns(10))); |
Daniel Jasper | 0fb382b | 2013-02-26 12:52:34 +0000 | [diff] [blame] | 3807 | |
| 3808 | // Only break as a last resort. |
| 3809 | verifyFormat( |
| 3810 | "aaaaaaaaaaaaaaaaaaaa(\n" |
| 3811 | " aaaaaaaaaaaaaaaaaaaa,\n" |
| 3812 | " aaaaaa(\"aaa aaaaa aaa aaa aaaaa aaa aaaaa aaa aaa aaaaaa\"));"); |
Manuel Klimek | af31fd7 | 2013-03-01 13:14:08 +0000 | [diff] [blame] | 3813 | |
| 3814 | EXPECT_EQ( |
| 3815 | "\"splitmea\"\n" |
Manuel Klimek | bc30c71 | 2013-03-01 13:29:19 +0000 | [diff] [blame] | 3816 | "\"trandomp\"\n" |
| 3817 | "\"oint\"", |
Manuel Klimek | af31fd7 | 2013-03-01 13:14:08 +0000 | [diff] [blame] | 3818 | format("\"splitmeatrandompoint\"", getLLVMStyleWithColumns(10))); |
Manuel Klimek | bc30c71 | 2013-03-01 13:29:19 +0000 | [diff] [blame] | 3819 | |
| 3820 | EXPECT_EQ( |
| 3821 | "\"split/\"\n" |
| 3822 | "\"pathat/\"\n" |
| 3823 | "\"slashes\"", |
| 3824 | format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10))); |
Daniel Jasper | 2972d04 | 2013-04-25 08:56:26 +0000 | [diff] [blame] | 3825 | |
| 3826 | FormatStyle AlignLeft = getLLVMStyleWithColumns(12); |
| 3827 | AlignLeft.AlignEscapedNewlinesLeft = true; |
| 3828 | EXPECT_EQ( |
| 3829 | "#define A \\\n" |
| 3830 | " \"some \" \\\n" |
| 3831 | " \"text \" \\\n" |
| 3832 | " \"other\";", |
| 3833 | format("#define A \"some text other\";", AlignLeft)); |
Manuel Klimek | 8092a94 | 2013-02-20 10:15:13 +0000 | [diff] [blame] | 3834 | } |
| 3835 | |
Manuel Klimek | aa62d0c | 2013-03-08 18:59:48 +0000 | [diff] [blame] | 3836 | TEST_F(FormatTest, DoNotBreakStringLiteralsInEscapeSequence) { |
| 3837 | EXPECT_EQ("\"\\a\"", |
| 3838 | format("\"\\a\"", getLLVMStyleWithColumns(3))); |
| 3839 | EXPECT_EQ("\"\\\"", |
| 3840 | format("\"\\\"", getLLVMStyleWithColumns(2))); |
| 3841 | EXPECT_EQ("\"test\"\n" |
| 3842 | "\"\\n\"", |
| 3843 | format("\"test\\n\"", getLLVMStyleWithColumns(7))); |
| 3844 | EXPECT_EQ("\"tes\\\\\"\n" |
| 3845 | "\"n\"", |
| 3846 | format("\"tes\\\\n\"", getLLVMStyleWithColumns(7))); |
| 3847 | EXPECT_EQ("\"\\\\\\\\\"\n" |
| 3848 | "\"\\n\"", |
| 3849 | format("\"\\\\\\\\\\n\"", getLLVMStyleWithColumns(7))); |
| 3850 | EXPECT_EQ("\"\\uff01\"", |
| 3851 | format("\"\\uff01\"", getLLVMStyleWithColumns(7))); |
| 3852 | EXPECT_EQ("\"\\uff01\"\n" |
| 3853 | "\"test\"", |
| 3854 | format("\"\\uff01test\"", getLLVMStyleWithColumns(8))); |
| 3855 | EXPECT_EQ("\"\\Uff01ff02\"", |
| 3856 | format("\"\\Uff01ff02\"", getLLVMStyleWithColumns(11))); |
| 3857 | EXPECT_EQ("\"\\x000000000001\"\n" |
| 3858 | "\"next\"", |
| 3859 | format("\"\\x000000000001next\"", getLLVMStyleWithColumns(16))); |
| 3860 | EXPECT_EQ("\"\\x000000000001next\"", |
| 3861 | format("\"\\x000000000001next\"", getLLVMStyleWithColumns(15))); |
| 3862 | EXPECT_EQ("\"\\x000000000001\"", |
| 3863 | format("\"\\x000000000001\"", getLLVMStyleWithColumns(7))); |
| 3864 | EXPECT_EQ("\"test\"\n" |
| 3865 | "\"\\000000\"\n" |
| 3866 | "\"000001\"", |
| 3867 | format("\"test\\000000000001\"", getLLVMStyleWithColumns(9))); |
| 3868 | EXPECT_EQ("\"test\\000\"\n" |
Alexander Kornienko | 70ce788 | 2013-04-15 14:28:00 +0000 | [diff] [blame] | 3869 | "\"00000000\"\n" |
| 3870 | "\"1\"", |
Manuel Klimek | aa62d0c | 2013-03-08 18:59:48 +0000 | [diff] [blame] | 3871 | format("\"test\\000000000001\"", getLLVMStyleWithColumns(10))); |
| 3872 | EXPECT_EQ("R\"(\\x\\x00)\"\n", |
| 3873 | format("R\"(\\x\\x00)\"\n", getLLVMStyleWithColumns(7))); |
| 3874 | } |
| 3875 | |
Manuel Klimek | 423dd93 | 2013-04-10 09:52:05 +0000 | [diff] [blame] | 3876 | TEST_F(FormatTest, DoNotCreateUnreasonableUnwrappedLines) { |
| 3877 | verifyFormat("void f() {\n" |
| 3878 | " return g() {}\n" |
| 3879 | " void h() {}"); |
| 3880 | verifyFormat("if (foo)\n" |
| 3881 | " return { forgot_closing_brace();\n" |
| 3882 | "test();"); |
| 3883 | verifyFormat("int a[] = { void forgot_closing_brace()\n" |
| 3884 | "{\n" |
| 3885 | " f();\n" |
| 3886 | " g();\n" |
| 3887 | "}"); |
| 3888 | } |
| 3889 | |
Daniel Jasper | cd16238 | 2013-01-07 13:26:07 +0000 | [diff] [blame] | 3890 | } // end namespace tooling |
| 3891 | } // end namespace clang |