Files
SwiftLint/Tests/MacroTests/SwiftSyntaxRuleTests.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

143 lines
4.5 KiB
Swift

import SwiftSyntaxMacroExpansion
import SwiftSyntaxMacrosGenericTestSupport
import Testing
@testable import SwiftLintCoreMacros
private let macros = [
"SwiftSyntaxRule": MacroSpec(type: SwiftSyntaxRule.self)
]
@Suite
struct SwiftSyntaxRuleTests {
@Test
func noArguments() {
assertMacroExpansion(
"""
@SwiftSyntaxRule
struct Hello {}
""",
expandedSource: """
struct Hello {}
extension Hello: SwiftSyntaxRule {
func makeVisitor(file: SwiftLintFile) -> ViolationsSyntaxVisitor<ConfigurationType> {
Visitor(configuration: configuration, file: file)
}
}
""",
macroSpecs: macros,
failureHandler: FailureHandler.instance
)
}
@Test
func falseArguments() {
assertMacroExpansion(
"""
@SwiftSyntaxRule(foldExpressions: false, explicitRewriter: false, correctable: false, optIn: false)
struct Hello {}
""",
expandedSource: """
struct Hello {}
extension Hello: SwiftSyntaxRule {
func makeVisitor(file: SwiftLintFile) -> ViolationsSyntaxVisitor<ConfigurationType> {
Visitor(configuration: configuration, file: file)
}
}
""",
macroSpecs: macros,
failureHandler: FailureHandler.instance
)
}
@Test
func trueArguments() {
assertMacroExpansion(
"""
@SwiftSyntaxRule(foldExpressions: true, explicitRewriter: true, correctable: true, optIn: true)
struct Hello {}
""",
expandedSource: """
struct Hello {}
extension Hello: SwiftSyntaxRule {
func makeVisitor(file: SwiftLintFile) -> ViolationsSyntaxVisitor<ConfigurationType> {
Visitor(configuration: configuration, file: file)
}
}
extension Hello {
func preprocess(file: SwiftLintFile) -> SourceFileSyntax? {
file.foldedSyntaxTree
}
}
extension Hello: SwiftSyntaxCorrectableRule {
func makeRewriter(file: SwiftLintFile) -> ViolationsSyntaxRewriter<ConfigurationType>? {
Rewriter(configuration: configuration, file: file)
}
}
extension Hello: OptInRule {
}
""",
macroSpecs: macros,
failureHandler: FailureHandler.instance
)
}
@Test
func correctableWithoutExplcitRewriter() {
assertMacroExpansion(
"""
@SwiftSyntaxRule(correctable: true)
struct Hello {}
""",
expandedSource: """
struct Hello {}
extension Hello: SwiftSyntaxRule {
func makeVisitor(file: SwiftLintFile) -> ViolationsSyntaxVisitor<ConfigurationType> {
Visitor(configuration: configuration, file: file)
}
}
extension Hello: SwiftSyntaxCorrectableRule {
}
""",
macroSpecs: macros,
failureHandler: FailureHandler.instance
)
}
@Test
func arbitraryArguments() {
// Fail with a diagnostic because the macro definition explicitly requires bool arguments.
assertMacroExpansion(
"""
@SwiftSyntaxRule(foldExpressions: arg, explicitRewriter: arg, correctable: arg, optIn: arg)
struct Hello {}
""",
expandedSource: """
struct Hello {}
extension Hello: SwiftSyntaxRule {
func makeVisitor(file: SwiftLintFile) -> ViolationsSyntaxVisitor<ConfigurationType> {
Visitor(configuration: configuration, file: file)
}
}
""",
diagnostics: [
DiagnosticSpec(message: SwiftLintCoreMacroError.noBooleanLiteral.message, line: 1, column: 35),
DiagnosticSpec(message: SwiftLintCoreMacroError.noBooleanLiteral.message, line: 1, column: 58),
DiagnosticSpec(message: SwiftLintCoreMacroError.noBooleanLiteral.message, line: 1, column: 76),
DiagnosticSpec(message: SwiftLintCoreMacroError.noBooleanLiteral.message, line: 1, column: 88),
],
macroSpecs: macros,
failureHandler: FailureHandler.instance
)
}
}