Skip to content

Commit

Permalink
Implement CPF tests
Browse files Browse the repository at this point in the history
  • Loading branch information
flavioltonon committed Apr 5, 2019
1 parent d8d6a97 commit 665682a
Show file tree
Hide file tree
Showing 2 changed files with 144 additions and 16 deletions.
36 changes: 20 additions & 16 deletions cpf.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,42 +10,46 @@ import (
// CPF struct
type cpf struct {
number cpfNumber
valid bool
}

func (c cpf) Number(mask bool) string {
var cNumber = c.number

if mask {
return string(cNumber[:3]) + "." + string(cNumber[3:6]) + "." + string(cNumber[6:9]) + "-" + string(cNumber[9:])
if c.valid && mask {
return string(c.number[:3]) +
"." +
string(c.number[3:6]) +
"." +
string(c.number[6:9]) +
"-" +
string(c.number[9:])
}
return string(cNumber)
return string(c.number)
}

func ParseCPF(number string) (cpf, error) {
var cNumber cpfNumber

number = regexp.MustCompile(`[^0-9]`).ReplaceAllString(number, "")

if len(number) != 11 {
return cpf{}, errIncorrectLenghtCpfNumber
return cpf{}, errIncorrectLenghtCPFNumber
}

cNumber = cpfNumber(number)
cpfNumber := cpfNumber(number)

if cNumber.isFalsePositive() {
return cpf{}, errInvalidCpfNumber
if cpfNumber.isFalsePositive() {
return cpf{}, errInvalidCPFNumber
}

if !cNumber.hasValidFirstDigit() {
return cpf{}, errInvalidCpfNumber
if !cpfNumber.hasValidFirstDigit() {
return cpf{}, errInvalidCPFNumber
}

if !cNumber.hasValidSecondDigit() {
return cpf{}, errInvalidCpfNumber
if !cpfNumber.hasValidSecondDigit() {
return cpf{}, errInvalidCPFNumber
}

return cpf{
number: cNumber,
number: cpfNumber,
valid: true,
}, nil
}

Expand Down
124 changes: 124 additions & 0 deletions cpf_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
package brazil_test

import (
"testing"

. "flavioltonon/go-brazil"

. "github.com/smartystreets/goconvey/convey"
)

func TestParseCPF(t *testing.T) {
Convey("Given a string named s", t, func() {
var s string

Convey("If s is empty", func() {
s = ""

Convey("And the function ParseCPF is called using it as an argument", func() {
cpf, err := ParseCPF(s)

Convey("It should return an error", func() {
So(err, ShouldNotEqual, nil)

Convey("And the CPF struct number should be empty", func() {
So(cpf.Number(false), ShouldEqual, "")
})
})
})
})

Convey("If s is a false-positive CPF number", func() {
s = "11111111111"

Convey("And the function ParseCPF is called using it as an argument", func() {
cpf, err := ParseCPF(s)

Convey("It should return an error", func() {
So(err, ShouldNotEqual, nil)

Convey("And the CPF struct number should be empty", func() {
So(cpf.Number(false), ShouldEqual, "")
})
})
})
})

Convey("If s is a CPF number with an invalid first digit", func() {
s = "05143026065"

Convey("And the function ParseCPF is called using it as an argument", func() {
cpf, err := ParseCPF(s)

Convey("It should return an error", func() {
So(err, ShouldNotEqual, nil)

Convey("And the CPF struct number should be empty", func() {
So(cpf.Number(false), ShouldEqual, "")
})
})
})
})

Convey("If s is a CPF number with an invalid second digit", func() {
s = "05143026074"

Convey("And the function ParseCPF is called using it as an argument", func() {
cpf, err := ParseCPF(s)

Convey("It should return an error", func() {
So(err, ShouldNotEqual, nil)

Convey("And the CPF struct number should be empty", func() {
So(cpf.Number(false), ShouldEqual, "")
})
})
})
})

Convey("If s is a valid CPF number", func() {
s = "05143026075"

Convey("And the function ParseCPF is called using it as an argument", func() {
cpf, err := ParseCPF(s)

Convey("It should not return an error", func() {
So(err, ShouldEqual, nil)

Convey("And the CPF struct number should exist", func() {
So(cpf.Number(false), ShouldEqual, "05143026075")
So(cpf.Number(true), ShouldEqual, "051.430.260-75")
})
})
})
})
})
}

func TestRandomCPFNumber(t *testing.T) {
Convey("Given the function RandomCPFNumber", t, func() {
Convey("If its mask argument equals true", func() {
number := RandomCPFNumber(true)

Convey("It should return a valid CPF number", func() {
cpf, err := ParseCPF(number)

So(cpf.Number(false), ShouldNotEqual, "")
So(cpf.Number(true), ShouldNotEqual, "")
So(err, ShouldEqual, nil)
})
})

Convey("If its mask argument equals false", func() {
number := RandomCPFNumber(false)

Convey("It should return a valid CPF number", func() {
cpf, err := ParseCPF(number)

So(cpf.Number(false), ShouldNotEqual, "")
So(cpf.Number(true), ShouldNotEqual, "")
So(err, ShouldEqual, nil)
})
})
})
}

0 comments on commit 665682a

Please sign in to comment.