From 27172d627e765ac421b440c8163f927bba702de8 Mon Sep 17 00:00:00 2001 From: Bernd Kolb Date: Sat, 23 Nov 2024 20:18:34 +0100 Subject: [PATCH] PrettyPrinter reports wrong line LineNumbersTests This is to reproduce issue #882. No fix is provided yet. Issue: #882 --- .../PrettyPrint/LineNumbersTests.swift | 106 ++++++++++++++++++ 1 file changed, 106 insertions(+) create mode 100644 Tests/SwiftFormatTests/PrettyPrint/LineNumbersTests.swift diff --git a/Tests/SwiftFormatTests/PrettyPrint/LineNumbersTests.swift b/Tests/SwiftFormatTests/PrettyPrint/LineNumbersTests.swift new file mode 100644 index 00000000..480e56ca --- /dev/null +++ b/Tests/SwiftFormatTests/PrettyPrint/LineNumbersTests.swift @@ -0,0 +1,106 @@ +import SwiftFormat +import _SwiftFormatTestSupport + +final class LineNumbersTests: PrettyPrintTestCase { + func testLineNumbers() { + let input = + """ + final class A { + @Test func b() throws { + doSomethingInAFunctionWithAVeryLongName() 1️⃣// Here we have a very long comment that should not be here because it is far too long + } + } + """ + + let expected = + """ + final class A { + @Test func b() throws { + doSomethingInAFunctionWithAVeryLongName() // Here we have a very long comment that should not be here because it is far too long + } + } + + """ + + assertPrettyPrintEqual(input: input, + expected: expected, + linelength: 120, + configuration: LineNumbersTests.myConfig, + whitespaceOnly: true, + findings: [ + FindingSpec("1️⃣", message: "move end-of-line comment that exceeds the line length") + ]) + } + + func testLineNumbersWithComments() { + let input = + """ + // Copyright (C) 2024 My Coorp. All rights reserved. + // + // This document is the property of My Coorp. + // It is considered confidential and proprietary. + // + // This document may not be reproduced or transmitted in any form, + // in whole or in part, without the express written permission of + // My Coorp. + + final class A { + @Test func b() throws { + doSomethingInAFunctionWithAVeryLongName() 1️⃣// Here we have a very long comment that should not be here because it is far too long + } + } + """ + + let expected = + """ + // Copyright (C) 2024 My Coorp. All rights reserved. + // + // This document is the property of My Coorp. + // It is considered confidential and proprietary. + // + // This document may not be reproduced or transmitted in any form, + // in whole or in part, without the express written permission of + // My Coorp. + + final class A { + @Test func b() throws { + doSomethingInAFunctionWithAVeryLongName() // Here we have a very long comment that should not be here because it is far too long + } + } + + """ + + assertPrettyPrintEqual(input: input, + expected: expected, + linelength: 120, + configuration: LineNumbersTests.myConfig, + whitespaceOnly: true, + findings: [ + FindingSpec("1️⃣", message: "move end-of-line comment that exceeds the line length") + ]) + } + + public static var myConfig: Configuration { + var config = Configuration() + config.rules = Configuration.defaultRuleEnablements + config.maximumBlankLines = 1 + config.lineLength = 120 + config.tabWidth = 8 + config.indentation = .spaces(4) + config.respectsExistingLineBreaks = true + config.lineBreakBeforeControlFlowKeywords = false + config.lineBreakBeforeEachArgument = false + config.lineBreakBeforeEachGenericRequirement = false + config.prioritizeKeepingFunctionOutputTogether = true + config.indentConditionalCompilationBlocks = false + config.lineBreakAroundMultilineExpressionChainComponents = false + config.fileScopedDeclarationPrivacy = FileScopedDeclarationPrivacyConfiguration() + config.indentSwitchCaseLabels = true + config.spacesAroundRangeFormationOperators = false + config.noAssignmentInExpressions = NoAssignmentInExpressionsConfiguration() + config.multiElementCollectionTrailingCommas = true + config.indentBlankLines = false + return config + } + +}