Skip to content

Commit

Permalink
Merge pull request #85 from prolificinteractive/swift-3.0
Browse files Browse the repository at this point in the history
Swift 3.0 update
  • Loading branch information
DannyVancura authored Dec 8, 2016
2 parents 9d0243b + 2005f51 commit 66df98d
Show file tree
Hide file tree
Showing 47 changed files with 645 additions and 574 deletions.
1 change: 1 addition & 0 deletions .swift-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
3.0
11 changes: 8 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,17 @@
# * https://github.com/supermarin/xcpretty#usage

language: objective-c
osx_image: xcode7.3
osx_image: xcode8
# cache: cocoapods
# podfile: Example/Podfile
# before_install:
# - gem install cocoapods # Since Travis is not always on latest version
# - pod install --project-directory=Example
script:
- set -o pipefail && xcodebuild test -project Caishen.xcodeproj -scheme CaishenTests -sdk iphonesimulator ONLY_ACTIVE_ARCH=NO | xcpretty
- pod lib lint
- xcodebuild test -project Caishen.xcodeproj -scheme CaishenTests -destination 'platform=iOS Simulator,name=iPhone 6,OS=10.0'
- pod lib lint --quick

before_install:
- rvm install ruby-2.3.1 && rvm use 2.3.1
- gem install bundler
- gem install cocoapods --no-rdoc --no-ri --no-document --quiet
2 changes: 1 addition & 1 deletion Caishen.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

Pod::Spec.new do |s|
s.name = "Caishen"
s.version = "1.5.0"
s.version = "2.0.0"
s.summary = "A Payment Card UI & Validator for iOS"

