diff --git a/.github/workflows/ci.yml b/.github/workflows/go.yml similarity index 58% rename from .github/workflows/ci.yml rename to .github/workflows/go.yml index 2e1730c..a10db46 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/go.yml @@ -1,4 +1,5 @@ -name: Go +name: Go CI + on: pull_request: branches: [ "main" ] @@ -8,13 +9,17 @@ jobs: build: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v4 + - name: Setup Go - uses: actions/setup-go@v2 + uses: actions/setup-go@v4 with: go-version: '1.23.x' + cache: true + - name: Install dependencies - run: go get . + run: go mod tidy + - name: Build run: go build -v ./... @@ -22,12 +27,16 @@ jobs: runs-on: ubuntu-latest needs: build steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v4 + - name: Setup Go - uses: actions/setup-go@v2 + uses: actions/setup-go@v4 with: go-version: '1.23.x' + cache: true + - name: Install dependencies - run: go get . - - name: Test with the Go CLI - run: go test ./... \ No newline at end of file + run: go mod tidy + + - name: Run tests + run: go test -v -race ./... diff --git a/cmd/cpf/cpf.go b/cmd/cpf/cpf.go index 517d3ca..ea917e4 100644 --- a/cmd/cpf/cpf.go +++ b/cmd/cpf/cpf.go @@ -87,3 +87,14 @@ func (CPFFunctions) UnformatCPF(cpf string) (string, error) { return cpf, nil } + +func (CPFFunctions) FormatGeneratedCPF() (string, error) { + cpf, err := (CPFFunctions{}).GenerateCPF() + + if err != nil { + return "", err + } + + return (CPFFunctions{}).FormatCPF(cpf) + +} diff --git a/cmd/cpf/cpf_test.go b/cmd/cpf/cpf_test.go index bcb9ff4..796b24f 100644 --- a/cmd/cpf/cpf_test.go +++ b/cmd/cpf/cpf_test.go @@ -1,6 +1,7 @@ package cpf import ( + "regexp" "testing" ) @@ -83,3 +84,19 @@ func TestFormatCPF(t *testing.T) { }) } } +func TestFormatGeneratedCPF(t *testing.T) { + for i := 0; i < 10; i++ { + t.Run("Format Generated CPF", func(t *testing.T) { + formattedCPF, err := (CPFFunctions{}).FormatGeneratedCPF() + if err != nil { + t.Errorf("FormatGeneratedCPF() error = %v", err) + } + if err := (CPFFunctions{}).ValidateCPF(formattedCPF); err != nil { + t.Errorf("Formatted generated CPF is invalid: %v", err) + } + if matched, _ := regexp.MatchString(`^\d{3}\.\d{3}\.\d{3}-\d{2}$`, formattedCPF); !matched { + t.Errorf("Formatted generated CPF does not match expected format: %s", formattedCPF) + } + }) + } +} diff --git a/validgen.go b/validgen.go index 01396f6..1609587 100644 --- a/validgen.go +++ b/validgen.go @@ -36,3 +36,11 @@ UnformatCpf - Unformat cpf func UnformatCPF(cpfString string) (string, error) { return cpf.CPFFunctions{}.UnformatCPF(cpfString) } + +/* +FormatGeneratedCpf - Format generated cpf + - @return {string, error} - cpf, error +*/ +func FormatGeneratedCPF() (string, error) { + return cpf.CPFFunctions{}.FormatGeneratedCPF() +} diff --git a/validgen_test.go b/validgen_test.go index cdab7e8..db586b1 100644 --- a/validgen_test.go +++ b/validgen_test.go @@ -26,3 +26,16 @@ func TestValidateCpf(t *testing.T) { t.Errorf("ValidateCpf(%v) error = nil; want error", invalidCpf) } } + +func TestFormatGeneratedCpf(t *testing.T) { + formattedCpf, err := FormatGeneratedCPF() + if err != nil { + t.Errorf("FormatGeneratedCpf() error = %v", err) + } + if len(formattedCpf) != 14 { + t.Errorf("FormatGeneratedCpf() = %v; want length 14", formattedCpf) + } + if formattedCpf[3] != '.' || formattedCpf[7] != '.' || formattedCpf[11] != '-' { + t.Errorf("FormatGeneratedCpf() = %v; want format XXX.XXX.XXX-XX", formattedCpf) + } +}