Skip to content

Commit

Permalink
Merge pull request realm#1580 from marcelofabri/fix-1532
Browse files Browse the repository at this point in the history
Don't trigger private_unit_test when a method has parameters
  • Loading branch information
marcelofabri authored May 28, 2017
2 parents fc5e610 + 6fdf80b commit da7c0d9
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 9 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@
[Marcelo Fabri](https://github.com/marcelofabri)
[#1554](https://github.com/realm/SwiftLint/issues/1554)

* Don't trigger violations from `private_unit_test` rule when a
method has parameters.
[Marcelo Fabri](https://github.com/marcelofabri)
[#1532](https://github.com/realm/SwiftLint/issues/1532)

##### Bug Fixes

* None.
Expand Down
30 changes: 21 additions & 9 deletions Source/SwiftLintFramework/Rules/PrivateUnitTestRule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,16 @@ private extension Dictionary where Key: ExpressibleByStringLiteral {
let className = inheritedTypes.first else { return nil }
return className
}

var parameters: [[String: SourceKitRepresentable]] {
return substructure.filter { dict in
guard let kind = dict.kind.flatMap(SwiftDeclarationKind.init) else {
return false
}

return kind == .varParameter
}
}
}

public struct PrivateUnitTestRule: ASTRule, ConfigurationProviderRule, CacheDescriptionProvider {
Expand Down Expand Up @@ -71,6 +81,10 @@ public struct PrivateUnitTestRule: ASTRule, ConfigurationProviderRule, CacheDesc
"func test1() {}\n " +
"internal func test2() {}\n " +
"public func test3() {}\n " +
"}",
// Methods with params
"public class FooTest: XCTestCase { " +
"func test1(param: Int) {}\n " +
"}"
],
triggeringExamples: [
Expand Down Expand Up @@ -119,11 +133,7 @@ public struct PrivateUnitTestRule: ASTRule, ConfigurationProviderRule, CacheDesc
guard classViolations.isEmpty else { return classViolations }

return dictionary.substructure.flatMap { subDict -> [StyleViolation] in
guard let kindString = subDict.kind,
let kind = KindType(rawValue: kindString), kind == .functionMethodInstance else {
return []
}
return validateFunction(file: file, kind: kind, dictionary: subDict)
return validateFunction(file: file, dictionary: subDict)
}
}

Expand All @@ -135,11 +145,13 @@ public struct PrivateUnitTestRule: ASTRule, ConfigurationProviderRule, CacheDesc
return !regex.matches(in: superclass, options: [], range: range).isEmpty
}

private func validateFunction(file: File, kind: SwiftDeclarationKind,
private func validateFunction(file: File,
dictionary: [String: SourceKitRepresentable]) -> [StyleViolation] {
assert(kind == .functionMethodInstance)
guard let name = dictionary.name, name.hasPrefix("test") else {
return []
guard let kind = dictionary.kind.flatMap(SwiftDeclarationKind.init),
kind == .functionMethodInstance,
let name = dictionary.name, name.hasPrefix("test"),
dictionary.parameters.isEmpty else {
return []
}
return validateAccessControlLevel(file: file, dictionary: dictionary)
}
Expand Down

0 comments on commit da7c0d9

Please sign in to comment.