From acd54d8fcc1ca1ecc55acef2f73b6367798d8348 Mon Sep 17 00:00:00 2001 From: KitsuneSemCalda Date: Fri, 29 Mar 2024 10:39:00 -0300 Subject: [PATCH] feat: adding some auxiliar structures and some tests from here --- Structure/Anime.go | 61 ++++++++++++++++++++++ Structure/Episode.go | 51 +++++++++++++++++++ tests/structure/anime_test.go | 34 +++++++++++++ tests/structure/episode_test.go | 90 +++++++++++++++++++++++++++++++++ 4 files changed, 236 insertions(+) create mode 100644 Structure/Anime.go create mode 100644 Structure/Episode.go create mode 100644 tests/structure/anime_test.go create mode 100644 tests/structure/episode_test.go diff --git a/Structure/Anime.go b/Structure/Anime.go new file mode 100644 index 0000000..f2ee2f1 --- /dev/null +++ b/Structure/Anime.go @@ -0,0 +1,61 @@ +package structure + +import "errors" + +type Anime struct { + Name string + Url string + Episodes []Episode +} + +func NewAnime(name string, url string) (*Anime, error) { + if name == "" { + return nil, errors.New("Anime name cannot be empty") + } + + if url == "" { + return nil, errors.New("Anime URL cannot be empty") + } + + return &Anime{ + Name: name, + Url: url, + Episodes: make([]Episode, 0), + }, nil +} + +func (a *Anime) AddEpisode(episode Episode) error { + if !IsValidEpisode(episode) { + return errors.New("invalid episode") + } + + a.Episodes = append(a.Episodes, episode) + return nil +} + +func (a *Anime) GetAnimeName() (*string, error) { + if a == nil { + return nil, errors.New("Anime is nil") + } + + return &a.Name, nil +} + +func (a *Anime) GetAnimeUrl() (*string, error) { + if a == nil { + return nil, errors.New("Anime is nil") + } + return &a.Url, nil +} + +func (a *Anime) GetAnimeEpisodes() (*[]Episode, error) { + + if a == (*Anime)(nil) { + return nil, errors.New("Anime is nil") + } + + episodes := make([]Episode, len(a.Episodes)) + copy(episodes, a.Episodes) + + return &episodes, nil +} diff --git a/Structure/Episode.go b/Structure/Episode.go new file mode 100644 index 0000000..d234738 --- /dev/null +++ b/Structure/Episode.go @@ -0,0 +1,51 @@ +package structure + +import "errors" + +type Episode struct { + Number int + Url string +} + +func IsValidEpisode(episode Episode) bool { + if episode.Number <= 0 { + return false + } + + if episode.Url == "" { + return false + } + + return true +} + +func NewEpisode(number int, url string) (*Episode, error) { + if number <= 0 { + return nil, errors.New("Episode number must be greater than 0") + } + + if url == "" { + return nil, errors.New("Episode URL cannot be empty") + } + + return &Episode{ + Number: number, + Url: url, + }, nil +} + +func (e *Episode) GetEpisodeNumber() (*int, error) { + if e == nil { + return nil, errors.New("Episode is nil") + } + + return &e.Number, nil +} + +func (e *Episode) GetEpisodeUrl() (*string, error) { + if e == nil { + return nil, errors.New("Episode is nil") + } + + return &e.Url, nil +} diff --git a/tests/structure/anime_test.go b/tests/structure/anime_test.go new file mode 100644 index 0000000..f6ccb52 --- /dev/null +++ b/tests/structure/anime_test.go @@ -0,0 +1,34 @@ +package structure + +import ( + structure "Animatic/Structure" + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestAnime(t *testing.T) { + t.Run("valid anime", func(t *testing.T) { + anime := structure.Anime{ + Name: "Example Anime", + Url: "https://example.com/anime/1", + Episodes: []structure.Episode{ + {Number: 1, Url: "https://example.com/anime/1/episode/1"}, + }, + } + + assert.Equal(t, "Example Anime", anime.Name) + assert.Equal(t, "https://example.com/anime/1", anime.Url) + assert.Equal(t, 1, len(anime.Episodes)) + assert.Equal(t, 1, anime.Episodes[0].Number) + assert.Equal(t, "https://example.com/anime/1/episode/1", anime.Episodes[0].Url) + }) + + t.Run("empty anime", func(t *testing.T) { + anime := structure.Anime{} + + assert.Empty(t, anime.Name) + assert.Empty(t, anime.Url) + assert.Empty(t, anime.Episodes) + }) +} diff --git a/tests/structure/episode_test.go b/tests/structure/episode_test.go new file mode 100644 index 0000000..b950f21 --- /dev/null +++ b/tests/structure/episode_test.go @@ -0,0 +1,90 @@ +package structure + +import ( + structure "Animatic/Structure" + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestEpisode(t *testing.T) { + t.Run("valid episode", func(t *testing.T) { + episode := structure.Episode{ + Number: 1, + Url: "https://example.com/episode/1", + } + + assert.Equal(t, 1, episode.Number) + assert.Equal(t, "https://example.com/episode/1", episode.Url) + }) + + t.Run("empty episode", func(t *testing.T) { + episode := structure.Episode{} + + assert.Zero(t, episode.Number) + assert.Empty(t, episode.Url) + }) +} +func TestIsValidEpisode(t *testing.T) { + t.Run("valid episode", func(t *testing.T) { + episode := structure.Episode{ + Number: 1, + Url: "https://example.com/episode/1", + } + + assert.True(t, structure.IsValidEpisode(episode)) + }) + + t.Run("invalid episode with zero number", func(t *testing.T) { + episode := structure.Episode{ + Number: 0, + Url: "https://example.com/episode/1", + } + + assert.False(t, structure.IsValidEpisode(episode)) + }) + + t.Run("invalid episode with empty url", func(t *testing.T) { + episode := structure.Episode{ + Number: 1, + Url: "", + } + + assert.False(t, structure.IsValidEpisode(episode)) + }) +} +func TestEpisodeAdditional(t *testing.T) { + + t.Run("episode with negative number", func(t *testing.T) { + episode := structure.Episode{ + Number: -1, + Url: "https://example.com/episode/-1", + } + + assert.False(t, structure.IsValidEpisode(episode)) + }) + + t.Run("episode with extremely large number", func(t *testing.T) { + episode := structure.Episode{ + Number: 999999, + Url: "https://example.com/episode/999999", + } + + assert.True(t, structure.IsValidEpisode(episode)) + }) + + t.Run("episode with invalid url", func(t *testing.T) { + episode := structure.Episode{ + Number: 1, + Url: "invalidurl", + } + + assert.False(t, structure.IsValidEpisode(episode)) + }) + + t.Run("episode with empty number and url", func(t *testing.T) { + episode := structure.Episode{} + + assert.False(t, structure.IsValidEpisode(episode)) + }) +}