-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpaymentcard_test.go
215 lines (194 loc) · 6.26 KB
/
paymentcard_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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
package mimir
import (
"fmt"
"testing"
)
// Tests MatchPaymentCard with payment card configurations examples
func TestMatchPaymentCard(t *testing.T) {
for _, configuration := range paymentCardConfigurations {
for _, ex := range configuration.Examples {
t.Run("default", func(t *testing.T) {
if m, err := MatchPaymentCard(ex); err != nil {
t.Errorf("[%s] Unexpected error, MatchPaymentCard(%s)= %v, %v.", configuration.MajorIndustryIdentifierName, ex, m, err)
} else if len(m) == 0 {
t.Errorf("[%s] Output was incorrect, MatchPaymentCard(%s)= %v, %v; want un-empty match list.", configuration.MajorIndustryIdentifierName, ex, m, err)
}
})
t.Run("match issuer", func(t *testing.T) {
if m, err := MatchPaymentCard(ex, configuration.MajorIndustryIdentifierName); err != nil {
t.Errorf("[%s] Unexpected error, MatchPaymentCard(%s, %s)= %v, %v.",
configuration.MajorIndustryIdentifierName,
ex,
configuration.MajorIndustryIdentifierName,
m,
err,
)
}
})
}
}
}
// Tests MatchPaymentCard with custom values
func TestMatchPaymentCardhWithCustomValues(t *testing.T) {
testCases := []struct {
input string
issuer []string
expectedResult []string
expectedErr error
}{
{"4012888888881881", []string{}, []string{Visa}, nil},
{"4012888888881881", []string{}, []string{Visa}, nil},
{"4012888888881881", []string{Visa}, []string{Visa}, nil},
{"4012888888881881", []string{Mastercard}, []string{}, ErrPaymentCardDoesNotMatchAnyIssuer},
{"foobar", []string{}, []string{}, ErrPaymentCardDoesNotMatchAnyIssuer},
{"", []string{}, []string{}, ErrPaymentCardDoesNotMatchAnyIssuer},
}
for i, testCase := range testCases {
r, err := MatchPaymentCard(testCase.input)
if err != nil && err != testCase.expectedErr {
t.Errorf("[%d] Unexpected error, MatchPaymentCard(%s)= %v, %v; want: %v, %v.",
i,
testCase.input,
r,
err,
testCase.expectedResult,
testCase.expectedErr,
)
} else if len(r) == 0 && len(testCase.expectedResult) != 0 {
t.Errorf("[%d] Output was incorrect, MatchPaymentCard(%s)= %v, %v; want: %v, %v.",
i,
testCase.input,
r,
err,
testCase.expectedResult,
testCase.expectedErr,
)
}
}
}
// Tests luhn with payment card configurations examples
func TestLuhn(t *testing.T) {
for _, configuration := range paymentCardConfigurations {
for _, ex := range configuration.Examples {
t.Run("default", func(t *testing.T) {
if r := luhn(ex); r != 0 {
t.Errorf("[%s] Output was incorrect, luhn(%s)= %v, want: %v.", configuration.MajorIndustryIdentifierName, ex, r, 0)
}
})
}
}
}
// Tests luhn with custom values
func TestLuhnWithCustomValues(t *testing.T) {
testCases := []struct {
input string
expectedResult int64
}{
{"4012888888881881", 0},
{"4012888888881882", 1},
}
for i, testCase := range testCases {
r := luhn(testCase.input)
if r != testCase.expectedResult {
t.Errorf("[%d] Output was incorrect, luhn(%s)= %v, want: %v.", i, testCase.input, r, testCase.expectedResult)
}
}
}
// Tests GetPaymentCardCheckDigits with payment card configurations examples
func TestGetPaymentCardCheckDigits(t *testing.T) {
for _, configuration := range paymentCardConfigurations {
for _, ex := range configuration.Examples {
t.Run("default", func(t *testing.T) {
expected := ex[len(ex)-1]
if checksum, number, err := GetPaymentCardCheckDigits(ex[:len(ex)-1] + "0"); number != ex || err != nil {
t.Errorf("[%s] Output was incorrect, GetPaymentCardCheckDigits(%s)= %v, %v; want: %v, %v.",
configuration.MajorIndustryIdentifierName,
ex[:len(ex)-1]+"0",
checksum,
number,
ex,
expected,
)
}
})
}
}
}
// Tests GetPaymentCardCheckDigits with custom values
func TestGetPaymentCardCheckDigitsWithCustomValues(t *testing.T) {
testCases := []struct {
input string
expectedChecksum string
expectedResult string
expectedErr error
}{
{"12340", "4", "12344", nil},
{"12344", "0", "12340", nil},
{"1234", "6", "1236", nil},
{"1", "9", "9", nil},
{"0", "0", "0", nil},
{"", "", "", ErrPaymentCardTooShort},
}
for i, testCase := range testCases {
checksum, number, err := GetPaymentCardCheckDigits(testCase.input)
if err != nil && err != testCase.expectedErr {
t.Errorf("[%d] Unexpected error, GetPaymentCardCheckDigits(%s)= %v; want: %v.", i, testCase.input, err, testCase.expectedErr)
} else if number != testCase.expectedResult {
t.Errorf("[%d] Output was incorrect, GetPaymentCardCheckDigits(%s)= %v, %v; want: %v, %v.",
i,
testCase.input,
checksum,
number,
testCase.expectedChecksum,
testCase.expectedResult,
)
}
}
}
// Tests FormatPaymentCard with custom values
func TestFormatPaymentCard(t *testing.T) {
testCases := []struct {
input string
issuer string
expectedResult string
expectedErr error
}{
{"4012888888881881", Visa, "4012 8888 8888 1881", nil},
{"4012888888881881", Mastercard, "4012 8888 8888 1881", nil},
{"377932215823195", AmericanExpress, "3779 322158 23195", nil},
{"4012888888881881", AmericanExpress, "", ErrStructureNotFound},
}
for i, testCase := range testCases {
result, err := FormatPaymentCard(testCase.input, testCase.issuer)
if err != nil && err != testCase.expectedErr {
t.Errorf("[%d] Unexpected error, FormatPaymentCard(%s, %s)= %v; want: %v.", i, testCase.input, testCase.issuer, err, testCase.expectedErr)
} else if result != testCase.expectedResult {
t.Errorf("[%d] Output was incorrect, FormatPaymentCard(%s, %s)= %v; want: %v.",
i,
testCase.input,
testCase.issuer,
result,
testCase.expectedResult,
)
}
}
}
// Examples
func ExampleGetPaymentCardCheckDigits() {
fmt.Println(GetPaymentCardCheckDigits("12340"))
fmt.Println(GetPaymentCardCheckDigits("1234"))
// Output:
// 4 12344 <nil>
// 6 1236 <nil>
}
func ExampleMatchPaymentCard() {
fmt.Println(MatchPaymentCard("4012888888881881"))
fmt.Println(MatchPaymentCard("4012888888881881", Visa))
fmt.Println(MatchPaymentCard("4012888888881881", Mastercard))
fmt.Println(MatchPaymentCard("30037174022893"))
// Output:
// [Visa] <nil>
// [Visa] <nil>
// [] Payment card does not match any issuer
// [Dinner Club Carte Blanche Dinner Club International] <nil>
}