Inline dedicated rule test (#6459)

This commit is contained in:
Danny Mösch
2026-01-23 20:25:19 +01:00
committed by GitHub
parent 1d7f3b627b
commit 335edded41
3 changed files with 22 additions and 32 deletions
@@ -5,6 +5,8 @@ import SwiftSyntax
struct TrailingClosureRule: Rule {
var configuration = TrailingClosureConfiguration()
private static let onlySingleMutedConfig = ["only_single_muted_parameter": true]
static let description = RuleDescription(
identifier: "trailing_closure",
name: "Trailing Closure",
@@ -36,6 +38,9 @@ struct TrailingClosureRule: Rule {
print(i)
}
"""),
Example("foo.reduce(0, combine: { $0 + 1 })", configuration: onlySingleMutedConfig),
Example("offsets.sorted(by: { $0.offset < $1.offset })", configuration: onlySingleMutedConfig),
Example("foo.something(0, { $0 + 1 })", configuration: onlySingleMutedConfig),
],
triggeringExamples: [
Example("foo.map(↓{ $0 + 1 })"),
@@ -48,6 +53,7 @@ struct TrailingClosureRule: Rule {
n.forEach(↓{ print($0) })
}
""", excludeFromDocumentation: true),
Example("foo.map(↓{ $0 + 1 })", configuration: onlySingleMutedConfig),
],
corrections: [
Example("foo.map(↓{ $0 + 1 })"):
@@ -79,6 +85,19 @@ struct TrailingClosureRule: Rule {
f(a: 1,
b: 2) { 3 }
"""),
Example("foo.map(↓{ $0 + 1 })", configuration: onlySingleMutedConfig):
Example("foo.map { $0 + 1 }", configuration: onlySingleMutedConfig),
Example("f(↓{ g(↓{ 1 }) })", configuration: onlySingleMutedConfig):
Example("f { g { 1 }}", configuration: onlySingleMutedConfig),
Example("""
for n in list {
n.forEach(↓{ print($0) })
}
""", configuration: onlySingleMutedConfig): Example("""
for n in list {
n.forEach { print($0) }
}
"""),
Example("""
f(a: 1, // comment
b: 2, /* comment */ c: { 3 })
@@ -140,12 +140,15 @@ extension Example: Hashable {
// Ignoring file/line metadata because two Examples could represent
// the same idea, but captured at two different points in the code
lhs.code == rhs.code
&& lhs.configuration?.mapValues(String.init(describing:))
== rhs.configuration?.mapValues(String.init(describing:))
}
public func hash(into hasher: inout Hasher) {
// Ignoring file/line metadata because two Examples could represent
// the same idea, but captured at two different points in the code
hasher.combine(code)
hasher.combine(configuration?.mapValues(String.init(describing:)))
}
}
@@ -1,32 +0,0 @@
@testable import SwiftLintBuiltInRules
import TestHelpers
final class TrailingClosureRuleTests: SwiftLintTestCase {
func testWithOnlySingleMutedParameterEnabled() {
let originalDescription = TrailingClosureRule.description
let description = originalDescription
.with(nonTriggeringExamples: originalDescription.nonTriggeringExamples + [
Example("foo.reduce(0, combine: { $0 + 1 })"),
Example("offsets.sorted(by: { $0.offset < $1.offset })"),
Example("foo.something(0, { $0 + 1 })"),
])
.with(triggeringExamples: [Example("foo.map(↓{ $0 + 1 })")])
.with(corrections: [
Example("foo.map(↓{ $0 + 1 })"):
Example("foo.map { $0 + 1 }"),
Example("f(↓{ g(↓{ 1 }) })"):
Example("f { g { 1 }}"),
Example("""
for n in list {
n.forEach(↓{ print($0) })
}
"""): Example("""
for n in list {
n.forEach { print($0) }
}
"""),
])
verifyRule(description, ruleConfiguration: ["only_single_muted_parameter": true])
}
}