From 847885a490c897b5a540a07d45bb25482aa2fc98 Mon Sep 17 00:00:00 2001 From: Cameron McCord Date: Tue, 5 May 2015 18:58:11 -0600 Subject: [PATCH] added float rule with tests --- Validator.xcodeproj/project.pbxproj | 4 ++++ Validator/FloatRule.swift | 29 +++++++++++++++++++++++++++++ ValidatorTests/ValidatorTests.swift | 14 ++++++++++++++ 3 files changed, 47 insertions(+) create mode 100644 Validator/FloatRule.swift diff --git a/Validator.xcodeproj/project.pbxproj b/Validator.xcodeproj/project.pbxproj index f0feb2f..4f69d77 100644 --- a/Validator.xcodeproj/project.pbxproj +++ b/Validator.xcodeproj/project.pbxproj @@ -26,6 +26,7 @@ 62DC8D6E1AAA42CE0095DFA7 /* PasswordRule.swift in Sources */ = {isa = PBXBuildFile; fileRef = 62DC8D6B1AAA42CE0095DFA7 /* PasswordRule.swift */; }; 62DC8D711AAA43110095DFA7 /* ZipCodeRule.swift in Sources */ = {isa = PBXBuildFile; fileRef = 62DC8D701AAA43110095DFA7 /* ZipCodeRule.swift */; }; 62E9E2AD1ACFB336000A939C /* RegexRule.swift in Sources */ = {isa = PBXBuildFile; fileRef = 62E9E2AC1ACFB336000A939C /* RegexRule.swift */; }; + DC5A35EC1AF99BA60013FE6B /* FloatRule.swift in Sources */ = {isa = PBXBuildFile; fileRef = DC5A35EB1AF99BA60013FE6B /* FloatRule.swift */; }; /* End PBXBuildFile section */ /* Begin PBXContainerItemProxy section */ @@ -62,6 +63,7 @@ 62DC8D6B1AAA42CE0095DFA7 /* PasswordRule.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PasswordRule.swift; sourceTree = ""; }; 62DC8D701AAA43110095DFA7 /* ZipCodeRule.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ZipCodeRule.swift; sourceTree = ""; }; 62E9E2AC1ACFB336000A939C /* RegexRule.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RegexRule.swift; sourceTree = ""; }; + DC5A35EB1AF99BA60013FE6B /* FloatRule.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FloatRule.swift; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -171,6 +173,7 @@ 62DC8D701AAA43110095DFA7 /* ZipCodeRule.swift */, 628637271AAA49E300BC8FCF /* ConfirmRule.swift */, 62E9E2AC1ACFB336000A939C /* RegexRule.swift */, + DC5A35EB1AF99BA60013FE6B /* FloatRule.swift */, ); name = Rules; sourceTree = ""; @@ -283,6 +286,7 @@ 628637281AAA49E300BC8FCF /* ConfirmRule.swift in Sources */, 62DC8D651AAA42520095DFA7 /* Rule.swift in Sources */, 62D1AE1F1A1E6D4400E4DFF8 /* ViewController.swift in Sources */, + DC5A35EC1AF99BA60013FE6B /* FloatRule.swift in Sources */, 62DC8D6D1AAA42CE0095DFA7 /* RequiredRule.swift in Sources */, 62D1AE1D1A1E6D4400E4DFF8 /* AppDelegate.swift in Sources */, 62D1AE581A1E700200E4DFF8 /* Validator.swift in Sources */, diff --git a/Validator/FloatRule.swift b/Validator/FloatRule.swift new file mode 100644 index 0000000..4bf1c28 --- /dev/null +++ b/Validator/FloatRule.swift @@ -0,0 +1,29 @@ +// +// FloatRule.swift +// Validator +// +// Created by Cameron McCord on 5/5/15. +// Copyright (c) 2015 jpotts18. All rights reserved. +// + +import Foundation + +public class FloatRule:Rule { + + public init(){ + + } + + public func validate(value: String) -> Bool { + let regex = NSRegularExpression(pattern: "[-+]?(\\d*[.])?\\d+", options: nil, error: nil) + if let regex = regex { + let match = regex.numberOfMatchesInString(value, options: nil, range: NSRange(location: 0, length: count(value))) + return match == 1 + } + return false + } + + public func errorMessage() -> String { + return "This must be a number with or without a decimal" + } +} \ No newline at end of file diff --git a/ValidatorTests/ValidatorTests.swift b/ValidatorTests/ValidatorTests.swift index acce060..4a1879d 100644 --- a/ValidatorTests/ValidatorTests.swift +++ b/ValidatorTests/ValidatorTests.swift @@ -27,6 +27,9 @@ class ValidatorTests: XCTestCase { 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" @@ -94,6 +97,17 @@ class ValidatorTests: XCTestCase { 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(){