# -------------------------------------------------------------------------
# luhn.test -*- tcl -*-
# (C) 2011 Andreas Kupries. BSD licensed.
# -------------------------------------------------------------------------

source [file join \
	[file dirname [file dirname [file join [pwd] [info script]]]] \
	devtools testutilities.tcl]

testsNeedTcl     8.5
testsNeedTcltest 2.0

support {
    use      snit/snit2.tcl snit            ;# snit v2 fixed, due Tcl 8.5
    useLocal valtype.tcl    valtype::common
}
testing {
    useLocal luhn.tcl valtype::luhn
}

# -------------------------------------------------------------------------

test valtype-luhn-1.0 {luhn validation wrong\#args} -body {
    valtype::luhn validate
} -returnCodes error \
    -result {wrong # args: should be "valtype::luhn validate value ?code?"}

test valtype-luhn-1.1 {luhn validation wrong\#args} -body {
    valtype::luhn validate A B C
} -returnCodes error \
    -result {wrong # args: should be "valtype::luhn validate value ?code?"}

# -------------------------------------------------------------------------

test valtype-luhn-2.0 {luhn validation failure, bad char} -body {
    valtype::luhn validate A
} -returnCodes error \
    -result {Not a LUHN number, expected only digits}

foreach {n in} {
    1  49999999
    2  37
    3  49927398717 
    4  1234567812345678
} {
    test valtype-luhn-2.1.$n {luhn validation failure, bad check} -body {
	valtype::luhn validate $in
    } -returnCodes error \
	-result {Not a LUHN number, the check digit is incorrect}
}

test valtype-luhn-3.0 {luhn checkdigit wrong\#args} -body {
    valtype::luhn checkdigit
} -returnCodes error \
    -result {wrong # args: should be "valtype::luhn checkdigit value ?code?"}

test valtype-luhn-3.1 {luhn checkdigit wrong\#args} -body {
    valtype::luhn checkdigit A B C
} -returnCodes error \
    -result {wrong # args: should be "valtype::luhn checkdigit value ?code?"}

# -------------------------------------------------------------------------

test valtype-luhn-5.0 {luhn checkdigit calculation failure, bad char} -body {
    valtype::luhn checkdigit A
} -returnCodes error \
    -result {Not a LUHN number, expected only digits}

foreach {n in check} {
    1          4999999 8
    2                3 4
    3               35 6
    4       4992739871 6
    5  123456781234567 0
} {
    test valtype-luhn-4.$n {luhn validation} -body {
	valtype::luhn validate $in$check
    } -result $in$check

    test valtype-luhn-6.$n {luhn checkdigit} -body {
	valtype::luhn checkdigit $in
    } -result $check
}

# -------------------------------------------------------------------------
testsuiteCleanup
return

# Local Variables:
#  mode: tcl
#  indent-tabs-mode: nil
# End:
