22// SwiftValidatorTests.swift
33// SwiftValidatorTests
44//
5- // Created by Rusty Zarse on 9/3/15 .
6- // Copyright (c) 2015 jpotts18. All rights reserved.
5+ // Created by Jeff Potter on 11/20/14 .
6+ // Copyright (c) 2014 jpotts18. All rights reserved.
77//
88
99import UIKit
1010import XCTest
11+ import Validator // example app
12+ import SwiftValidator // framework
1113
1214class SwiftValidatorTests : XCTestCase {
1315
16+ let USERNAME_REGEX = " ^[a-z0-9_-]{3,16}$ "
17+
18+ let VALID_ZIP = " 12345 "
19+ let INVALID_ZIP = " 1234 "
20+
21+ let VALID_EMAIL = " jiggy@gmail.com "
22+ let INVALID_EMAIL = " This is not a valid email "
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 VALID_FLOAT = " 1234.444 "
32+ let INVALID_FLOAT = " 1234.44.44 "
33+
34+ let LEN_3 = " hey "
35+ let LEN_5 = " Howdy "
36+ let LEN_20 = " Paint the cat orange "
37+
38+ let REGISTER_TXT_FIELD = UITextField ( )
39+ let REGISTER_VALIDATOR = Validator ( )
40+ let REGISTER_RULES = [ Rule] ( )
41+
42+ let UNREGISTER_TXT_FIELD = UITextField ( )
43+ let UNREGISTER_VALIDATOR = Validator ( )
44+ let UNREGISTER_RULES = [ Rule] ( )
45+
46+ let UNREGISTER_ERRORS_TXT_FIELD = UITextField ( )
47+ let UNREGISTER_ERRORS_VALIDATOR = Validator ( )
48+
49+ let ERROR_LABEL = UILabel ( )
50+
1451 override func setUp( ) {
1552 super. setUp ( )
1653 // Put setup code here. This method is called before the invocation of each test method in the class.
@@ -21,16 +58,217 @@ class SwiftValidatorTests: XCTestCase {
2158 super. tearDown ( )
2259 }
2360
24- func testExample( ) {
25- // This is an example of a functional test case.
26- XCTAssert ( true , " Pass " )
61+ // MARK: Required
62+
63+ func testRequired( ) {
64+ XCTAssertTrue ( RequiredRule ( ) . validate ( " Something " ) , " Required should be valid " )
2765 }
2866
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.
67+ func testRequiredInvalid( ) {
68+ XCTAssertFalse ( RequiredRule ( ) . validate ( " " ) , " Required should be invalid " )
69+ }
70+
71+ // MARK: Regex
72+
73+ func testRegex( ) {
74+ XCTAssertTrue ( RegexRule ( regex: USERNAME_REGEX) . validate ( " darth_vader8 " ) , " RegexRule should be valid " )
75+ }
76+
77+ func testRegexInvalid( ) {
78+ XCTAssertFalse ( RegexRule ( regex: USERNAME_REGEX) . validate ( " DarthVader " ) , " RegexRule should be invalid " )
79+ }
80+
81+ // MARK: Zipcode
82+
83+ func testZipCode( ) {
84+ XCTAssertTrue ( ZipCodeRule ( ) . validate ( VALID_ZIP) , " Zipcode should be valid " )
85+ }
86+
87+ func testZipCodeInvalid( ) {
88+ XCTAssertFalse ( ZipCodeRule ( ) . validate ( INVALID_ZIP) , " Zipcode should be invalid " )
89+ }
90+
91+ // MARK: Email
92+
93+ func testEmail( ) {
94+ XCTAssertTrue ( EmailRule ( ) . validate ( VALID_EMAIL) , " Email should be valid " )
95+ }
96+
97+ func testEmailInvalid( ) {
98+ XCTAssertFalse ( EmailRule ( ) . validate ( INVALID_EMAIL) , " Email should be invalid " )
99+ }
100+
101+ // MARK: Float
102+
103+ func testFloat( ) {
104+ XCTAssert ( FloatRule ( ) . validate ( VALID_FLOAT) , " Float should be valid " )
105+ }
106+
107+ func testFloatInvalid( ) {
108+ XCTAssert ( !FloatRule( ) . validate ( INVALID_FLOAT) , " Float should be invalid " )
109+ XCTAssert ( !FloatRule( ) . validate ( VALID_EMAIL) , " Float should be invalid " )
110+ }
111+
112+ // MARK: Confirm against field
113+
114+ func testConfirmSame( ) {
115+ CONFIRM_TXT_FIELD . text = CONFIRM_TEXT
116+ XCTAssertTrue ( ConfirmationRule ( confirmField: CONFIRM_TXT_FIELD) . validate ( CONFIRM_TEXT) , " Should confirm successfully " )
117+ }
118+
119+ func testConfirmDifferent( ) {
120+ CONFIRM_TXT_FIELD . text = CONFIRM_TEXT
121+ XCTAssertFalse ( ConfirmationRule ( confirmField: CONFIRM_TXT_FIELD) . validate ( CONFIRM_TEXT_DIFF) , " Should fail confirm " )
122+ }
123+
124+ // MARK: Password
125+
126+ func testPassword( ) {
127+ XCTAssertTrue ( PasswordRule ( ) . validate ( VALID_PASSWORD) , " Password should be valid " )
128+ }
129+
130+ func testPasswordInvalid( ) {
131+ XCTAssertFalse ( EmailRule ( ) . validate ( INVALID_PASSWORD) , " Password is invalid " )
132+ }
133+
134+ // MARK: Max Length
135+
136+ func testMaxLength( ) {
137+ XCTAssertTrue ( MaxLengthRule ( ) . validate ( LEN_3) , " Max Length should be valid " )
138+ }
139+
140+ func testMaxLengthInvalid( ) {
141+ XCTAssertFalse ( MaxLengthRule ( ) . validate ( LEN_20) , " Max Length should be invalid " )
142+ }
143+
144+ func testMaxLengthParameterAndGreaterThan( ) {
145+ XCTAssertTrue ( MaxLengthRule ( length: 20 ) . validate ( LEN_20) , " Max Length should be 20 and <= length " )
146+ }
147+
148+ // MARK: Min Length
149+ func testMinLength( ) {
150+ XCTAssertTrue ( MinLengthRule ( ) . validate ( LEN_3) , " Min Length should be valid " )
151+ }
152+
153+ func testMinLengthInvalid( ) {
154+ XCTAssertFalse ( MinLengthRule ( ) . validate ( " no " ) , " Min Length should be Invalid " )
155+ }
156+
157+ func testMinLengthWithParameter( ) {
158+ XCTAssertTrue ( MinLengthRule ( length: 5 ) . validate ( LEN_5) , " Min Len should be set to 5 and >= length " )
159+ }
160+
161+ // MARK: Full Name
162+
163+ func testFullName( ) {
164+ XCTAssertTrue ( FullNameRule ( ) . validate ( " Jeff Potter " ) , " Full Name should be valid " )
165+ }
166+
167+ func testFullNameWith3Names( ) {
168+ XCTAssertTrue ( FullNameRule ( ) . validate ( " Jeff Van Buren " ) , " Full Name should be valid " )
169+ }
170+
171+ func testFullNameInvalid( ) {
172+ XCTAssertFalse ( FullNameRule ( ) . validate ( " Carl " ) , " Full Name should be invalid " )
173+ }
174+
175+ // MARK: Register Field
176+
177+ func testRegisterField( ) {
178+ REGISTER_VALIDATOR . registerField ( REGISTER_TXT_FIELD, rules: REGISTER_RULES)
179+ XCTAssert ( REGISTER_VALIDATOR . validations [ REGISTER_TXT_FIELD] != nil , " Textfield should register " )
180+ }
181+
182+ func testUnregisterField( ) {
183+ UNREGISTER_VALIDATOR . registerField ( UNREGISTER_TXT_FIELD, rules: UNREGISTER_RULES)
184+ UNREGISTER_VALIDATOR . unregisterField ( UNREGISTER_TXT_FIELD)
185+ XCTAssert ( UNREGISTER_VALIDATOR . validations [ UNREGISTER_TXT_FIELD] == nil , " Textfield should unregister " )
186+ }
187+
188+ func testUnregisterError( ) {
189+ UNREGISTER_ERRORS_VALIDATOR . registerField ( UNREGISTER_ERRORS_TXT_FIELD, rules: [ EmailRule ( ) ] )
190+ UNREGISTER_ERRORS_TXT_FIELD . text = INVALID_EMAIL
191+ UNREGISTER_ERRORS_VALIDATOR . validate { ( errors) -> Void in
192+ XCTAssert ( errors. count == 1 , " Should come back with errors " )
193+ }
194+ UNREGISTER_ERRORS_VALIDATOR . unregisterField ( UNREGISTER_ERRORS_TXT_FIELD)
195+ UNREGISTER_ERRORS_VALIDATOR . validate { ( errors) -> Void in
196+ XCTAssert ( errors. count == 0 , " Should not come back with errors " )
197+ }
198+ }
199+
200+ // MARK: Validate Functions
201+
202+ func testValidateWithCallback( ) {
203+ REGISTER_VALIDATOR . registerField ( REGISTER_TXT_FIELD, rules: [ EmailRule ( ) ] )
204+ REGISTER_TXT_FIELD . text = VALID_EMAIL
205+ REGISTER_VALIDATOR . validate { ( errors) -> Void in
206+ XCTAssert ( errors. count == 0 , " Should not come back with errors " )
207+ }
208+ REGISTER_TXT_FIELD . text = INVALID_EMAIL
209+ REGISTER_VALIDATOR . validate { ( errors) -> Void in
210+ XCTAssert ( errors. count == 1 , " Should come back with 1 error " )
211+ }
212+ }
213+
214+ // MARK: Validate error field gets it's text set to the error, if supplied
215+
216+ func testNoErrorMessageSet( ) {
217+ REGISTER_VALIDATOR . registerField ( REGISTER_TXT_FIELD, errorLabel: ERROR_LABEL, rules: [ EmailRule ( ) ] )
218+ REGISTER_TXT_FIELD . text = VALID_EMAIL
219+ REGISTER_VALIDATOR . validate { ( errors) -> Void in
220+ XCTAssert ( errors. count == 0 , " Should not come back with errors " )
221+ }
222+ }
223+
224+ func testErrorMessageSet( ) {
225+ REGISTER_VALIDATOR . registerField ( REGISTER_TXT_FIELD, errorLabel: ERROR_LABEL, rules: [ EmailRule ( ) ] )
226+ var successCount = 0
227+ var errorCount = 0
228+ REGISTER_VALIDATOR . styleTransformers ( success: { ( validationRule) -> Void in
229+ successCount++
230+ } ) { ( validationError) -> Void in
231+ errorCount++
232+ }
233+ REGISTER_TXT_FIELD . text = INVALID_EMAIL
234+ REGISTER_VALIDATOR . validate { ( errors) -> Void in
235+ XCTAssert ( errors. count == 1 , " Should come back with errors " )
236+ XCTAssert ( errorCount == 1 , " Should have called the error style transform once " )
237+ XCTAssert ( successCount == 0 , " Should not have called the success style transform as there are no successful fields " )
33238 }
34239 }
35240
241+ func testErrorMessageSetAndThenUnset( ) {
242+ REGISTER_VALIDATOR . registerField ( REGISTER_TXT_FIELD, errorLabel: ERROR_LABEL, rules: [ EmailRule ( ) ] )
243+
244+ var successCount = 0
245+ var errorCount = 0
246+ REGISTER_VALIDATOR . styleTransformers ( success: { ( validationRule) -> Void in
247+ successCount++
248+ } ) { ( validationError) -> Void in
249+ errorCount++
250+ }
251+
252+ REGISTER_TXT_FIELD . text = INVALID_EMAIL
253+ REGISTER_VALIDATOR . validate { ( errors) -> Void in
254+ XCTAssert ( errors. count == 1 , " Should come back with errors " )
255+ XCTAssert ( errorCount == 1 , " Should have called the error style transform once " )
256+ XCTAssert ( successCount == 0 , " Should not have called the success style transform as there are no successful fields " )
257+ self . REGISTER_TXT_FIELD. text = self . VALID_EMAIL
258+ self . REGISTER_VALIDATOR. validate { ( errors) -> Void in
259+ XCTAssert ( errors. count == 0 , " Should not come back with errors: \( errors) " )
260+ XCTAssert ( successCount == 1 , " Should have called the success style transform once " )
261+ XCTAssert ( errorCount == 1 , " Should not have called the error style transform again " )
262+ }
263+ }
264+ }
265+
266+ func testTextFieldBorderColorNotSet( ) {
267+ REGISTER_VALIDATOR . registerField ( REGISTER_TXT_FIELD, errorLabel: ERROR_LABEL, rules: [ EmailRule ( ) ] )
268+ REGISTER_TXT_FIELD . text = INVALID_EMAIL
269+ REGISTER_VALIDATOR . validate { ( errors) -> Void in
270+ XCTAssert ( errors. count == 1 , " Should come back with errors " )
271+ XCTAssert ( !CGColorEqualToColor( self . REGISTER_TXT_FIELD. layer. borderColor, UIColor . redColor ( ) . CGColor) , " Color shouldn't get set at all " )
272+ }
273+ }
36274}
0 commit comments