From 488488ef0dc5391beec8ef4ab66ee146d0a6a2bc Mon Sep 17 00:00:00 2001 From: Ornithologist Coder Date: Fri, 16 Feb 2018 23:31:33 +0100 Subject: [PATCH] Prepare for SR-5717 apple/swift#11542 --- .../Rules/XCTSpecificMatcherRule.swift | 19 ++++++++++--------- .../XCTSpecificMatcherRuleTests.swift | 8 -------- 2 files changed, 10 insertions(+), 17 deletions(-) diff --git a/Source/SwiftLintFramework/Rules/XCTSpecificMatcherRule.swift b/Source/SwiftLintFramework/Rules/XCTSpecificMatcherRule.swift index 533ca0e25..f8d8a7f91 100644 --- a/Source/SwiftLintFramework/Rules/XCTSpecificMatcherRule.swift +++ b/Source/SwiftLintFramework/Rules/XCTSpecificMatcherRule.swift @@ -34,7 +34,7 @@ public struct XCTSpecificMatcherRule: ASTRule, OptInRule, ConfigurationProviderR /* * - Get the first two arguments and creates an array where the protected - * word is the first one (if any). + * word is the first one (if any). */ let arguments = dictionary.substructure .filter { $0.offset != nil } @@ -60,15 +60,16 @@ public struct XCTSpecificMatcherRule: ASTRule, OptInRule, ConfigurationProviderR } /* - * - Check if the first one is a protected word, otherwise there's not need to continue. - * - Retrieve the suggestion for the protected word, making sure that optional arguments are considered. - * - * Note that optional arguments don't show on dictionary.substructure, therefore arguments.count == 1 implies - * it contains an optional argument. + * - Check if the number of arguments is two (otherwise there's no need to continue) + * - Check if the first argument is a protected word (otherwise there's no need to continue) + * - Get the suggestion for the given protected word (taking in consideration the presence of + * optionals */ guard + arguments.count == 2, let argument = arguments.first, protectedArguments.contains(argument), - let suggestedMatcher = matcher.suggestion(for: argument, containsOptionalArgument: arguments.count == 1) + let hasOptional = arguments.last?.contains("?"), + let suggestedMatcher = matcher.suggestion(for: argument, hasOptional: hasOptional) else { return [] } return [ @@ -88,8 +89,8 @@ private enum XCTestMatcher: String { case equal = "XCTAssertEqual" case notEqual = "XCTAssertNotEqual" - func suggestion(for protectedArgument: String, containsOptionalArgument: Bool) -> String? { - switch (self, protectedArgument, containsOptionalArgument) { + func suggestion(for protectedArgument: String, hasOptional: Bool) -> String? { + switch (self, protectedArgument, hasOptional) { case (.equal, "true", false): return "XCTAssertTrue" case (.equal, "false", false): return "XCTAssertFalse" case (.equal, "nil", _): return "XCTAssertNil" diff --git a/Tests/SwiftLintFrameworkTests/XCTSpecificMatcherRuleTests.swift b/Tests/SwiftLintFrameworkTests/XCTSpecificMatcherRuleTests.swift index 463d5f4c5..87b4cd65c 100644 --- a/Tests/SwiftLintFrameworkTests/XCTSpecificMatcherRuleTests.swift +++ b/Tests/SwiftLintFrameworkTests/XCTSpecificMatcherRuleTests.swift @@ -82,14 +82,6 @@ class XCTSpecificMatcherRuleTests: XCTestCase { XCTAssertEqual(violations.first?.reason, "Prefer the specific matcher 'XCTAssertFalse' instead.") } - func testEqualUnwrappedOptionalTrue() { - let string = "XCTAssertEqual((a?.b)!, true)" - let violations = self.violations(string) - - XCTAssertEqual(violations.count, 1) - XCTAssertEqual(violations.first?.reason, "Prefer the specific matcher 'XCTAssertTrue' instead.") - } - func testEqualNilNil() { let string = "XCTAssertEqual(nil, nil)" let violations = self.violations(string)