From 6bb99ca2a1b18d9db37ae20df600b558c176bf3a Mon Sep 17 00:00:00 2001 From: dilowagner Date: Thu, 11 May 2023 19:54:49 -0300 Subject: [PATCH] Ajustando novas regras de validacao para o estado de Goias --- validator_test.go | 14 +++++++++++++ validators/go.go | 2 +- validators/rules.go | 25 +++++++++++++++++++++++ validators/rules_test.go | 43 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 83 insertions(+), 1 deletion(-) create mode 100644 validators/rules_test.go diff --git a/validator_test.go b/validator_test.go index eda6806..32a5a6e 100644 --- a/validator_test.go +++ b/validator_test.go @@ -717,6 +717,20 @@ func TestValidatorGOInvalidNotStartWith10Or11Or15(t *testing.T) { assert.False(t, result) } +func TestValidatorGOValidStartWith20(t *testing.T) { + + validator := NewIEValidator() + + validator.IE = "200193023" + validator.UF = validators.GO + + result, err := validator.Validate() + if err != nil { + t.Error("Erro na validacao do inicio do estado de Goias") + } + assert.True(t, result) +} + func TestValidatorGOWithCharactersInvalid(t *testing.T) { validator := NewIEValidator() diff --git a/validators/go.go b/validators/go.go index 2a51b50..62d40f1 100644 --- a/validators/go.go +++ b/validators/go.go @@ -17,7 +17,7 @@ func (v Goias) IsValid(insc string) bool { return false } - if !rule.IsStartWith(insc, "10") && !rule.IsStartWith(insc, "11") && !rule.IsStartWith(insc, "15") { + if !rule.IsStartWith(insc, "10") && !rule.IsStartWith(insc, "11") && !rule.IsStartWith(insc, "15") && !rule.IsStartBetween(insc, "20", "29") { return false } diff --git a/validators/rules.go b/validators/rules.go index dc9909f..705fa09 100644 --- a/validators/rules.go +++ b/validators/rules.go @@ -32,6 +32,31 @@ func (r *Rules) IsStartWith(insc string, value string) bool { return insc[:len(value)] == value } +// IsStartBetween function +func (r *Rules) IsStartBetween(insc string, start string, end string) bool { + + istart, err := strconv.Atoi(start) + if err != nil { + return false + } + + iend, err := strconv.Atoi(end) + if err != nil { + return false + } + + if istart > iend { + return false + } + + value, err := strconv.Atoi(insc[:len(start)]) + if err != nil { + return false + } + + return value >= istart && value <= iend +} + // GetWeight function func (r *Rules) GetWeight(start int, size int) []int { diff --git a/validators/rules_test.go b/validators/rules_test.go new file mode 100644 index 0000000..3a0023a --- /dev/null +++ b/validators/rules_test.go @@ -0,0 +1,43 @@ +package validators + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestRuleIsStartBetweenNotOk(t *testing.T) { + + rule := NewRule() + + result := rule.IsStartBetween("0100482300112", "20", "30") + assert.False(t, result) +} + +func TestRuleIsStartBetweenOk(t *testing.T) { + + rule := NewRule() + + result := rule.IsStartBetween("2100482300112", "20", "30") + assert.True(t, result) + + result = rule.IsStartBetween("0200482300112", "01", "03") + assert.True(t, result) + + result = rule.IsStartBetween("0300482300112", "01", "03") + assert.True(t, result) +} + +func TestRuleIsStartBetweenWithInvalidValues(t *testing.T) { + + rule := NewRule() + + result := rule.IsStartBetween("2100482300112", "", "30") + assert.False(t, result) + + result = rule.IsStartBetween("2100482300112", "40", "30") + assert.False(t, result) + + result = rule.IsStartBetween("2100482300112", "P-", "30") + assert.False(t, result) +}