diff --git a/Demo/Navajo/ViewController.swift b/Demo/Navajo/ViewController.swift index 9331fd2..9972c70 100644 --- a/Demo/Navajo/ViewController.swift +++ b/Demo/Navajo/ViewController.swift @@ -29,7 +29,7 @@ final class ViewController: UIViewController { @IBOutlet private weak var strengthLabel: UILabel! = nil @IBOutlet private weak var validationLabel: UILabel! = nil - private var validator = NJOPasswordValidator.standardValidator() + private var validator = NJOPasswordValidator.standardValidator override func viewDidAppear(_ animated: Bool) { super.viewDidAppear(animated) @@ -47,7 +47,7 @@ final class ViewController: UIViewController { validator = NJOPasswordValidator(rules: [lengthRule, emailFilteringRule]) } else { - validator = NJOPasswordValidator.standardValidator() + validator = NJOPasswordValidator.standardValidator } checkPassword() @@ -55,15 +55,15 @@ final class ViewController: UIViewController { private func checkPassword() { let password = passwordField.text ?? "" - let strength = Navajo.strengthOfPassword(password: password) + let strength = Navajo.strength(of: password) - strengthLabel.text = Navajo.localizedStringForPasswordStrength(strength: strength) + strengthLabel.text = Navajo.localizedString(for: strength) - if let failingRules = validator.validatePassword(password: password) { + if let failingRules = validator.validate(password) { var errorMessages: [String] = [] failingRules.forEach { rule in - errorMessages.append(rule.localizedErrorDescription()) + errorMessages.append(rule.localizedErrorDescription) } validationLabel.textColor = UIColor.red diff --git a/README.md b/README.md index 54b1d62..fc15157 100644 --- a/README.md +++ b/README.md @@ -54,9 +54,9 @@ Just copy the files in Source folder into your project. @IBOutlet private weak var strengthLabel: UILabel! = nil let password = passwordField.text ?? "" -let strength = Navajo.strengthOfPassword(password: password) +let strength = Navajo.strength(of: password) -strengthLabel.text = Navajo.localizedStringForPasswordStrength(strength: strength) +strengthLabel.text = Navajo.localizedString(for: strength) ``` ### Validating Password @@ -67,11 +67,11 @@ var uppercaseRule = NJORequiredCharacterRule(preset: .LowercaseCharacter) validator = NJOPasswordValidator(rules: [lengthRule, uppercaseRule]) -if let failingRules = validator.validatePassword(password: "PASSWORD") { +if let failingRules = validator.validate(password) { var errorMessages: [String] = [] failingRules.forEach { rule in - errorMessages.append(rule.localizedErrorDescription()) + errorMessages.append(rule.localizedErrorDescription) } validationLabel.textColor = UIColor.red diff --git a/Source/NJOPasswordRule.swift b/Source/NJOPasswordRule.swift index efb1e9a..b82b067 100644 --- a/Source/NJOPasswordRule.swift +++ b/Source/NJOPasswordRule.swift @@ -32,14 +32,15 @@ import Foundation /// By adopting NJOPasswordRule protocol you can build your own rules. public protocol NJOPasswordRule { - /** - Evaluating the password - - Parameter password: Password string to be evaluated - - Returns: true is considered to be failed and false is passed. - */ - func evaluateWithString(string: String) -> Bool + /// Evaluating the password + /// + /// - parameter password: Password string to be evaluated + /// + /// - returns: true is considered to be failed and false is passed. + func evaluate(_ password: String) -> Bool + /// Error description - func localizedErrorDescription() -> String + var localizedErrorDescription: String { get } } /// NJORequiredCharacterRulePreset makes initializing NJORequiredCharacterRule easy. @@ -56,7 +57,7 @@ public enum NJORequiredCharacterRulePreset { /// NJOAllowedCharacterRule checks if the password only has allowed characters. open class NJOAllowedCharacterRule: NSObject, NJOPasswordRule { - var disallowedCharacters: CharacterSet? = nil + open var disallowedCharacters: CharacterSet? = nil /// Initialize with an NSCharacterSet object. public convenience init(characterSet: CharacterSet) { @@ -65,17 +66,17 @@ open class NJOAllowedCharacterRule: NSObject, NJOPasswordRule { } /// Evaluate password. Return false if it is passed and true if failed. - open func evaluateWithString(string: String) -> Bool { + open func evaluate(_ password: String) -> Bool { guard let disallowedCharacters = disallowedCharacters else { return false } - return (string as NSString).rangeOfCharacter(from: disallowedCharacters).location != NSNotFound + return (password as NSString).rangeOfCharacter(from: disallowedCharacters).location != NSNotFound } /// Error description. /// Localization Key - "NAVAJO_ALLOWED_CHARACTER_ERROR" - open func localizedErrorDescription() -> String { + open var localizedErrorDescription: String { return NSLocalizedString("NAVAJO_ALLOWED_CHARACTER_ERROR", tableName: nil, bundle: Bundle.main, value: "Must not include disallowed character", comment: "Navajo - Allowed character rule") } } @@ -84,20 +85,20 @@ open class NJOAllowedCharacterRule: NSObject, NJOPasswordRule { open class NJORequiredCharacterRule: NSObject, NJOPasswordRule { private var requiredCharacterSetToCompare: CharacterSet? = nil - var requiredCharacterSet: CharacterSet? = nil { + open var requiredCharacterSet: CharacterSet? = nil { didSet { if let requiredCharacterSet = requiredCharacterSet { preset = nil - updateRequiredCharacterSetToCompare(requiredCharacterSet: requiredCharacterSet) + updateRequiredCharacterSetToCompare(with: requiredCharacterSet) } } } - var preset: NJORequiredCharacterRulePreset? = nil { + open var preset: NJORequiredCharacterRulePreset? = nil { didSet { if let preset = preset { requiredCharacterSet = nil - updateRequiredCharacterSetToCompare(preset: preset) + updateRequiredCharacterSetToCompare(with: preset) } } } @@ -106,21 +107,21 @@ open class NJORequiredCharacterRule: NSObject, NJOPasswordRule { public convenience init(preset: NJORequiredCharacterRulePreset) { self.init() self.preset = preset - updateRequiredCharacterSetToCompare(preset: preset) + updateRequiredCharacterSetToCompare(with: preset) } /// Initialize with an NSCharacterSet object. public convenience init(characterSet: CharacterSet) { self.init() requiredCharacterSet = characterSet - updateRequiredCharacterSetToCompare(requiredCharacterSet: characterSet) + updateRequiredCharacterSetToCompare(with: characterSet) } - private func updateRequiredCharacterSetToCompare(requiredCharacterSet: CharacterSet) { + private func updateRequiredCharacterSetToCompare(with requiredCharacterSet: CharacterSet) { requiredCharacterSetToCompare = requiredCharacterSet } - private func updateRequiredCharacterSetToCompare(preset: NJORequiredCharacterRulePreset) { + private func updateRequiredCharacterSetToCompare(with preset: NJORequiredCharacterRulePreset) { switch preset { case .lowercaseCharacter: requiredCharacterSetToCompare = CharacterSet.lowercaseLetters @@ -136,12 +137,12 @@ open class NJORequiredCharacterRule: NSObject, NJOPasswordRule { } /// Evaluate password. Return false if it is passed and true if failed. - open func evaluateWithString(string: String) -> Bool { + open func evaluate(_ password: String) -> Bool { guard let requiredCharacterSetToCompare = requiredCharacterSetToCompare else { return false } - return (string as NSString).rangeOfCharacter(from: requiredCharacterSetToCompare).location == NSNotFound + return (password as NSString).rangeOfCharacter(from: requiredCharacterSetToCompare).location == NSNotFound } /// Error description. @@ -151,7 +152,7 @@ open class NJORequiredCharacterRule: NSObject, NJOPasswordRule { /// - Decimal digit error "NAVAJO_REQUIRED_CHARACTER_DECIMAL_DIGIT_ERROR" /// - Symbol error "NAVAJO_REQUIRED_CHARACTER_SYMBOL_ERROR" /// - Default error "NAVAJO_REQUIRED_CHARACTER_REQUIRED_ERROR" - open func localizedErrorDescription() -> String { + open var localizedErrorDescription: String { if let preset = preset { switch preset { case .lowercaseCharacter: @@ -174,24 +175,24 @@ open class NJODictionaryWordRule: NSObject, NJOPasswordRule { private let nonLowercaseCharacterSet = CharacterSet.lowercaseLetters.inverted /// Evaluate password. Return false if it is passed and true if failed. - open func evaluateWithString(string: String) -> Bool { + open func evaluate(_ password: String) -> Bool { #if os(OSX) - return DCSGetTermRangeInString(nil, string as CFString, 0).location != kCFNotFound + return DCSGetTermRangeInString(nil, password as CFString, 0).location != kCFNotFound #else - return UIReferenceLibraryViewController.dictionaryHasDefinition(forTerm: string.lowercased().trimmingCharacters(in: nonLowercaseCharacterSet)) + return UIReferenceLibraryViewController.dictionaryHasDefinition(forTerm: password.lowercased().trimmingCharacters(in: nonLowercaseCharacterSet)) #endif } /// Error description. /// Localization Key - "NAVAJO_DICTIONARYWORD_ERROR" - open func localizedErrorDescription() -> String { + open var localizedErrorDescription: String { return NSLocalizedString("NAVAJO_DICTIONARYWORD_ERROR", tableName: nil, bundle: Bundle.main, value: "Must not be dictionary word", comment: "Navajo - Dictionary word rule") } } /// NJOLengthRule checks the length of password. open class NJOLengthRule: NSObject, NJOPasswordRule { - var range: NSRange? = nil + open var range: NSRange? = nil /// Initialize with minimum and maximum values. public convenience init(min: Int, max: Int) { @@ -200,17 +201,17 @@ open class NJOLengthRule: NSObject, NJOPasswordRule { } /// Evaluate password. Return false if it is passed and true if failed. - open func evaluateWithString(string: String) -> Bool { + open func evaluate(_ password: String) -> Bool { guard let range = range else { return false } - return !NSLocationInRange(string.characters.count, range) + return !NSLocationInRange(password.characters.count, range) } /// Error description. /// Localization Key - "NAVAJO_LENGTH_ERROR" - open func localizedErrorDescription() -> String { + open var localizedErrorDescription: String { var rangeDescription = "nil" if let range = range { @@ -223,7 +224,7 @@ open class NJOLengthRule: NSObject, NJOPasswordRule { /// NJOPredicateRule checks password with a NSPredicate object. open class NJOPredicateRule: NSObject, NJOPasswordRule { - var predicate: NSPredicate? = nil + open var predicate: NSPredicate? = nil /// Initialize with an NSPredicate object. public convenience init(predicate: NSPredicate) { @@ -232,24 +233,24 @@ open class NJOPredicateRule: NSObject, NJOPasswordRule { } /// Evaluate password. Return false if it is passed and true if failed. - open func evaluateWithString(string: String) -> Bool { + open func evaluate(_ password: String) -> Bool { guard let predicate = predicate else { return false } - return predicate.evaluate(with: string) + return predicate.evaluate(with: password) } /// Error description. /// Localization Key - "NAVAJO_PREDICATE_ERROR" - open func localizedErrorDescription() -> String { + open var localizedErrorDescription: String { return NSLocalizedString("NAVAJO_PREDICATE_ERROR", tableName: nil, bundle: Bundle.main, value: "Must not match predicate", comment: "Navajo - Predicate rule") } } /// NJORegularExpressionRule checks password with a NSRegularExpression object. open class NJORegularExpressionRule: NSObject, NJOPasswordRule { - var regularExpression: NSRegularExpression? = nil + open var regularExpression: NSRegularExpression? = nil /// Initialize with an NSRegularExpression object. public convenience init(regularExpression: NSRegularExpression) { @@ -258,24 +259,24 @@ open class NJORegularExpressionRule: NSObject, NJOPasswordRule { } /// Evaluate password. Return false if it is passed and true if failed. - open func evaluateWithString(string: String) -> Bool { + open func evaluate(_ password: String) -> Bool { guard let regularExpression = regularExpression else { return false } - return regularExpression.numberOfMatches(in: string, options: [], range: NSMakeRange(0, string.characters.count)) > 0 + return regularExpression.numberOfMatches(in: password, options: [], range: NSMakeRange(0, password.characters.count)) > 0 } /// Error description. /// Localization Key - "NAVAJO_REGEX_ERROR" - open func localizedErrorDescription() -> String { + open var localizedErrorDescription: String { return NSLocalizedString("NAVAJO_REGEX_ERROR", tableName: nil, bundle: Bundle.main, value: "Must not match regular expression", comment: "Navajo - Regex rule") } } /// NJOBlockRule checks password with a block which gets a string and returns a bool value. open class NJOBlockRule: NSObject, NJOPasswordRule { - var evaluation: ((String) -> Bool)? = nil + open var evaluation: ((String) -> Bool)? = nil /// Initialize with a Block. public convenience init(evaluation: @escaping (String) -> Bool) { @@ -284,17 +285,17 @@ open class NJOBlockRule: NSObject, NJOPasswordRule { } /// Evaluate password. Return false if it is passed and true if failed. - open func evaluateWithString(string: String) -> Bool { + open func evaluate(_ password: String) -> Bool { guard let evaluation = evaluation else { return false } - return evaluation(string) + return evaluation(password) } /// Error description. /// Localization Key - "NAVAJO_BLOCK_ERROR" - open func localizedErrorDescription() -> String { + open var localizedErrorDescription: String { return NSLocalizedString("NAVAJO_BLOCK_ERROR", tableName: nil, bundle: Bundle.main, value: "Must not satisfy precondition", comment: "Navajo - Block rule") } } diff --git a/Source/NJOPasswordValidator.swift b/Source/NJOPasswordValidator.swift index ff4539b..cd09ad3 100644 --- a/Source/NJOPasswordValidator.swift +++ b/Source/NJOPasswordValidator.swift @@ -27,34 +27,38 @@ import Foundation /// NJOPasswordValidator validates passwords with custom rules. open class NJOPasswordValidator: NSObject { - var rules: [NJOPasswordRule] = [] + open var rules: [NJOPasswordRule] = [] /// Initialize NJOPasswordValidator with an array of NJOPasswordRule. + /// + /// - parameter rules: Password rule(s) + /// + /// - returns: Password validator public convenience init(rules: [NJOPasswordRule]) { self.init() self.rules = rules } /// NJOPasswordValidator object which checks if the length of password is between 6 and 24. - open class func standardValidator() -> NJOPasswordValidator { - return NJOPasswordValidator(rules: [standardLengthRule()]) + open class var standardValidator: NJOPasswordValidator { + return NJOPasswordValidator(rules: [standardLengthRule]) } /// Length rule having minimum of 6 and maximum of 24. - open class func standardLengthRule() -> NJOLengthRule { + open class var standardLengthRule: NJOLengthRule { return NJOLengthRule(min: 6, max: 24) } - /** - Executes validation with a password and returns failing rules. - - Parameter password: Password string to be validated - - Returns: Failing rules. nil if all of the rules are passed. - */ - open func validatePassword(password: String) -> [NJOPasswordRule]? { + /// Executes validation with a password and returns failing rules. + /// + /// - parameter password: Password string to be validated + /// + /// - returns: Failing rules. nil if all of the rules are passed. + open func validate(_ password: String) -> [NJOPasswordRule]? { var failingRules: [NJOPasswordRule] = [] for rule in rules { - if rule.evaluateWithString(string: password) { + if rule.evaluate(password) { failingRules.insert(rule, at: failingRules.count) } } diff --git a/Source/Navajo.swift b/Source/Navajo.swift index 58a797a..37f7e94 100644 --- a/Source/Navajo.swift +++ b/Source/Navajo.swift @@ -42,18 +42,21 @@ public enum NJOPasswordStrength { /// Navajo validates strength of passwords. open class Navajo: NSObject { - - /** - Gets strength of a password. - - Parameter password: Password string to be calculated - - Returns: Level of strength in NJOPasswordStrength - */ - open class func strengthOfPassword(password: String) -> NJOPasswordStrength { - return NJOPasswordStrengthForEntropy(entropy: NJOEntropyForString(password)) + /// Gets strength of a password. + /// + /// - parameter password: Password string to be calculated + /// + /// - returns: Level of strength in NJOPasswordStrength + open class func strength(of password: String) -> NJOPasswordStrength { + return NJOPasswordStrength(for: NJOEntropy(for: password)) } /// Converts NJOPasswordStrength to localized string. - open class func localizedStringForPasswordStrength(strength: NJOPasswordStrength) -> String { + /// + /// - parameter strength: NJOPasswordStrength to be converted + /// + /// - returns: Localized string + open class func localizedString(for strength: NJOPasswordStrength) -> String { switch strength { case .veryWeak: return NSLocalizedString("NAVAJO_VERY_WEAK", tableName: nil, bundle: Bundle.main, value: "Very Weak", comment: "Navajo - Very weak") @@ -68,7 +71,7 @@ open class Navajo: NSObject { } } - private class func NJOEntropyForString(_ string: String) -> Float { + private class func NJOEntropy(for string: String) -> Float { guard string.characters.count > 0 else { return 0.0 } @@ -112,7 +115,7 @@ open class Navajo: NSObject { return log2f(sizeOfCharacterSet) * Float(string.characters.count) } - private class func NJOPasswordStrengthForEntropy(entropy: Float) -> NJOPasswordStrength { + private class func NJOPasswordStrength(for entropy: Float) -> NJOPasswordStrength { if entropy < 28 { return .veryWeak } else if entropy < 36 { diff --git a/Tests/NavajoTests.swift b/Tests/NavajoTests.swift index 2502cdd..ab6f724 100644 --- a/Tests/NavajoTests.swift +++ b/Tests/NavajoTests.swift @@ -41,96 +41,96 @@ class NavajoTests: XCTestCase { func testAllowedCharacterRule() { let ruleWithoutInitialCharacterSet = NJOAllowedCharacterRule() - XCTAssert(ruleWithoutInitialCharacterSet.evaluateWithString(string: "PASSWORD") == false, "Allowed character rule not ignored") + XCTAssert(ruleWithoutInitialCharacterSet.evaluate("PASSWORD") == false, "Allowed character rule not ignored") let ruleWithInitialCharacterSet = NJOAllowedCharacterRule(characterSet: CharacterSet(charactersIn: "abc")) - XCTAssert(ruleWithInitialCharacterSet.evaluateWithString(string: "abcd") == true, "Disallowed characters are not filtered out") - XCTAssert(ruleWithInitialCharacterSet.evaluateWithString(string: "aaa") == false, "Allowed characters are used but failed to pass") - XCTAssert(ruleWithInitialCharacterSet.evaluateWithString(string: "aabbccaaabbbcccaaaabbbbcccc") == false, "Allowed characters are used but failed to pass") + XCTAssert(ruleWithInitialCharacterSet.evaluate("abcd") == true, "Disallowed characters are not filtered out") + XCTAssert(ruleWithInitialCharacterSet.evaluate("aaa") == false, "Allowed characters are used but failed to pass") + XCTAssert(ruleWithInitialCharacterSet.evaluate("aabbccaaabbbcccaaaabbbbcccc") == false, "Allowed characters are used but failed to pass") } func testRequiredCharacterRule() { let ruleWithoutInitialOption = NJORequiredCharacterRule() - XCTAssert(ruleWithoutInitialOption.evaluateWithString(string: "PASSWORD") == false, "Required character rule without initial option not ignored") + XCTAssert(ruleWithoutInitialOption.evaluate("PASSWORD") == false, "Required character rule without initial option not ignored") let ruleWithRequiredCharacters = NJORequiredCharacterRule(characterSet: CharacterSet(charactersIn: "!@#ABCabc")) - XCTAssert(ruleWithRequiredCharacters.evaluateWithString(string: "!d@e#fAgBhCabc") == false, "All of the required characters are used but not passed") - XCTAssert(ruleWithRequiredCharacters.evaluateWithString(string: "!d@e#fAg") == false, "Some of the required characters are used but not passed") - XCTAssert(ruleWithRequiredCharacters.evaluateWithString(string: "deg$%^") == true, "Required characters are not used but passed") + XCTAssert(ruleWithRequiredCharacters.evaluate("!d@e#fAgBhCabc") == false, "All of the required characters are used but not passed") + XCTAssert(ruleWithRequiredCharacters.evaluate("!d@e#fAg") == false, "Some of the required characters are used but not passed") + XCTAssert(ruleWithRequiredCharacters.evaluate("deg$%^") == true, "Required characters are not used but passed") let ruleWithInitialLowercasePreset = NJORequiredCharacterRule(preset: .lowercaseCharacter) - XCTAssert(ruleWithInitialLowercasePreset.evaluateWithString(string: "Abcdef12345!@#") == false, "Some of the characters are lowercase but not passed") - XCTAssert(ruleWithInitialLowercasePreset.evaluateWithString(string: "ABCDEF12345!@#") == true, "All of them are uppercase but passed") + XCTAssert(ruleWithInitialLowercasePreset.evaluate("Abcdef12345!@#") == false, "Some of the characters are lowercase but not passed") + XCTAssert(ruleWithInitialLowercasePreset.evaluate("ABCDEF12345!@#") == true, "All of them are uppercase but passed") let ruleWithInitialUppercasePreset = NJORequiredCharacterRule(preset: .uppercaseCharacter) - XCTAssert(ruleWithInitialUppercasePreset.evaluateWithString(string: "AbcDef12345!@#") == false, "Some of the characters are uppercase but not passed") - XCTAssert(ruleWithInitialUppercasePreset.evaluateWithString(string: "abcdef12345!@#") == true, "All of them are lowercase but passed") + XCTAssert(ruleWithInitialUppercasePreset.evaluate("AbcDef12345!@#") == false, "Some of the characters are uppercase but not passed") + XCTAssert(ruleWithInitialUppercasePreset.evaluate("abcdef12345!@#") == true, "All of them are lowercase but passed") let ruleWithInitialDecimalDigitPreset = NJORequiredCharacterRule(preset: .decimalDigitCharacter) - XCTAssert(ruleWithInitialDecimalDigitPreset.evaluateWithString(string: "Abcdef12345!@#") == false, "Some of the characters are decimal digits but not passed") - XCTAssert(ruleWithInitialDecimalDigitPreset.evaluateWithString(string: "abcdef!@#") == true, "No decimal digits but passed") + XCTAssert(ruleWithInitialDecimalDigitPreset.evaluate("Abcdef12345!@#") == false, "Some of the characters are decimal digits but not passed") + XCTAssert(ruleWithInitialDecimalDigitPreset.evaluate("abcdef!@#") == true, "No decimal digits but passed") let ruleWithInitialSymbolPreset = NJORequiredCharacterRule(preset: .symbolCharacter) - XCTAssert(ruleWithInitialSymbolPreset.evaluateWithString(string: "Abcdef12345!") == false, "Some of the characters are symbols but not passed") - XCTAssert(ruleWithInitialSymbolPreset.evaluateWithString(string: "abcdef") == true, "No symbols but passed") + XCTAssert(ruleWithInitialSymbolPreset.evaluate("Abcdef12345!") == false, "Some of the characters are symbols but not passed") + XCTAssert(ruleWithInitialSymbolPreset.evaluate("abcdef") == true, "No symbols but passed") } func testLengthRule() { let ruleWithoutInitialRange = NJOLengthRule() - XCTAssert(ruleWithoutInitialRange.evaluateWithString(string: "PASSWORD") == false, "Length rule without initial range but not ignored") + XCTAssert(ruleWithoutInitialRange.evaluate("PASSWORD") == false, "Length rule without initial range but not ignored") let ruleWithInitialRange = NJOLengthRule(min: 6, max: 9) - XCTAssert(ruleWithInitialRange.evaluateWithString(string: "12345") == true, "Password is too short but passed") - XCTAssert(ruleWithInitialRange.evaluateWithString(string: "123456") == false, "Password is in range but not passed") - XCTAssert(ruleWithInitialRange.evaluateWithString(string: "123456789") == false, "Password is in range but not passed") - XCTAssert(ruleWithInitialRange.evaluateWithString(string: "1234567890") == true, "Password is too long but passed") + XCTAssert(ruleWithInitialRange.evaluate("12345") == true, "Password is too short but passed") + XCTAssert(ruleWithInitialRange.evaluate("123456") == false, "Password is in range but not passed") + XCTAssert(ruleWithInitialRange.evaluate("123456789") == false, "Password is in range but not passed") + XCTAssert(ruleWithInitialRange.evaluate("1234567890") == true, "Password is too long but passed") } func testPredicateRule() { let ruleWithoutInitialPredicate = NJOPredicateRule() - XCTAssert(ruleWithoutInitialPredicate.evaluateWithString(string: "PASSWORD") == false, "Length rule without initial predicate but not ignored") + XCTAssert(ruleWithoutInitialPredicate.evaluate("PASSWORD") == false, "Length rule without initial predicate but not ignored") let ruleWithInitialPredicate = NJOPredicateRule(predicate: NSPredicate(format: "SELF BEGINSWITH %@", "PASSWORD")) - XCTAssert(ruleWithInitialPredicate.evaluateWithString(string: "PASSWORDINPUT!") == true, "Password begins with PASSWORD but passed") - XCTAssert(ruleWithInitialPredicate.evaluateWithString(string: "PASS") == false, "Password doesn't begin with PASSWORD but not passed") + XCTAssert(ruleWithInitialPredicate.evaluate("PASSWORDINPUT!") == true, "Password begins with PASSWORD but passed") + XCTAssert(ruleWithInitialPredicate.evaluate("PASS") == false, "Password doesn't begin with PASSWORD but not passed") } func testRegexRule() { let ruleWithoutInitialRegex = NJORegularExpressionRule() - XCTAssert(ruleWithoutInitialRegex.evaluateWithString(string: "PASSWORD") == false, "Length rule without initial regex but not ignored") + XCTAssert(ruleWithoutInitialRegex.evaluate("PASSWORD") == false, "Length rule without initial regex but not ignored") // Email Regex let ruleWithInitialRegex = NJORegularExpressionRule(regularExpression: try! NSRegularExpression(pattern: "[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,6}", options: [])) - XCTAssert(ruleWithInitialRegex.evaluateWithString(string: "contact@jasonnam.com") == true, "Password is an email but passed") - XCTAssert(ruleWithInitialRegex.evaluateWithString(string: "PASSWORD") == false, "Password is not an email but not passed") + XCTAssert(ruleWithInitialRegex.evaluate("contact@jasonnam.com") == true, "Password is an email but passed") + XCTAssert(ruleWithInitialRegex.evaluate("PASSWORD") == false, "Password is not an email but not passed") } func testBlockRule() { let ruleWithoutInitialBlock = NJOBlockRule() - XCTAssert(ruleWithoutInitialBlock.evaluateWithString(string: "PASSWORD") == false, "Length rule without initial block but not ignored") + XCTAssert(ruleWithoutInitialBlock.evaluate("PASSWORD") == false, "Length rule without initial block but not ignored") let ruleWithInitialBlock = NJOBlockRule() { (password: String) in @@ -138,7 +138,7 @@ class NavajoTests: XCTestCase { return emailRegex.numberOfMatches(in: password, options: [], range: NSMakeRange(0, password.characters.count)) > 0 } - XCTAssert(ruleWithInitialBlock.evaluateWithString(string: "contact@jasonnam.com") == true, "Password is an email but passed") - XCTAssert(ruleWithInitialBlock.evaluateWithString(string: "PASSWORD") == false, "Password is not an email but not passed") + XCTAssert(ruleWithInitialBlock.evaluate("contact@jasonnam.com") == true, "Password is an email but passed") + XCTAssert(ruleWithInitialBlock.evaluate("PASSWORD") == false, "Password is not an email but not passed") } }