mirror of
https://github.com/realm/SwiftLint.git
synced 2026-06-06 20:18:40 +00:00
449190d324
A rule must conform to ManuallyTestedExamplesRule to skip generation of a test for its examples.
48 lines
1.9 KiB
Swift
48 lines
1.9 KiB
Swift
import SourceKittenFramework
|
|
|
|
public struct EmptyXCTestMethodRule: Rule, OptInRule, ConfigurationProviderRule {
|
|
public var configuration = SeverityConfiguration(.warning)
|
|
|
|
public init() {}
|
|
|
|
public static let description = RuleDescription(
|
|
identifier: "empty_xctest_method",
|
|
name: "Empty XCTest Method",
|
|
description: "Empty XCTest method should be avoided.",
|
|
kind: .lint,
|
|
nonTriggeringExamples: EmptyXCTestMethodRuleExamples.nonTriggeringExamples,
|
|
triggeringExamples: EmptyXCTestMethodRuleExamples.triggeringExamples
|
|
)
|
|
|
|
public func validate(file: SwiftLintFile) -> [StyleViolation] {
|
|
return testClasses(in: file).flatMap { violations(in: file, for: $0) }
|
|
}
|
|
|
|
// MARK: - Private
|
|
|
|
private func testClasses(in file: SwiftLintFile) -> [SourceKittenDictionary] {
|
|
let dict = file.structureDictionary
|
|
return dict.substructure.filter { dictionary in
|
|
guard dictionary.declarationKind == .class else { return false }
|
|
return dictionary.inheritedTypes.contains("XCTestCase")
|
|
}
|
|
}
|
|
|
|
private func violations(in file: SwiftLintFile,
|
|
for dictionary: SourceKittenDictionary) -> [StyleViolation] {
|
|
return dictionary.substructure.compactMap { subDictionary -> StyleViolation? in
|
|
guard
|
|
let kind = subDictionary.declarationKind,
|
|
let name = subDictionary.name,
|
|
XCTestHelpers.isXCTestMember(kind: kind, name: name, dictionary: subDictionary),
|
|
let offset = subDictionary.offset,
|
|
subDictionary.enclosedVarParameters.isEmpty,
|
|
subDictionary.substructure.isEmpty else { return nil }
|
|
|
|
return StyleViolation(ruleDescription: Self.description,
|
|
severity: configuration.severity,
|
|
location: Location(file: file, byteOffset: offset))
|
|
}
|
|
}
|
|
}
|