-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerator_test.go
146 lines (107 loc) · 2.53 KB
/
generator_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 drivinglicence_test
import (
"drivinglicence"
"github.com/stretchr/testify/suite"
"strings"
"testing"
)
func (s *DrivingLicenceSuite) TestUnderageApplicant() {
a := UnderageApplicant{}
l := &SpyLogger{}
r := FakeRand{}
lg := drivinglicence.NewNumberGenerator(l, r)
_, err := lg.Generate(a)
s.Error(err)
s.Contains(err.Error(), "Underaged")
s.Equal(1, l.callCount)
s.Contains(l.lastMessage, "Underaged")
}
func (s *DrivingLicenceSuite) TestNoSecondLicence() {
a := LicenceHolderApplicant{}
l := &SpyLogger{}
r := FakeRand{}
lg := drivinglicence.NewNumberGenerator(l, r)
_, err := lg.Generate(a)
s.Error(err)
s.Contains(err.Error(), "Duplicate")
s.Equal(1, l.callCount)
s.Contains(l.lastMessage, "Duplicate")
}
func (s *DrivingLicenceSuite) TestLicenceGeneration() {
a := ValidApplicant{"MDB", "23082011"}
l := &SpyLogger{}
r := FakeRand{}
lg := drivinglicence.NewNumberGenerator(l, r)
ln, err := lg.Generate(a)
s.NoError(err)
s.Equal("MDB2308201100000", ln)
}
func (s *DrivingLicenceSuite) TestLicenceGenerationShorterInitials() {
a := ValidApplicant{"MB", "23082011"}
l := &SpyLogger{}
r := FakeRand{}
lg := drivinglicence.NewNumberGenerator(l, r)
ln, err := lg.Generate(a)
s.NoError(err)
s.Equal("MB23082011000000", ln)
}
type DrivingLicenceSuite struct {
suite.Suite
}
func TestDrivingLicenceSuite(t *testing.T) {
suite.Run(t, new(DrivingLicenceSuite))
}
type UnderageApplicant struct {}
func (u UnderageApplicant) GetDOB() string {
return ""
}
func (u UnderageApplicant) GetInitials() string {
return ""
}
func (u UnderageApplicant) IsOver17() bool {
return false
}
func (u UnderageApplicant) HoldsLicence() bool {
return false
}
type LicenceHolderApplicant struct {}
func (l LicenceHolderApplicant) IsOver17() bool {
return true
}
func (l LicenceHolderApplicant) HoldsLicence() bool {
return true
}
func (l LicenceHolderApplicant) GetInitials() string {
return ""
}
func (l LicenceHolderApplicant) GetDOB() string {
return ""
}
type ValidApplicant struct {
initials string
dob string
}
func (v ValidApplicant) IsOver17() bool {
return true
}
func (v ValidApplicant) HoldsLicence() bool {
return false
}
func (v ValidApplicant) GetInitials() string {
return v.initials
}
func (v ValidApplicant) GetDOB() string {
return v.dob
}
type FakeRand struct {}
func (f FakeRand) GetRandomNumbers(len int) string {
return strings.Repeat("0", len)
}
type SpyLogger struct {
callCount int
lastMessage string
}
func (s *SpyLogger) LogStuff(v string) {
s.callCount++
s.lastMessage = v
}