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