-
Notifications
You must be signed in to change notification settings - Fork 0
/
codabar_test.go
96 lines (89 loc) · 1.96 KB
/
codabar_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
package codabar
import (
"fmt"
"testing"
)
type mockCheckDigitStrategy func(seed seed) checkDigit
func (m mockCheckDigitStrategy) Apply(seed seed) checkDigit {
return m(seed)
}
func TestNewCodabar(t *testing.T) {
tests := []struct {
name string
prefix prefix
body body
suffix suffix
options []Option
expected codabar
expectedErr error
}{
{
name: "No Check Digit Strategy",
prefix: "A",
body: "123456",
suffix: "B",
options: nil,
expected: "A123456B",
},
{
name: "With Check Digit Strategy",
prefix: "A",
body: "123456",
suffix: "B",
options: []Option{
WithCheckDigit(CheckDigitStrategy(mockCheckDigitStrategy(func(seed seed) checkDigit {
return checkDigit("7")
}))),
},
expected: "A1234567B",
},
{
name: "With Seed Generator",
prefix: "A",
body: "123456",
suffix: "B",
options: []Option{
WithSeed(func(body body) (seed, error) {
return seed(654321), nil
}),
WithCheckDigit(CheckDigitStrategy(mockCheckDigitStrategy(func(seed seed) checkDigit {
return checkDigit("5")
}))),
},
expected: "A1234565B",
},
{
name: "Error in Seed Generation",
prefix: "A",
body: "invalid",
suffix: "B",
options: []Option{
WithSeed(func(body body) (seed, error) {
return 0, ErrInvalidArgument
}),
WithCheckDigit(CheckDigitStrategy(mockCheckDigitStrategy(func(seed seed) checkDigit {
return checkDigit("0")
}))),
},
expected: "",
expectedErr: fmt.Errorf(": %w %s", ErrInvalidArgument, "invalid"),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
codabar, err := NewCodabar(tt.prefix, tt.body, tt.suffix, tt.options...)
if tt.expectedErr != nil {
if err == nil {
t.Fatalf("%v", err)
}
return
}
if err != nil {
t.Fatalf("%v", err)
}
if codabar != tt.expected {
t.Errorf("%v, %v", tt.expected, codabar)
}
})
}
}