tangled
alpha
login
or
join now
cuducos.me
/
go-cpf
0
fork
atom
🇧🇷 CPF validation in Go
0
fork
atom
overview
issues
pulls
pipelines
Better tests for mask
Thanks @danielfireman
Eduardo Cuducos
6 years ago
e79833b1
4fcfdb79
+13
-5
2 changed files
expand all
collapse all
unified
split
cpf.go
cpf_test.go
+2
-2
cpf.go
···
57
57
return checksum(ds[:9]) == ds[9] && checksum(ds[:10]) == ds[10]
58
58
}
59
59
60
60
-
//Mask return the formated value
60
60
+
//Mask returns the Cpf number formatted
61
61
func (c Cpf) Mask() string {
62
62
u := c.Unmask()
63
63
-
if len(u) < 11 {
63
63
+
if len(u) != 11 {
64
64
return string(c)
65
65
}
66
66
return fmt.Sprintf("%s.%s.%s-%s", u[:3], u[3:6], u[6:9], u[9:])
+11
-3
cpf_test.go
···
3
3
import "testing"
4
4
5
5
func TestMask(t *testing.T) {
6
6
-
if got := Cpf("11111111111").Mask(); "111.111.111-11" != got {
7
7
-
t.Errorf("Cpf(\"11111111111\").Mask() = %v; want 111.111.111-11", got)
6
6
+
for _, tc := range []struct {
7
7
+
cpf string
8
8
+
expected string
9
9
+
}{
10
10
+
{"11111111111", "111.111.111-11"},
11
11
+
{"123456", "123456"},
12
12
+
{"11223344556677889900", "11223344556677889900"},
13
13
+
} {
14
14
+
if got := Cpf(tc.cpf).Mask(); tc.expected != got {
15
15
+
t.Errorf("Cpf(\"%s\").Mask() = %v; want %s", tc.cpf, got, tc.expected)
16
16
+
}
8
17
}
9
18
}
10
19
···
12
21
if got := Cpf("111.111.111-11").Unmask(); "11111111111" != got {
13
22
t.Errorf("Cpf(\"111.111.111-11\").Unmask() = %v; want 11111111111", got)
14
23
}
15
15
-
16
24
}
17
25
18
26
func TestValidate(t *testing.T) {