Skip to content

Instantly share code, notes, and snippets.

@nhatlee
Last active June 17, 2020 04:01
Show Gist options
  • Save nhatlee/727efc6d67522ad02cf99c3a1363c2d4 to your computer and use it in GitHub Desktop.
Save nhatlee/727efc6d67522ad02cf99c3a1363c2d4 to your computer and use it in GitHub Desktop.
import Foundation
import XCTest
func checkPhoneWithPatterns(_ phone: String) -> Bool{
let patterns = [
"^(840|84|0|)8[0-9]{8}$",
"(840|84|0|)9\\d{8}",
"^(840|84|0|)5(2|6|8|9)\\d{7}$",
"(840|84|0|)7(0|6|7|8|9)\\d{7}",
"(840|84|0|)3[2-9]\\d{7}"
]
guard patterns.count > 0 else {
return true
}
for regex in patterns {
let p = NSPredicate(format: "SELF MATCHES %@", regex)
if p.evaluate(with: phone) {
return true
}
continue
}
return false
}
class PhoneNumberTest: XCTestCase {
let suffixSizeSeven = 7
let suffixSizeSix = 6
func randomListNumber(with size: Int) -> [Int] {
(0..<size).map { _ in Int.random(in: 0...9) }
}
func checkPhones(prefixes: [String],
suffixSize: Int,
expectedResult: Bool) {
let firstNumbers = ["0", "84", "840"]
for first in firstNumbers {
let newPrefixs = prefixes.map { first + $0.dropFirst() }
for prefix in newPrefixs {
let suffix = randomListNumber(with: suffixSize).map { "\($0) "}.joined().replacingOccurrences(of: " ", with: "")
let phoneNumber = prefix + suffix
XCTAssert(checkPhoneWithPatterns(phoneNumber) == expectedResult, "Failed checked for phone: \(phoneNumber) expected result:\(expectedResult)")
}
}
}
func testValidViettelPhone() {
let prefixs = [
"086",
"096",
"097",
"098",
"032",
"033",
"034",
"035",
"036",
"037",
"038",
"039"
]
let expected = true
checkPhones(prefixes: prefixs,
suffixSize: suffixSizeSeven,
expectedResult: expected)
}
func testUnvalidViettelPhone() {
let prefixes = [
"0162",
"0163",
"0164",
"0165",
"0166",
"0167",
"0168",
"0169"
]
// Test 11 số
let expected = false
checkPhones(prefixes: prefixes,
suffixSize: suffixSizeSeven,
expectedResult: expected)
// Test với 10 số với list đầu số như trên
checkPhones(prefixes: prefixes,
suffixSize: suffixSizeSix,
expectedResult: expected)
}
func testValidVinaPhone() {
let prefixes = [
"088",
"091",
"094",
"083",
"084",
"085",
"081",
"082"
]
let expected = true
checkPhones(prefixes: prefixes,
suffixSize: suffixSizeSeven,
expectedResult: expected)
}
func testInvalidVinaPhone() {
let prefixes = [
"0123",
"0124",
"0125",
"0127",
"0129"
]
let expected = false
// Các đầu số trên với phone 11 số
checkPhones(prefixes: prefixes,
suffixSize: suffixSizeSeven,
expectedResult: expected)
// Các đầu số trên với phone 10 số
checkPhones(prefixes: prefixes,
suffixSize: suffixSizeSix,
expectedResult: expected)
}
func testValidMobiPhone() {
let prefixs = [
"089",
"090",
"093",
"070",
"079",
"077",
"076",
"078"
]
let expected = true
checkPhones(prefixes: prefixs,
suffixSize: suffixSizeSeven,
expectedResult: expected)
// 11 numbers
checkPhones(prefixes: prefixs,
suffixSize: 8,
expectedResult: false)
}
func testInvalidMobiPhone() {
let prefixs = [
"0120",
"0121",
"0122",
"0126",
"0128"
]
let expected = false
// 11 numbers
checkPhones(prefixes: prefixs,
suffixSize: suffixSizeSeven,
expectedResult: expected)
// 10 numbers
checkPhones(prefixes: prefixs,
suffixSize: suffixSizeSix,
expectedResult: expected)
}
func testValidVietNamMobile() {
let prefixs = [
"092",
"056",
"058"
]
// 11 numbers
checkPhones(prefixes: prefixs,
suffixSize: 8,
expectedResult: false)
// 10 numbers
checkPhones(prefixes: prefixs,
suffixSize: suffixSizeSeven,
expectedResult: true)
}
func testValidGMobilePhone() {
let prefixs = [
"099",
"059"
]
checkPhones(prefixes: prefixs,
suffixSize: suffixSizeSeven,
expectedResult: true)
// 11 numbers
checkPhones(prefixes: prefixs,
suffixSize: 8,
expectedResult: false)
}
func testInvalidGMobilePhone() {
let prefixs = [
"0199"
]
checkPhones(prefixes: prefixs,
suffixSize: suffixSizeSeven,
expectedResult: false)
// 11 numbers
checkPhones(prefixes: prefixs,
suffixSize: suffixSizeSix,
expectedResult: false)
}
}
PhoneNumberTest.defaultTestSuite.run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment