-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: adding some auxiliar structures and some tests from here
- Loading branch information
1 parent
ea53717
commit acd54d8
Showing
4 changed files
with
236 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)) | ||
}) | ||
} |