88
99import UIKit
1010import XCTest
11+ import Validator
1112
1213class 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