Skip to content

Commit

Permalink
fix cnpj trim and add strings randoms
Browse files Browse the repository at this point in the history
  • Loading branch information
thiagozs committed Nov 1, 2023
1 parent 431ff6b commit 567ba73
Show file tree
Hide file tree
Showing 5 changed files with 152 additions and 23 deletions.
48 changes: 29 additions & 19 deletions calc/calc.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,38 +11,48 @@ func New() *Calc {
return &Calc{}
}

// CalculateLimitAndOffset calculates limit and offset
func (c Calc) CalculateLimitAndOffsetStr(pageNumberStr, pageSizeStr string) (limit, offset int32, err error) {
pageNumber, err := strconv.Atoi(pageNumberStr)
func (c Calc) CalculateLimitAndOffset(pageNumber, pageSize int32) (int32, int32, error) {
return CalculateLimitAndOffset(Int32Convertible(pageNumber), Int32Convertible(pageSize))
}

func (c Calc) CalculateLimitAndOffsetStr(pageNumber, pageSize string) (int32, int32, error) {
return CalculateLimitAndOffset(StringConvertible(pageNumber), StringConvertible(pageSize))
}

func CalculateLimitAndOffset[T Convertible](pageNumber, pageSize T) (int32, int32, error) {
pageNumberInt, err := pageNumber.ToInt32()
if err != nil {
return 0, 0, fmt.Errorf("calc: error converting pageNumberStr to int: %v", err)
return 0, 0, fmt.Errorf("calc: error converting pageNumber to int32: %w", err)
}

pageSize, err := strconv.Atoi(pageSizeStr)
pageSizeInt, err := pageSize.ToInt32()
if err != nil {
return 0, 0, fmt.Errorf("calc: error converting pageSizeStr to int: %v", err)
return 0, 0, fmt.Errorf("calc: error converting pageSize to int32: %w", err)
}

if pageNumber < 1 || pageSize < 1 {
if pageNumberInt < 1 || pageSizeInt < 1 {
return 0, 0, fmt.Errorf("calc: pageNumber and pageSize must be greater than 0")
}

limit, offset = c.CalculateLimitAndOffsetInt32(int32(pageNumber), int32(pageSize))
limit := pageSizeInt
offset := (pageNumberInt - 1) * pageSizeInt

return limit, offset, nil
}

// CalculateLimitAndOffsetInt32 calculates limit and offset
func (c Calc) CalculateLimitAndOffsetInt32(pageNumber, pageSize int32) (limit, offset int32) {
if pageNumber < 1 {
pageNumber = 1
}
type Convertible interface {
ToInt32() (int32, error)
}

if pageSize < 1 {
pageSize = 10
}
type StringConvertible string

func (s StringConvertible) ToInt32() (int32, error) {
i, err := strconv.Atoi(string(s))
return int32(i), err
}

limit = int32(pageSize)
offset = int32((pageNumber - 1) * pageSize)
type Int32Convertible int32

return limit, offset
func (i Int32Convertible) ToInt32() (int32, error) {
return int32(i), nil
}
66 changes: 65 additions & 1 deletion calc/calc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,11 @@ func TestCalc(t *testing.T) {
expectedLimit := int32(20)
expectedOffset := int32(40)

limit, offset := c.CalculateLimitAndOffsetInt32(pageNumber, pageSize)
limit, offset, err := c.CalculateLimitAndOffset(pageNumber, pageSize)
if err != nil {
t.Errorf("unexpected error: %v", err)
}

if limit != expectedLimit || offset != expectedOffset {
t.Errorf("expected limit: %d, offset: %d, but got limit: %d, offset: %d", expectedLimit, expectedOffset, limit, offset)
}
Expand All @@ -54,4 +58,64 @@ func TestCalc(t *testing.T) {
t.Errorf("expected error, but got nil")
}
})

t.Run("Error on Negative Page Size", func(t *testing.T) {
pageNumberStr := "1"
pageSizeStr := "-10"

_, _, err := c.CalculateLimitAndOffsetStr(pageNumberStr, pageSizeStr)
if err == nil {
t.Errorf("expected error, but got nil")
}
})

t.Run("Error on Zero Page Number", func(t *testing.T) {
pageNumberStr := "0"
pageSizeStr := "10"

_, _, err := c.CalculateLimitAndOffsetStr(pageNumberStr, pageSizeStr)
if err == nil {
t.Errorf("expected error, but got nil")
}
})

t.Run("Error on Zero Page Size", func(t *testing.T) {
pageNumberStr := "1"
pageSizeStr := "0"

_, _, err := c.CalculateLimitAndOffsetStr(pageNumberStr, pageSizeStr)
if err == nil {
t.Errorf("expected error, but got nil")
}
})

t.Run("Error on Invalid Int32 Inputs", func(t *testing.T) {
pageNumber := int32(-1)
pageSize := int32(10)

_, _, err := c.CalculateLimitAndOffset(pageNumber, pageSize)
if err == nil {
t.Errorf("expected error, but got nil")
}
})

t.Run("Error on Zero Int32 Inputs", func(t *testing.T) {
pageNumber := int32(0)
pageSize := int32(10)

_, _, err := c.CalculateLimitAndOffset(pageNumber, pageSize)
if err == nil {
t.Errorf("expected error, but got nil")
}
})

t.Run("Error on Negative Int32 Inputs", func(t *testing.T) {
pageNumber := int32(-1)
pageSize := int32(10)

_, _, err := c.CalculateLimitAndOffset(pageNumber, pageSize)
if err == nil {
t.Errorf("expected error, but got nil")
}
})
}
1 change: 1 addition & 0 deletions cnpj/cnpj.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,5 +102,6 @@ func (c *CNPJ) TrimCNPJ(cnpj string) string {
cnpj = strings.ReplaceAll(cnpj, ".", "")
cnpj = strings.ReplaceAll(cnpj, "-", "")
cnpj = strings.ReplaceAll(cnpj, "/", "")
cnpj = strings.ReplaceAll(cnpj, " ", "")
return cnpj
}
24 changes: 24 additions & 0 deletions strings/strings.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package strings

import (
"fmt"
"math/rand"
"regexp"
"strings"
"time"
"unicode"

"github.com/google/uuid"
Expand Down Expand Up @@ -148,3 +150,25 @@ func (s *Strings) EscapeString(input string) string {

return builder.String()
}

func (s *Strings) RandomStrE(length int) string {
const charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*()"
var seededRand *rand.Rand = rand.New(rand.NewSource(time.Now().UnixNano()))

b := make([]byte, length)
for i := range b {
b[i] = charset[seededRand.Intn(len(charset))]
}
return string(b)
}

func (s *Strings) RandomStr(length int) string {
var result string
for len(result) < length {
str := s.RandomStrE(length)
re, _ := regexp.Compile(`[^a-zA-Z0-9\s]+`)
str = re.ReplaceAllString(str, "")
result += str
}
return result[:length]
}
36 changes: 33 additions & 3 deletions strings/strings_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package strings

import (
"regexp"
"strings"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -40,7 +41,6 @@ func (suite *StringsSuite) TestToCamelCase() {
}

func (suite *StringsSuite) TestGenerateUniqueSlug() {
strs := New()

tests := []struct {
input string
Expand All @@ -53,11 +53,11 @@ func (suite *StringsSuite) TestGenerateUniqueSlug() {
slugRegex, _ := regexp.Compile("^[a-z0-9]+(-[a-z0-9]+)*-[a-z0-9]{6}$")

for _, test := range tests {
slug := strs.GenerateUniqueSlug(test.input)
slug := suite.str.GenerateUniqueSlug(test.input)
assert.Regexp(suite.T(), slugRegex, slug, "The slug does not match the expected pattern")

// Ensure uniqueness by generating another slug and comparing
anotherSlug := strs.GenerateUniqueSlug(test.input)
anotherSlug := suite.str.GenerateUniqueSlug(test.input)
assert.NotEqual(suite.T(), slug, anotherSlug, "The slugs are not unique")
}
}
Expand Down Expand Up @@ -155,6 +155,36 @@ func (suite *StringsSuite) TestEscapeString() {
}
}

func (suite *StringsSuite) TestRandomStrE() {
length := 10
result := suite.str.RandomStrE(length)
if len(result) != length {
suite.T().Errorf("Expected string of length %d, got %d", length, len(result))
}

// Check if all characters in result are in the allowed charset
charset := "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*()"
for _, char := range result {
if !strings.ContainsRune(charset, char) {
suite.T().Errorf("Character '%c' not in allowed charset", char)
}
}
}

func (suite *StringsSuite) TestRandomStr() {
length := 10
result := suite.str.RandomStr(length)
if len(result) != length {
suite.T().Errorf("Expected string of length %d, got %d", length, len(result))
}

// Check if result contains only alphanumeric characters
re := regexp.MustCompile(`^[a-zA-Z0-9]+$`)
if !re.MatchString(result) {
suite.T().Errorf("Result contains special characters: %s", result)
}
}

func TestStringsSuite(t *testing.T) {
suite.Run(t, new(StringsSuite))
}

0 comments on commit 567ba73

Please sign in to comment.