Adds additional tests for the reasons

This commit is contained in:
Ornithologist Coder
2018-12-24 01:01:11 -08:00
committed by Marcelo Fabri
parent 6d60d03fca
commit e497f63fe0
3 changed files with 84 additions and 1 deletions
@@ -50,7 +50,7 @@ public struct XCTSpecificMatcherRule: ASTRule, OptInRule, ConfigurationProviderR
let body = file.contents.bridge().substringWithByteRange(start: paramOffset, length: paramLength)
else { return nil }
return ["false", "true", "nil"].contains(body) ? body : nil
return protectedArguments.contains(body) ? body : nil
}
// If the call has "protected" words, provides suggestion based on the first one.
@@ -65,6 +65,10 @@ public struct XCTSpecificMatcherRule: ASTRule, OptInRule, ConfigurationProviderR
reason: "Prefer the specific matcher '\(reason)' instead.")
]
}
private let protectedArguments: Set<String> = [
"false", "true", "nil"
]
}
private enum XCTestMatcher: String {
+14
View File
@@ -1333,6 +1333,20 @@ extension WeakDelegateRuleTests {
extension XCTFailMessageRuleTests {
static var allTests: [(String, (XCTFailMessageRuleTests) -> () throws -> Void)] = [
("testWithDefaultConfiguration", testWithDefaultConfiguration)
}
extension XCTSpecificMatcherRuleTests {
static var allTests: [(String, (XCTSpecificMatcherRuleTests) -> () throws -> Void)] = [
("testRule", testRule),
("testEqualTrue", testEqualTrue),
("testEqualFalse", testEqualFalse),
("testEqualNil", testEqualNil),
("testNotEqualTrue", testNotEqualTrue),
("testNotEqualFalse", testNotEqualFalse),
("testNotEqualNil", testNotEqualNil),
("testEqualNilNil", testEqualNilNil),
("testEqualTrueTrue", testEqualTrueTrue),
("testEqualFalseFalse", testEqualFalseFalse)
]
}
@@ -15,6 +15,50 @@ class XCTSpecificMatcherRuleTests: XCTestCase {
verifyRule(XCTSpecificMatcherRule.description)
}
// MARK: - Reasons
func testEqualTrue() {
let string = "XCTAssertEqual(a, true)"
let violations = self.violations(string)
XCTAssertEqual(violations.count, 1)
XCTAssertEqual(violations.first!.reason, "Prefer the specific matcher 'XCTAssertTrue' instead.")
}
func testEqualFalse() {
let string = "XCTAssertEqual(a, false)"
let violations = self.violations(string)
XCTAssertEqual(violations.count, 1)
XCTAssertEqual(violations.first!.reason, "Prefer the specific matcher 'XCTAssertFalse' instead.")
}
func testEqualNil() {
let string = "XCTAssertEqual(a, nil)"
let violations = self.violations(string)
XCTAssertEqual(violations.count, 1)
XCTAssertEqual(violations.first!.reason, "Prefer the specific matcher 'XCTAssertNil' instead.")
}
func testNotEqualTrue() {
let string = "XCTAssertNotEqual(a, true)"
let violations = self.violations(string)
XCTAssertEqual(violations.count, 1)
XCTAssertEqual(violations.first!.reason, "Prefer the specific matcher 'XCTAssertFalse' instead.")
}
func testNotEqualFalse() {
let string = "XCTAssertNotEqual(a, false)"
let violations = self.violations(string)
XCTAssertEqual(violations.count, 1)
XCTAssertEqual(violations.first!.reason, "Prefer the specific matcher 'XCTAssertTrue' instead.")
}
func testNotEqualNil() {
let string = "XCTAssertNotEqual(a, nil)"
let violations = self.violations(string)
XCTAssertEqual(violations.count, 1)
XCTAssertEqual(violations.first!.reason, "Prefer the specific matcher 'XCTAssertNotNil' instead.")
}
// MARK: - Additional Tests
func testEqualNilNil() {
@@ -38,6 +82,27 @@ class XCTSpecificMatcherRuleTests: XCTestCase {
XCTAssertEqual(violations.first!.reason, "Prefer the specific matcher 'XCTAssertFalse' instead.")
}
func testNotEqualNilNil() {
let string = "XCTAssertNotEqual(nil, nil)"
let violations = self.violations(string)
XCTAssertEqual(violations.count, 1)
XCTAssertEqual(violations.first!.reason, "Prefer the specific matcher 'XCTAssertNotNil' instead.")
}
func testNotEqualTrueTrue() {
let string = "XCTAssertNotEqual(true, true)"
let violations = self.violations(string)
XCTAssertEqual(violations.count, 1)
XCTAssertEqual(violations.first!.reason, "Prefer the specific matcher 'XCTAssertFalse' instead.")
}
func testNotEqualFalseFalse() {
let string = "XCTAssertNotEqual(false, false)"
let violations = self.violations(string)
XCTAssertEqual(violations.count, 1)
XCTAssertEqual(violations.first!.reason, "Prefer the specific matcher 'XCTAssertTrue' instead.")
}
private func violations(_ string: String) -> [StyleViolation] {
let config = makeConfig(nil, XCTSpecificMatcherRule.description.identifier)!
return SwiftLintFrameworkTests.violations(string, config: config)