diff --git a/CHANGELOG.md b/CHANGELOG.md index 301253a8a..9e78aa69c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -794,7 +794,10 @@ The next release will require Swift 4.0 or higher to build. #### Enhancements -* None. +* Adds `xct_specific_matcher` opt-in rule to enforce specific matchers + over `XCTAssertEqual` and `XCTAssertNotEqual`. + [Ornithologist Coder](https://github.com/ornithocoder) + [#1874](https://github.com/realm/SwiftLint/issues/1874) #### Bug Fixes diff --git a/Tests/LinuxMain.swift b/Tests/LinuxMain.swift index 5e3dd2ab6..81d177407 100644 --- a/Tests/LinuxMain.swift +++ b/Tests/LinuxMain.swift @@ -1346,7 +1346,10 @@ extension XCTSpecificMatcherRuleTests { ("testNotEqualNil", testNotEqualNil), ("testEqualNilNil", testEqualNilNil), ("testEqualTrueTrue", testEqualTrueTrue), - ("testEqualFalseFalse", testEqualFalseFalse) + ("testEqualFalseFalse", testEqualFalseFalse), + ("testNotEqualNilNil", testNotEqualNilNil), + ("testNotEqualTrueTrue", testNotEqualTrueTrue), + ("testNotEqualFalseFalse", testNotEqualFalseFalse) ] } diff --git a/Tests/SwiftLintFrameworkTests/XCTSpecificMatcherRuleTests.swift b/Tests/SwiftLintFrameworkTests/XCTSpecificMatcherRuleTests.swift index dd3e4402e..b760d471b 100644 --- a/Tests/SwiftLintFrameworkTests/XCTSpecificMatcherRuleTests.swift +++ b/Tests/SwiftLintFrameworkTests/XCTSpecificMatcherRuleTests.swift @@ -20,43 +20,49 @@ class XCTSpecificMatcherRuleTests: XCTestCase { 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.") + 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.") + 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.") + 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.") + 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.") + 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.") + XCTAssertEqual(violations.first?.reason, "Prefer the specific matcher 'XCTAssertNotNil' instead.") } // MARK: - Additional Tests @@ -64,43 +70,49 @@ class XCTSpecificMatcherRuleTests: XCTestCase { func testEqualNilNil() { let string = "XCTAssertEqual(nil, nil)" let violations = self.violations(string) + XCTAssertEqual(violations.count, 1) - XCTAssertEqual(violations.first!.reason, "Prefer the specific matcher 'XCTAssertNil' instead.") + XCTAssertEqual(violations.first?.reason, "Prefer the specific matcher 'XCTAssertNil' instead.") } func testEqualTrueTrue() { let string = "XCTAssertEqual(true, true)" let violations = self.violations(string) + XCTAssertEqual(violations.count, 1) - XCTAssertEqual(violations.first!.reason, "Prefer the specific matcher 'XCTAssertTrue' instead.") + XCTAssertEqual(violations.first?.reason, "Prefer the specific matcher 'XCTAssertTrue' instead.") } func testEqualFalseFalse() { let string = "XCTAssertEqual(false, false)" let violations = self.violations(string) + XCTAssertEqual(violations.count, 1) - XCTAssertEqual(violations.first!.reason, "Prefer the specific matcher 'XCTAssertFalse' instead.") + 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.") + 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.") + 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.") + XCTAssertEqual(violations.first?.reason, "Prefer the specific matcher 'XCTAssertTrue' instead.") } private func violations(_ string: String) -> [StyleViolation] {