mirror of
https://github.com/realm/SwiftLint.git
synced 2026-05-07 20:12:49 +00:00
Inline dedicated rule test (#6459)
This commit is contained in:
@@ -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])
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user