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