-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcpf_test.go
198 lines (186 loc) · 3.44 KB
/
cpf_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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
package br
import "testing"
var cpfSink CPF
func BenchmarkGenerateCPF(b *testing.B) {
b.ReportAllocs()
for range b.N {
cpfSink = GenerateCPF()
}
}
func TestGenerateCPF(t *testing.T) {
for range 1_000_000 {
if cpf := GenerateCPF(); !cpf.IsValid() {
t.Errorf("invalid CPF generated: %s", string(cpf))
}
}
}
func BenchmarkCPF_IsValid14(b *testing.B) {
const cpf = CPF("453.178.287-91")
if !cpf.IsValid() {
b.Error("invalid cpf on benchmark")
b.FailNow()
}
b.ReportAllocs()
for range b.N {
boolSink = cpf.IsValid()
}
}
func BenchmarkCPF_IsValid11(b *testing.B) {
const cpf = CPF("45317828791")
if !cpf.IsValid() {
b.Error("invalid cpf on benchmark")
b.FailNow()
}
b.ReportAllocs()
for range b.N {
boolSink = cpf.IsValid()
}
}
func BenchmarkCPF_IsValid14Invalid(b *testing.B) {
const cpf = CPF("453.178.287-92")
if cpf.IsValid() {
b.Error("valid cpf on benchmark")
b.FailNow()
}
b.ReportAllocs()
for range b.N {
boolSink = cpf.IsValid()
}
}
func BenchmarkCPF_IsValid11Invalid(b *testing.B) {
const cpf = CPF("45317828792")
if cpf.IsValid() {
b.Error("valid cpf on benchmark")
b.FailNow()
}
b.ReportAllocs()
for range b.N {
boolSink = cpf.IsValid()
}
}
func BenchmarkCPF_String14(b *testing.B) {
const cpf = CPF("453.178.287-91")
if !cpf.IsValid() {
b.Error("invalid cpf on benchmark")
b.FailNow()
}
b.ReportAllocs()
for range b.N {
stringSink = cpf.String()
}
}
func BenchmarkCPF_String11(b *testing.B) {
const cpf = CPF("45317828791")
if !cpf.IsValid() {
b.Error("invalid cpf on benchmark")
b.FailNow()
}
b.ReportAllocs()
for range b.N {
stringSink = cpf.String()
}
}
func TestCPF_IsValid(t *testing.T) {
for _, tc := range []struct {
name string
cpf CPF
valid bool
}{
{
name: "formatted CPF",
cpf: CPF("453.178.287-91"),
valid: true,
},
{
name: "raw CPF",
cpf: CPF("45317828791"),
valid: true,
},
{
name: "invalid first digit formatted CPF",
cpf: CPF("453.178.287-81"),
valid: false,
},
{
name: "invalid first digit raw CPF",
cpf: CPF("45317828781"),
valid: false,
},
{
name: "invalid second digit formatted CPF",
cpf: CPF("453.178.287-92"),
valid: false,
},
{
name: "invalid second digit raw CPF",
cpf: CPF("45317828792"),
valid: false,
},
{
name: "empty cpf",
cpf: CPF(""),
valid: false,
},
{
name: "incorrect length cpf",
cpf: CPF("123"),
valid: false,
},
{
name: "invalid characters",
cpf: CPF("abc.def.ghi-jk"),
valid: false,
},
{
name: "invalid separators",
cpf: CPF("453-178-287.92"),
valid: false,
},
} {
t.Run(tc.name, func(t *testing.T) {
if tc.cpf.IsValid() != tc.valid {
t.Errorf(
"\ncpf: %s\nshould be valid: %v\nis valid: %v",
tc.cpf, tc.valid, tc.cpf.IsValid(),
)
}
})
}
}
func TestCPF_String(t *testing.T) {
for _, tc := range []struct {
name string
cpf CPF
want string
}{
{
name: "formatted CPF",
cpf: CPF("453.178.287-91"),
want: "453.178.287-91",
},
{
name: "raw CPF",
cpf: CPF("45317828791"),
want: "453.178.287-91",
},
{
name: "empty CPF",
cpf: CPF(""),
want: "",
},
{
name: "invalid",
cpf: CPF("123"),
want: "",
},
} {
t.Run(tc.name, func(t *testing.T) {
if tc.cpf.String() != tc.want {
t.Errorf(
"\ncpf: %s\nshould be formatted like: %s\nis formatted like: %s",
tc.cpf, tc.want, tc.cpf.String(),
)
}
})
}
}