Skip to content

Commit

Permalink
Ajustando novas regras de validacao para o estado de Goias
Browse files Browse the repository at this point in the history
  • Loading branch information
dilowagner committed May 11, 2023
1 parent 651a194 commit 6bb99ca
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 1 deletion.
14 changes: 14 additions & 0 deletions validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion validators/go.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
25 changes: 25 additions & 0 deletions validators/rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down
43 changes: 43 additions & 0 deletions validators/rules_test.go
Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit 6bb99ca

Please sign in to comment.