-
Notifications
You must be signed in to change notification settings - Fork 0
/
rtn_test.go
92 lines (85 loc) · 2 KB
/
rtn_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
// Copyright (c) 2020 Matt Schultz <matt@schultz.is>. All rights reserved.
// Use of this source code is governed by an ISC license that can be found in
// the LICENSE file.
package rtnutil
import "testing"
func TestValidate(t *testing.T) {
tests := []struct {
input string
expected error
}{
{"asdf", ErrIncorrectLength},
{"1234", ErrIncorrectLength},
{"0123456789", ErrIncorrectLength},
{"R00000000", ErrInvalidCharacter},
{"123456789", ErrChecksumMismatch},
{"322286188", nil},
{"021200025", nil},
{"111000025", nil},
{"026014601", nil},
}
var actual error
for _, test := range tests {
t.Run(
test.input,
func(t *testing.T) {
actual = Validate(test.input)
if actual != test.expected {
t.Fatalf(
"input \"%s\" generated actual output \"%t\" (expected \"%t\")",
test.input,
actual,
test.expected,
)
}
},
)
}
}
func TestGetMissingDigit(t *testing.T) {
tests := []struct {
input string
expectedDigit int
expectedError error
}{
{"asdf", 0, ErrIncorrectLength},
{"1234", 0, ErrIncorrectLength},
{"0123456789", 0, ErrIncorrectLength},
{"XX2286188", 0, ErrTooManyMissingDigits},
{"R22286188", 0, ErrInvalidCharacter},
{"322286188", 0, ErrNoMissingDigits},
{"X22286188", 3, nil},
{"3X2286188", 2, nil},
{"32X286188", 2, nil},
{"322X86188", 2, nil},
{"3222X6188", 8, nil},
{"32228X188", 6, nil},
{"322286X88", 1, nil},
{"3222861X8", 8, nil},
{"32228618X", 8, nil},
{"03110064X", 9, nil},
}
var (
actualDigit int
actualError error
)
for _, test := range tests {
actualDigit, actualError = GetMissingDigit(test.input)
if actualDigit != test.expectedDigit {
t.Fatalf(
"input \"%s\" generated actual digit \"%d\" (expected \"%d\")",
test.input,
actualDigit,
test.expectedDigit,
)
}
if actualError != test.expectedError {
t.Fatalf(
"input \"%s\" generated actual error \"%s\" (expected \"%s\")",
test.input,
actualError,
test.expectedError,
)
}
}
}