diff --git a/payment.go b/payment.go index f741581..425dfc7 100644 --- a/payment.go +++ b/payment.go @@ -2,6 +2,7 @@ package faker import ( "fmt" + "time" ) var cardVendors = []string{ @@ -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) } diff --git a/payment_test.go b/payment_test.go index ba78bac..92088bb 100644 --- a/payment_test.go +++ b/payment_test.go @@ -1,7 +1,7 @@ package faker import ( - "strings" + "regexp" "testing" ) @@ -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)) }