Skip to content

Commit

Permalink
tests: cover utils funcs
Browse files Browse the repository at this point in the history
  • Loading branch information
izn committed Jun 9, 2024
1 parent c8b63f3 commit 3f77eae
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 5 deletions.
6 changes: 3 additions & 3 deletions genius.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ func FetchLyrics(track Track) (string, error) {
}

func buildGeniusURL(track Track) string {
artist := cleanString(track.Artist)
title := cleanString(track.Title)
artist := CleanString(track.Artist)
title := CleanString(track.Title)

artistSlug := strings.ReplaceAll(artist, " ", "-")
titleSlug := strings.ReplaceAll(title, " ", "-")
Expand All @@ -57,7 +57,7 @@ func extractLyrics(rawHtml string) (string, error) {
lyrics := matches[1]
lyrics = html.UnescapeString(lyrics)
lyrics = strings.ReplaceAll(lyrics, "<br/>", "\n")
lyrics = removeHTMLTags(lyrics)
lyrics = RemoveHTMLTags(lyrics)

return lyrics, nil
}
4 changes: 2 additions & 2 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"strings"
)

func cleanString(s string) string {
func CleanString(s string) string {
reg := regexp.MustCompile(`[^\w\s-]`)
s = reg.ReplaceAllString(s, "")

Expand All @@ -14,7 +14,7 @@ func cleanString(s string) string {
return s
}

func removeHTMLTags(htmlString string) string {
func RemoveHTMLTags(htmlString string) string {
re := regexp.MustCompile(`<[^>]*>`)
return re.ReplaceAllString(htmlString, "")
}
42 changes: 42 additions & 0 deletions utils_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package main

import (
"testing"
)

func TestCleanString(t *testing.T) {
testCases := []struct {
input string
expected string
}{
{"Hello, World!", "Hello World"},
{"Hello-World123!", "Hello-World123"},
{"Special@Characters", "SpecialCharacters"},
{" Trimmed Spaces ", "Trimmed Spaces"},
}

for _, tc := range testCases {
result := CleanString(tc.input)
if result != tc.expected {
t.Errorf("expected '%s', got '%s'", tc.expected, result)
}
}
}

func TestRemoveHTMLTags(t *testing.T) {
testCases := []struct {
input string
expected string
}{
{"<p>Hello, <b>World</b>!</p>", "Hello, World!"},
{"<div><p>Test</p></div>", "Test"},
{"<h1>Title</h1><p>Content</p>", "TitleContent"},
}

for _, tc := range testCases {
result := RemoveHTMLTags(tc.input)
if result != tc.expected {
t.Errorf("expected '%s', got '%s'", tc.expected, result)
}
}
}

0 comments on commit 3f77eae

Please sign in to comment.