Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -78,25 +78,24 @@ struct CreditCardValidator {
}

func isExpirationValidFor(date: String) -> Bool {
Copy link
Contributor

@lmarceau lmarceau Jan 9, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should see some tests added in CreditCardValidatorTests ? This logic should be tested

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are some hard-coded ones β€” would you want me to add one that dynamically tries "this month", too? (Perhaps a few more β€” a month before this, this one, and the month after I can imagine…)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah a couple of use cases like you said would be perfect!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would 1153e17 be alright for a test, or it should be more smart, or be mocking some logic?

(I'm never comfortable around testing live values as a form of "moving target" inside tests;D)

This comment was marked as resolved.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you @janbrasna ! It's green I'll merge

let userCalendar = Calendar(identifier: .gregorian)

guard date.count == 4,
let inputMonth = Int(date.prefix(2)),
let inputYear = Int(date.suffix(2)),
let currentYear = Calendar(identifier: .gregorian).dateComponents([.year], from: Date()).year,
inputMonth < 13 && inputMonth > 0,
(inputYear + 2000) < currentYear + 100 && (inputYear + 2000) >= currentYear else {
let currentMonth = userCalendar.dateComponents([.month], from: Date()).month,
let currentYear = userCalendar.dateComponents([.year], from: Date()).year,
inputMonth >= 1 && inputMonth <= 12 else {
return false
}

var userInputtedDate = DateComponents()
userInputtedDate.year = inputYear + 2000
userInputtedDate.month = inputMonth
let userCalendar = Calendar(identifier: .gregorian)

guard let userCardExpirationDate = userCalendar.date(from: userInputtedDate),
userCardExpirationDate >= Date() else {
let expiryYear = inputYear + 2000
if expiryYear < currentYear {
return false
} else if expiryYear == currentYear {
return inputMonth >= currentMonth
} else {
return true
}

return true
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -126,19 +126,38 @@ class CreditCardValidatorTests: XCTestCase {
var result = creditCardValidator.isExpirationValidFor(date: "1230")
XCTAssert(result)

result = creditCardValidator.isExpirationValidFor(date: "0926")
result = creditCardValidator.isExpirationValidFor(date: "0928")
XCTAssert(result)
}

func testExpirationIsValidForCurrentMonth() {
let t = Date()
let c = Calendar(identifier: .gregorian)
let f = DateFormatter()
f.dateFormat = "MMyy"

// Uses current month e.g. 0125
var result = creditCardValidator.isExpirationValidFor(date: f.string(from: t))
XCTAssert(result)

// Next month too for year boundary corner cases
result = creditCardValidator.isExpirationValidFor(date: f.string(from: c.date(byAdding: .month, value: 1, to: t)!))
XCTAssert(result)

// Previous month e.g. 1224 should not pass
result = creditCardValidator.isExpirationValidFor(date: f.string(from: c.date(byAdding: .month, value: -1, to: t)!))
XCTAssertFalse(result)
}

func testExpirationIsInvalidOnIncorrectInput() {
var result = creditCardValidator.isExpirationValidFor(date: "0000")
XCTAssertFalse(result)

result = creditCardValidator.isExpirationValidFor(date: "1525")
result = creditCardValidator.isExpirationValidFor(date: "1545")
XCTAssertFalse(result)
}

func tetsExpirationIsInvalidOnBlankInput() {
func testExpirationIsInvalidOnBlankInput() {
let result = creditCardValidator.isExpirationValidFor(date: "")
XCTAssertFalse(result)
}
Expand Down