-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
gtin_test.go
143 lines (131 loc) · 2.24 KB
/
gtin_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
package checkdigit_test
import (
"testing"
"github.com/osamingo/checkdigit"
)
func TestGtin_Verify(t *testing.T) {
t.Parallel()
cases := map[string]struct {
fn func() checkdigit.Provider
in string
out bool
}{
"EAN8": {
fn: checkdigit.NewEAN8,
in: "96385074",
out: true,
},
"EAN13": {
fn: checkdigit.NewEAN13,
in: "5901234123457",
out: true,
},
"JAN8": {
fn: checkdigit.NewJAN8,
in: "49968712",
out: true,
},
"JAN13": {
fn: checkdigit.NewJAN13,
in: "4569951116179",
out: true,
},
"ITF": {
fn: checkdigit.NewITF,
in: "14569951116176",
out: true,
},
"UPC": {
fn: checkdigit.NewUPC,
in: "012345678905",
out: true,
},
"SSCC": {
fn: checkdigit.NewSSCC,
in: "045699511100000016",
out: true,
},
"Empty": {
fn: checkdigit.NewEAN8,
in: "",
},
}
for name, c := range cases {
c := c
t.Run(name, func(t *testing.T) {
t.Parallel()
ret := c.fn().Verify(c.in)
if c.out != ret {
t.Errorf("not equal, expected = %v, given = %v", c.out, ret)
}
})
}
}
func TestGtin_Generate(t *testing.T) {
t.Parallel()
cases := map[string]struct {
fn func() checkdigit.Provider
in string
out int
isError bool
}{
"EAN8": {
fn: checkdigit.NewEAN8,
in: "9638112",
out: 0,
},
"EAN13": {
fn: checkdigit.NewEAN13,
in: "590123412345",
out: 7,
},
"JAN8": {
fn: checkdigit.NewJAN8,
in: "4996871",
out: 2,
},
"JAN13": {
fn: checkdigit.NewJAN13,
in: "456995111617",
out: 9,
},
"ITF": {
fn: checkdigit.NewITF,
in: "1456995111617",
out: 6,
},
"UPC": {
fn: checkdigit.NewUPC,
in: "01234567890",
out: 5,
},
"SSCC": {
fn: checkdigit.NewSSCC,
in: "04569951110000001",
out: 6,
},
"Empty": {
fn: checkdigit.NewEAN8,
in: "",
isError: true,
},
"Alphabet": {
fn: checkdigit.NewEAN8,
in: "aaaaaaa",
isError: true,
},
}
for name, c := range cases {
c := c
t.Run(name, func(t *testing.T) {
t.Parallel()
r, err := c.fn().Generate(c.in)
if c.isError && err == nil {
t.Error("unexpected error")
}
if c.out != r {
t.Errorf("not equal, expected = %d, given = %d", c.out, r)
}
})
}
}