-
Notifications
You must be signed in to change notification settings - Fork 135
Tests | CardImageParser #2069
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Tests | CardImageParser #2069
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
df62c6e
Merge branch 'COIOS-826_OCR_feature' into card-scanner-tests-part-3
nauaros 0bd9e41
Add ExpirationDateFormattingMock
nauaros 3171e88
Test parse method
nauaros eb53dd0
Add parse test with low contrast image
nauaros b5b1be7
Merge branch 'COIOS-826_OCR_feature' into card-scanner-tests-part-3
nauaros ee2019c
Add third card image for tests
nauaros fffab36
Update card for invalid luhn test
nauaros dc2a5de
Test ExpirationDateFormatter
nauaros 4a6cabf
Remove TODO statement
nauaros bfa527a
Merge branch 'COIOS-826_OCR_feature' into card-scanner-tests-part-3
nauaros cc3c7c1
Solve Github comments
nauaros File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
21 changes: 21 additions & 0 deletions
21
AdyenCardScannerTests/Assets.xcassets/test-card-number-1.imageset/Contents.json
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| { | ||
| "images" : [ | ||
| { | ||
| "filename" : "test-card-number-1.jpg", | ||
| "idiom" : "universal", | ||
| "scale" : "1x" | ||
| }, | ||
| { | ||
| "idiom" : "universal", | ||
| "scale" : "2x" | ||
| }, | ||
| { | ||
| "idiom" : "universal", | ||
| "scale" : "3x" | ||
| } | ||
| ], | ||
| "info" : { | ||
| "author" : "xcode", | ||
| "version" : 1 | ||
| } | ||
| } |
Binary file added
BIN
+37 KB
...ScannerTests/Assets.xcassets/test-card-number-1.imageset/test-card-number-1.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
21 changes: 21 additions & 0 deletions
21
AdyenCardScannerTests/Assets.xcassets/test-card-number-2.imageset/Contents.json
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| { | ||
| "images" : [ | ||
| { | ||
| "filename" : "test-card-number-2.jpg", | ||
| "idiom" : "universal", | ||
| "scale" : "1x" | ||
| }, | ||
| { | ||
| "idiom" : "universal", | ||
| "scale" : "2x" | ||
| }, | ||
| { | ||
| "idiom" : "universal", | ||
| "scale" : "3x" | ||
| } | ||
| ], | ||
| "info" : { | ||
| "author" : "xcode", | ||
| "version" : 1 | ||
| } | ||
| } |
Binary file added
BIN
+104 KB
...ScannerTests/Assets.xcassets/test-card-number-2.imageset/test-card-number-2.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
21 changes: 21 additions & 0 deletions
21
AdyenCardScannerTests/Assets.xcassets/test-card-number-3.imageset/Contents.json
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| { | ||
| "images" : [ | ||
| { | ||
| "filename" : "test-card-number-3.png", | ||
| "idiom" : "universal", | ||
| "scale" : "1x" | ||
| }, | ||
| { | ||
| "idiom" : "universal", | ||
| "scale" : "2x" | ||
| }, | ||
| { | ||
| "idiom" : "universal", | ||
| "scale" : "3x" | ||
| } | ||
| ], | ||
| "info" : { | ||
| "author" : "xcode", | ||
| "version" : 1 | ||
| } | ||
| } |
Binary file added
BIN
+68.9 KB
...ScannerTests/Assets.xcassets/test-card-number-3.imageset/test-card-number-3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,158 @@ | ||
| // | ||
| // Copyright (c) 2025 Adyen N.V. | ||
| // | ||
| // This file is open source and available under the MIT license. See the LICENSE file for more info. | ||
| // | ||
|
|
||
| @testable import AdyenCardScanner | ||
| import XCTest | ||
|
|
||
|
|
||
| final class CardImageParserTests: XCTestCase { | ||
|
|
||
| var sut: CardImageParser! | ||
| var expirationDateFormatter: ExpirationDateFormatter! | ||
|
|
||
| override func setUpWithError() throws { | ||
| try super.setUpWithError() | ||
| expirationDateFormatter = ExpirationDateFormatter() | ||
| } | ||
|
|
||
| override func tearDownWithError() throws { | ||
| sut = nil | ||
| expirationDateFormatter = nil | ||
| try super.tearDownWithError() | ||
| } | ||
|
|
||
| func testParseImageWithHighContrastCardImage() throws { | ||
| // Given | ||
| let expirationDateFormatter = ExpirationDateFormatter() | ||
| sut = CardImageParser(expirationDateFormatter: expirationDateFormatter) | ||
|
|
||
| let testCreditCard = try XCTUnwrap(testCreditCardHighContrast) | ||
| let expectation = expectation(description: "Image should be parsed") | ||
|
|
||
| // When | ||
| sut.parse(image: testCreditCard.image) { receivedCreditCard in | ||
| expectation.fulfill() | ||
|
|
||
| let expectedCreditCard = testCreditCard.creditCard | ||
| XCTAssertEqual(expectedCreditCard.number, receivedCreditCard.number) | ||
| XCTAssertEqual(expectedCreditCard.expirationDate, receivedCreditCard.expirationDate) | ||
| } | ||
|
|
||
| // Then | ||
| wait(for: [expectation], timeout: 0.1) | ||
| } | ||
|
|
||
| func testParseImageWithLowContrastCardImage() throws { | ||
| // Given | ||
| let expirationDateFormatter = ExpirationDateFormatter() | ||
| sut = CardImageParser(expirationDateFormatter: expirationDateFormatter) | ||
|
|
||
| let testCreditCard = try XCTUnwrap(testCreditCardLowContrast) | ||
| let expectation = expectation(description: "Image should be parsed") | ||
|
|
||
| // When | ||
| sut.parse(image: testCreditCard.image) { receivedCreditCard in | ||
| expectation.fulfill() | ||
|
|
||
| let expectedCreditCard = testCreditCard.creditCard | ||
| XCTAssertEqual(expectedCreditCard.number, receivedCreditCard.number) | ||
| XCTAssertEqual(expectedCreditCard.expirationDate, receivedCreditCard.expirationDate) | ||
| } | ||
|
|
||
| // Then | ||
| wait(for: [expectation], timeout: 0.1) | ||
| } | ||
|
|
||
| func testParseImageWithInvalidLuhnCheck() throws { | ||
| // Given | ||
| let expirationDateFormatter = ExpirationDateFormatter() | ||
| sut = CardImageParser(expirationDateFormatter: expirationDateFormatter) | ||
|
|
||
| let testCreditCard = try XCTUnwrap(testCreditCardInvalidLuhn) | ||
| let expectation = expectation(description: "Image should be parsed") | ||
| expectation.isInverted = true | ||
|
|
||
| // When | ||
| sut.parse(image: testCreditCard.image) { receivedCreditCard in | ||
| expectation.fulfill() | ||
|
|
||
| let expectedCreditCard = testCreditCard.creditCard | ||
| XCTAssertEqual(expectedCreditCard.number, receivedCreditCard.number) | ||
| XCTAssertEqual(expectedCreditCard.expirationDate, receivedCreditCard.expirationDate) | ||
| } | ||
|
|
||
| // Then | ||
| wait(for: [expectation], timeout: 0.1) | ||
| } | ||
|
|
||
| // MARK: - Private | ||
|
|
||
| private struct TestCreditCard { | ||
| let image: CIImage | ||
| let creditCard: CreditCard | ||
| } | ||
|
|
||
| private var testCreditCardHighContrast: TestCreditCard? { | ||
| let image = UIImage( | ||
| named: "test-card-number-1", | ||
| in: Bundle(for: type(of: self)), | ||
| compatibleWith: nil | ||
| ) | ||
| guard let cgImage = image?.cgImage else { return nil } | ||
| let originalImage = CIImage(cgImage: cgImage) | ||
|
|
||
| let creditCard = CreditCard( | ||
| number: "4111110003920001", | ||
| expirationDate: dateFrom("03/30") | ||
| ) | ||
| return TestCreditCard( | ||
| image: originalImage, | ||
| creditCard: creditCard | ||
| ) | ||
| } | ||
|
|
||
| private var testCreditCardLowContrast: TestCreditCard? { | ||
| let image = UIImage( | ||
| named: "test-card-number-2", | ||
| in: Bundle(for: type(of: self)), | ||
| compatibleWith: nil | ||
| ) | ||
| guard let cgImage = image?.cgImage else { return nil } | ||
| let originalImage = CIImage(cgImage: cgImage) | ||
|
|
||
| let creditCard = CreditCard( | ||
| number: "5413330089099999", | ||
| expirationDate: dateFrom("02/28") | ||
| ) | ||
| return TestCreditCard( | ||
| image: originalImage, | ||
| creditCard: creditCard | ||
| ) | ||
| } | ||
|
|
||
| private var testCreditCardInvalidLuhn: TestCreditCard? { | ||
| let image = UIImage( | ||
| named: "test-card-number-3", | ||
| in: Bundle(for: type(of: self)), | ||
| compatibleWith: nil | ||
| ) | ||
| guard let cgImage = image?.cgImage else { return nil } | ||
| let originalImage = CIImage(cgImage: cgImage) | ||
|
|
||
| let creditCard = CreditCard( | ||
| number: "5412751234123456", | ||
| expirationDate: dateFrom("12/23") | ||
| ) | ||
| return TestCreditCard( | ||
| image: originalImage, | ||
| creditCard: creditCard | ||
| ) | ||
| } | ||
|
|
||
| private func dateFrom(_ string: String) -> Date? { | ||
| return expirationDateFormatter.date(from: string) | ||
| } | ||
| } |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| // | ||
| // Copyright (c) 2025 Adyen N.V. | ||
| // | ||
| // This file is open source and available under the MIT license. See the LICENSE file for more info. | ||
| // | ||
|
|
||
| import XCTest | ||
| @testable import AdyenCardScanner | ||
|
|
||
| final class ExpirationDateFormatterTests: XCTestCase { | ||
|
|
||
| var sut: ExpirationDateFormatter! | ||
| var dateFormatter = DateFormatter() | ||
|
|
||
| override func setUpWithError() throws { | ||
| try super.setUpWithError() | ||
| dateFormatter.dateFormat = "MM/yyyy" | ||
| dateFormatter.locale = Locale.current | ||
| dateFormatter.timeZone = TimeZone(secondsFromGMT: 0) | ||
| } | ||
|
|
||
| override func tearDownWithError() throws { | ||
| sut = nil | ||
| try super.tearDownWithError() | ||
| } | ||
|
|
||
| func testDateFromStringWithShortFormatDate() throws { | ||
| // Given | ||
| let stringDate = "03/2028" | ||
| let expectedDate = dateFormatter.date(from: stringDate) | ||
|
|
||
| let shortFormatDate = "03/28" | ||
| sut = ExpirationDateFormatter() | ||
|
|
||
| // When | ||
| let receivedDate = try XCTUnwrap(sut.date(from: shortFormatDate)) | ||
|
|
||
| // Then | ||
| XCTAssertEqual(receivedDate, expectedDate) | ||
| } | ||
|
|
||
| func testDateFromStringWithLongFormatDate() throws { | ||
| // Given | ||
| let stringDate = "03/2028" | ||
| let expectedDate = dateFormatter.date(from: stringDate) | ||
|
|
||
| let longFormatDate = "03/2028" | ||
| sut = ExpirationDateFormatter() | ||
|
|
||
| // When | ||
| let receivedDate = try XCTUnwrap(sut.date(from: longFormatDate)) | ||
|
|
||
| // Then | ||
| XCTAssertEqual(receivedDate, expectedDate) | ||
| } | ||
|
|
||
| func testDateFromStringWithInvalidFormatDateShouldReturnNil() throws { | ||
| // Given | ||
| let longFormatDate = "2028/03" | ||
| sut = ExpirationDateFormatter() | ||
|
|
||
| // When | ||
| let receivedDate = sut.date(from: longFormatDate) | ||
|
|
||
| // Then | ||
| XCTAssertNil(receivedDate) | ||
| } | ||
|
|
||
| } |
29 changes: 29 additions & 0 deletions
29
AdyenCardScannerTests/Mocks/ExpirationDateFormattingMock.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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| // | ||
| // Copyright (c) 2025 Adyen N.V. | ||
| // | ||
| // This file is open source and available under the MIT license. See the LICENSE file for more info. | ||
| // | ||
|
|
||
| @testable import AdyenCardScanner | ||
| import Foundation | ||
|
|
||
| class ExpirationDateFormattingMock: ExpirationDateFormatting { | ||
| // MARK: - date(from:) | ||
|
|
||
| var dateCallsCount = 0 | ||
| var dateCalled: Bool { | ||
| dateCallsCount > 0 | ||
| } | ||
|
|
||
| var dateReceivedString: String? | ||
| var dateReceivedInvocations: [String] = [] | ||
| var dateReturnValue: Date? | ||
| var dateClosure: ((String) -> Date?)? | ||
|
|
||
| func date(from string: String) -> Date? { | ||
| dateCallsCount += 1 | ||
| dateReceivedString = string | ||
| dateReceivedInvocations.append(string) | ||
| return dateClosure?(string) ?? dateReturnValue | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.