-
Notifications
You must be signed in to change notification settings - Fork 2
/
cpf_test.go
91 lines (87 loc) · 1.74 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
package br
import "testing"
func BenchmarkCPF_IsValid(b *testing.B) {
const cpfBolsonaro = CPF("453.178.287-91")
if !cpfBolsonaro.IsValid() {
b.Error("invalid cpfBolsonaro on benchmark")
b.FailNow()
}
b.ReportAllocs()
for i := 0; i < b.N; i++ {
sink = cpfBolsonaro.IsValid()
}
}
func TestCPF_IsValid(t *testing.T) {
for _, tc := range []struct {
name string
cpf CPF
valid bool
}{
{
name: "formatted CPF Bolsonaro",
cpf: CPF("453.178.287-91"),
valid: true,
},
{
name: "raw CPF Bolsonaro",
cpf: CPF("45317828791"),
valid: true,
},
{
name: "invalid first digit formatted CPF Bolsonaro",
cpf: CPF("453.178.287-81"),
valid: false,
},
{
name: "invalid first digit raw CPF Bolsonaro",
cpf: CPF("453.178.287-81"),
valid: false,
},
{
name: "invalid second digit formatted CPF Bolsonaro",
cpf: CPF("453.178.287-92"),
valid: false,
},
{
name: "invalid second digit raw CPF Bolsonaro",
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 Bolsonaro",
cpf: CPF("453.178.287-91"),
want: "453.178.287-91",
},
{
name: "raw CPF Bolsonaro",
cpf: CPF("45317828791"),
want: "453.178.287-91",
},
} {
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(),
)
}
})
}
}