🇧🇷 CPF validation in Go

Better tests for mask

Thanks @danielfireman

+13 -5
+2 -2
cpf.go
··· 57 57 return checksum(ds[:9]) == ds[9] && checksum(ds[:10]) == ds[10] 58 58 } 59 59 60 - //Mask return the formated value 60 + //Mask returns the Cpf number formatted 61 61 func (c Cpf) Mask() string { 62 62 u := c.Unmask() 63 - if len(u) < 11 { 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 - if got := Cpf("11111111111").Mask(); "111.111.111-11" != got { 7 - t.Errorf("Cpf(\"11111111111\").Mask() = %v; want 111.111.111-11", got) 6 + for _, tc := range []struct { 7 + cpf string 8 + expected string 9 + }{ 10 + {"11111111111", "111.111.111-11"}, 11 + {"123456", "123456"}, 12 + {"11223344556677889900", "11223344556677889900"}, 13 + } { 14 + if got := Cpf(tc.cpf).Mask(); tc.expected != got { 15 + t.Errorf("Cpf(\"%s\").Mask() = %v; want %s", tc.cpf, got, tc.expected) 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 - 16 24 } 17 25 18 26 func TestValidate(t *testing.T) {