Files
SwiftLint/Tests/BuiltInRulesTests/AttributesRuleTests.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

158 lines
5.3 KiB
Swift

import TestHelpers
import Testing
@testable import SwiftLintBuiltInRules
@Suite(.rulesRegistered)
struct AttributesRuleTests {
@Test
func attributesWithAlwaysOnSameLine() {
// Test with custom `always_on_same_line`
let nonTriggeringExamples = [
Example("@objc var x: String"),
Example("@objc func foo()"),
Example("@nonobjc\n func foo()"),
Example(
"""
class Foo {
@objc private var object: RLMWeakObjectHandle?
@objc private var property: RLMProperty?
}
"""),
Example(
"""
@objc(XYZFoo) class Foo: NSObject {}
"""),
]
let triggeringExamples = [
Example("@objc\n ↓var x: String"),
Example("@objc\n ↓func foo()"),
Example("@nonobjc ↓func foo()"),
]
let alwaysOnSameLineDescription = AttributesRule.description
.with(triggeringExamples: triggeringExamples)
.with(nonTriggeringExamples: nonTriggeringExamples)
verifyRule(
alwaysOnSameLineDescription,
ruleConfiguration: ["always_on_same_line": ["@objc"]])
}
@Test
func attributesWithAlwaysOnLineAbove() {
// Test with custom `always_on_line_above`
let nonTriggeringExamples = [
Example("@objc\n var x: String"),
Example("@objc\n func foo()"),
Example("@nonobjc\n func foo()"),
]
let triggeringExamples = [
Example("@objc ↓var x: String"),
Example("@objc ↓func foo()"),
Example("@nonobjc ↓func foo()"),
]
let alwaysOnNewLineDescription = AttributesRule.description
.with(triggeringExamples: triggeringExamples)
.with(nonTriggeringExamples: nonTriggeringExamples)
verifyRule(
alwaysOnNewLineDescription,
ruleConfiguration: ["always_on_line_above": ["@objc"]])
}
@Test
func attributesWithAttributesOnLineAboveButOnOtherDeclaration() {
let nonTriggeringExamples = [
Example(
"""
@IBDesignable open class TagListView: UIView {
@IBInspectable open dynamic var textColor: UIColor = UIColor.white {
didSet {}
}
}
"""),
Example(
"""
@objc public protocol TagListViewDelegate {
@objc optional func tagDidSelect(_ title: String, sender: TagListView)
@objc optional func tagDidDeselect(_ title: String, sender: TagListView)
}
"""),
]
let triggeringExamples = [
Example(
"""
@IBDesignable open class TagListView: UIView {
@IBInspectable
open dynamic ↓var textColor: UIColor = UIColor.white {
didSet {}
}
}
"""),
Example(
"""
@objc public protocol TagListViewDelegate {
@objc
optional ↓func tagDidSelect(_ title: String, sender: TagListView)
@objc optional func tagDidDeselect(_ title: String, sender: TagListView)
}
"""),
]
let alwaysOnNewLineDescription = AttributesRule.description
.with(triggeringExamples: triggeringExamples)
.with(nonTriggeringExamples: nonTriggeringExamples)
verifyRule(
alwaysOnNewLineDescription,
ruleConfiguration: [
"always_on_same_line": [
"@discardableResult", "@objc", "@IBAction", "@IBDesignable",
],
]
)
}
@Test
func attributesWithArgumentsAlwaysOnLineAboveFalse() {
let nonTriggeringExamples = [
Example("@Environment(\\.presentationMode) private var presentationMode")
]
let triggeringExamples = [
Example("""
@Environment(\\.presentationMode)
private ↓var presentationMode
"""),
]
let argumentsAlwaysOnLineDescription = AttributesRule.description
.with(triggeringExamples: triggeringExamples)
.with(nonTriggeringExamples: nonTriggeringExamples)
verifyRule(
argumentsAlwaysOnLineDescription,
ruleConfiguration: ["attributes_with_arguments_always_on_line_above": false])
}
@Test
func attributesWithArgumentsAlwaysOnLineAboveTrue() {
let nonTriggeringExamples = [
Example("@Environment(\\.presentationMode)\nprivate var presentationMode")
]
let triggeringExamples = [
Example("@Environment(\\.presentationMode) private ↓var presentationMode")
]
let argumentsAlwaysOnLineDescription = AttributesRule.description
.with(triggeringExamples: triggeringExamples)
.with(nonTriggeringExamples: nonTriggeringExamples)
verifyRule(
argumentsAlwaysOnLineDescription,
ruleConfiguration: ["attributes_with_arguments_always_on_line_above": true])
}
}