Skip to content

Commit

Permalink
test(utils): Add tests for slugify function
Browse files Browse the repository at this point in the history
  • Loading branch information
alexstojda committed Jul 27, 2023
1 parent 1ec6aec commit 75c8765
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 4 deletions.
9 changes: 5 additions & 4 deletions internal/utils/slugify.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,18 +42,19 @@ var (

// Slugify function returns slugifies string "s"
func Slugify(s string, maxLength ...int) string {
if len(maxLength) > 0 && maxLength[0] > 0 && len(s) > maxLength[0] {
s = s[:maxLength[0]]
}

for _, r := range rExps {
s = r.re.ReplaceAllString(s, r.ch)
}

s = strings.ToLower(s)
s = strings.TrimSpace(s)
s = spacereg.ReplaceAllString(s, "-")
s = noncharreg.ReplaceAllString(s, "")
s = minusrepeatreg.ReplaceAllString(s, "-")

if len(maxLength) > 0 && maxLength[0] > 0 && len(s) > maxLength[0] {
s = s[:maxLength[0]]
}

return s
}
31 changes: 31 additions & 0 deletions internal/utils/slugify_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package utils_test

import (
"github.com/onsi/ginkgo/v2"
"github.com/onsi/gomega"
"pinman/internal/utils"
)

var _ = ginkgo.Describe("Slugify", func() {
ginkgo.Context("when it receives a string to be slugified", func() {
ginkgo.It("should return a slugified string", func() {
value := "This is a string to be slugified"
expected := "this-is-a-string-to-be-slugified"
gomega.Expect(utils.Slugify(value)).To(gomega.Equal(expected))
})
})
ginkgo.Context("when it receives a string with special characters to be slugified", func() {
ginkgo.It("should return a slugified string", func() {
value := "This is a string to be slugified with special characters: áéíóúñ"
expected := "this-is-a-string-to-be-slugified-with-special-characters-aeioun"
gomega.Expect(utils.Slugify(value)).To(gomega.Equal(expected))
})
})
ginkgo.Context("when it receives a string to be slugified and a max length", func() {
ginkgo.It("should return a slugified string with the max length", func() {
value := "This is a string to be slugified"
expected := "this-is-a-string"
gomega.Expect(utils.Slugify(value, 17)).To(gomega.Equal(expected))
})
})
})

0 comments on commit 75c8765

Please sign in to comment.