Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 18 additions & 9 deletions .github/workflows/ci.yml → .github/workflows/go.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
name: Go
name: Go CI

on:
pull_request:
branches: [ "main" ]
Expand All @@ -8,26 +9,34 @@ 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 ./...

test:
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 ./...
run: go mod tidy

- name: Run tests
run: go test -v -race ./...
11 changes: 11 additions & 0 deletions cmd/cpf/cpf.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

}
17 changes: 17 additions & 0 deletions cmd/cpf/cpf_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cpf

import (
"regexp"
"testing"
)

Expand Down Expand Up @@ -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)
}
})
}
}
8 changes: 8 additions & 0 deletions validgen.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
13 changes: 13 additions & 0 deletions validgen_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}