# This description is used to generate tags and improve search results.
Expand Down
22 changes: 15 additions & 7 deletions Caishen.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@
2FDD15481CC7B1AC0086CEAC /* CardTextField+InterfaceBuilder.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "CardTextField+InterfaceBuilder.swift"; sourceTree = "<group>"; };
2FDD15491CC7B1AC0086CEAC /* CardTextField+PrefillInformation.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "CardTextField+PrefillInformation.swift"; sourceTree = "<group>"; };
2FDD154A1CC7B1AC0086CEAC /* CardTextField+ViewAnimations.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "CardTextField+ViewAnimations.swift"; sourceTree = "<group>"; };
2FDD154B1CC7B1AC0086CEAC /* CardTextField.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CardTextField.swift; sourceTree = "<group>"; };
2FDD154B1CC7B1AC0086CEAC /* CardTextField.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = CardTextField.swift; path = ../UI/CardTextField.swift; sourceTree = "<group>"; };
2FDD154C1CC7B1AC0086CEAC /* CardTextFieldDelegate.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CardTextFieldDelegate.swift; sourceTree = "<group>"; };
2FDD154D1CC7B1AC0086CEAC /* CardTypeImageStore.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CardTypeImageStore.swift; sourceTree = "<group>"; };
2FDD154E1CC7B1AC0086CEAC /* CardView.xib */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.xib; path = CardView.xib; sourceTree = "<group>"; };
Expand Down Expand Up @@ -250,11 +250,11 @@
2FDD15461CC7B1AC0086CEAC /* UI */ = {
isa = PBXGroup;
children = (
2FDD154B1CC7B1AC0086CEAC /* CardTextField.swift */,
2FDD15471CC7B1AC0086CEAC /* CardTextField+CardInfoTextFieldDelegate.swift */,
2FDD15481CC7B1AC0086CEAC /* CardTextField+InterfaceBuilder.swift */,
2FDD15491CC7B1AC0086CEAC /* CardTextField+PrefillInformation.swift */,
2FDD154A1CC7B1AC0086CEAC /* CardTextField+ViewAnimations.swift */,
2FDD154B1CC7B1AC0086CEAC /* CardTextField.swift */,
2FDD154C1CC7B1AC0086CEAC /* CardTextFieldDelegate.swift */,
2FDD154D1CC7B1AC0086CEAC /* CardTypeImageStore.swift */,
2FDD154E1CC7B1AC0086CEAC /* CardView.xib */,
Expand Down Expand Up @@ -361,7 +361,7 @@
isa = PBXProject;
attributes = {
LastSwiftUpdateCheck = 0730;
LastUpgradeCheck = 0730;
LastUpgradeCheck = 0810;
ORGANIZATIONNAME = "Prolific Interactive";
TargetAttributes = {
2FDD150F1CC7B0B90086CEAC = {
Expand Down Expand Up @@ -494,8 +494,10 @@
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
CLANG_WARN_EMPTY_BODY = YES;
CLANG_WARN_ENUM_CONVERSION = YES;
CLANG_WARN_INFINITE_RECURSION = YES;
CLANG_WARN_INT_CONVERSION = YES;
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
CLANG_WARN_SUSPICIOUS_MOVE = YES;
CLANG_WARN_UNREACHABLE_CODE = YES;
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";
Expand Down Expand Up @@ -543,8 +545,10 @@
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
CLANG_WARN_EMPTY_BODY = YES;
CLANG_WARN_ENUM_CONVERSION = YES;
CLANG_WARN_INFINITE_RECURSION = YES;
CLANG_WARN_INT_CONVERSION = YES;
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
CLANG_WARN_SUSPICIOUS_MOVE = YES;
CLANG_WARN_UNREACHABLE_CODE = YES;
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";
Expand Down Expand Up @@ -574,6 +578,7 @@
2FDD15251CC7B0B90086CEAC /* Debug */ = {
isa = XCBuildConfiguration;
buildSettings = {
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "";
DEFINES_MODULE = YES;
DYLIB_COMPATIBILITY_VERSION = 1;
DYLIB_CURRENT_VERSION = 1;
Expand All @@ -584,13 +589,14 @@
PRODUCT_BUNDLE_IDENTIFIER = com.prolificinteractive.Caishen;
PRODUCT_NAME = "$(TARGET_NAME)";
SKIP_INSTALL = YES;
SWIFT_VERSION = 2.3;
SWIFT_VERSION = 3.0;
};
name = Debug;
};
2FDD15261CC7B0B90086CEAC /* Release */ = {
isa = XCBuildConfiguration;
buildSettings = {
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "";
DEFINES_MODULE = YES;
DYLIB_COMPATIBILITY_VERSION = 1;
DYLIB_CURRENT_VERSION = 1;
Expand All @@ -601,7 +607,8 @@
PRODUCT_BUNDLE_IDENTIFIER = com.prolificinteractive.Caishen;
PRODUCT_NAME = "$(TARGET_NAME)";
SKIP_INSTALL = YES;
SWIFT_VERSION = 2.3;
SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule";
SWIFT_VERSION = 3.0;
};
name = Release;
};
Expand All @@ -612,7 +619,7 @@
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = com.prolificinteractive.CaishenTests;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 2.3;
SWIFT_VERSION = 3.0;
};
name = Debug;
};
Expand All @@ -623,7 +630,8 @@
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = com.prolificinteractive.CaishenTests;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 2.3;
SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule";
SWIFT_VERSION = 3.0;
};
name = Release;
};
Expand Down
2 changes: 1 addition & 1 deletion Caishen.xcodeproj/xcshareddata/xcschemes/Caishen.xcscheme
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "0730"
LastUpgradeVersion = "0810"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "0730"
LastUpgradeVersion = "0810"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand Down
2 changes: 1 addition & 1 deletion CaishenTests/CaishenTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class CaishenTests: XCTestCase {

func testPerformanceExample() {
// This is an example of a performance test case.
self.measureBlock {
self.measure {
// Put the code you want to measure the time of here.
}
}
Expand Down
16 changes: 8 additions & 8 deletions CaishenTests/CardCVCValidatorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,21 @@ class CardCVCValidatorTests: XCTestCase {
let invalidLengthObject1 = CVC(rawValue: "23")
let invalidLengthObject2 = CVC(rawValue: "2345")

XCTAssertValid(Visa().validateCVC(validObject))
XCTAssertValid(AmericanExpress().validateCVC(invalidLengthObject2))
XCTAssertIncompleteCVC(AmericanExpress().validateCVC(validObject))
XCTAssertIncompleteCVC(Visa().validateCVC(invalidLengthObject1))
XCTAssertInvalidCVC(Visa().validateCVC(invalidLengthObject2))
XCTAssertValid(Visa().validate(cvc: validObject))
XCTAssertValid(AmericanExpress().validate(cvc: invalidLengthObject2))
XCTAssertIncompleteCVC(AmericanExpress().validate(cvc: validObject))
XCTAssertIncompleteCVC(Visa().validate(cvc: invalidLengthObject1))
XCTAssertInvalidCVC(Visa().validate(cvc: invalidLengthObject2))
}

func testValidateCharacters() {
let validObject = CVC(rawValue: "356")
let invalidCharacterObject1 = CVC(rawValue: "23a")
let invalidCharacterObject2 = CVC(rawValue: "2.5")

XCTAssertValid(Visa().validateCVC(validObject))
XCTAssertInvalidCVC(Visa().validateCVC(invalidCharacterObject1))
XCTAssertInvalidCVC(Visa().validateCVC(invalidCharacterObject2))
XCTAssertValid(Visa().validate(cvc: validObject))
XCTAssertInvalidCVC(Visa().validate(cvc: invalidCharacterObject1))
XCTAssertInvalidCVC(Visa().validate(cvc: invalidCharacterObject2))
}

}
8 changes: 4 additions & 4 deletions CaishenTests/CardExpiryValidatorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ class CardExpiryValidatorTests: XCTestCase {
let lastYear = Expiry(month: 01, year: 2015)!
let future = Expiry(month: 12, year: 2115)!

XCTAssertValid(cardType.validateExpiry(future))
XCTAssertCardExpired(cardType.validateExpiry(lastYear))
XCTAssertValid(cardType.validate(expiry: future))
XCTAssertCardExpired(cardType.validate(expiry: lastYear))
}

func testInvalidExpiryCreation() {
Expand Down Expand Up @@ -62,7 +62,7 @@ class CardExpiryValidatorTests: XCTestCase {
XCTAssertNotNil(obj, "Object \(i) should not be nil, but is")
XCTAssertEqual(obj?.description, "02/2006")
if let obj = obj {
XCTAssertCardExpired(cardType.validateExpiry(obj))
XCTAssertCardExpired(cardType.validate(expiry: obj))
}
}

Expand All @@ -71,7 +71,7 @@ class CardExpiryValidatorTests: XCTestCase {
XCTAssertNotNil(obj, "Object \(i) should not be nil, but is")
XCTAssertEqual(obj?.description, "02/2096")
if let obj = obj {
XCTAssertCardNotExpired(cardType.validateExpiry(obj))
XCTAssertCardNotExpired(cardType.validate(expiry: obj))
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions CaishenTests/CardNumberFormatterTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@ class CardNumberFormatterTests: XCTestCase {
func testCorrectSeparator() {
let testNumber = Number(rawValue: "4123123412341234")

let formattedTestNumber = self.formatter.formattedCardNumber(testNumber.description)
let formattedTestNumber = self.formatter.format(cardNumber: testNumber.description)

XCTAssertEqual(formattedTestNumber, "4123-1234-1234-1234")
}

func testPerformanceExample() {
// This is an example of a performance test case.
self.measureBlock {
self.measure {
// Put the code you want to measure the time of here.
}
}
Expand Down
66 changes: 33 additions & 33 deletions CaishenTests/CardNumberValidatorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -75,38 +75,38 @@ class CardNumberValidatorTests: XCTestCase {
func testValidCards() {
print("Validate Visa")
self.validVisaNumbers.forEach({
XCTAssertEqual(CardTypeRegister.sharedCardTypeRegister.cardTypeForNumber(Number(rawValue: $0)).name, Visa().name)
XCTAssertValid(Visa().validateNumber(Number(rawValue: $0)))
XCTAssertEqual(CardTypeRegister.sharedCardTypeRegister.cardType(for: Number(rawValue: $0)).name, Visa().name)
XCTAssertValid(Visa().validate(number: Number(rawValue: $0)))
})

print("Validate Amex")
self.validAmexNumbers.forEach({
XCTAssertEqual(CardTypeRegister.sharedCardTypeRegister.cardTypeForNumber(Number(rawValue: $0)).name, AmericanExpress().name)
XCTAssertValid(AmericanExpress().validateNumber(Number(rawValue: $0)))
XCTAssertEqual(CardTypeRegister.sharedCardTypeRegister.cardType(for: Number(rawValue: $0)).name, AmericanExpress().name)
XCTAssertValid(AmericanExpress().validate(number: Number(rawValue: $0)))
})

print("Validate China UnionPay")
self.validChinaUnionPayNumbers.forEach({
XCTAssertEqual(CardTypeRegister.sharedCardTypeRegister.cardTypeForNumber(Number(rawValue: $0)).name, ChinaUnionPay().name)
XCTAssertValid(ChinaUnionPay().validateNumber(Number(rawValue: $0)))
XCTAssertEqual(CardTypeRegister.sharedCardTypeRegister.cardType(for: Number(rawValue: $0)).name, ChinaUnionPay().name)
XCTAssertValid(ChinaUnionPay().validate(number: Number(rawValue: $0)))
})

print("Validate Diners Club")
self.validDinersClubNumbers.forEach({
XCTAssertEqual(CardTypeRegister.sharedCardTypeRegister.cardTypeForNumber(Number(rawValue: $0)).name, DinersClub().name)
XCTAssertValid(DinersClub().validateNumber(Number(rawValue: $0)))
XCTAssertEqual(CardTypeRegister.sharedCardTypeRegister.cardType(for: Number(rawValue: $0)).name, DinersClub().name)
XCTAssertValid(DinersClub().validate(number: Number(rawValue: $0)))
})

print("Validate Discover")
self.validDiscoverNumbers.forEach({
XCTAssertEqual(CardTypeRegister.sharedCardTypeRegister.cardTypeForNumber(Number(rawValue: $0)).name, Discover().name)
XCTAssertValid(Discover().validateNumber(Number(rawValue: $0)))
XCTAssertEqual(CardTypeRegister.sharedCardTypeRegister.cardType(for: Number(rawValue: $0)).name, Discover().name)
XCTAssertValid(Discover().validate(number: Number(rawValue: $0)))
})

print("Validate JCB")
self.validJCBNumbers.forEach({
XCTAssertEqual(CardTypeRegister.sharedCardTypeRegister.cardTypeForNumber(Number(rawValue: $0)).name, JCB().name, "Card number was interpreted as wrong kind: \($0)")
XCTAssertValid(JCB().validateNumber(Number(rawValue: $0)))
XCTAssertEqual(CardTypeRegister.sharedCardTypeRegister.cardType(for: Number(rawValue: $0)).name, JCB().name, "Card number was interpreted as wrong kind: \($0)")
XCTAssertValid(JCB().validate(number: Number(rawValue: $0)))
})
}

Expand All @@ -127,53 +127,53 @@ class CardNumberValidatorTests: XCTestCase {
let tooLongMasterCard = "55555555555544444"
let tooLongChinaUnionPay = "62812636660717755"

XCTAssertIncompleteNumber(Visa().validateNumber(Number(rawValue: tooShortVisa)))
XCTAssertIncompleteNumber(AmericanExpress().validateNumber(Number(rawValue: tooShortAmex)))
XCTAssertIncompleteNumber(DinersClub().validateNumber(Number(rawValue: tooShortDiners)))
XCTAssertIncompleteNumber(Discover().validateNumber(Number(rawValue: tooShortDiscover)))
XCTAssertIncompleteNumber(JCB().validateNumber(Number(rawValue: tooShortJCB)))
XCTAssertIncompleteNumber(MasterCard().validateNumber(Number(rawValue: tooShortMasterCard)))
XCTAssertIncompleteNumber(ChinaUnionPay().validateNumber(Number(rawValue: tooShortChinaUnionPay)))
XCTAssertIncompleteNumber(Visa().validate(number: Number(rawValue: tooShortVisa)))
XCTAssertIncompleteNumber(AmericanExpress().validate(number: Number(rawValue: tooShortAmex)))
XCTAssertIncompleteNumber(DinersClub().validate(number: Number(rawValue: tooShortDiners)))
XCTAssertIncompleteNumber(Discover().validate(number: Number(rawValue: tooShortDiscover)))
XCTAssertIncompleteNumber(JCB().validate(number: Number(rawValue: tooShortJCB)))
XCTAssertIncompleteNumber(MasterCard().validate(number: Number(rawValue: tooShortMasterCard)))
XCTAssertIncompleteNumber(ChinaUnionPay().validate(number: Number(rawValue: tooShortChinaUnionPay)))

XCTAssertInvalidNumberForType(Visa().validateNumber(Number(rawValue: tooLongVisa)))
XCTAssertInvalidNumberForType(AmericanExpress().validateNumber(Number(rawValue: tooLongAmex)))
XCTAssertInvalidNumberForType(DinersClub().validateNumber(Number(rawValue: tooLongDiners)))
XCTAssertInvalidNumberForType(Discover().validateNumber(Number(rawValue: tooLongDiscover)))
XCTAssertInvalidNumberForType(JCB().validateNumber(Number(rawValue: tooLongJCB)))
XCTAssertInvalidNumberForType(MasterCard().validateNumber(Number(rawValue: tooLongMasterCard)))
XCTAssertInvalidNumberForType(ChinaUnionPay().validateNumber(Number(rawValue: tooLongChinaUnionPay)))
XCTAssertInvalidNumberForType(Visa().validate(number: Number(rawValue: tooLongVisa)))
XCTAssertInvalidNumberForType(AmericanExpress().validate(number: Number(rawValue: tooLongAmex)))
XCTAssertInvalidNumberForType(DinersClub().validate(number: Number(rawValue: tooLongDiners)))
XCTAssertInvalidNumberForType(Discover().validate(number: Number(rawValue: tooLongDiscover)))
XCTAssertInvalidNumberForType(JCB().validate(number: Number(rawValue: tooLongJCB)))
XCTAssertInvalidNumberForType(MasterCard().validate(number: Number(rawValue: tooLongMasterCard)))
XCTAssertInvalidNumberForType(ChinaUnionPay().validate(number: Number(rawValue: tooLongChinaUnionPay)))
}

/**
Changes the last digit of the card number (i.e. the digit that is used for validation with the Luhn test) and tests, if the Luhn test fails.
*/
func testInvalidLuhnTest() {
var allValidCardNumbers = validVisaNumbers
allValidCardNumbers.appendContentsOf(self.validAmexNumbers)
allValidCardNumbers.appendContentsOf(self.validDinersClubNumbers)
allValidCardNumbers.appendContentsOf(self.validDiscoverNumbers)
allValidCardNumbers.appendContentsOf(self.validJCBNumbers)
allValidCardNumbers.append(contentsOf: self.validAmexNumbers)
allValidCardNumbers.append(contentsOf: self.validDinersClubNumbers)
allValidCardNumbers.append(contentsOf: self.validDiscoverNumbers)
allValidCardNumbers.append(contentsOf: self.validJCBNumbers)

let invalidLuhnTestVisa: [String] = allValidCardNumbers.map({
guard let intValue = Int64($0) else {
XCTFail("The credit card number \($0) is supposed to be parseable to an Integer!")
return ""
}

var randomNumber = random() % 10
var randomNumber = arc4random() % 10
if randomNumber == 0 {
randomNumber = 1
}

let changedLastDigit = (intValue + randomNumber) % 10
let changedLastDigit = (intValue + Int64(randomNumber)) % 10
let invalidValue = intValue - (intValue % 10) + changedLastDigit

return "\(invalidValue)"
})


invalidLuhnTestVisa.forEach({
XCTAssertLuhnTestFailed(Visa().validateNumber(Number(rawValue: $0)))
XCTAssertLuhnTestFailed(Visa().validate(number: Number(rawValue: $0)))
})
}

Expand Down
Loading

0 comments on commit 66df98d

Please sign in to comment.