Skip to content

Commit 0bc6e7a

Browse files
committed
adding tests, refactoring to make easier to understand
1 parent 8a6c7f9 commit 0bc6e7a

5 files changed

Lines changed: 136 additions & 17 deletions

File tree

Validator/ConfirmRule.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ import UIKit
1111

1212
public class ConfirmationRule: Rule {
1313

14-
let confirmField: UITextField
14+
private let confirmField: UITextField
1515

16-
init(confirmField: UITextField) {
16+
public init(confirmField: UITextField) {
1717
self.confirmField = confirmField
1818
}
1919

Validator/FullNameRule.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ public class FullNameRule : Rule {
1515
return "Please provide a first & last name"
1616
}
1717

18+
public init(){}
19+
1820
public func validate(value: String) -> Bool {
1921
var nameArray: [String] = split(value) { $0 == " " }
2022
return nameArray.count >= 2

Validator/MaxLengthRule.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,19 @@ import Foundation
99

1010
public class MaxLengthRule: Rule {
1111

12-
private var DEFAULT_MAX_LENGTH: Int = 16
12+
private var DEFAULT_LENGTH: Int = 16
1313

1414
public init(){}
1515

1616
public init(length: Int){
17-
self.DEFAULT_MAX_LENGTH = length
17+
self.DEFAULT_LENGTH = length
1818
}
1919

2020
public func validate(value: String) -> Bool {
21-
return countElements(value) <= DEFAULT_MAX_LENGTH
21+
return countElements(value) <= DEFAULT_LENGTH
2222
}
2323

2424
public func errorMessage() -> String {
25-
return "Must be at most \(DEFAULT_MAX_LENGTH) characters long"
25+
return "Must be at most \(DEFAULT_LENGTH) characters long"
2626
}
2727
}

Validator/MinLengthRule.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,19 @@ import Foundation
1010

1111
public class MinLengthRule: Rule {
1212

13-
private var DEFAULT_MIN_LENGTH: Int = 3
13+
private var DEFAULT_LENGTH: Int = 3
1414

1515
public init(){}
1616

1717
public init(length: Int){
18-
self.DEFAULT_MIN_LENGTH = length
18+
self.DEFAULT_LENGTH = length
1919
}
2020

2121
public func validate(value: String) -> Bool {
22-
return countElements(value) >= DEFAULT_MIN_LENGTH
22+
return countElements(value) >= DEFAULT_LENGTH
2323
}
2424

2525
public func errorMessage() -> String {
26-
return "Must be at least \(DEFAULT_MIN_LENGTH) characters long"
26+
return "Must be at least \(DEFAULT_LENGTH) characters long"
2727
}
2828
}

ValidatorTests/ValidatorTests.swift

Lines changed: 124 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,30 @@
88

99
import UIKit
1010
import XCTest
11+
import Validator
1112

1213
class ValidatorTests: XCTestCase {
1314

15+
let USERNAME_REGEX = "^[a-z0-9_-]{3,16}$"
16+
17+
let VALID_ZIP = "12345"
18+
let INVALID_ZIP = "1234"
19+
20+
let VALID_EMAIL = "jiggy@gmail.com"
21+
let INVALID_EMAIL = "This is not a valid email"
22+
23+
24+
let CONFIRM_TXT_FIELD = UITextField()
25+
let CONFIRM_TEXT = "Confirm this!"
26+
let CONFIRM_TEXT_DIFF = "I am not the same as the string above"
27+
28+
let VALID_PASSWORD = "Super$ecret"
29+
let INVALID_PASSWORD = "abc"
30+
31+
let LEN_3 = "hey"
32+
let LEN_5 = "Howdy"
33+
let LEN_20 = "Paint the cat orange"
34+
1435
override func setUp() {
1536
super.setUp()
1637
// 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 {
2142
super.tearDown()
2243
}
2344

45+
// MARK: Required
46+
47+
func testRequired() {
48+
XCTAssertTrue(RequiredRule().validate("Something"), "Required should be valid")
49+
}
50+
51+
func testRequiredInvalid() {
52+
XCTAssertFalse(RequiredRule().validate(""), "Required should be invalid")
53+
}
54+
55+
// MARK: Regex
56+
57+
func testRegex(){
58+
XCTAssertTrue(RegexRule(regex: USERNAME_REGEX).validate("darth_vader8"), "RegexRule should be valid")
59+
}
60+
61+
func testRegexInvalid(){
62+
XCTAssertFalse(RegexRule(regex: USERNAME_REGEX).validate("DarthVader"), "RegexRule should be invalid")
63+
}
64+
65+
// MARK: Zipcode
66+
67+
func testZipCode() {
68+
XCTAssertTrue(ZipCodeRule().validate(VALID_ZIP), "Zipcode should be valid")
69+
}
70+
71+
func testZipCodeInvalid() {
72+
XCTAssertFalse(ZipCodeRule().validate(INVALID_ZIP), "Zipcode should be invalid")
73+
}
74+
75+
// MARK: Email
76+
77+
func testEmail() {
78+
XCTAssertTrue(EmailRule().validate(VALID_EMAIL), "Email should be valid")
79+
}
80+
81+
func testEmailInvalid() {
82+
XCTAssertFalse(EmailRule().validate(INVALID_EMAIL), "Email should be invalid")
83+
}
84+
85+
// MARK: Confirm against field
86+
87+
func testConfirmSame(){
88+
CONFIRM_TXT_FIELD.text = CONFIRM_TEXT
89+
XCTAssertTrue(ConfirmationRule(confirmField: CONFIRM_TXT_FIELD).validate(CONFIRM_TEXT), "Should confirm successfully")
90+
}
91+
92+
func testConfirmDifferent() {
93+
CONFIRM_TXT_FIELD.text = CONFIRM_TEXT
94+
XCTAssertFalse(ConfirmationRule(confirmField: CONFIRM_TXT_FIELD).validate(CONFIRM_TEXT_DIFF), "Should fail confirm")
95+
}
96+
97+
// MARK: Password
98+
99+
func testPassword() {
100+
XCTAssertTrue(PasswordRule().validate(VALID_PASSWORD), "Password should be valid")
101+
}
102+
103+
func testPasswordInvalid(){
104+
XCTAssertFalse(EmailRule().validate(INVALID_PASSWORD), "Password is invalid")
105+
}
106+
107+
// MARK: Max Length
108+
109+
func testMaxLength(){
110+
XCTAssertTrue(MaxLengthRule().validate(LEN_3),"Max Length should be valid")
111+
}
112+
113+
func testMaxLengthInvalid(){
114+
XCTAssertFalse(MaxLengthRule().validate(LEN_20),"Max Length should be invalid")
115+
}
116+
117+
func testMaxLengthParameterAndGreaterThan(){
118+
XCTAssertTrue(MaxLengthRule(length: 20).validate(LEN_20), "Max Length should be 20 and <= length")
119+
}
120+
121+
// MARK: Min Length
122+
func testMinLength(){
123+
XCTAssertTrue(MinLengthRule().validate(LEN_3),"Min Length should be valid")
124+
}
125+
126+
func testMinLengthInvalid(){
127+
XCTAssertFalse(MinLengthRule().validate("no"),"Min Length should be Invalid")
128+
}
129+
130+
func testMinLengthWithParameter(){
131+
XCTAssertTrue(MinLengthRule(length: 5).validate(LEN_5), "Min Len should be set to 5 and >= length")
132+
}
133+
134+
// MARK: Full Name
135+
136+
func testFullName(){
137+
XCTAssertTrue(FullNameRule().validate("Jeff Potter"), "Full Name should be valid")
138+
}
139+
140+
func testFullNameWith3Names(){
141+
XCTAssertTrue(FullNameRule().validate("Jeff Van Buren"), "Full Name should be valid")
142+
}
143+
144+
func testFullNameInvalid(){
145+
XCTAssertFalse(FullNameRule().validate("Carl"), "Full Name should be invalid")
146+
}
147+
24148
func testExample() {
25149
// This is an example of a functional test case.
26150
XCTAssert(true, "Pass")
27151
}
28152

29-
func testPerformanceExample() {
30-
// This is an example of a performance test case.
31-
self.measureBlock() {
32-
// Put the code you want to measure the time of here.
33-
}
34-
}
35-
36153
}

0 commit comments

Comments
 (0)