mirror of
https://github.com/realm/SwiftLint.git
synced 2026-06-06 20:18:40 +00:00
# Conflicts: # Tests/BuiltInRulesTests/FileHeaderRuleTests.swift # Tests/BuiltInRulesTests/FileNameNoSpaceRuleTests.swift # Tests/BuiltInRulesTests/IndentationWidthRuleTests.swift # Tests/CoreTests/RegexConfigurationTests.swift # Tests/CoreTests/SwiftLintFileTests.swift # Tests/FileSystemAccessTests/BaselineTests.swift # Tests/FileSystemAccessTests/ConfigurationTests+Mock.swift # Tests/FileSystemAccessTests/ConfigurationTests.swift # Tests/FileSystemAccessTests/GlobTests.swift # Tests/FileSystemAccessTests/MultipleConfigurationsTests.swift # Tests/FileSystemAccessTests/ReporterTests.swift # Tests/FileSystemAccessTests/SourceKitCrashTests.swift # Tests/FrameworkTests/CustomRulesTests.swift # Tests/FrameworkTests/LinterCacheTests.swift # Tests/FrameworkTests/RuleConfigurationTests.swift # Tests/FrameworkTests/SwiftVersionTests.swift # Tests/GeneratedTests/GeneratedTests_02.swift # Tests/GeneratedTests/GeneratedTests_03.swift # Tests/GeneratedTests/GeneratedTests_04.swift # Tests/GeneratedTests/GeneratedTests_05.swift # Tests/GeneratedTests/GeneratedTests_06.swift # Tests/GeneratedTests/GeneratedTests_07.swift # Tests/GeneratedTests/GeneratedTests_08.swift # Tests/GeneratedTests/GeneratedTests_09.swift # Tests/GeneratedTests/GeneratedTests_10.swift # Tests/IntegrationTests/ConfigPathResolutionTests.swift # Tests/IntegrationTests/IntegrationTests.swift # Tests/TestHelpers/TestHelpers.swift
162 lines
5.4 KiB
Swift
162 lines
5.4 KiB
Swift
import TestHelpers
|
|
import Testing
|
|
|
|
@testable import SwiftLintBuiltInRules
|
|
|
|
@Suite(.rulesRegistered)
|
|
struct PatternMatchingKeywordsRuleTests {
|
|
@Test
|
|
func violationReasonForTuples() throws {
|
|
let config = try #require(makeConfig(nil, PatternMatchingKeywordsRule.identifier))
|
|
let example = Example("switch foo { case (let x, let y): break }")
|
|
let violations = violations(example, config: config)
|
|
|
|
#expect(violations.count == 2)
|
|
#expect(violations.first?.reason == PatternMatchingKeywordsRule.Reason.tuples)
|
|
}
|
|
|
|
@Test
|
|
func violationReasonForEnumAssociatedValues() throws {
|
|
let config = try #require(makeConfig(nil, PatternMatchingKeywordsRule.identifier))
|
|
let example = Example("switch foo { case .bar(let x, let y): break }")
|
|
let violations = violations(example, config: config)
|
|
|
|
#expect(violations.count == 2)
|
|
#expect(violations.first?.reason == PatternMatchingKeywordsRule.Reason.enumAssociatedValues)
|
|
}
|
|
|
|
@Test
|
|
func regressionExamples() {
|
|
let triggering = [
|
|
"switch foo { case (.yamlParsing(↓let x), .yamlParsing(↓let y)): break }",
|
|
"switch foo { case (.yamlParsing(↓var x), (.yamlParsing(↓var y), _)): break }",
|
|
"""
|
|
do {} catch Foo.outer(.inner(↓let x), .inner(↓let y)) {}
|
|
""",
|
|
]
|
|
|
|
let nonTriggering = [
|
|
"switch foo { case (.yamlParsing(var x), (.yamlParsing(var y), z)): break }",
|
|
"switch foo { case (foo, let x): break }",
|
|
"if case (foo, let x) = value {}",
|
|
]
|
|
|
|
verifyRule(
|
|
PatternMatchingKeywordsRule.description.with(
|
|
nonTriggeringExamples: nonTriggering.map { Example($0) },
|
|
triggeringExamples: triggering.map { Example($0) }
|
|
)
|
|
)
|
|
}
|
|
|
|
@Test
|
|
func labeledAssociatedValues() {
|
|
let triggering = [
|
|
"switch foo { case .foo(bar: ↓let lhs, baz: ↓let rhs): break }",
|
|
"switch foo { case .foo(bar: ↓var lhs, baz: ↓var rhs): break }",
|
|
"do {} catch .foo(bar: ↓let x, baz: ↓let y) {}",
|
|
]
|
|
|
|
let nonTriggering = [
|
|
"switch foo { case let .foo(bar: lhs, baz: rhs): break }",
|
|
"switch foo { case var .foo(bar: lhs, baz: rhs): break }",
|
|
"switch foo { case .foo(bar: existingValue, baz: let x): break }",
|
|
"switch foo { case .foo(bar: let x, baz: existingValue): break }",
|
|
"do {} catch let .foo(bar: x, baz: y) {}",
|
|
]
|
|
|
|
verifyRule(
|
|
PatternMatchingKeywordsRule.description.with(
|
|
nonTriggeringExamples: nonTriggering.map { Example($0) },
|
|
triggeringExamples: triggering.map { Example($0) }
|
|
)
|
|
)
|
|
}
|
|
|
|
@Test
|
|
func singleBindingDoesNotTrigger() {
|
|
let examples = [
|
|
"switch foo { case (let x, y): break }",
|
|
"switch foo { case .foo(let x, y): break }",
|
|
"switch foo { case (.foo(let x), y): break }",
|
|
]
|
|
|
|
verifyRule(
|
|
PatternMatchingKeywordsRule.description.with(
|
|
nonTriggeringExamples: examples.map { Example($0) }
|
|
)
|
|
)
|
|
}
|
|
|
|
@Test
|
|
func neutralElementsDoNotBlockLift() {
|
|
let examples = [
|
|
"switch foo { case (↓let x, ↓let y, _): break }",
|
|
"switch foo { case (↓let x, ↓let y, 1): break }",
|
|
"switch foo { case (↓let x, ↓let y, .foo): break }",
|
|
"switch foo { case (↓let x, ↓let y, s.t): break }",
|
|
"switch foo { case .foo(↓let x, ↓let y, _): break }",
|
|
"switch foo { case .foo(.bar(↓let x), .bar(↓let y), .baz): break }",
|
|
]
|
|
|
|
verifyRule(
|
|
PatternMatchingKeywordsRule.description.with(
|
|
triggeringExamples: examples.map { Example($0) }
|
|
)
|
|
)
|
|
}
|
|
|
|
@Test
|
|
func threeAlternativeMultiPatternCase() {
|
|
let triggering = [
|
|
"switch foo { case .foo(↓let x, ↓let y), .bar(↓let x, ↓let y), .baz(↓let x, ↓let y): break }",
|
|
]
|
|
|
|
let nonTriggering = [
|
|
"switch foo { case let .foo(x, y), let .bar(x, y), let .baz(x, y): break }",
|
|
]
|
|
|
|
verifyRule(
|
|
PatternMatchingKeywordsRule.description.with(
|
|
nonTriggeringExamples: nonTriggering.map { Example($0) },
|
|
triggeringExamples: triggering.map { Example($0) }
|
|
)
|
|
)
|
|
}
|
|
|
|
@Test
|
|
func forCaseWithEnumAssociatedValues() {
|
|
let triggering = [
|
|
"for case .foo(↓let x, ↓let y) in values {}",
|
|
]
|
|
|
|
let nonTriggering = [
|
|
"for case let .foo(x, y) in values {}",
|
|
"for case .foo(existingValue, let x) in values {}",
|
|
]
|
|
|
|
verifyRule(
|
|
PatternMatchingKeywordsRule.description.with(
|
|
nonTriggeringExamples: nonTriggering.map { Example($0) },
|
|
triggeringExamples: triggering.map { Example($0) }
|
|
)
|
|
)
|
|
}
|
|
|
|
@Test
|
|
func sanityChecksForNonInterestingPatterns() {
|
|
let examples = [
|
|
"switch foo { case _: break }",
|
|
"switch foo { case .foo: break }",
|
|
"switch foo { case 42: break }",
|
|
"switch foo { case existingValue: break }",
|
|
]
|
|
|
|
verifyRule(
|
|
PatternMatchingKeywordsRule.description.with(
|
|
nonTriggeringExamples: examples.map { Example($0) }
|
|
)
|
|
)
|
|
}
|
|
}
|