-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
97831ec
commit fb6afb1
Showing
3 changed files
with
90 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package brdocs | ||
|
||
import ( | ||
"fmt" | ||
"github.com/brazanation/go-documents/internal" | ||
"github.com/brazanation/go-documents/internal/calculator" | ||
) | ||
|
||
const RenavamType internal.DocumentType = "Renavam" | ||
|
||
const ( | ||
renavamLength int = 11 | ||
renavamNumberOfDigits int = 1 | ||
renavamValidatorRegex string = `^([\d]{4})([\d]{6})([\d]{1})$` | ||
renavamFormatterRegex string = "$1.$2-$3" | ||
) | ||
|
||
type renavam struct {} | ||
|
||
// NewRenavam is the constructor of renavam | ||
func NewRenavam(number string) (internal.Document, error) { | ||
d, err := internal.NewDocument( | ||
RenavamType, | ||
number, | ||
renavamLength, | ||
renavamNumberOfDigits, | ||
renavamFormatterRegex, | ||
renavamValidatorRegex, | ||
renavam{}, | ||
) | ||
return d, err | ||
} | ||
|
||
func (r renavam) CalculateDigit(base string) string { | ||
m := calculator.NewModule11(base) | ||
m.WithMultipliers(2, 3, 4, 5, 6, 7, 8, 9, 2, 3) | ||
m.ReplaceWhen(0, 10) | ||
m.MultiplySumBy(10) | ||
return fmt.Sprintf("%d", m.Calculate()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package brdocs_test | ||
|
||
import ( | ||
"github.com/brazanation/go-documents" | ||
"github.com/brazanation/go-documents/internal/test" | ||
|
||
"testing" | ||
) | ||
|
||
func TestRenavamShouldBeValid(t *testing.T) { | ||
renavam, err := brdocs.NewRenavam("61855253306") | ||
internal_test.AssertValidDocument(t, renavam, err) | ||
} | ||
|
||
func TestRenavamShouldBeFormatted(t *testing.T) { | ||
renavam, err := brdocs.NewRenavam("73197444810") | ||
internal_test.AssertValidDocument(t, renavam, err) | ||
internal_test.AssertDocumentFormatted(t, renavam, "7319.744481-0") | ||
} | ||
|
||
func TestRenavamShouldReturnErrorForRepeatedNumber(t *testing.T) { | ||
number := "11111111111" | ||
_, err := brdocs.NewRenavam(number) | ||
internal_test.AssertNotValidDocument(t, number, err, "Renavam(11111111111) is invalid") | ||
} | ||
|
||
func TestRenavamShouldReturnErrorForInvalidNumber(t *testing.T) { | ||
number := "61855253307" | ||
_, err := brdocs.NewRenavam(number) | ||
internal_test.AssertNotValidDocument(t, number, err, "Renavam(61855253307) is invalid") | ||
} | ||
|
||
func TestRenavamShouldReturnErrorForEmptyNumber(t *testing.T) { | ||
number := `` | ||
_, err := brdocs.NewRenavam(number) | ||
internal_test.AssertNotValidDocument(t, number, err, "Renavam must not be empty") | ||
} |