-
Notifications
You must be signed in to change notification settings - Fork 0
/
bban_test.go
114 lines (102 loc) · 2.16 KB
/
bban_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
package bban_test
import (
"fmt"
"strconv"
"testing"
bban "github.com/m1ome/bban_gen"
"github.com/stretchr/testify/assert"
)
func Example() {
account := bban.Random("040577", "13439317554524", bban.DoubleMod)
fmt.Printf("Generate random account with length %d\n", len(account))
next := bban.Next("040577", "40954426", "13439317554524", bban.DoubleMod)
fmt.Printf("Next account: %s\n", next)
fmt.Printf("Validity passing: %v\n", bban.Validate("040577", next, "13439317554524", bban.DoubleMod))
// Output:
// Generate random account with length 8
// Next account: 40954431
// Validity passing: true
}
func TestRandom(t *testing.T) {
for i := 0; i < 10; i++ {
account := bban.Random("040577", "13439317554524", bban.DoubleMod)
assert.True(t, len(account) > 0)
}
}
func TestNext(t *testing.T) {
start := strconv.Itoa(1000)
numbers := []string{}
for i := 0; i < 10; i++ {
account := bban.Next("040577", start, "13439317554524", bban.DoubleMod)
numbers = append(numbers, account)
start = account
}
assert.Equal(t, []string{"00001008", "00001013", "00001039", "00001044", "00001051", "00001065", "00001070", "00001077", "00001082", "00001096"}, numbers)
}
func TestValidate(t *testing.T) {
tests := []struct {
sortCode string
account string
weight string
vtype int
result bool
}{
{
"040577",
"77777777",
"13439317554524",
bban.DoubleMod,
true,
},
{
"089999",
"66374958",
"00000071371371",
bban.Mod10,
true,
},
{
"107999",
"88837491",
"00000087654321",
bban.Mod11,
true,
},
{
"202959",
"63748472",
"00000007654321",
bban.Mod11,
true,
},
{
"202959",
"63748472",
"21212121212121",
bban.DoubleMod,
true,
},
{
"1234",
"567890",
"123456789",
bban.DoubleMod,
false,
},
{
"202959",
"63748472",
"21212121212121",
-1,
false,
},
}
for _, test := range tests {
v := bban.Validate(test.sortCode, test.account, test.weight, test.vtype)
assert.Equal(
t, test.result, v,
"expected %s %s / %s [%d] to have validation result '%v', but have '%v'",
test.sortCode, test.account, test.weight, test.vtype, test.result, v,
)
}
}