Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
package luhn
import (
"strconv"
"strings"
"testing"
)
type testCase struct {
input string
expected bool
}
func (t testCase) getInput() (result []int) {
for _, char := range strings.Split(t.input, "") {
i, _ := strconv.Atoi(char)
result = append(result, i)
}
return
}
// cf.http://www.webcreatorbox.com/tech/creditcard-test-numbers/
var testCases []testCase = []testCase{
{"5555555555554444", true},
{"5105105105105100", true},
{"4111111111111111", true},
{"4012888888881881", true},
{"3530111333300000", true},
{"3566002020360505", true},
{"30569309025904", true},
{"38520000023237", true},
{"378282246310005", true},
{"371449635398431", true},
{"378734493671000", true},
{"6011111111111117", true},
{"6011000990139424", true},
{"6011000990139425", false},
{"6011000990139426", false},
}
func TestMain(m *testing.M) {
m.Run()
}
func TestLuhn(t *testing.T) {
for _, c := range testCases {
result := IsValid(c.getInput())
if result != c.expected {
t.Errorf("Oops! [%v] is invalid ", c.input)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment