Files
SwiftLint/Tests/BuiltInRulesTests/TrailingCommaRuleTests.swift
Danny Mösch ac2eda6732 Adopt Swift Testing
# 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
2026-06-06 11:07:04 +02:00

83 lines
3.5 KiB
Swift

import Foundation
import TestHelpers
import Testing
@testable import SwiftLintBuiltInRules
@Suite(.rulesRegistered)
struct TrailingCommaRuleTests {
@Test
func trailingCommaRuleWithDefaultConfiguration() {
// Verify TrailingCommaRule with test values for when mandatory_comma is false (default).
let triggeringExamples = TrailingCommaRule.description.triggeringExamples +
[Example("class C {\n #if true\n func f() {\n let foo = [1, 2, 3↓,]\n }\n #endif\n}")]
verifyRule(TrailingCommaRule.description.with(triggeringExamples: triggeringExamples))
// Ensure the rule produces the correct reason string.
let failingCase = Example("let array = [\n\t1,\n\t2,\n]\n")
#expect(
trailingCommaViolations(failingCase).first?.reason == "Collection literals should not have trailing commas")
}
private static let triggeringExamples = [
Example("let foo = [1, 2,\n 3↓]\n"),
Example("let foo = [1: 2,\n 2: 3↓]\n"),
Example("let foo = [1: 2,\n 2: 3↓ ]\n"),
Example("struct Bar {\n let foo = [1: 2,\n 2: 3↓]\n}\n"),
Example("let foo = [1, 2,\n 3↓] + [4,\n 5, 6↓]\n"),
Example("let foo = [1, 2,\n 3↓ ]"),
Example("let foo = [\"אבג\", \"αβγ\",\n\"🇺🇸\"↓]\n"),
]
private static let nonTriggeringExamples = [
Example("let foo = []\n"),
Example("let foo = [:]\n"),
Example("let foo = [1, 2, 3,]\n"),
Example("let foo = [1, 2, 3, ]\n"),
Example("let foo = [1, 2, 3 ,]\n"),
Example("let foo = [1: 2, 2: 3, ]\n"),
Example("struct Bar {\n let foo = [1: 2, 2: 3,]\n}\n"),
Example("let foo = [Void]()\n"),
Example("let foo = [(Void, Void)]()\n"),
Example("let foo = [1, 2, 3]\n"),
Example("let foo = [1: 2, 2: 3]\n"),
Example("let foo = [1: 2, 2: 3 ]\n"),
Example("struct Bar {\n let foo = [1: 2, 2: 3]\n}\n"),
Example("let foo = [1, 2, 3] + [4, 5, 6]\n"),
]
private static let corrections: [Example: Example] = {
let fixed = triggeringExamples.map { $0.with(code: $0.code.replacingOccurrences(of: "↓", with: ",")) }
var result: [Example: Example] = [:]
for (triggering, correction) in zip(triggeringExamples, fixed) {
result[triggering] = correction
}
return result
}()
private let mandatoryCommaRuleDescription = TrailingCommaRule.description
.with(nonTriggeringExamples: Self.nonTriggeringExamples)
.with(triggeringExamples: Self.triggeringExamples)
.with(corrections: Self.corrections)
@Test
func trailingCommaRuleWithMandatoryComma() {
// Verify TrailingCommaRule with test values for when mandatory_comma is true.
let ruleDescription = mandatoryCommaRuleDescription
let ruleConfiguration = ["mandatory_comma": true]
verifyRule(ruleDescription, ruleConfiguration: ruleConfiguration)
// Ensure the rule produces the correct reason string.
let failingCase = Example("let array = [\n\t1,\n\t2\n]\n")
#expect(
trailingCommaViolations(failingCase, ruleConfiguration: ruleConfiguration).first?.reason
== "Multi-line collection literals should have trailing commas")
}
private func trailingCommaViolations(_ example: Example, ruleConfiguration: Any? = nil) -> [StyleViolation] {
let config = makeConfig(ruleConfiguration, TrailingCommaRule.identifier)!
return violations(example, config: config)
}
}