summaryrefslogtreecommitdiffstats
path: root/tcllib/modules/valtype/luhn.test
blob: dea48e66afcca53249a954a78ca6712a031c0128 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# -------------------------------------------------------------------------
# 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: