Small chnages to Scheme and moving tests
This commit is contained in:
parent
5e32b891c4
commit
3e881899d4
|
|
@ -7,7 +7,7 @@
|
|||
<key>CFBundleExecutable</key>
|
||||
<string>$(EXECUTABLE_NAME)</string>
|
||||
<key>CFBundleIdentifier</key>
|
||||
<string>me.jeffpotter.$(PRODUCT_NAME:rfc1034identifier)</string>
|
||||
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
|
||||
<key>CFBundleInfoDictionaryVersion</key>
|
||||
<string>6.0</string>
|
||||
<key>CFBundleName</key>
|
||||
|
|
|
|||
|
|
@ -7,7 +7,7 @@
|
|||
<key>CFBundleExecutable</key>
|
||||
<string>$(EXECUTABLE_NAME)</string>
|
||||
<key>CFBundleIdentifier</key>
|
||||
<string>com.levous.$(PRODUCT_NAME:rfc1034identifier)</string>
|
||||
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
|
||||
<key>CFBundleInfoDictionaryVersion</key>
|
||||
<string>6.0</string>
|
||||
<key>CFBundleName</key>
|
||||
|
|
|
|||
|
|
@ -2,15 +2,52 @@
|
|||
// SwiftValidatorTests.swift
|
||||
// SwiftValidatorTests
|
||||
//
|
||||
// Created by Rusty Zarse on 9/3/15.
|
||||
// Copyright (c) 2015 jpotts18. All rights reserved.
|
||||
// Created by Jeff Potter on 11/20/14.
|
||||
// Copyright (c) 2014 jpotts18. All rights reserved.
|
||||
//
|
||||
|
||||
import UIKit
|
||||
import XCTest
|
||||
import Validator // example app
|
||||
import SwiftValidator // framework
|
||||
|
||||
class SwiftValidatorTests: 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 VALID_FLOAT = "1234.444"
|
||||
let INVALID_FLOAT = "1234.44.44"
|
||||
|
||||
let LEN_3 = "hey"
|
||||
let LEN_5 = "Howdy"
|
||||
let LEN_20 = "Paint the cat orange"
|
||||
|
||||
let REGISTER_TXT_FIELD = UITextField()
|
||||
let REGISTER_VALIDATOR = Validator()
|
||||
let REGISTER_RULES = [Rule]()
|
||||
|
||||
let UNREGISTER_TXT_FIELD = UITextField()
|
||||
let UNREGISTER_VALIDATOR = Validator()
|
||||
let UNREGISTER_RULES = [Rule]()
|
||||
|
||||
let UNREGISTER_ERRORS_TXT_FIELD = UITextField()
|
||||
let UNREGISTER_ERRORS_VALIDATOR = Validator()
|
||||
|
||||
let ERROR_LABEL = UILabel()
|
||||
|
||||
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 +58,217 @@ class SwiftValidatorTests: XCTestCase {
|
|||
super.tearDown()
|
||||
}
|
||||
|
||||
func testExample() {
|
||||
// This is an example of a functional test case.
|
||||
XCTAssert(true, "Pass")
|
||||
// MARK: Required
|
||||
|
||||
func testRequired() {
|
||||
XCTAssertTrue(RequiredRule().validate("Something"), "Required should be valid")
|
||||
}
|
||||
|
||||
func testPerformanceExample() {
|
||||
// This is an example of a performance test case.
|
||||
self.measureBlock() {
|
||||
// Put the code you want to measure the time of here.
|
||||
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: Float
|
||||
|
||||
func testFloat() {
|
||||
XCTAssert(FloatRule().validate(VALID_FLOAT), "Float should be valid")
|
||||
}
|
||||
|
||||
func testFloatInvalid() {
|
||||
XCTAssert(!FloatRule().validate(INVALID_FLOAT), "Float should be invalid")
|
||||
XCTAssert(!FloatRule().validate(VALID_EMAIL), "Float 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")
|
||||
}
|
||||
|
||||
// MARK: Register Field
|
||||
|
||||
func testRegisterField(){
|
||||
REGISTER_VALIDATOR.registerField(REGISTER_TXT_FIELD, rules: REGISTER_RULES)
|
||||
XCTAssert(REGISTER_VALIDATOR.validations[REGISTER_TXT_FIELD] != nil, "Textfield should register")
|
||||
}
|
||||
|
||||
func testUnregisterField(){
|
||||
UNREGISTER_VALIDATOR.registerField(UNREGISTER_TXT_FIELD, rules: UNREGISTER_RULES)
|
||||
UNREGISTER_VALIDATOR.unregisterField(UNREGISTER_TXT_FIELD)
|
||||
XCTAssert(UNREGISTER_VALIDATOR.validations[UNREGISTER_TXT_FIELD] == nil, "Textfield should unregister")
|
||||
}
|
||||
|
||||
func testUnregisterError(){
|
||||
UNREGISTER_ERRORS_VALIDATOR.registerField(UNREGISTER_ERRORS_TXT_FIELD, rules: [EmailRule()])
|
||||
UNREGISTER_ERRORS_TXT_FIELD.text = INVALID_EMAIL
|
||||
UNREGISTER_ERRORS_VALIDATOR.validate { (errors) -> Void in
|
||||
XCTAssert(errors.count == 1, "Should come back with errors")
|
||||
}
|
||||
UNREGISTER_ERRORS_VALIDATOR.unregisterField(UNREGISTER_ERRORS_TXT_FIELD)
|
||||
UNREGISTER_ERRORS_VALIDATOR.validate { (errors) -> Void in
|
||||
XCTAssert(errors.count == 0, "Should not come back with errors")
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: Validate Functions
|
||||
|
||||
func testValidateWithCallback() {
|
||||
REGISTER_VALIDATOR.registerField(REGISTER_TXT_FIELD, rules: [EmailRule()])
|
||||
REGISTER_TXT_FIELD.text = VALID_EMAIL
|
||||
REGISTER_VALIDATOR.validate { (errors) -> Void in
|
||||
XCTAssert(errors.count == 0, "Should not come back with errors")
|
||||
}
|
||||
REGISTER_TXT_FIELD.text = INVALID_EMAIL
|
||||
REGISTER_VALIDATOR.validate { (errors) -> Void in
|
||||
XCTAssert(errors.count == 1, "Should come back with 1 error")
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: Validate error field gets it's text set to the error, if supplied
|
||||
|
||||
func testNoErrorMessageSet() {
|
||||
REGISTER_VALIDATOR.registerField(REGISTER_TXT_FIELD, errorLabel: ERROR_LABEL, rules: [EmailRule()])
|
||||
REGISTER_TXT_FIELD.text = VALID_EMAIL
|
||||
REGISTER_VALIDATOR.validate { (errors) -> Void in
|
||||
XCTAssert(errors.count == 0, "Should not come back with errors")
|
||||
}
|
||||
}
|
||||
|
||||
func testErrorMessageSet() {
|
||||
REGISTER_VALIDATOR.registerField(REGISTER_TXT_FIELD, errorLabel: ERROR_LABEL, rules: [EmailRule()])
|
||||
var successCount = 0
|
||||
var errorCount = 0
|
||||
REGISTER_VALIDATOR.styleTransformers(success: { (validationRule) -> Void in
|
||||
successCount++
|
||||
}) { (validationError) -> Void in
|
||||
errorCount++
|
||||
}
|
||||
REGISTER_TXT_FIELD.text = INVALID_EMAIL
|
||||
REGISTER_VALIDATOR.validate { (errors) -> Void in
|
||||
XCTAssert(errors.count == 1, "Should come back with errors")
|
||||
XCTAssert(errorCount == 1, "Should have called the error style transform once")
|
||||
XCTAssert(successCount == 0, "Should not have called the success style transform as there are no successful fields")
|
||||
}
|
||||
}
|
||||
|
||||
func testErrorMessageSetAndThenUnset() {
|
||||
REGISTER_VALIDATOR.registerField(REGISTER_TXT_FIELD, errorLabel: ERROR_LABEL, rules: [EmailRule()])
|
||||
|
||||
var successCount = 0
|
||||
var errorCount = 0
|
||||
REGISTER_VALIDATOR.styleTransformers(success: { (validationRule) -> Void in
|
||||
successCount++
|
||||
}) { (validationError) -> Void in
|
||||
errorCount++
|
||||
}
|
||||
|
||||
REGISTER_TXT_FIELD.text = INVALID_EMAIL
|
||||
REGISTER_VALIDATOR.validate { (errors) -> Void in
|
||||
XCTAssert(errors.count == 1, "Should come back with errors")
|
||||
XCTAssert(errorCount == 1, "Should have called the error style transform once")
|
||||
XCTAssert(successCount == 0, "Should not have called the success style transform as there are no successful fields")
|
||||
self.REGISTER_TXT_FIELD.text = self.VALID_EMAIL
|
||||
self.REGISTER_VALIDATOR.validate { (errors) -> Void in
|
||||
XCTAssert(errors.count == 0, "Should not come back with errors: \(errors)")
|
||||
XCTAssert(successCount == 1, "Should have called the success style transform once")
|
||||
XCTAssert(errorCount == 1, "Should not have called the error style transform again")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func testTextFieldBorderColorNotSet() {
|
||||
REGISTER_VALIDATOR.registerField(REGISTER_TXT_FIELD, errorLabel: ERROR_LABEL, rules: [EmailRule()])
|
||||
REGISTER_TXT_FIELD.text = INVALID_EMAIL
|
||||
REGISTER_VALIDATOR.validate { (errors) -> Void in
|
||||
XCTAssert(errors.count == 1, "Should come back with errors")
|
||||
XCTAssert(!CGColorEqualToColor(self.REGISTER_TXT_FIELD.layer.borderColor, UIColor.redColor().CGColor), "Color shouldn't get set at all")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -11,7 +11,6 @@
|
|||
62D1AE221A1E6D4400E4DFF8 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 62D1AE201A1E6D4400E4DFF8 /* Main.storyboard */; };
|
||||
62D1AE241A1E6D4400E4DFF8 /* Images.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 62D1AE231A1E6D4400E4DFF8 /* Images.xcassets */; };
|
||||
62D1AE271A1E6D4400E4DFF8 /* LaunchScreen.xib in Resources */ = {isa = PBXBuildFile; fileRef = 62D1AE251A1E6D4400E4DFF8 /* LaunchScreen.xib */; };
|
||||
62D1AE331A1E6D4500E4DFF8 /* ValidatorTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 62D1AE321A1E6D4500E4DFF8 /* ValidatorTests.swift */; };
|
||||
FB465CB81B9884F400398388 /* SwiftValidator.h in Headers */ = {isa = PBXBuildFile; fileRef = FB465CB71B9884F400398388 /* SwiftValidator.h */; settings = {ATTRIBUTES = (Public, ); }; };
|
||||
FB465CBE1B9884F400398388 /* SwiftValidator.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = FB465CB31B9884F400398388 /* SwiftValidator.framework */; };
|
||||
FB465CC71B9884F400398388 /* SwiftValidatorTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = FB465CC61B9884F400398388 /* SwiftValidatorTests.swift */; };
|
||||
|
|
@ -90,7 +89,6 @@
|
|||
62D1AE261A1E6D4400E4DFF8 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.xib; name = Base; path = Base.lproj/LaunchScreen.xib; sourceTree = "<group>"; };
|
||||
62D1AE2C1A1E6D4500E4DFF8 /* ValidatorTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = ValidatorTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
|
||||
62D1AE311A1E6D4500E4DFF8 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
|
||||
62D1AE321A1E6D4500E4DFF8 /* ValidatorTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ValidatorTests.swift; sourceTree = "<group>"; };
|
||||
FB465CB31B9884F400398388 /* SwiftValidator.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = SwiftValidator.framework; sourceTree = BUILT_PRODUCTS_DIR; };
|
||||
FB465CB61B9884F400398388 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
|
||||
FB465CB71B9884F400398388 /* SwiftValidator.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = SwiftValidator.h; sourceTree = "<group>"; };
|
||||
|
|
@ -194,7 +192,6 @@
|
|||
62D1AE2F1A1E6D4500E4DFF8 /* ValidatorTests */ = {
|
||||
isa = PBXGroup;
|
||||
children = (
|
||||
62D1AE321A1E6D4500E4DFF8 /* ValidatorTests.swift */,
|
||||
62D1AE301A1E6D4500E4DFF8 /* Supporting Files */,
|
||||
);
|
||||
path = ValidatorTests;
|
||||
|
|
@ -368,6 +365,7 @@
|
|||
isa = PBXProject;
|
||||
attributes = {
|
||||
LastSwiftMigration = 0700;
|
||||
LastSwiftUpdateCheck = 0700;
|
||||
LastUpgradeCheck = 0700;
|
||||
ORGANIZATIONNAME = jpotts18;
|
||||
TargetAttributes = {
|
||||
|
|
@ -456,7 +454,6 @@
|
|||
isa = PBXSourcesBuildPhase;
|
||||
buildActionMask = 2147483647;
|
||||
files = (
|
||||
62D1AE331A1E6D4500E4DFF8 /* ValidatorTests.swift in Sources */,
|
||||
);
|
||||
runOnlyForDeploymentPostprocessing = 0;
|
||||
};
|
||||
|
|
@ -681,6 +678,7 @@
|
|||
INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks";
|
||||
IPHONEOS_DEPLOYMENT_TARGET = 8.1;
|
||||
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
|
||||
PRODUCT_BUNDLE_IDENTIFIER = "me.jeffpotter.$(PRODUCT_NAME:rfc1034identifier)";
|
||||
PRODUCT_NAME = "$(TARGET_NAME)";
|
||||
SKIP_INSTALL = YES;
|
||||
TARGETED_DEVICE_FAMILY = "1,2";
|
||||
|
|
@ -704,6 +702,7 @@
|
|||
INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks";
|
||||
IPHONEOS_DEPLOYMENT_TARGET = 8.1;
|
||||
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
|
||||
PRODUCT_BUNDLE_IDENTIFIER = "me.jeffpotter.$(PRODUCT_NAME:rfc1034identifier)";
|
||||
PRODUCT_NAME = "$(TARGET_NAME)";
|
||||
SKIP_INSTALL = YES;
|
||||
TARGETED_DEVICE_FAMILY = "1,2";
|
||||
|
|
@ -728,6 +727,7 @@
|
|||
INFOPLIST_FILE = SwiftValidatorTests/Info.plist;
|
||||
IPHONEOS_DEPLOYMENT_TARGET = 8.4;
|
||||
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
|
||||
PRODUCT_BUNDLE_IDENTIFIER = "com.levous.$(PRODUCT_NAME:rfc1034identifier)";
|
||||
PRODUCT_NAME = "$(TARGET_NAME)";
|
||||
TEST_HOST = "$(BUILT_PRODUCTS_DIR)/Validator.app/Validator";
|
||||
};
|
||||
|
|
@ -746,6 +746,7 @@
|
|||
INFOPLIST_FILE = SwiftValidatorTests/Info.plist;
|
||||
IPHONEOS_DEPLOYMENT_TARGET = 8.4;
|
||||
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
|
||||
PRODUCT_BUNDLE_IDENTIFIER = "com.levous.$(PRODUCT_NAME:rfc1034identifier)";
|
||||
PRODUCT_NAME = "$(TARGET_NAME)";
|
||||
TEST_HOST = "$(BUILT_PRODUCTS_DIR)/Validator.app/Validator";
|
||||
};
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<Scheme
|
||||
LastUpgradeVersion = "0640"
|
||||
LastUpgradeVersion = "0700"
|
||||
version = "1.3">
|
||||
<BuildAction
|
||||
parallelizeBuildables = "YES"
|
||||
|
|
@ -23,21 +23,43 @@
|
|||
</BuildActionEntries>
|
||||
</BuildAction>
|
||||
<TestAction
|
||||
buildConfiguration = "Debug"
|
||||
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
|
||||
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
|
||||
shouldUseLaunchSchemeArgsEnv = "YES"
|
||||
buildConfiguration = "Debug">
|
||||
shouldUseLaunchSchemeArgsEnv = "YES">
|
||||
<Testables>
|
||||
<TestableReference
|
||||
skipped = "NO">
|
||||
<BuildableReference
|
||||
BuildableIdentifier = "primary"
|
||||
BlueprintIdentifier = "FB465CBC1B9884F400398388"
|
||||
BuildableName = "SwiftValidatorTests.xctest"
|
||||
BlueprintName = "SwiftValidatorTests"
|
||||
ReferencedContainer = "container:Validator.xcodeproj">
|
||||
</BuildableReference>
|
||||
</TestableReference>
|
||||
</Testables>
|
||||
<MacroExpansion>
|
||||
<BuildableReference
|
||||
BuildableIdentifier = "primary"
|
||||
BlueprintIdentifier = "FB465CB21B9884F400398388"
|
||||
BuildableName = "SwiftValidator.framework"
|
||||
BlueprintName = "SwiftValidator"
|
||||
ReferencedContainer = "container:Validator.xcodeproj">
|
||||
</BuildableReference>
|
||||
</MacroExpansion>
|
||||
<AdditionalOptions>
|
||||
</AdditionalOptions>
|
||||
</TestAction>
|
||||
<LaunchAction
|
||||
buildConfiguration = "Debug"
|
||||
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
|
||||
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
|
||||
launchStyle = "0"
|
||||
useCustomWorkingDirectory = "NO"
|
||||
buildConfiguration = "Debug"
|
||||
ignoresPersistentStateOnLaunch = "NO"
|
||||
debugDocumentVersioning = "YES"
|
||||
debugServiceExtension = "internal"
|
||||
allowLocationSimulation = "YES">
|
||||
<MacroExpansion>
|
||||
<BuildableReference
|
||||
|
|
@ -52,10 +74,10 @@
|
|||
</AdditionalOptions>
|
||||
</LaunchAction>
|
||||
<ProfileAction
|
||||
buildConfiguration = "Release"
|
||||
shouldUseLaunchSchemeArgsEnv = "YES"
|
||||
savedToolIdentifier = ""
|
||||
useCustomWorkingDirectory = "NO"
|
||||
buildConfiguration = "Release"
|
||||
debugDocumentVersioning = "YES">
|
||||
<MacroExpansion>
|
||||
<BuildableReference
|
||||
|
|
|
|||
|
|
@ -1,274 +0,0 @@
|
|||
//
|
||||
// ValidatorTests.swift
|
||||
// ValidatorTests
|
||||
//
|
||||
// Created by Jeff Potter on 11/20/14.
|
||||
// Copyright (c) 2014 jpotts18. All rights reserved.
|
||||
//
|
||||
|
||||
import UIKit
|
||||
import XCTest
|
||||
import Validator // example app
|
||||
import SwiftValidator // framework
|
||||
|
||||
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 VALID_FLOAT = "1234.444"
|
||||
let INVALID_FLOAT = "1234.44.44"
|
||||
|
||||
let LEN_3 = "hey"
|
||||
let LEN_5 = "Howdy"
|
||||
let LEN_20 = "Paint the cat orange"
|
||||
|
||||
let REGISTER_TXT_FIELD = UITextField()
|
||||
let REGISTER_VALIDATOR = Validator()
|
||||
let REGISTER_RULES = [Rule]()
|
||||
|
||||
let UNREGISTER_TXT_FIELD = UITextField()
|
||||
let UNREGISTER_VALIDATOR = Validator()
|
||||
let UNREGISTER_RULES = [Rule]()
|
||||
|
||||
let UNREGISTER_ERRORS_TXT_FIELD = UITextField()
|
||||
let UNREGISTER_ERRORS_VALIDATOR = Validator()
|
||||
|
||||
let ERROR_LABEL = UILabel()
|
||||
|
||||
override func setUp() {
|
||||
super.setUp()
|
||||
// Put setup code here. This method is called before the invocation of each test method in the class.
|
||||
}
|
||||
|
||||
override func tearDown() {
|
||||
// Put teardown code here. This method is called after the invocation of each test method in the class.
|
||||
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: Float
|
||||
|
||||
func testFloat() {
|
||||
XCTAssert(FloatRule().validate(VALID_FLOAT), "Float should be valid")
|
||||
}
|
||||
|
||||
func testFloatInvalid() {
|
||||
XCTAssert(!FloatRule().validate(INVALID_FLOAT), "Float should be invalid")
|
||||
XCTAssert(!FloatRule().validate(VALID_EMAIL), "Float 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")
|
||||
}
|
||||
|
||||
// MARK: Register Field
|
||||
|
||||
func testRegisterField(){
|
||||
REGISTER_VALIDATOR.registerField(REGISTER_TXT_FIELD, rules: REGISTER_RULES)
|
||||
XCTAssert(REGISTER_VALIDATOR.validations[REGISTER_TXT_FIELD] != nil, "Textfield should register")
|
||||
}
|
||||
|
||||
func testUnregisterField(){
|
||||
UNREGISTER_VALIDATOR.registerField(UNREGISTER_TXT_FIELD, rules: UNREGISTER_RULES)
|
||||
UNREGISTER_VALIDATOR.unregisterField(UNREGISTER_TXT_FIELD)
|
||||
XCTAssert(UNREGISTER_VALIDATOR.validations[UNREGISTER_TXT_FIELD] == nil, "Textfield should unregister")
|
||||
}
|
||||
|
||||
func testUnregisterError(){
|
||||
UNREGISTER_ERRORS_VALIDATOR.registerField(UNREGISTER_ERRORS_TXT_FIELD, rules: [EmailRule()])
|
||||
UNREGISTER_ERRORS_TXT_FIELD.text = INVALID_EMAIL
|
||||
UNREGISTER_ERRORS_VALIDATOR.validate { (errors) -> Void in
|
||||
XCTAssert(errors.count == 1, "Should come back with errors")
|
||||
}
|
||||
UNREGISTER_ERRORS_VALIDATOR.unregisterField(UNREGISTER_ERRORS_TXT_FIELD)
|
||||
UNREGISTER_ERRORS_VALIDATOR.validate { (errors) -> Void in
|
||||
XCTAssert(errors.count == 0, "Should not come back with errors")
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: Validate Functions
|
||||
|
||||
func testValidateWithCallback() {
|
||||
REGISTER_VALIDATOR.registerField(REGISTER_TXT_FIELD, rules: [EmailRule()])
|
||||
REGISTER_TXT_FIELD.text = VALID_EMAIL
|
||||
REGISTER_VALIDATOR.validate { (errors) -> Void in
|
||||
XCTAssert(errors.count == 0, "Should not come back with errors")
|
||||
}
|
||||
REGISTER_TXT_FIELD.text = INVALID_EMAIL
|
||||
REGISTER_VALIDATOR.validate { (errors) -> Void in
|
||||
XCTAssert(errors.count == 1, "Should come back with 1 error")
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: Validate error field gets it's text set to the error, if supplied
|
||||
|
||||
func testNoErrorMessageSet() {
|
||||
REGISTER_VALIDATOR.registerField(REGISTER_TXT_FIELD, errorLabel: ERROR_LABEL, rules: [EmailRule()])
|
||||
REGISTER_TXT_FIELD.text = VALID_EMAIL
|
||||
REGISTER_VALIDATOR.validate { (errors) -> Void in
|
||||
XCTAssert(errors.count == 0, "Should not come back with errors")
|
||||
}
|
||||
}
|
||||
|
||||
func testErrorMessageSet() {
|
||||
REGISTER_VALIDATOR.registerField(REGISTER_TXT_FIELD, errorLabel: ERROR_LABEL, rules: [EmailRule()])
|
||||
var successCount = 0
|
||||
var errorCount = 0
|
||||
REGISTER_VALIDATOR.styleTransformers(success: { (validationRule) -> Void in
|
||||
successCount++
|
||||
}) { (validationError) -> Void in
|
||||
errorCount++
|
||||
}
|
||||
REGISTER_TXT_FIELD.text = INVALID_EMAIL
|
||||
REGISTER_VALIDATOR.validate { (errors) -> Void in
|
||||
XCTAssert(errors.count == 1, "Should come back with errors")
|
||||
XCTAssert(errorCount == 1, "Should have called the error style transform once")
|
||||
XCTAssert(successCount == 0, "Should not have called the success style transform as there are no successful fields")
|
||||
}
|
||||
}
|
||||
|
||||
func testErrorMessageSetAndThenUnset() {
|
||||
REGISTER_VALIDATOR.registerField(REGISTER_TXT_FIELD, errorLabel: ERROR_LABEL, rules: [EmailRule()])
|
||||
|
||||
var successCount = 0
|
||||
var errorCount = 0
|
||||
REGISTER_VALIDATOR.styleTransformers(success: { (validationRule) -> Void in
|
||||
successCount++
|
||||
}) { (validationError) -> Void in
|
||||
errorCount++
|
||||
}
|
||||
|
||||
REGISTER_TXT_FIELD.text = INVALID_EMAIL
|
||||
REGISTER_VALIDATOR.validate { (errors) -> Void in
|
||||
XCTAssert(errors.count == 1, "Should come back with errors")
|
||||
XCTAssert(errorCount == 1, "Should have called the error style transform once")
|
||||
XCTAssert(successCount == 0, "Should not have called the success style transform as there are no successful fields")
|
||||
self.REGISTER_TXT_FIELD.text = self.VALID_EMAIL
|
||||
self.REGISTER_VALIDATOR.validate { (errors) -> Void in
|
||||
XCTAssert(errors.count == 0, "Should not come back with errors: \(errors)")
|
||||
XCTAssert(successCount == 1, "Should have called the success style transform once")
|
||||
XCTAssert(errorCount == 1, "Should not have called the error style transform again")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func testTextFieldBorderColorNotSet() {
|
||||
REGISTER_VALIDATOR.registerField(REGISTER_TXT_FIELD, errorLabel: ERROR_LABEL, rules: [EmailRule()])
|
||||
REGISTER_TXT_FIELD.text = INVALID_EMAIL
|
||||
REGISTER_VALIDATOR.validate { (errors) -> Void in
|
||||
XCTAssert(errors.count == 1, "Should come back with errors")
|
||||
XCTAssert(!CGColorEqualToColor(self.REGISTER_TXT_FIELD.layer.borderColor, UIColor.redColor().CGColor), "Color shouldn't get set at all")
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue