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