1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
|
import SwiftFormat
import SwiftSyntax
import _SwiftFormatTestSupport
@_spi(Rules) import SwiftFormat
private typealias TestConfiguration = (
original: String,
desired: FileScopedDeclarationPrivacyConfiguration.AccessLevel,
expected: String
)
/// Test configurations for file-scoped declarations, which should be changed if the access level
/// does not match the desired level in the formatter configuration.
private let changingTestConfigurations: [TestConfiguration] = [
(original: "private", desired: .fileprivate, expected: "fileprivate"),
(original: "private", desired: .private, expected: "private"),
(original: "fileprivate", desired: .fileprivate, expected: "fileprivate"),
(original: "fileprivate", desired: .private, expected: "private"),
]
/// Test configurations for declarations that should not have their access level changed; extensions
/// and nested declarations (i.e., not at file scope).
private let unchangingTestConfigurations: [TestConfiguration] = [
(original: "private", desired: .fileprivate, expected: "private"),
(original: "private", desired: .private, expected: "private"),
(original: "fileprivate", desired: .fileprivate, expected: "fileprivate"),
(original: "fileprivate", desired: .private, expected: "fileprivate"),
]
final class FileScopedDeclarationPrivacyTests: LintOrFormatRuleTestCase {
func testFileScopeDecls() {
runWithMultipleConfigurations(
source: """
1️⃣$access$ class Foo {}
2️⃣$access$ struct Foo {}
3️⃣$access$ enum Foo {}
4️⃣$access$ protocol Foo {}
5️⃣$access$ typealias Foo = Bar
6️⃣$access$ func foo() {}
7️⃣$access$ var foo: Bar
""",
testConfigurations: changingTestConfigurations
) { original, expected in
[
FindingSpec("1️⃣", message: "replace '\(original)' with '\(expected)' on file-scoped declarations"),
FindingSpec("2️⃣", message: "replace '\(original)' with '\(expected)' on file-scoped declarations"),
FindingSpec("3️⃣", message: "replace '\(original)' with '\(expected)' on file-scoped declarations"),
FindingSpec("4️⃣", message: "replace '\(original)' with '\(expected)' on file-scoped declarations"),
FindingSpec("5️⃣", message: "replace '\(original)' with '\(expected)' on file-scoped declarations"),
FindingSpec("6️⃣", message: "replace '\(original)' with '\(expected)' on file-scoped declarations"),
FindingSpec("7️⃣", message: "replace '\(original)' with '\(expected)' on file-scoped declarations"),
]
}
}
func testFileScopeExtensionsAreNotChanged() {
runWithMultipleConfigurations(
source: """
$access$ extension Foo {}
""",
testConfigurations: unchangingTestConfigurations
) { _, _ in [] }
}
func testNonFileScopeDeclsAreNotChanged() {
runWithMultipleConfigurations(
source: """
enum Namespace {
$access$ class Foo {}
$access$ struct Foo {}
$access$ enum Foo {}
$access$ typealias Foo = Bar
$access$ func foo() {}
$access$ var foo: Bar
}
""",
testConfigurations: unchangingTestConfigurations
) { _, _ in [] }
}
func testFileScopeDeclsInsideConditionals() {
runWithMultipleConfigurations(
source: """
#if FOO
1️⃣$access$ class Foo {}
#elseif BAR
2️⃣$access$ class Foo {}
#else
3️⃣$access$ class Foo {}
#endif
""",
testConfigurations: changingTestConfigurations
) { original, expected in
[
FindingSpec("1️⃣", message: "replace '\(original)' with '\(expected)' on file-scoped declarations"),
FindingSpec("2️⃣", message: "replace '\(original)' with '\(expected)' on file-scoped declarations"),
FindingSpec("3️⃣", message: "replace '\(original)' with '\(expected)' on file-scoped declarations"),
]
}
}
func testFileScopeDeclsInsideNestedConditionals() {
runWithMultipleConfigurations(
source: """
#if FOO
#if BAR
1️⃣$access$ class Foo {}
2️⃣$access$ struct Foo {}
3️⃣$access$ enum Foo {}
4️⃣$access$ protocol Foo {}
5️⃣$access$ typealias Foo = Bar
6️⃣$access$ func foo() {}
7️⃣$access$ var foo: Bar
#endif
#endif
""",
testConfigurations: changingTestConfigurations
) { original, expected in
[
FindingSpec("1️⃣", message: "replace '\(original)' with '\(expected)' on file-scoped declarations"),
FindingSpec("2️⃣", message: "replace '\(original)' with '\(expected)' on file-scoped declarations"),
FindingSpec("3️⃣", message: "replace '\(original)' with '\(expected)' on file-scoped declarations"),
FindingSpec("4️⃣", message: "replace '\(original)' with '\(expected)' on file-scoped declarations"),
FindingSpec("5️⃣", message: "replace '\(original)' with '\(expected)' on file-scoped declarations"),
FindingSpec("6️⃣", message: "replace '\(original)' with '\(expected)' on file-scoped declarations"),
FindingSpec("7️⃣", message: "replace '\(original)' with '\(expected)' on file-scoped declarations"),
]
}
}
func testLeadingTriviaIsPreserved() {
runWithMultipleConfigurations(
source: """
/// Some doc comment
1️⃣$access$ class Foo {}
@objc /* comment */ 2️⃣$access$ class Bar {}
""",
testConfigurations: changingTestConfigurations
) { original, expected in
[
FindingSpec("1️⃣", message: "replace '\(original)' with '\(expected)' on file-scoped declarations"),
FindingSpec("2️⃣", message: "replace '\(original)' with '\(expected)' on file-scoped declarations"),
]
}
}
func testModifierDetailIsPreserved() {
runWithMultipleConfigurations(
source: """
public 1️⃣$access$(set) var foo: Int
""",
testConfigurations: changingTestConfigurations
) { original, expected in
[
FindingSpec("1️⃣", message: "replace '\(original)' with '\(expected)' on file-scoped declarations"),
]
}
}
/// Runs a test for this rule in multiple configurations.
private func runWithMultipleConfigurations(
source: String,
testConfigurations: [TestConfiguration],
file: StaticString = #file,
line: UInt = #line,
findingsProvider: (String, String) -> [FindingSpec]
) {
for testConfig in testConfigurations {
var configuration = Configuration.forTesting
configuration.fileScopedDeclarationPrivacy.accessLevel = testConfig.desired
let substitutedInput = source.replacingOccurrences(of: "$access$", with: testConfig.original)
let markedSource = MarkedText(textWithMarkers: source)
let substitutedExpected = markedSource.textWithoutMarkers.replacingOccurrences(
of: "$access$", with: testConfig.expected)
// Only use the findings if the output was expected to change. If it didn't change, then the
// rule wouldn't have emitted anything.
let findingSpecs: [FindingSpec]
if testConfig.original == testConfig.expected {
findingSpecs = []
} else {
findingSpecs = findingsProvider(testConfig.original, testConfig.expected)
}
assertFormatting(
FileScopedDeclarationPrivacy.self,
input: substitutedInput,
expected: substitutedExpected,
findings: findingSpecs,
configuration: configuration,
file: file,
line: line)
}
}
}
|