Skip to content

Commit 3e88189

Browse files
committed
Small chnages to Scheme and moving tests
1 parent 5e32b89 commit 3e88189

File tree

6 files changed

+281
-294
lines changed

6 files changed

+281
-294
lines changed

SwiftValidator/Info.plist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<key>CFBundleExecutable</key>
88
<string>$(EXECUTABLE_NAME)</string>
99
<key>CFBundleIdentifier</key>
10-
<string>me.jeffpotter.$(PRODUCT_NAME:rfc1034identifier)</string>
10+
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
1111
<key>CFBundleInfoDictionaryVersion</key>
1212
<string>6.0</string>
1313
<key>CFBundleName</key>

SwiftValidatorTests/Info.plist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<key>CFBundleExecutable</key>
88
<string>$(EXECUTABLE_NAME)</string>
99
<key>CFBundleIdentifier</key>
10-
<string>com.levous.$(PRODUCT_NAME:rfc1034identifier)</string>
10+
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
1111
<key>CFBundleInfoDictionaryVersion</key>
1212
<string>6.0</string>
1313
<key>CFBundleName</key>

SwiftValidatorTests/SwiftValidatorTests.swift

Lines changed: 247 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,52 @@
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

99
import UIKit
1010
import XCTest
11+
import Validator // example app
12+
import SwiftValidator // framework
1113

1214
class 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
}

Validator.xcodeproj/project.pbxproj

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
62D1AE221A1E6D4400E4DFF8 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 62D1AE201A1E6D4400E4DFF8 /* Main.storyboard */; };
1212
62D1AE241A1E6D4400E4DFF8 /* Images.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 62D1AE231A1E6D4400E4DFF8 /* Images.xcassets */; };
1313
62D1AE271A1E6D4400E4DFF8 /* LaunchScreen.xib in Resources */ = {isa = PBXBuildFile; fileRef = 62D1AE251A1E6D4400E4DFF8 /* LaunchScreen.xib */; };
14-
62D1AE331A1E6D4500E4DFF8 /* ValidatorTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 62D1AE321A1E6D4500E4DFF8 /* ValidatorTests.swift */; };
1514
FB465CB81B9884F400398388 /* SwiftValidator.h in Headers */ = {isa = PBXBuildFile; fileRef = FB465CB71B9884F400398388 /* SwiftValidator.h */; settings = {ATTRIBUTES = (Public, ); }; };
1615
FB465CBE1B9884F400398388 /* SwiftValidator.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = FB465CB31B9884F400398388 /* SwiftValidator.framework */; };
1716
FB465CC71B9884F400398388 /* SwiftValidatorTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = FB465CC61B9884F400398388 /* SwiftValidatorTests.swift */; };
@@ -90,7 +89,6 @@
9089
62D1AE261A1E6D4400E4DFF8 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.xib; name = Base; path = Base.lproj/LaunchScreen.xib; sourceTree = "<group>"; };
9190
62D1AE2C1A1E6D4500E4DFF8 /* ValidatorTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = ValidatorTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
9291
62D1AE311A1E6D4500E4DFF8 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
93-
62D1AE321A1E6D4500E4DFF8 /* ValidatorTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ValidatorTests.swift; sourceTree = "<group>"; };
9492
FB465CB31B9884F400398388 /* SwiftValidator.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = SwiftValidator.framework; sourceTree = BUILT_PRODUCTS_DIR; };
9593
FB465CB61B9884F400398388 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
9694
FB465CB71B9884F400398388 /* SwiftValidator.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = SwiftValidator.h; sourceTree = "<group>"; };
@@ -194,7 +192,6 @@
194192
62D1AE2F1A1E6D4500E4DFF8 /* ValidatorTests */ = {
195193
isa = PBXGroup;
196194
children = (
197-
62D1AE321A1E6D4500E4DFF8 /* ValidatorTests.swift */,
198195
62D1AE301A1E6D4500E4DFF8 /* Supporting Files */,
199196
);
200197
path = ValidatorTests;
@@ -368,6 +365,7 @@
368365
isa = PBXProject;
369366
attributes = {
370367
LastSwiftMigration = 0700;
368+
LastSwiftUpdateCheck = 0700;
371369
LastUpgradeCheck = 0700;
372370
ORGANIZATIONNAME = jpotts18;
373371
TargetAttributes = {
@@ -456,7 +454,6 @@
456454
isa = PBXSourcesBuildPhase;
457455
buildActionMask = 2147483647;
458456
files = (
459-
62D1AE331A1E6D4500E4DFF8 /* ValidatorTests.swift in Sources */,
460457
);
461458
runOnlyForDeploymentPostprocessing = 0;
462459
};
@@ -681,6 +678,7 @@
681678
INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks";
682679
IPHONEOS_DEPLOYMENT_TARGET = 8.1;
683680
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
681+
PRODUCT_BUNDLE_IDENTIFIER = "me.jeffpotter.$(PRODUCT_NAME:rfc1034identifier)";
684682
PRODUCT_NAME = "$(TARGET_NAME)";
685683
SKIP_INSTALL = YES;
686684
TARGETED_DEVICE_FAMILY = "1,2";
@@ -704,6 +702,7 @@
704702
INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks";
705703
IPHONEOS_DEPLOYMENT_TARGET = 8.1;
706704
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
705+
PRODUCT_BUNDLE_IDENTIFIER = "me.jeffpotter.$(PRODUCT_NAME:rfc1034identifier)";
707706
PRODUCT_NAME = "$(TARGET_NAME)";
708707
SKIP_INSTALL = YES;
709708
TARGETED_DEVICE_FAMILY = "1,2";
@@ -728,6 +727,7 @@
728727
INFOPLIST_FILE = SwiftValidatorTests/Info.plist;
729728
IPHONEOS_DEPLOYMENT_TARGET = 8.4;
730729
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
730+
PRODUCT_BUNDLE_IDENTIFIER = "com.levous.$(PRODUCT_NAME:rfc1034identifier)";
731731
PRODUCT_NAME = "$(TARGET_NAME)";
732732
TEST_HOST = "$(BUILT_PRODUCTS_DIR)/Validator.app/Validator";
733733
};
@@ -746,6 +746,7 @@
746746
INFOPLIST_FILE = SwiftValidatorTests/Info.plist;
747747
IPHONEOS_DEPLOYMENT_TARGET = 8.4;
748748
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
749+
PRODUCT_BUNDLE_IDENTIFIER = "com.levous.$(PRODUCT_NAME:rfc1034identifier)";
749750
PRODUCT_NAME = "$(TARGET_NAME)";
750751
TEST_HOST = "$(BUILT_PRODUCTS_DIR)/Validator.app/Validator";
751752
};

0 commit comments

Comments
 (0)