Skip to content

Commit

Permalink
Fix credit card expiration date (#174)
Browse files Browse the repository at this point in the history
The generated expiration dates were in date/month format, while dates should be in month/year format.
  • Loading branch information
antfroger authored Apr 25, 2024
1 parent 937f93c commit 7b2bfdb
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
11 changes: 7 additions & 4 deletions payment.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package faker

import (
"fmt"
"time"
)

var cardVendors = []string{
Expand All @@ -25,9 +26,11 @@ func (p Payment) CreditCardNumber() string {
return p.Faker.Numerify("################")
}

// CreditCardExpirationDateString returns a fake credit card expiration date in string format for Payment
// CreditCardExpirationDateString returns a fake credit card expiration date in string format (i.e. mm/yy) for Payment
func (p Payment) CreditCardExpirationDateString() string {
day := p.Faker.IntBetween(0, 30)
month := p.Faker.IntBetween(12, 30)
return fmt.Sprintf("%02d/%02d", day, month)
month := p.Faker.IntBetween(1, 12)
currentYear := time.Now().Year()
year := p.Faker.IntBetween(currentYear, currentYear+3)

return fmt.Sprintf("%02d/%02d", month, year%100)
}
6 changes: 3 additions & 3 deletions payment_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package faker

import (
"strings"
"regexp"
"testing"
)

Expand All @@ -19,6 +19,6 @@ func TestCreditCardExpirationDateString(t *testing.T) {
p := New().Payment()

date := p.CreditCardExpirationDateString()
Expect(t, 5, len(date))
Expect(t, true, strings.Contains(date, "/"))
re := regexp.MustCompile(`^(0[1-9]|1[0-2])/\d{2}$`)
Expect(t, true, re.MatchString(date))
}

0 comments on commit 7b2bfdb

Please sign in to comment.