Created
August 2, 2015 06:35
-
-
Save GE-N/cccc79a478b28f7954f5 to your computer and use it in GitHub Desktop.
phone number validate with regexp - BDD on swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Phone class part | |
// Phone.swift | |
import Foundation | |
class Phone { | |
class func validateNumber(number: String) -> Bool { | |
let pattern = "^(08|09)[0-9]{8}$" | |
let regexp = NSRegularExpression(pattern: pattern, options: .CaseInsensitive, error: nil) | |
let range = NSRange(location: 0, length: count(number)) | |
let matchCount = regexp?.numberOfMatchesInString(number, options: nil, range: range) | |
return matchCount > 0 | |
} | |
} | |
// ==================================================================================== | |
// Test Phone part | |
// PhoneNumberSpec.swift | |
import Quick | |
import Nimble | |
import SMSME | |
class PhoneNumberSpec : QuickSpec { | |
override func spec() { | |
describe("Phone number") { | |
context("Validate") { | |
it("should length equal 10 digit") { | |
expect(Phone.validateNumber("12345")).to(beFalse()) | |
expect(Phone.validateNumber("1234567890")).to(beFalse()) | |
expect(Phone.validateNumber("0811122222")).to(beTrue()) | |
} | |
it("should begins with 08/09") { | |
expect(Phone.validateNumber("998876")).to(beFalse()) | |
expect(Phone.validateNumber("081122")).to(beFalse()) | |
expect(Phone.validateNumber("0811122222")).to(beTrue()) | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment