adding tests, refactoring to make easier to understand
This commit is contained in:
parent
8a6c7f99cd
commit
0bc6e7a50a
|
|
@ -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
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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"
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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"
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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.
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue