馃嚙馃嚪 CPF validation in Go
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

at main 75 lines 1.5 kB view raw
1package cpf 2 3import ( 4 "fmt" 5 "testing" 6) 7 8func TestMask(t *testing.T) { 9 for _, tc := range []struct { 10 cpf string 11 expected string 12 }{ 13 {"11111111111", "111.111.111-11"}, 14 {"123456", "123456"}, 15 {"11223344556677889900", "11223344556677889900"}, 16 } { 17 if got := Mask(tc.cpf); tc.expected != got { 18 t.Errorf("Mask(\"%s\") = %v; expected %s", tc.cpf, got, tc.expected) 19 } 20 } 21} 22 23func TestUnmask(t *testing.T) { 24 if got := Unmask("111.111.111-11"); got != "11111111111" { 25 t.Errorf("Unmask(\"111.111.111-11\") = %v; want 11111111111", got) 26 } 27} 28 29func TestIsValid(t *testing.T) { 30 for _, tc := range []struct { 31 cpf string 32 expected bool 33 }{ 34 {"23858488135", true}, 35 {"238.584.881-35", true}, 36 {"123", false}, 37 {"111.111.111-11", false}, 38 {"123.456.769/01", false}, 39 {"ABC.DEF.GHI-JK", false}, 40 } { 41 if got := IsValid(tc.cpf); tc.expected != got { 42 t.Errorf("IsValid(%v) = %v; expected %v", tc.cpf, got, tc.expected) 43 } 44 } 45} 46 47func ExampleIsValid_validUnmasked() { 48 fmt.Println(IsValid("23858488135")) 49 // Output: true 50} 51 52func ExampleIsValid_validMasked() { 53 fmt.Println(IsValid("238.584.881-35")) 54 // Output: true 55} 56 57func ExampleIsValid_invalid() { 58 fmt.Println(IsValid("111.111.111-11")) 59 // Output: false 60} 61 62func ExampleMask_valid() { 63 fmt.Println(Mask("11111111111")) 64 // Output: 111.111.111-11 65} 66 67func ExampleMask_invalid() { 68 fmt.Println(Mask("42")) 69 // Output: 42 70} 71 72func ExampleUnmask() { 73 fmt.Println(Unmask("111.111.111-11")) 74 // Output: 11111111111 75}