Skip to content

Commit

Permalink
add test api_sort_animes
Browse files Browse the repository at this point in the history
  • Loading branch information
alvarorichard committed Jul 12, 2024
1 parent 0057eaf commit 95be5bd
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 1 deletion.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/alvarorichard/Goanime

go 1.22.3
go 1.22.4

require (
github.com/PuerkitoBio/goquery v1.9.2
Expand Down
92 changes: 92 additions & 0 deletions test/util/api_sort_animes_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package util_test

import (
"sort"
"testing"

"github.com/alvarorichard/Goanime/internal/api"
"github.com/stretchr/testify/assert"
)

// sortAnimes is a helper function to sort animes
func sortAnime(animeList []api.Anime) []api.Anime {
sort.Slice(animeList, func(i, j int) bool {
return animeList[i].Name < animeList[j].Name
})
return animeList
}

func TestSortAnimes(t *testing.T) {
tests := []struct {
name string
input []api.Anime
expected []api.Anime
}{
{
name: "Basic sorting",
input: []api.Anime{
{Name: "Naruto"},
{Name: "Bleach"},
{Name: "One Piece"},
{Name: "Dragon Ball"},
},
expected: []api.Anime{
{Name: "Bleach"},
{Name: "Dragon Ball"},
{Name: "Naruto"},
{Name: "One Piece"},
},
},
{
name: "Already sorted",
input: []api.Anime{
{Name: "Bleach"},
{Name: "Dragon Ball"},
{Name: "Naruto"},
{Name: "One Piece"},
},
expected: []api.Anime{
{Name: "Bleach"},
{Name: "Dragon Ball"},
{Name: "Naruto"},
{Name: "One Piece"},
},
},
{
name: "Reverse order",
input: []api.Anime{
{Name: "One Piece"},
{Name: "Naruto"},
{Name: "Dragon Ball"},
{Name: "Bleach"},
},
expected: []api.Anime{
{Name: "Bleach"},
{Name: "Dragon Ball"},
{Name: "Naruto"},
{Name: "One Piece"},
},
},
{
name: "Single element",
input: []api.Anime{
{Name: "Naruto"},
},
expected: []api.Anime{
{Name: "Naruto"},
},
},
{
name: "Empty list",
input: []api.Anime{},
expected: []api.Anime{},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := sortAnimes(tt.input)
assert.Equal(t, tt.expected, result)
})
}
}

0 comments on commit 95be5bd

Please sign in to comment.