Files
SwiftLint/Tests/BuiltInRulesTests/ColonRuleTests.swift
Danny Mösch 15b285527a Separate built-in rule tests from framework tests (#5924)
* Short names for test modules
* Lint plugins and `Package.swift` in integration tests
* Simplify and merge file groups in Bazel
* Move common functions to `TestHelpers`
2024-12-30 12:26:46 +01:00

136 lines
8.1 KiB
Swift

@testable import SwiftLintBuiltInRules
import TestHelpers
final class ColonRuleTests: SwiftLintTestCase {
func testColonWithFlexibleRightSpace() {
// Verify Colon rule with test values for when flexible_right_spacing
// is true.
let nonTriggeringExamples = ColonRule.description.nonTriggeringExamples + [
Example("let abc: Void\n"),
Example("let abc: (Void, String, Int)\n"),
Example("let abc: ([Void], String, Int)\n"),
Example("let abc: [([Void], String, Int)]\n"),
Example("func abc(def: Void) {}\n"),
Example("let abc = [Void: Void]()\n"),
]
let triggeringExamples: [Example] = [
Example("let abc↓:Void\n"),
Example("let abc↓ :Void\n"),
Example("let abc↓ : Void\n"),
Example("let abc↓ : [Void: Void]\n"),
Example("let abc↓ : (Void, String, Int)\n"),
Example("let abc↓ : ([Void], String, Int)\n"),
Example("let abc↓ : [([Void], String, Int)]\n"),
Example("let abc↓ :String=\"def\"\n"),
Example("let abc↓ :Int=0\n"),
Example("let abc↓ :Int = 0\n"),
Example("let abc↓:Int=0\n"),
Example("let abc↓:Int = 0\n"),
Example("let abc↓:Enum=Enum.Value\n"),
Example("func abc(def↓:Void) {}\n"),
Example("func abc(def↓ :Void) {}\n"),
Example("func abc(def↓ : Void) {}\n"),
Example("func abc(def: Void, ghi↓ :Void) {}\n"),
Example("let abc = [Void↓:Void]()\n"),
Example("let abc = [Void↓ : Void]()\n"),
Example("let abc = [Void↓ : Void]()\n"),
Example("let abc = [1: [3↓ : 2], 3: 4]\n"),
Example("let abc = [1: [3↓ : 2], 3: 4]\n"),
]
let corrections: [Example: Example] = [
Example("let abc↓:Void\n"): Example("let abc: Void\n"),
Example("let abc↓ :Void\n"): Example("let abc: Void\n"),
Example("let abc↓ : Void\n"): Example("let abc: Void\n"),
Example("let abc↓ : [Void: Void]\n"): Example("let abc: [Void: Void]\n"),
Example("let abc↓ : (Void, String, Int)\n"): Example("let abc: (Void, String, Int)\n"),
Example("let abc↓ : ([Void], String, Int)\n"): Example("let abc: ([Void], String, Int)\n"),
Example("let abc↓ : [([Void], String, Int)]\n"): Example("let abc: [([Void], String, Int)]\n"),
Example("let abc↓ :String=\"def\"\n"): Example("let abc: String=\"def\"\n"),
Example("let abc↓ :Int=0\n"): Example("let abc: Int=0\n"),
Example("let abc↓ :Int = 0\n"): Example("let abc: Int = 0\n"),
Example("let abc↓:Int=0\n"): Example("let abc: Int=0\n"),
Example("let abc↓:Int = 0\n"): Example("let abc: Int = 0\n"),
Example("let abc↓:Enum=Enum.Value\n"): Example("let abc: Enum=Enum.Value\n"),
Example("func abc(def↓:Void) {}\n"): Example("func abc(def: Void) {}\n"),
Example("func abc(def↓ :Void) {}\n"): Example("func abc(def: Void) {}\n"),
Example("func abc(def↓ : Void) {}\n"): Example("func abc(def: Void) {}\n"),
Example("func abc(def: Void, ghi↓ :Void) {}\n"): Example("func abc(def: Void, ghi: Void) {}\n"),
Example("let abc = [Void↓:Void]()\n"): Example("let abc = [Void: Void]()\n"),
Example("let abc = [Void↓ : Void]()\n"): Example("let abc = [Void: Void]()\n"),
Example("let abc = [Void↓ : Void]()\n"): Example("let abc = [Void: Void]()\n"),
Example("let abc = [1: [3↓ : 2], 3: 4]\n"): Example("let abc = [1: [3: 2], 3: 4]\n"),
Example("let abc = [1: [3↓ : 2], 3: 4]\n"): Example("let abc = [1: [3: 2], 3: 4]\n"),
]
let description = ColonRule.description.with(triggeringExamples: triggeringExamples)
.with(nonTriggeringExamples: nonTriggeringExamples)
.with(corrections: corrections)
verifyRule(description, ruleConfiguration: ["flexible_right_spacing": true])
}
func testColonWithoutApplyToDictionaries() {
let nonTriggeringExamples = ColonRule.description.nonTriggeringExamples + [
Example("let abc = [Void:Void]()\n"),
Example("let abc = [Void : Void]()\n"),
Example("let abc = [Void: Void]()\n"),
Example("let abc = [Void : Void]()\n"),
Example("let abc = [1: [3 : 2], 3: 4]\n"),
Example("let abc = [1: [3 : 2], 3: 4]\n"),
]
let triggeringExamples: [Example] = [
Example("let abc↓:Void\n"),
Example("let abc↓: Void\n"),
Example("let abc↓ :Void\n"),
Example("let abc↓ : Void\n"),
Example("let abc↓ : [Void: Void]\n"),
Example("let abc↓ : (Void, String, Int)\n"),
Example("let abc↓ : ([Void], String, Int)\n"),
Example("let abc↓ : [([Void], String, Int)]\n"),
Example("let abc↓: (Void, String, Int)\n"),
Example("let abc↓: ([Void], String, Int)\n"),
Example("let abc↓: [([Void], String, Int)]\n"),
Example("let abc↓ :String=\"def\"\n"),
Example("let abc↓ :Int=0\n"),
Example("let abc↓ :Int = 0\n"),
Example("let abc↓:Int=0\n"),
Example("let abc↓:Int = 0\n"),
Example("let abc↓:Enum=Enum.Value\n"),
Example("func abc(def↓:Void) {}\n"),
Example("func abc(def↓: Void) {}\n"),
Example("func abc(def↓ :Void) {}\n"),
Example("func abc(def↓ : Void) {}\n"),
Example("func abc(def: Void, ghi↓ :Void) {}\n"),
]
let corrections: [Example: Example] = [
Example("let abc↓:Void\n"): Example("let abc: Void\n"),
Example("let abc↓: Void\n"): Example("let abc: Void\n"),
Example("let abc↓ :Void\n"): Example("let abc: Void\n"),
Example("let abc↓ : Void\n"): Example("let abc: Void\n"),
Example("let abc↓ : [Void: Void]\n"): Example("let abc: [Void: Void]\n"),
Example("let abc↓ : (Void, String, Int)\n"): Example("let abc: (Void, String, Int)\n"),
Example("let abc↓ : ([Void], String, Int)\n"): Example("let abc: ([Void], String, Int)\n"),
Example("let abc↓ : [([Void], String, Int)]\n"): Example("let abc: [([Void], String, Int)]\n"),
Example("let abc↓: (Void, String, Int)\n"): Example("let abc: (Void, String, Int)\n"),
Example("let abc↓: ([Void], String, Int)\n"): Example("let abc: ([Void], String, Int)\n"),
Example("let abc↓: [([Void], String, Int)]\n"): Example("let abc: [([Void], String, Int)]\n"),
Example("let abc↓ :String=\"def\"\n"): Example("let abc: String=\"def\"\n"),
Example("let abc↓ :Int=0\n"): Example("let abc: Int=0\n"),
Example("let abc↓ :Int = 0\n"): Example("let abc: Int = 0\n"),
Example("let abc↓:Int=0\n"): Example("let abc: Int=0\n"),
Example("let abc↓:Int = 0\n"): Example("let abc: Int = 0\n"),
Example("let abc↓:Enum=Enum.Value\n"): Example("let abc: Enum=Enum.Value\n"),
Example("func abc(def↓:Void) {}\n"): Example("func abc(def: Void) {}\n"),
Example("func abc(def↓: Void) {}\n"): Example("func abc(def: Void) {}\n"),
Example("func abc(def↓ :Void) {}\n"): Example("func abc(def: Void) {}\n"),
Example("func abc(def↓ : Void) {}\n"): Example("func abc(def: Void) {}\n"),
Example("func abc(def: Void, ghi↓ :Void) {}\n"): Example("func abc(def: Void, ghi: Void) {}\n"),
]
let description = ColonRule.description.with(triggeringExamples: triggeringExamples)
.with(nonTriggeringExamples: nonTriggeringExamples)
.with(corrections: corrections)
verifyRule(description, ruleConfiguration: ["apply_to_dictionaries": false])
}
}