From 4ea6b9a60c1412e22ebeaf660535a9adc53643df Mon Sep 17 00:00:00 2001 From: Vitor Rocha Date: Wed, 26 Mar 2025 10:53:36 -0300 Subject: [PATCH 1/3] refactor: replace src package with utils package for document processing --- src/cnh/cnh.go | 14 +++---- src/cnpj/cnpj.go | 26 ++++++------- src/cpf/cpf.go | 22 +++++------ src/{ => utils}/utils.go | 2 +- src/{ => utils}/utils_test.go | 44 +++++++++++----------- src/voterRegistration/voterRegistration.go | 22 +++++------ 6 files changed, 65 insertions(+), 65 deletions(-) rename src/{ => utils}/utils.go (99%) rename src/{ => utils}/utils_test.go (82%) diff --git a/src/cnh/cnh.go b/src/cnh/cnh.go index a5d993f..328522a 100644 --- a/src/cnh/cnh.go +++ b/src/cnh/cnh.go @@ -3,12 +3,12 @@ package cnh import ( "fmt" - "github.com/potatowski/brazilcode/src" + "github.com/potatowski/brazilcode/src/utils" ) var ( - ErrCNHInvalidLength = fmt.Errorf("Invalid CNH length") - ErrCNHInvalid = fmt.Errorf("Invalid CNH") + ErrCNHInvalidLength = fmt.Errorf("invalid CNH length") + ErrCNHInvalid = fmt.Errorf("invalid CNH") ) /* @@ -17,12 +17,12 @@ IsValid check if the CNH is valid - @return {error} */ func IsValid(doc string) error { - doc = src.RemoveChar(doc) + doc = utils.RemoveChar(doc) if len(doc) != 11 { return ErrCNHInvalidLength } - dv1, dv2, err := src.CalculateCNHDVs(doc) + dv1, dv2, err := utils.CalculateCNHDVs(doc) if err != nil { return err } @@ -40,8 +40,8 @@ Generate is to create a random CNH - @return {error} */ func Generate() (string, error) { - cnh := src.GenerateRandomDoc(9, 10) - dv1, dv2, err := src.CalculateCNHDVs(cnh) + cnh := utils.GenerateRandomDoc(9, 10) + dv1, dv2, err := utils.CalculateCNHDVs(cnh) if err != nil { return "", err } diff --git a/src/cnpj/cnpj.go b/src/cnpj/cnpj.go index a48af8d..3507f2d 100644 --- a/src/cnpj/cnpj.go +++ b/src/cnpj/cnpj.go @@ -4,12 +4,12 @@ import ( "errors" "fmt" - "github.com/potatowski/brazilcode/src" + "github.com/potatowski/brazilcode/src/utils" ) var ( - ErrCNPJInvalid = errors.New("Invalid CNPJ") - ErrCNPJInvalidLength = errors.New("Invalid CNPJ length") + ErrCNPJInvalid = errors.New("invalid CNPJ") + ErrCNPJInvalidLength = errors.New("invalid CNPJ length") ) /* @@ -18,7 +18,7 @@ IsValid check if the CNPJ is valid - @return {error} */ func IsValid(doc string) error { - doc = src.RemoveChar(doc) + doc = utils.RemoveChar(doc) if len(doc) != 14 { return ErrCNPJInvalidLength } @@ -28,23 +28,23 @@ func IsValid(doc string) error { doc = doc[:12] var sum int - sum, err := src.Calculator(doc, 5) + sum, err := utils.Calculator(doc, 5) if err != nil { return err } - firstDigit := src.GetDigit(sum) + firstDigit := utils.GetDigit(sum) if firstDigit != int(firstDigitCheck) { return ErrCNPJInvalid } doc += fmt.Sprintf("%d", firstDigit) - sum, err = src.Calculator(doc, 6) + sum, err = utils.Calculator(doc, 6) if err != nil { return err } - secondDigit := src.GetDigit(sum) + secondDigit := utils.GetDigit(sum) if secondDigit != int(secondDigitCheck) { return ErrCNPJInvalid } @@ -69,21 +69,21 @@ Generate is to create a random CNPJ - @return {string} */ func Generate() (string, error) { - cnpj := src.GenerateRandomDoc(12, 9) + cnpj := utils.GenerateRandomDoc(12, 9) - sum, err := src.Calculator(cnpj, 5) + sum, err := utils.Calculator(cnpj, 5) if err != nil { return "", err } - cnpj += fmt.Sprintf("%d", src.GetDigit(sum)) + cnpj += fmt.Sprintf("%d", utils.GetDigit(sum)) - sum, err = src.Calculator(cnpj, 6) + sum, err = utils.Calculator(cnpj, 6) if err != nil { return "", err } - cnpj += fmt.Sprintf("%d", src.GetDigit(sum)) + cnpj += fmt.Sprintf("%d", utils.GetDigit(sum)) if err := IsValid(cnpj); err != nil { return "", err diff --git a/src/cpf/cpf.go b/src/cpf/cpf.go index b053a41..a5fe85a 100644 --- a/src/cpf/cpf.go +++ b/src/cpf/cpf.go @@ -4,12 +4,12 @@ import ( "errors" "fmt" - "github.com/potatowski/brazilcode/src" + "github.com/potatowski/brazilcode/src/utils" ) var ( - ErrCPFInvalidLength = errors.New("Invalid CPF length") - ErrCPFInvalid = errors.New("Invalid CPF") + ErrCPFInvalidLength = errors.New("invalid CPF length") + ErrCPFInvalid = errors.New("invalid CPF") ) /* @@ -18,7 +18,7 @@ IsValid check if the CPF is valid - @return {error} */ func IsValid(doc string) error { - doc = src.RemoveChar(doc) + doc = utils.RemoveChar(doc) if len(doc) != 11 { return ErrCPFInvalidLength } @@ -28,7 +28,7 @@ func IsValid(doc string) error { sum += int(doc[i]-'0') * (10 - i) } - firstDigit := src.GetDigit(sum) + firstDigit := utils.GetDigit(sum) if firstDigit != int(doc[9]-'0') { return ErrCPFInvalid } @@ -38,7 +38,7 @@ func IsValid(doc string) error { sum += int(doc[i]-'0') * (11 - i) } - secondDigit := src.GetDigit(sum) + secondDigit := utils.GetDigit(sum) if secondDigit != int(doc[10]-'0') { return ErrCPFInvalid @@ -65,21 +65,21 @@ Generate is to create a random CPF - @return {string, error} */ func Generate() (string, error) { - cpf := src.GenerateRandomDoc(9, 9) + cpf := utils.GenerateRandomDoc(9, 9) - sum, err := src.Calculator(cpf, 10) + sum, err := utils.Calculator(cpf, 10) if err != nil { return "", err } - cpf += fmt.Sprintf("%d", src.GetDigit(sum)) + cpf += fmt.Sprintf("%d", utils.GetDigit(sum)) - sum, err = src.Calculator(cpf, 11) + sum, err = utils.Calculator(cpf, 11) if err != nil { return "", err } - cpf += fmt.Sprintf("%d", src.GetDigit(sum)) + cpf += fmt.Sprintf("%d", utils.GetDigit(sum)) if err := IsValid(cpf); err != nil { return "", err diff --git a/src/utils.go b/src/utils/utils.go similarity index 99% rename from src/utils.go rename to src/utils/utils.go index fa3b40b..3ff8375 100644 --- a/src/utils.go +++ b/src/utils/utils.go @@ -1,4 +1,4 @@ -package src +package utils import ( "errors" diff --git a/src/utils_test.go b/src/utils/utils_test.go similarity index 82% rename from src/utils_test.go rename to src/utils/utils_test.go index c5448d0..bb1ddac 100644 --- a/src/utils_test.go +++ b/src/utils/utils_test.go @@ -1,16 +1,16 @@ -package src_test +package utils_test import ( "math/rand" "testing" "time" - "github.com/potatowski/brazilcode/src" + "github.com/potatowski/brazilcode/src/utils" ) func TestCalculator(t *testing.T) { // Test case 1: doc with less than 10 characters and first positive - result, err := src.Calculator("1234", 4) + result, err := utils.Calculator("1234", 4) expected := 20 if err != nil { t.Errorf("unexpected error: %v", err) @@ -20,7 +20,7 @@ func TestCalculator(t *testing.T) { } // Test case 2: doc with more than 10 characters and first positive - result, err = src.Calculator("12345678901", 1) + result, err = utils.Calculator("12345678901", 1) expected = 244 if err != nil { t.Errorf("unexpected error: %v", err) @@ -30,19 +30,19 @@ func TestCalculator(t *testing.T) { } // Test case 3: document equals "" - _, err = src.Calculator("", 1) + _, err = utils.Calculator("", 1) if err == nil { t.Errorf("expected an error about document") } // Test case 4: param fisrt more than 0 - _, err = src.Calculator("12345", -2) + _, err = utils.Calculator("12345", -2) if err == nil { t.Errorf("expected an error about first more than 0") } // Test case 5: param first equals 0 - _, err = src.Calculator("12345", 0) + _, err = utils.Calculator("12345", 0) if err == nil { t.Errorf("expected an error about first equals 0") } @@ -50,14 +50,14 @@ func TestCalculator(t *testing.T) { func TestGetDigit(t *testing.T) { // Test case 1: sum with rest less than 2 - result := src.GetDigit(287) + result := utils.GetDigit(287) expected := 0 if result != expected { t.Errorf("GetDigit(22) = %d; expected %d", result, expected) } // Test case 2: sum with rest more than or equals 2 - result = src.GetDigit(237) + result = utils.GetDigit(237) expected = 5 if result != expected { t.Errorf("GetDigit(27) = %d; expected %d", result, expected) @@ -66,28 +66,28 @@ func TestGetDigit(t *testing.T) { func TestRemoveChar(t *testing.T) { // Test case 1: str with only numbers - result := src.RemoveChar("12345") + result := utils.RemoveChar("12345") expected := "12345" if result != expected { t.Errorf("RemoveChar(\"12345\") = %s; expected %s", result, expected) } // Test case 2: str with only letters - result = src.RemoveChar("abcde") + result = utils.RemoveChar("abcde") expected = "" if result != expected { t.Errorf("RemoveChar(\"abcde\") = %s; expected %s", result, expected) } // Test case 3: str with numbers and letters - result = src.RemoveChar("1a2b3c4d5e") + result = utils.RemoveChar("1a2b3c4d5e") expected = "12345" if result != expected { t.Errorf("RemoveChar(\"1a2b3c4d5e\") = %s; expected %s", result, expected) } // Test case 4: str equals "" - result = src.RemoveChar("") + result = utils.RemoveChar("") expected = "" if result != expected { t.Errorf("RemoveChar(\"\") = %s; expected %s", result, expected) @@ -99,7 +99,7 @@ func TestGenerateRandomDoc(t *testing.T) { length := 10 numberInRand := 10 - doc := src.GenerateRandomDoc(length, numberInRand) + doc := utils.GenerateRandomDoc(length, numberInRand) expectedLen := length if len(doc) != expectedLen { @@ -119,7 +119,7 @@ func TestCalculatorCNH(t *testing.T) { first := 2 incrementType := "increment" - result, err := src.CalculatorCNH(doc, first, incrementType) + result, err := utils.CalculatorCNH(doc, first, incrementType) if err != nil { t.Errorf("CalculatorCNH(%q, %d, %q) returned an error: %v", doc, first, incrementType, err) } @@ -130,7 +130,7 @@ func TestCalculatorCNH(t *testing.T) { } incrementType = "invalid" - _, err = src.CalculatorCNH(doc, first, incrementType) + _, err = utils.CalculatorCNH(doc, first, incrementType) if err == nil { t.Errorf("CalculatorCNH(%q, %d, %q) did not return an error for invalid incrementType", doc, first, incrementType) } @@ -140,7 +140,7 @@ func TestCalculateCNHDVs(t *testing.T) { // Test case 1: valid CNH cnh := "97625655678" - dv1, dv2, err := src.CalculateCNHDVs(cnh) + dv1, dv2, err := utils.CalculateCNHDVs(cnh) if err != nil { t.Errorf("CalculateCNHDVs(%q) returned an error: %v", cnh, err) } @@ -156,7 +156,7 @@ func TestCalculateCNHDVs(t *testing.T) { // Test case 2: invalid CNH length cnh = "12345678" - _, _, err = src.CalculateCNHDVs(cnh) + _, _, err = utils.CalculateCNHDVs(cnh) if err == nil { t.Errorf("CalculateCNHDVs(%q) did not return an error for invalid CNH", cnh) } @@ -168,7 +168,7 @@ func TestGetDigitMoreThen(t *testing.T) { withAux := true expectedResult := 9 - result := src.GetDigitMoreThen(sum, withAux) + result := utils.GetDigitMoreThen(sum, withAux) if result != expectedResult { t.Errorf("GetDigitMoreThen(%d, %t) = %d, expected %d", sum, withAux, result, expectedResult) } @@ -178,7 +178,7 @@ func TestGetDigitMoreThen(t *testing.T) { withAux = false expectedResult = 9 - result = src.GetDigitMoreThen(sum, withAux) + result = utils.GetDigitMoreThen(sum, withAux) if result != expectedResult { t.Errorf("GetDigitMoreThen(%d, %t) = %d, expected %d", sum, withAux, result, expectedResult) } @@ -187,7 +187,7 @@ func TestGetDigitMoreThen(t *testing.T) { sum = 21 withAux = false expectedResult = 0 - result = src.GetDigitMoreThen(sum, withAux) + result = utils.GetDigitMoreThen(sum, withAux) if result != expectedResult { t.Errorf("GetDigitMoreThen(%d, %t) = %d, expected %d", sum, withAux, result, expectedResult) } @@ -197,7 +197,7 @@ func TestGetDigitMoreThen(t *testing.T) { withAux = true expectedResult = -1 - result = src.GetDigitMoreThen(sum, withAux) + result = utils.GetDigitMoreThen(sum, withAux) if result != expectedResult { t.Errorf("GetDigitMoreThen(%d, %t) = %d, expected %d", sum, withAux, result, expectedResult) } diff --git a/src/voterRegistration/voterRegistration.go b/src/voterRegistration/voterRegistration.go index b3d2ccf..5ebb495 100644 --- a/src/voterRegistration/voterRegistration.go +++ b/src/voterRegistration/voterRegistration.go @@ -4,7 +4,7 @@ import ( "errors" "fmt" - "github.com/potatowski/brazilcode/src" + "github.com/potatowski/brazilcode/src/utils" ) var ufToCode = map[string]string{ @@ -70,10 +70,10 @@ var codeToUf = map[string]string{ } var ( - ErrVoterRegistrationInvalid = errors.New("Invalid Voter Registration") - ErrVoterRegistrationInvalidLength = errors.New("Invalid Voter Registration length") - ErrVoterRegistrationInvalidUF = errors.New("Invalid UF") - ErrVoterRegistrationLimit = errors.New("Invalid Limit") + ErrVoterRegistrationInvalid = errors.New("invalid Voter Registration") + ErrVoterRegistrationInvalidLength = errors.New("invalid Voter Registration length") + ErrVoterRegistrationInvalidUF = errors.New("invalid UF") + ErrVoterRegistrationLimit = errors.New("invalid Limit") ) /* @@ -82,7 +82,7 @@ IsValid check if the Voter Registration is valid - @return {error} */ func IsValid(voterRegistration string) error { - voterRegistration = src.RemoveChar(voterRegistration) + voterRegistration = utils.RemoveChar(voterRegistration) if len(voterRegistration) != 12 { return ErrVoterRegistrationInvalidLength } @@ -96,7 +96,7 @@ func IsValid(voterRegistration string) error { if err != nil { return err } - dv1 := src.GetDigitMoreThen(sum, false) + dv1 := utils.GetDigitMoreThen(sum, false) if dv1 != int(voterRegistration[10]-'0') { fmt.Println(dv1, voterRegistration[10]-'0') return ErrVoterRegistrationInvalid @@ -107,7 +107,7 @@ func IsValid(voterRegistration string) error { return err } - dv2 := src.GetDigitMoreThen(sum, false) + dv2 := utils.GetDigitMoreThen(sum, false) if dv2 != int(voterRegistration[11]-'0') { return ErrVoterRegistrationInvalid } @@ -139,13 +139,13 @@ Generate is to create a random Voter Registration - @return {string, error} */ func Generate(uf string) (string, error) { - voter := src.GenerateRandomDoc(8, 9) + voter := utils.GenerateRandomDoc(8, 9) sum, err := calc(voter, 2, 9) if err != nil { return "", err } - dv1 := src.GetDigitMoreThen(sum, false) + dv1 := utils.GetDigitMoreThen(sum, false) if uf == "" { uf = getRandomUF() } @@ -162,7 +162,7 @@ func Generate(uf string) (string, error) { return "", err } - dv2 := src.GetDigitMoreThen(sum, false) + dv2 := utils.GetDigitMoreThen(sum, false) voter += fmt.Sprintf("%d", dv2) err = IsValid(voter) if err != nil { From 8607cfa1d6ef7cd6a10e54a299bb6815c796a527 Mon Sep 17 00:00:00 2001 From: Vitor Rocha Date: Wed, 26 Mar 2025 10:57:11 -0300 Subject: [PATCH 2/3] test: update voter registration test to ignore voter output --- src/voterRegistration/voterRegistration_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/voterRegistration/voterRegistration_test.go b/src/voterRegistration/voterRegistration_test.go index c81bfc8..303622d 100644 --- a/src/voterRegistration/voterRegistration_test.go +++ b/src/voterRegistration/voterRegistration_test.go @@ -128,7 +128,7 @@ func TestGenerate(t *testing.T) { // Test case 3: empty UF uf = "" - voter, err = Generate(uf) + _, err = Generate(uf) if err != nil { t.Errorf("unexpected error: %v", err) } From e6bb7e02ce72a3067a710a1dcc459242ce1df799 Mon Sep 17 00:00:00 2001 From: Vitor Rocha Date: Wed, 26 Mar 2025 13:15:03 -0300 Subject: [PATCH 3/3] refactor: add utility functions for document processing and validation --- src/utils/{utils.go => calculator.go} | 60 -------- src/utils/calculator_test.go | 94 ++++++++++++ src/utils/digit.go | 34 +++++ src/utils/digit_test.go | 64 ++++++++ src/utils/text.go | 31 ++++ src/utils/text_test.go | 59 ++++++++ src/utils/utils_test.go | 204 -------------------------- 7 files changed, 282 insertions(+), 264 deletions(-) rename src/utils/{utils.go => calculator.go} (63%) create mode 100644 src/utils/calculator_test.go create mode 100644 src/utils/digit.go create mode 100644 src/utils/digit_test.go create mode 100644 src/utils/text.go create mode 100644 src/utils/text_test.go delete mode 100644 src/utils/utils_test.go diff --git a/src/utils/utils.go b/src/utils/calculator.go similarity index 63% rename from src/utils/utils.go rename to src/utils/calculator.go index 3ff8375..b7080b2 100644 --- a/src/utils/utils.go +++ b/src/utils/calculator.go @@ -2,9 +2,6 @@ package utils import ( "errors" - "fmt" - "math/rand" - "regexp" ) /* @@ -46,43 +43,6 @@ func Calculator(doc string, first int) (int, error) { return sum, nil } -/* -GetDigit returns the digit of the document - - @param {int} sum - - @return {int} -*/ -func GetDigit(sum int) int { - rest := sum % 11 - if rest < 2 { - return 0 - } - return 11 - rest -} - -/* -RemoveChar returns the string without special characters - - @param {string} str - - @return {string} -*/ -func RemoveChar(str string) string { - return regexp.MustCompile("[^0-9]+").ReplaceAllString(str, "") -} - -/* -GenerateRandomDoc returns a random document - - @param {int} len - - @param {int} numberInRand - - @return {string} -*/ -func GenerateRandomDoc(len, numberInRand int) string { - var doc string - for i := 0; i < len; i++ { - doc += fmt.Sprintf("%d", rand.Intn(numberInRand)) - } - - return doc -} - /* CalculatorCNH returns the sum of the document - @param {string} doc @@ -149,23 +109,3 @@ func CalculateCNHDVs(cnh string) (int, int, error) { return dv1, dv2, nil } - -/* -GetDigitMoreThen returns the digit of the document - - @param {int} sum - - @param {bool} withAux - - @return {int} -*/ -func GetDigitMoreThen(sum int, withAux bool) (result int) { - digitCheck := sum % 11 - aux := 0 - if digitCheck >= 10 { - digitCheck = 0 - if withAux { - aux = 2 - } - } - - result = digitCheck - aux*2 - return result -} diff --git a/src/utils/calculator_test.go b/src/utils/calculator_test.go new file mode 100644 index 0000000..4b20259 --- /dev/null +++ b/src/utils/calculator_test.go @@ -0,0 +1,94 @@ +package utils_test + +import ( + "testing" + + "github.com/potatowski/brazilcode/src/utils" +) + +func TestCalculator(t *testing.T) { + // Test case 1: doc with less than 10 characters and first positive + result, err := utils.Calculator("1234", 4) + expected := 20 + if err != nil { + t.Errorf("unexpected error: %v", err) + } + if result != expected { + t.Errorf("Calculator(\"1234\", 4) = %d; expected %d", result, expected) + } + + // Test case 2: doc with more than 10 characters and first positive + result, err = utils.Calculator("12345678901", 1) + expected = 244 + if err != nil { + t.Errorf("unexpected error: %v", err) + } + if result != expected { + t.Errorf("Calculator(\"12345678901\", 1) = %d; expected %d", result, expected) + } + + // Test case 3: document equals "" + _, err = utils.Calculator("", 1) + if err == nil { + t.Errorf("expected an error about document") + } + + // Test case 4: param fisrt more than 0 + _, err = utils.Calculator("12345", -2) + if err == nil { + t.Errorf("expected an error about first more than 0") + } + + // Test case 5: param first equals 0 + _, err = utils.Calculator("12345", 0) + if err == nil { + t.Errorf("expected an error about first equals 0") + } +} +func TestCalculatorCNH(t *testing.T) { + doc := "12345678901" + first := 2 + incrementType := "increment" + + result, err := utils.CalculatorCNH(doc, first, incrementType) + if err != nil { + t.Errorf("CalculatorCNH(%q, %d, %q) returned an error: %v", doc, first, incrementType, err) + } + + expectedResult := 330 + if result != expectedResult { + t.Errorf("CalculatorCNH(%q, %d, %q) = %d, expected %d", doc, first, incrementType, result, expectedResult) + } + + incrementType = "invalid" + _, err = utils.CalculatorCNH(doc, first, incrementType) + if err == nil { + t.Errorf("CalculatorCNH(%q, %d, %q) did not return an error for invalid incrementType", doc, first, incrementType) + } +} + +func TestCalculateCNHDVs(t *testing.T) { + // Test case 1: valid CNH + cnh := "97625655678" + + dv1, dv2, err := utils.CalculateCNHDVs(cnh) + if err != nil { + t.Errorf("CalculateCNHDVs(%q) returned an error: %v", cnh, err) + } + + expectedDv1 := 7 + if dv1 != expectedDv1 { + t.Errorf("CalculateCNHDVs(%q) dv1 = %d, expected %d", cnh, dv1, expectedDv1) + } + expectedDv2 := 8 + if dv2 != expectedDv2 { + t.Errorf("CalculateCNHDVs(%q) dv2 = %d, expected %d", cnh, dv2, expectedDv2) + } + + // Test case 2: invalid CNH length + cnh = "12345678" + _, _, err = utils.CalculateCNHDVs(cnh) + if err == nil { + t.Errorf("CalculateCNHDVs(%q) did not return an error for invalid CNH", cnh) + } +} diff --git a/src/utils/digit.go b/src/utils/digit.go new file mode 100644 index 0000000..01c3970 --- /dev/null +++ b/src/utils/digit.go @@ -0,0 +1,34 @@ +package utils + +/* +GetDigit returns the digit of the document + - @param {int} sum + - @return {int} +*/ +func GetDigit(sum int) int { + rest := sum % 11 + if rest < 2 { + return 0 + } + return 11 - rest +} + +/* +GetDigitMoreThen returns the digit of the document + - @param {int} sum + - @param {bool} withAux + - @return {int} +*/ +func GetDigitMoreThen(sum int, withAux bool) (result int) { + digitCheck := sum % 11 + aux := 0 + if digitCheck >= 10 { + digitCheck = 0 + if withAux { + aux = 2 + } + } + + result = digitCheck - aux*2 + return result +} diff --git a/src/utils/digit_test.go b/src/utils/digit_test.go new file mode 100644 index 0000000..6d05b4b --- /dev/null +++ b/src/utils/digit_test.go @@ -0,0 +1,64 @@ +package utils_test + +import ( + "testing" + + "github.com/potatowski/brazilcode/src/utils" +) + +func TestGetDigit(t *testing.T) { + // Test case 1: sum with rest less than 2 + result := utils.GetDigit(287) + expected := 0 + if result != expected { + t.Errorf("GetDigit(22) = %d; expected %d", result, expected) + } + + // Test case 2: sum with rest more than or equals 2 + result = utils.GetDigit(237) + expected = 5 + if result != expected { + t.Errorf("GetDigit(27) = %d; expected %d", result, expected) + } +} + +func TestGetDigitMoreThen(t *testing.T) { + // Test case 1: valid sum with aux + sum := 20 + withAux := true + expectedResult := 9 + + result := utils.GetDigitMoreThen(sum, withAux) + if result != expectedResult { + t.Errorf("GetDigitMoreThen(%d, %t) = %d, expected %d", sum, withAux, result, expectedResult) + } + + // Test case 2: valid sum without aux + sum = 20 + withAux = false + expectedResult = 9 + + result = utils.GetDigitMoreThen(sum, withAux) + if result != expectedResult { + t.Errorf("GetDigitMoreThen(%d, %t) = %d, expected %d", sum, withAux, result, expectedResult) + } + + // Test case 3: digit check more than 9 + sum = 21 + withAux = false + expectedResult = 0 + result = utils.GetDigitMoreThen(sum, withAux) + if result != expectedResult { + t.Errorf("GetDigitMoreThen(%d, %t) = %d, expected %d", sum, withAux, result, expectedResult) + } + + // Test case 4: sum less than 0 + sum = -1 + withAux = true + expectedResult = -1 + + result = utils.GetDigitMoreThen(sum, withAux) + if result != expectedResult { + t.Errorf("GetDigitMoreThen(%d, %t) = %d, expected %d", sum, withAux, result, expectedResult) + } +} diff --git a/src/utils/text.go b/src/utils/text.go new file mode 100644 index 0000000..0f26b8c --- /dev/null +++ b/src/utils/text.go @@ -0,0 +1,31 @@ +package utils + +import ( + "fmt" + "math/rand" + "regexp" +) + +/* +RemoveChar returns the string without special characters + - @param {string} str + - @return {string} +*/ +func RemoveChar(str string) string { + return regexp.MustCompile("[^0-9]+").ReplaceAllString(str, "") +} + +/* +GenerateRandomDoc returns a random document + - @param {int} len + - @param {int} numberInRand + - @return {string} +*/ +func GenerateRandomDoc(len, numberInRand int) string { + var doc string + for i := 0; i < len; i++ { + doc += fmt.Sprintf("%d", rand.Intn(numberInRand)) + } + + return doc +} diff --git a/src/utils/text_test.go b/src/utils/text_test.go new file mode 100644 index 0000000..90c2b24 --- /dev/null +++ b/src/utils/text_test.go @@ -0,0 +1,59 @@ +package utils_test + +import ( + "math/rand" + "testing" + "time" + + "github.com/potatowski/brazilcode/src/utils" +) + +func TestRemoveChar(t *testing.T) { + // Test case 1: str with only numbers + result := utils.RemoveChar("12345") + expected := "12345" + if result != expected { + t.Errorf("RemoveChar(\"12345\") = %s; expected %s", result, expected) + } + + // Test case 2: str with only letters + result = utils.RemoveChar("abcde") + expected = "" + if result != expected { + t.Errorf("RemoveChar(\"abcde\") = %s; expected %s", result, expected) + } + + // Test case 3: str with numbers and letters + result = utils.RemoveChar("1a2b3c4d5e") + expected = "12345" + if result != expected { + t.Errorf("RemoveChar(\"1a2b3c4d5e\") = %s; expected %s", result, expected) + } + + // Test case 4: str equals "" + result = utils.RemoveChar("") + expected = "" + if result != expected { + t.Errorf("RemoveChar(\"\") = %s; expected %s", result, expected) + } +} + +func TestGenerateRandomDoc(t *testing.T) { + rand.Seed(time.Now().UnixNano()) + + length := 10 + numberInRand := 10 + doc := utils.GenerateRandomDoc(length, numberInRand) + + expectedLen := length + if len(doc) != expectedLen { + t.Errorf("GenerateRandomDoc() = %q, expected length %d", doc, expectedLen) + } + + for _, char := range doc { + if !('0' <= char && char <= '9') { + t.Errorf("GenerateRandomDoc() = %q, contains non-digit character %q", doc, char) + break + } + } +} diff --git a/src/utils/utils_test.go b/src/utils/utils_test.go deleted file mode 100644 index bb1ddac..0000000 --- a/src/utils/utils_test.go +++ /dev/null @@ -1,204 +0,0 @@ -package utils_test - -import ( - "math/rand" - "testing" - "time" - - "github.com/potatowski/brazilcode/src/utils" -) - -func TestCalculator(t *testing.T) { - // Test case 1: doc with less than 10 characters and first positive - result, err := utils.Calculator("1234", 4) - expected := 20 - if err != nil { - t.Errorf("unexpected error: %v", err) - } - if result != expected { - t.Errorf("Calculator(\"1234\", 4) = %d; expected %d", result, expected) - } - - // Test case 2: doc with more than 10 characters and first positive - result, err = utils.Calculator("12345678901", 1) - expected = 244 - if err != nil { - t.Errorf("unexpected error: %v", err) - } - if result != expected { - t.Errorf("Calculator(\"12345678901\", 1) = %d; expected %d", result, expected) - } - - // Test case 3: document equals "" - _, err = utils.Calculator("", 1) - if err == nil { - t.Errorf("expected an error about document") - } - - // Test case 4: param fisrt more than 0 - _, err = utils.Calculator("12345", -2) - if err == nil { - t.Errorf("expected an error about first more than 0") - } - - // Test case 5: param first equals 0 - _, err = utils.Calculator("12345", 0) - if err == nil { - t.Errorf("expected an error about first equals 0") - } -} - -func TestGetDigit(t *testing.T) { - // Test case 1: sum with rest less than 2 - result := utils.GetDigit(287) - expected := 0 - if result != expected { - t.Errorf("GetDigit(22) = %d; expected %d", result, expected) - } - - // Test case 2: sum with rest more than or equals 2 - result = utils.GetDigit(237) - expected = 5 - if result != expected { - t.Errorf("GetDigit(27) = %d; expected %d", result, expected) - } -} - -func TestRemoveChar(t *testing.T) { - // Test case 1: str with only numbers - result := utils.RemoveChar("12345") - expected := "12345" - if result != expected { - t.Errorf("RemoveChar(\"12345\") = %s; expected %s", result, expected) - } - - // Test case 2: str with only letters - result = utils.RemoveChar("abcde") - expected = "" - if result != expected { - t.Errorf("RemoveChar(\"abcde\") = %s; expected %s", result, expected) - } - - // Test case 3: str with numbers and letters - result = utils.RemoveChar("1a2b3c4d5e") - expected = "12345" - if result != expected { - t.Errorf("RemoveChar(\"1a2b3c4d5e\") = %s; expected %s", result, expected) - } - - // Test case 4: str equals "" - result = utils.RemoveChar("") - expected = "" - if result != expected { - t.Errorf("RemoveChar(\"\") = %s; expected %s", result, expected) - } -} - -func TestGenerateRandomDoc(t *testing.T) { - rand.Seed(time.Now().UnixNano()) - - length := 10 - numberInRand := 10 - doc := utils.GenerateRandomDoc(length, numberInRand) - - expectedLen := length - if len(doc) != expectedLen { - t.Errorf("GenerateRandomDoc() = %q, expected length %d", doc, expectedLen) - } - - for _, char := range doc { - if !('0' <= char && char <= '9') { - t.Errorf("GenerateRandomDoc() = %q, contains non-digit character %q", doc, char) - break - } - } -} - -func TestCalculatorCNH(t *testing.T) { - doc := "12345678901" - first := 2 - incrementType := "increment" - - result, err := utils.CalculatorCNH(doc, first, incrementType) - if err != nil { - t.Errorf("CalculatorCNH(%q, %d, %q) returned an error: %v", doc, first, incrementType, err) - } - - expectedResult := 330 - if result != expectedResult { - t.Errorf("CalculatorCNH(%q, %d, %q) = %d, expected %d", doc, first, incrementType, result, expectedResult) - } - - incrementType = "invalid" - _, err = utils.CalculatorCNH(doc, first, incrementType) - if err == nil { - t.Errorf("CalculatorCNH(%q, %d, %q) did not return an error for invalid incrementType", doc, first, incrementType) - } -} - -func TestCalculateCNHDVs(t *testing.T) { - // Test case 1: valid CNH - cnh := "97625655678" - - dv1, dv2, err := utils.CalculateCNHDVs(cnh) - if err != nil { - t.Errorf("CalculateCNHDVs(%q) returned an error: %v", cnh, err) - } - - expectedDv1 := 7 - if dv1 != expectedDv1 { - t.Errorf("CalculateCNHDVs(%q) dv1 = %d, expected %d", cnh, dv1, expectedDv1) - } - expectedDv2 := 8 - if dv2 != expectedDv2 { - t.Errorf("CalculateCNHDVs(%q) dv2 = %d, expected %d", cnh, dv2, expectedDv2) - } - - // Test case 2: invalid CNH length - cnh = "12345678" - _, _, err = utils.CalculateCNHDVs(cnh) - if err == nil { - t.Errorf("CalculateCNHDVs(%q) did not return an error for invalid CNH", cnh) - } -} - -func TestGetDigitMoreThen(t *testing.T) { - // Test case 1: valid sum with aux - sum := 20 - withAux := true - expectedResult := 9 - - result := utils.GetDigitMoreThen(sum, withAux) - if result != expectedResult { - t.Errorf("GetDigitMoreThen(%d, %t) = %d, expected %d", sum, withAux, result, expectedResult) - } - - // Test case 2: valid sum without aux - sum = 20 - withAux = false - expectedResult = 9 - - result = utils.GetDigitMoreThen(sum, withAux) - if result != expectedResult { - t.Errorf("GetDigitMoreThen(%d, %t) = %d, expected %d", sum, withAux, result, expectedResult) - } - - // Test case 3: digit check more than 9 - sum = 21 - withAux = false - expectedResult = 0 - result = utils.GetDigitMoreThen(sum, withAux) - if result != expectedResult { - t.Errorf("GetDigitMoreThen(%d, %t) = %d, expected %d", sum, withAux, result, expectedResult) - } - - // Test case 4: sum less than 0 - sum = -1 - withAux = true - expectedResult = -1 - - result = utils.GetDigitMoreThen(sum, withAux) - if result != expectedResult { - t.Errorf("GetDigitMoreThen(%d, %t) = %d, expected %d", sum, withAux, result, expectedResult) - } -}