From 0bc6e7a50aa57947e90ccab4a1df9aa4b5bcc571 Mon Sep 17 00:00:00 2001 From: Jeff Potter Date: Fri, 10 Apr 2015 13:50:28 -0600 Subject: [PATCH] adding tests, refactoring to make easier to understand --- Validator/ConfirmRule.swift | 4 +- Validator/FullNameRule.swift | 2 + Validator/MaxLengthRule.swift | 8 +- Validator/MinLengthRule.swift | 8 +- ValidatorTests/ValidatorTests.swift | 131 ++++++++++++++++++++++++++-- 5 files changed, 136 insertions(+), 17 deletions(-) diff --git a/Validator/ConfirmRule.swift b/Validator/ConfirmRule.swift index 94005df..46ff215 100644 --- a/Validator/ConfirmRule.swift +++ b/Validator/ConfirmRule.swift @@ -11,9 +11,9 @@ import UIKit public class ConfirmationRule: Rule { - let confirmField: UITextField + private let confirmField: UITextField - init(confirmField: UITextField) { + public init(confirmField: UITextField) { self.confirmField = confirmField } diff --git a/Validator/FullNameRule.swift b/Validator/FullNameRule.swift index acc26b3..a030214 100644 --- a/Validator/FullNameRule.swift +++ b/Validator/FullNameRule.swift @@ -15,6 +15,8 @@ public class FullNameRule : Rule { return "Please provide a first & last name" } + public init(){} + public func validate(value: String) -> Bool { var nameArray: [String] = split(value) { $0 == " " } return nameArray.count >= 2 diff --git a/Validator/MaxLengthRule.swift b/Validator/MaxLengthRule.swift index a659f3b..b9bcee5 100644 --- a/Validator/MaxLengthRule.swift +++ b/Validator/MaxLengthRule.swift @@ -9,19 +9,19 @@ import Foundation public class MaxLengthRule: Rule { - private var DEFAULT_MAX_LENGTH: Int = 16 + private var DEFAULT_LENGTH: Int = 16 public init(){} public init(length: Int){ - self.DEFAULT_MAX_LENGTH = length + self.DEFAULT_LENGTH = length } public func validate(value: String) -> Bool { - return countElements(value) <= DEFAULT_MAX_LENGTH + return countElements(value) <= DEFAULT_LENGTH } public func errorMessage() -> String { - return "Must be at most \(DEFAULT_MAX_LENGTH) characters long" + return "Must be at most \(DEFAULT_LENGTH) characters long" } } diff --git a/Validator/MinLengthRule.swift b/Validator/MinLengthRule.swift index afe1933..952e0ce 100644 --- a/Validator/MinLengthRule.swift +++ b/Validator/MinLengthRule.swift @@ -10,19 +10,19 @@ import Foundation public class MinLengthRule: Rule { - private var DEFAULT_MIN_LENGTH: Int = 3 + private var DEFAULT_LENGTH: Int = 3 public init(){} public init(length: Int){ - self.DEFAULT_MIN_LENGTH = length + self.DEFAULT_LENGTH = length } public func validate(value: String) -> Bool { - return countElements(value) >= DEFAULT_MIN_LENGTH + return countElements(value) >= DEFAULT_LENGTH } public func errorMessage() -> String { - return "Must be at least \(DEFAULT_MIN_LENGTH) characters long" + return "Must be at least \(DEFAULT_LENGTH) characters long" } } diff --git a/ValidatorTests/ValidatorTests.swift b/ValidatorTests/ValidatorTests.swift index 0fa29a9..21347a6 100644 --- a/ValidatorTests/ValidatorTests.swift +++ b/ValidatorTests/ValidatorTests.swift @@ -8,9 +8,30 @@ import UIKit import XCTest +import Validator class ValidatorTests: XCTestCase { + let USERNAME_REGEX = "^[a-z0-9_-]{3,16}$" + + let VALID_ZIP = "12345" + let INVALID_ZIP = "1234" + + let VALID_EMAIL = "jiggy@gmail.com" + let INVALID_EMAIL = "This is not a valid email" + + + let CONFIRM_TXT_FIELD = UITextField() + let CONFIRM_TEXT = "Confirm this!" + let CONFIRM_TEXT_DIFF = "I am not the same as the string above" + + let VALID_PASSWORD = "Super$ecret" + let INVALID_PASSWORD = "abc" + + let LEN_3 = "hey" + let LEN_5 = "Howdy" + let LEN_20 = "Paint the cat orange" + override func setUp() { super.setUp() // Put setup code here. This method is called before the invocation of each test method in the class. @@ -21,16 +42,112 @@ class ValidatorTests: XCTestCase { super.tearDown() } + // MARK: Required + + func testRequired() { + XCTAssertTrue(RequiredRule().validate("Something"), "Required should be valid") + } + + func testRequiredInvalid() { + XCTAssertFalse(RequiredRule().validate(""), "Required should be invalid") + } + + // MARK: Regex + + func testRegex(){ + XCTAssertTrue(RegexRule(regex: USERNAME_REGEX).validate("darth_vader8"), "RegexRule should be valid") + } + + func testRegexInvalid(){ + XCTAssertFalse(RegexRule(regex: USERNAME_REGEX).validate("DarthVader"), "RegexRule should be invalid") + } + + // MARK: Zipcode + + func testZipCode() { + XCTAssertTrue(ZipCodeRule().validate(VALID_ZIP), "Zipcode should be valid") + } + + func testZipCodeInvalid() { + XCTAssertFalse(ZipCodeRule().validate(INVALID_ZIP), "Zipcode should be invalid") + } + + // MARK: Email + + func testEmail() { + XCTAssertTrue(EmailRule().validate(VALID_EMAIL), "Email should be valid") + } + + func testEmailInvalid() { + XCTAssertFalse(EmailRule().validate(INVALID_EMAIL), "Email should be invalid") + } + + // MARK: Confirm against field + + func testConfirmSame(){ + CONFIRM_TXT_FIELD.text = CONFIRM_TEXT + XCTAssertTrue(ConfirmationRule(confirmField: CONFIRM_TXT_FIELD).validate(CONFIRM_TEXT), "Should confirm successfully") + } + + func testConfirmDifferent() { + CONFIRM_TXT_FIELD.text = CONFIRM_TEXT + XCTAssertFalse(ConfirmationRule(confirmField: CONFIRM_TXT_FIELD).validate(CONFIRM_TEXT_DIFF), "Should fail confirm") + } + + // MARK: Password + + func testPassword() { + XCTAssertTrue(PasswordRule().validate(VALID_PASSWORD), "Password should be valid") + } + + func testPasswordInvalid(){ + XCTAssertFalse(EmailRule().validate(INVALID_PASSWORD), "Password is invalid") + } + + // MARK: Max Length + + func testMaxLength(){ + XCTAssertTrue(MaxLengthRule().validate(LEN_3),"Max Length should be valid") + } + + func testMaxLengthInvalid(){ + XCTAssertFalse(MaxLengthRule().validate(LEN_20),"Max Length should be invalid") + } + + func testMaxLengthParameterAndGreaterThan(){ + XCTAssertTrue(MaxLengthRule(length: 20).validate(LEN_20), "Max Length should be 20 and <= length") + } + + // MARK: Min Length + func testMinLength(){ + XCTAssertTrue(MinLengthRule().validate(LEN_3),"Min Length should be valid") + } + + func testMinLengthInvalid(){ + XCTAssertFalse(MinLengthRule().validate("no"),"Min Length should be Invalid") + } + + func testMinLengthWithParameter(){ + XCTAssertTrue(MinLengthRule(length: 5).validate(LEN_5), "Min Len should be set to 5 and >= length") + } + + // MARK: Full Name + + func testFullName(){ + XCTAssertTrue(FullNameRule().validate("Jeff Potter"), "Full Name should be valid") + } + + func testFullNameWith3Names(){ + XCTAssertTrue(FullNameRule().validate("Jeff Van Buren"), "Full Name should be valid") + } + + func testFullNameInvalid(){ + XCTAssertFalse(FullNameRule().validate("Carl"), "Full Name should be invalid") + } + func testExample() { // This is an example of a functional test case. XCTAssert(true, "Pass") } - func testPerformanceExample() { - // This is an example of a performance test case. - self.measureBlock() { - // Put the code you want to measure the time of here. - } - } - }