cards/validation/validate_test.go
2025-01-31 21:08:14 +02:00

102 lines
1.5 KiB
Go

package validation
import "testing"
func TestLuhn(t *testing.T) {
tests := []struct {
name string
pan string
want bool
}{
{
"Mastercard",
"5555555555554444",
true,
},
{
"Visa",
"4111111111111111",
true,
},
{
"example from wiki",
"17893729974",
true,
},
{
"another example",
"79927398713",
true,
},
{
"worong check digit",
"17893729975",
false,
},
{
"off by one error",
"27893729974",
false,
},
{
"empty",
"",
false,
},
{
"not a digit",
"27893a29974",
false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := Luhn(tt.pan)
if got != tt.want {
t.Errorf("Luhn(%s) = %v, want %v", tt.name, got, tt.want)
}
})
}
}
func TestValidate(t *testing.T) {
tests := []struct {
name string
card Card
result ValidationResult
}{
{
"simple valid card",
Card{"4111111111111111", "09", "30"},
ErrValid,
},
{
"invalid PAN",
Card{"4111111111111114", "10", "26"},
ErrInvalidPAN,
},
{
"invalid year",
Card{"4111111111111111", "10", "2"},
ErrInvalidYear,
},
{
"invalid month",
Card{"4111111111111111", "13", "27"},
ErrInvalidMonth,
},
{
"expired",
Card{"4111111111111111", "10", "20"},
ErrExpire,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := Validate(tt.card)
if got != tt.result {
t.Errorf("Validate() = %v, want %v", got, tt.result)
}
})
}
}