|
1 | 1 | import _SwiftFormatTestSupport
|
| 2 | +import SwiftFormat |
2 | 3 |
|
3 | 4 | final class CommentTests: PrettyPrintTestCase {
|
4 | 5 | func testDocumentationComments() {
|
@@ -199,6 +200,152 @@ final class CommentTests: PrettyPrintTestCase {
|
199 | 200 | assertPrettyPrintEqual(input: input, expected: expected, linelength: 45)
|
200 | 201 | }
|
201 | 202 |
|
| 203 | + func testLineCommentsWithCustomLeadingSpaces() { |
| 204 | + let pairs: [(String, String)] = [ |
| 205 | + ( |
| 206 | + """ |
| 207 | + // Line Comment0 |
| 208 | +
|
| 209 | + // Line Comment1 |
| 210 | + // Line Comment2 |
| 211 | + let a = 123 |
| 212 | + let b = "456" // End of line comment |
| 213 | + let c = "More content" |
| 214 | +
|
| 215 | + """, |
| 216 | + """ |
| 217 | + // Line Comment0 |
| 218 | +
|
| 219 | + // Line Comment1 |
| 220 | + // Line Comment2 |
| 221 | + let a = 123 |
| 222 | + let b = "456" // End of line comment |
| 223 | + let c = "More content" |
| 224 | +
|
| 225 | + """ |
| 226 | + ), |
| 227 | + ( |
| 228 | + """ |
| 229 | + // Comment 3 |
| 230 | + // Comment 4 |
| 231 | +
|
| 232 | + let reallyLongVariableName = 123 // This comment should not wrap |
| 233 | + // and should not combine with this comment |
| 234 | +
|
| 235 | + func MyFun() { |
| 236 | + // just a comment |
| 237 | + } |
| 238 | + """, |
| 239 | + """ |
| 240 | + // Comment 3 |
| 241 | + // Comment 4 |
| 242 | +
|
| 243 | + let reallyLongVariableName = 123 // This comment should not wrap |
| 244 | + // and should not combine with this comment |
| 245 | +
|
| 246 | + func MyFun() { |
| 247 | + // just a comment |
| 248 | + } |
| 249 | +
|
| 250 | + """ |
| 251 | + ), |
| 252 | + ( |
| 253 | + """ |
| 254 | + func MyFun() { |
| 255 | + // Comment 1 |
| 256 | + // Comment 2 |
| 257 | + let a = 123 |
| 258 | +
|
| 259 | + let b = 456 // Comment 3 |
| 260 | + } |
| 261 | +
|
| 262 | + func MyFun() { |
| 263 | + let c = 789 // Comment 4 |
| 264 | + // Comment 5 |
| 265 | + } |
| 266 | + """, |
| 267 | + """ |
| 268 | + func MyFun() { |
| 269 | + // Comment 1 |
| 270 | + // Comment 2 |
| 271 | + let a = 123 |
| 272 | +
|
| 273 | + let b = 456 // Comment 3 |
| 274 | + } |
| 275 | +
|
| 276 | + func MyFun() { |
| 277 | + let c = 789 // Comment 4 |
| 278 | + // Comment 5 |
| 279 | + } |
| 280 | +
|
| 281 | + """ |
| 282 | + ), |
| 283 | + ( |
| 284 | + """ |
| 285 | + let a = myfun(123 // Cmt 7 |
| 286 | + ) |
| 287 | + let a = myfun(var1: 123 // Cmt 7 |
| 288 | + ) |
| 289 | +
|
| 290 | + guard condition else { return // Cmt 6 |
| 291 | + } |
| 292 | +
|
| 293 | + switch myvar { |
| 294 | + case .one, .two, // three |
| 295 | + .four: |
| 296 | + dostuff() |
| 297 | + default: () |
| 298 | + } |
| 299 | +
|
| 300 | + """, |
| 301 | + """ |
| 302 | + let a = myfun( |
| 303 | + 123 // Cmt 7 |
| 304 | + ) |
| 305 | + let a = myfun( |
| 306 | + var1: 123 // Cmt 7 |
| 307 | + ) |
| 308 | +
|
| 309 | + guard condition else { |
| 310 | + return // Cmt 6 |
| 311 | + } |
| 312 | +
|
| 313 | + switch myvar { |
| 314 | + case .one, .two, // three |
| 315 | + .four: |
| 316 | + dostuff() |
| 317 | + default: () |
| 318 | + } |
| 319 | +
|
| 320 | + """ |
| 321 | + ), |
| 322 | + ( |
| 323 | + """ |
| 324 | + let a = 123 + // comment |
| 325 | + b + c |
| 326 | +
|
| 327 | + let d = 123 |
| 328 | + // Trailing Comment |
| 329 | + """, |
| 330 | + """ |
| 331 | + let a = |
| 332 | + 123 // comment |
| 333 | + + b + c |
| 334 | +
|
| 335 | + let d = 123 |
| 336 | + // Trailing Comment |
| 337 | +
|
| 338 | + """ |
| 339 | + ), |
| 340 | + ] |
| 341 | + |
| 342 | + var config = Configuration.forTesting |
| 343 | + config.spacesBeforeEndOfLineComments = 3 |
| 344 | + for (input, expected) in pairs { |
| 345 | + assertPrettyPrintEqual(input: input, expected: expected, linelength: 45, configuration: config) |
| 346 | + } |
| 347 | + } |
| 348 | + |
202 | 349 | func testContainerLineComments() {
|
203 | 350 | let input =
|
204 | 351 | """
|
@@ -274,6 +421,82 @@ final class CommentTests: PrettyPrintTestCase {
|
274 | 421 | assertPrettyPrintEqual(input: input, expected: expected, linelength: 80)
|
275 | 422 | }
|
276 | 423 |
|
| 424 | + func testContainerLineCommentsWithCustomLeadingSpaces() { |
| 425 | + let input = |
| 426 | + """ |
| 427 | + // Array comment |
| 428 | + let a = [456, // small comment |
| 429 | + 789] |
| 430 | +
|
| 431 | + // Dictionary comment |
| 432 | + let b = ["abc": 456, // small comment |
| 433 | + "def": 789] |
| 434 | +
|
| 435 | + // Trailing comment |
| 436 | + let c = [123, 456 // small comment |
| 437 | + ] |
| 438 | +
|
| 439 | + // Multiline comment |
| 440 | + let d = [123, |
| 441 | + // comment line 1 |
| 442 | + // comment line 2 |
| 443 | + 456 |
| 444 | + ] |
| 445 | +
|
| 446 | + /* Array comment */ |
| 447 | + let a = [456, /* small comment */ |
| 448 | + 789] |
| 449 | +
|
| 450 | + /* Dictionary comment */ |
| 451 | + let b = ["abc": 456, /* small comment */ |
| 452 | + "def": 789] |
| 453 | + """ |
| 454 | + |
| 455 | + let expected = |
| 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 | + // Trailing comment |
| 470 | + let c = [ |
| 471 | + 123, 456, // small comment |
| 472 | + ] |
| 473 | +
|
| 474 | + // Multiline comment |
| 475 | + let d = [ |
| 476 | + 123, |
| 477 | + // comment line 1 |
| 478 | + // comment line 2 |
| 479 | + 456, |
| 480 | + ] |
| 481 | +
|
| 482 | + /* Array comment */ |
| 483 | + let a = [ |
| 484 | + 456, /* small comment */ |
| 485 | + 789, |
| 486 | + ] |
| 487 | +
|
| 488 | + /* Dictionary comment */ |
| 489 | + let b = [ |
| 490 | + "abc": 456, /* small comment */ |
| 491 | + "def": 789, |
| 492 | + ] |
| 493 | +
|
| 494 | + """ |
| 495 | + var config = Configuration.forTesting |
| 496 | + config.spacesBeforeEndOfLineComments = 1 |
| 497 | + assertPrettyPrintEqual(input: input, expected: expected, linelength: 80, configuration: config) |
| 498 | + } |
| 499 | + |
277 | 500 | func testDocumentationBlockComments() {
|
278 | 501 | let input =
|
279 | 502 | """
|
|
0 commit comments