|
1 | 1 | import _SwiftFormatTestSupport
|
| 2 | +import SwiftFormat |
2 | 3 |
|
3 | 4 | final class CommentTests: PrettyPrintTestCase {
|
4 | 5 | func testDocumentationComments() {
|
@@ -199,6 +200,127 @@ final class CommentTests: PrettyPrintTestCase {
|
199 | 200 | assertPrettyPrintEqual(input: input, expected: expected, linelength: 45)
|
200 | 201 | }
|
201 | 202 |
|
| 203 | + func testLineCommentsWithCustomLeadingSpaces() { |
| 204 | + let input = |
| 205 | + """ |
| 206 | + // Line Comment0 |
| 207 | +
|
| 208 | + // Line Comment1 |
| 209 | + // Line Comment2 |
| 210 | + let a = 123 |
| 211 | + let b = "456" // End of line comment |
| 212 | + let c = "More content" |
| 213 | +
|
| 214 | + // Comment 3 |
| 215 | + // Comment 4 |
| 216 | +
|
| 217 | + let reallyLongVariableName = 123 // This comment should not wrap |
| 218 | + // and should not combine with this comment |
| 219 | +
|
| 220 | + func MyFun() { |
| 221 | + // just a comment |
| 222 | + } |
| 223 | +
|
| 224 | + func MyFun() { |
| 225 | + // Comment 1 |
| 226 | + // Comment 2 |
| 227 | + let a = 123 |
| 228 | +
|
| 229 | + let b = 456 // Comment 3 |
| 230 | + } |
| 231 | +
|
| 232 | + func MyFun() { |
| 233 | + let c = 789 // Comment 4 |
| 234 | + // Comment 5 |
| 235 | + } |
| 236 | +
|
| 237 | + let a = myfun(123 // Cmt 7 |
| 238 | + ) |
| 239 | + let a = myfun(var1: 123 // Cmt 7 |
| 240 | + ) |
| 241 | +
|
| 242 | + guard condition else { return // Cmt 6 |
| 243 | + } |
| 244 | +
|
| 245 | + switch myvar { |
| 246 | + case .one, .two, // three |
| 247 | + .four: |
| 248 | + dostuff() |
| 249 | + default: () |
| 250 | + } |
| 251 | +
|
| 252 | + let a = 123 + // comment |
| 253 | + b + c |
| 254 | +
|
| 255 | + let d = 123 |
| 256 | + // Trailing Comment |
| 257 | + """ |
| 258 | + |
| 259 | + let expected = |
| 260 | + """ |
| 261 | + // Line Comment0 |
| 262 | +
|
| 263 | + // Line Comment1 |
| 264 | + // Line Comment2 |
| 265 | + let a = 123 |
| 266 | + let b = "456" // End of line comment |
| 267 | + let c = "More content" |
| 268 | +
|
| 269 | + // Comment 3 |
| 270 | + // Comment 4 |
| 271 | +
|
| 272 | + let reallyLongVariableName = 123 // This comment should not wrap |
| 273 | + // and should not combine with this comment |
| 274 | +
|
| 275 | + func MyFun() { |
| 276 | + // just a comment |
| 277 | + } |
| 278 | +
|
| 279 | + func MyFun() { |
| 280 | + // Comment 1 |
| 281 | + // Comment 2 |
| 282 | + let a = 123 |
| 283 | +
|
| 284 | + let b = 456 // Comment 3 |
| 285 | + } |
| 286 | +
|
| 287 | + func MyFun() { |
| 288 | + let c = 789 // Comment 4 |
| 289 | + // Comment 5 |
| 290 | + } |
| 291 | +
|
| 292 | + let a = myfun( |
| 293 | + 123 // Cmt 7 |
| 294 | + ) |
| 295 | + let a = myfun( |
| 296 | + var1: 123 // Cmt 7 |
| 297 | + ) |
| 298 | +
|
| 299 | + guard condition else { |
| 300 | + return // Cmt 6 |
| 301 | + } |
| 302 | +
|
| 303 | + switch myvar { |
| 304 | + case .one, .two, // three |
| 305 | + .four: |
| 306 | + dostuff() |
| 307 | + default: () |
| 308 | + } |
| 309 | +
|
| 310 | + let a = |
| 311 | + 123 // comment |
| 312 | + + b + c |
| 313 | +
|
| 314 | + let d = 123 |
| 315 | + // Trailing Comment |
| 316 | +
|
| 317 | + """ |
| 318 | + |
| 319 | + var config = Configuration.forTesting |
| 320 | + config.spacesBeforeLineComments = 3 |
| 321 | + assertPrettyPrintEqual(input: input, expected: expected, linelength: 45, configuration: config) |
| 322 | + } |
| 323 | + |
202 | 324 | func testContainerLineComments() {
|
203 | 325 | let input =
|
204 | 326 | """
|
@@ -274,6 +396,82 @@ final class CommentTests: PrettyPrintTestCase {
|
274 | 396 | assertPrettyPrintEqual(input: input, expected: expected, linelength: 80)
|
275 | 397 | }
|
276 | 398 |
|
| 399 | + func testContainerLineCommentsWithCustomLeadingSpaces() { |
| 400 | + let input = |
| 401 | + """ |
| 402 | + // Array comment |
| 403 | + let a = [456, // small comment |
| 404 | + 789] |
| 405 | +
|
| 406 | + // Dictionary comment |
| 407 | + let b = ["abc": 456, // small comment |
| 408 | + "def": 789] |
| 409 | +
|
| 410 | + // Trailing comment |
| 411 | + let c = [123, 456 // small comment |
| 412 | + ] |
| 413 | +
|
| 414 | + // Multiline comment |
| 415 | + let d = [123, |
| 416 | + // comment line 1 |
| 417 | + // comment line 2 |
| 418 | + 456 |
| 419 | + ] |
| 420 | +
|
| 421 | + /* Array comment */ |
| 422 | + let a = [456, /* small comment */ |
| 423 | + 789] |
| 424 | +
|
| 425 | + /* Dictionary comment */ |
| 426 | + let b = ["abc": 456, /* small comment */ |
| 427 | + "def": 789] |
| 428 | + """ |
| 429 | + |
| 430 | + let expected = |
| 431 | + """ |
| 432 | + // Array comment |
| 433 | + let a = [ |
| 434 | + 456, // small comment |
| 435 | + 789, |
| 436 | + ] |
| 437 | +
|
| 438 | + // Dictionary comment |
| 439 | + let b = [ |
| 440 | + "abc": 456, // small comment |
| 441 | + "def": 789, |
| 442 | + ] |
| 443 | +
|
| 444 | + // Trailing comment |
| 445 | + let c = [ |
| 446 | + 123, 456, // small comment |
| 447 | + ] |
| 448 | +
|
| 449 | + // Multiline comment |
| 450 | + let d = [ |
| 451 | + 123, |
| 452 | + // comment line 1 |
| 453 | + // comment line 2 |
| 454 | + 456, |
| 455 | + ] |
| 456 | +
|
| 457 | + /* Array comment */ |
| 458 | + let a = [ |
| 459 | + 456, /* small comment */ |
| 460 | + 789, |
| 461 | + ] |
| 462 | +
|
| 463 | + /* Dictionary comment */ |
| 464 | + let b = [ |
| 465 | + "abc": 456, /* small comment */ |
| 466 | + "def": 789, |
| 467 | + ] |
| 468 | +
|
| 469 | + """ |
| 470 | + var config = Configuration.forTesting |
| 471 | + config.spacesBeforeLineComments = 1 |
| 472 | + assertPrettyPrintEqual(input: input, expected: expected, linelength: 80, configuration: config) |
| 473 | + } |
| 474 | + |
277 | 475 | func testDocumentationBlockComments() {
|
278 | 476 | let input =
|
279 | 477 | """
|
|
0 commit comments