forked from ferdypruis/go-luhn
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathluhn_test.go
198 lines (176 loc) · 3.77 KB
/
luhn_test.go
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
package luhn_test
import (
"fmt"
"testing"
"github.com/ferdypruis/go-luhn"
)
func TestChecksum(t *testing.T) {
tests := []struct {
in string
want string
}{
{"0", "0"},
{"1", "8"},
{"05", "9"},
{"009", "1"},
{"99999999999999990", "6"},
{"99999999999999909", "6"},
{"7992739871", "3"},
}
for _, tt := range tests {
t.Run(tt.in, func(t *testing.T) {
got, err := luhn.Checksum(tt.in)
if err != nil {
t.Errorf("Checksum() error = %s", err)
}
if got != tt.want {
t.Errorf("Checksum() got = %s, want %s", got, tt.want)
}
})
}
}
func TestChecksumShortInputError(t *testing.T) {
_, err := luhn.Checksum("")
if _, ok := err.(luhn.Error); !ok {
t.Errorf("Checksum() error = %s, want luhn.Error", err)
}
}
func TestChecksumNotNumericError(t *testing.T) {
tests := []string{
"nein",
"1mississippi",
"2three4",
"?",
"三",
"3.14159265359",
}
for _, tt := range tests {
t.Run(tt, func(t *testing.T) {
_, err := luhn.Checksum(tt)
if _, ok := err.(luhn.Error); !ok {
t.Errorf("Checksum() error = %s, want luhn.Error", err)
}
})
}
}
func TestValid(t *testing.T) {
tests := []struct {
in string
want bool
}{
{"00", true},
{"18", true},
{"059", true},
{"0091", true},
{"1234", false},
{"01234", false},
{"001234", false},
{"79927398710", false},
{"79927398711", false},
{"79927398712", false},
{"79927398713", true},
{"79927398714", false},
{"79927398715", false},
{"79927398716", false},
{"79927398717", false},
{"79927398718", false},
{"79927398719", false},
{"999999999999999906", true},
{"999999999999999096", true},
// too short to contain a number and check digit
{"", false},
{"9", false},
// non-numerics
{`nein`, false},
{`3.14`, false},
{`1mississippi`, false},
{`2three4`, false},
{`!?`, false},
{`测试`, false},
}
for _, tt := range tests {
t.Run(tt.in, func(t *testing.T) {
if got := luhn.Valid(tt.in); got != tt.want {
t.Errorf("Valid() got = %t, want %t", got, tt.want)
}
})
}
}
func TestSign(t *testing.T) {
tests := []struct {
in string
want string
}{
{"0", "00"},
{"1", "18"},
{"05", "059"},
{"009", "0091"},
{"7992739871", "79927398713"},
}
for _, tt := range tests {
t.Run(tt.in, func(t *testing.T) {
got, err := luhn.Sign(tt.in)
if err != nil {
t.Errorf("Sign() unexpected error = %v", err)
}
if got != tt.want {
t.Errorf("Sign() got = %v, want %v", got, tt.want)
}
})
}
}
func TestSignShortInputError(t *testing.T) {
_, err := luhn.Sign("")
if _, ok := err.(luhn.Error); !ok {
t.Errorf("Sign() error = %s, want luhn.Error", err)
}
}
func TestSignNotNumericError(t *testing.T) {
tests := []string{
`nein`,
`1mississippi`,
`2three4`,
`!?`,
`测试`,
`3.14`,
}
for _, tt := range tests {
t.Run(tt, func(t *testing.T) {
_, err := luhn.Sign(tt)
if _, ok := err.(luhn.Error); !ok {
t.Errorf("Sign() error = %s, want luhn.Error", err)
}
})
}
}
func TestError(t *testing.T) {
err := luhn.Error("message")
want := "luhn: message"
if got := err.Error(); got != want {
t.Errorf("Error() got = %s, want %s", got, want)
}
}
func ExampleChecksum() {
number := "7992739871"
checkdigit, _ := luhn.Checksum(number) // Ignoring error for simplicity
fmt.Println("The Lühn check digit for", number, "is", checkdigit)
// Output:
// The Lühn check digit for 7992739871 is 3
}
func ExampleSign() {
number := "7992739871"
number, _ = luhn.Sign(number) // Ignoring error for simplicity
fmt.Println("Your account number is", number)
// Output:
// Your account number is 79927398713
}
func ExampleValid() {
number := "79927398713"
if luhn.Valid(number) {
fmt.Println("The number is valid")
} else {
fmt.Println("The number is not valid")
}
// Output:
// The number is valid
}