testing native go plugins
This commit is contained in:
parent
6e107f80f9
commit
49cc689a89
@ -24,9 +24,13 @@ type Plugin interface {
|
|||||||
var namePlugins = map[string]Plugin{}
|
var namePlugins = map[string]Plugin{}
|
||||||
var nameSourceType = map[string]data.SourceType{}
|
var nameSourceType = map[string]data.SourceType{}
|
||||||
|
|
||||||
func RegisterPlugin(plugin Plugin) {
|
func RegisterPlugin(plugin Plugin) error {
|
||||||
name := plugin.Name()
|
name := plugin.Name()
|
||||||
|
|
||||||
|
if _, ok := namePlugins[name]; ok {
|
||||||
|
return errors.New("plugin " + name + " is already registered")
|
||||||
|
}
|
||||||
|
|
||||||
nameSourceType[name] = data.SourceType{
|
nameSourceType[name] = data.SourceType{
|
||||||
Name: name,
|
Name: name,
|
||||||
Regex: plugin.Regex(),
|
Regex: plugin.Regex(),
|
||||||
@ -36,6 +40,7 @@ func RegisterPlugin(plugin Plugin) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
namePlugins[name] = plugin
|
namePlugins[name] = plugin
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func compileSourceType(source data.Source) (data.Source, error) {
|
func compileSourceType(source data.Source) (data.Source, error) {
|
||||||
|
143
internal/plugin/plugin_test.go
Normal file
143
internal/plugin/plugin_test.go
Normal file
@ -0,0 +1,143 @@
|
|||||||
|
package plugin
|
||||||
|
|
||||||
|
import (
|
||||||
|
"regexp"
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"gitea.elara.ws/Hazel/music-kraken/internal/data"
|
||||||
|
)
|
||||||
|
|
||||||
|
func extractNameTest(s string) string {
|
||||||
|
parts := strings.Split(s, "/")
|
||||||
|
lastPart := parts[len(parts)-1]
|
||||||
|
hyphenParts := strings.Split(lastPart, "-")
|
||||||
|
result := strings.Join(hyphenParts[:len(hyphenParts)-1], " ")
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
type MusifyTest struct {
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m MusifyTest) Name() string {
|
||||||
|
return "MusifyTest"
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m MusifyTest) Regex() *regexp.Regexp {
|
||||||
|
return regexp.MustCompile(`(?i)https?://musify\.club/(artist|release|track)/[a-z\-0-9]+`)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m MusifyTest) RegexArtist() *regexp.Regexp {
|
||||||
|
return regexp.MustCompile(`(?i)https?://musify\.club/artist/[a-z\-0-9]+`)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m MusifyTest) RegexAlbum() *regexp.Regexp {
|
||||||
|
return regexp.MustCompile(`(?i)https?://musify\.club/release/[a-z\-0-9]+`)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m MusifyTest) RegexSong() *regexp.Regexp {
|
||||||
|
return regexp.MustCompile(`(?i)https?://musify\.club/track/[a-z\-0-9]+`)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m MusifyTest) FetchSong(source data.Source) (data.Song, error) {
|
||||||
|
return data.Song{
|
||||||
|
Name: extractNameTest(source.Url),
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m MusifyTest) FetchAlbum(source data.Source) (data.Album, error) {
|
||||||
|
return data.Album{
|
||||||
|
Name: extractNameTest(source.Url),
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m MusifyTest) FetchArtist(source data.Source) (data.Artist, error) {
|
||||||
|
return data.Artist{
|
||||||
|
Name: extractNameTest(source.Url),
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m MusifyTest) Search(query string) ([]data.MusicObject, error) {
|
||||||
|
panic("unimplemented")
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestRegister(t *testing.T) {
|
||||||
|
if RegisterPlugin(MusifyTest{}) != nil {
|
||||||
|
t.Errorf(`registering first plugin shouldn't return an error`)
|
||||||
|
}
|
||||||
|
|
||||||
|
if RegisterPlugin(MusifyTest{}) == nil {
|
||||||
|
t.Errorf(`registering same plugin twice should return an error`)
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(namePlugins) != 1 {
|
||||||
|
t.Errorf(`%d plugins where registered`, len(namePlugins))
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(nameSourceType) != 1 {
|
||||||
|
t.Errorf(`%d source types were registered`, len(nameSourceType))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestFetchSong(t *testing.T) {
|
||||||
|
RegisterPlugin(MusifyTest{})
|
||||||
|
|
||||||
|
s, err := Fetch(data.Source{
|
||||||
|
Url: "https://musify.club/track/linkin-park-in-the-end-3058",
|
||||||
|
})
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
song, ok := s.(data.Song)
|
||||||
|
if !ok {
|
||||||
|
t.Errorf(`fetch should have returned song`)
|
||||||
|
}
|
||||||
|
|
||||||
|
if song.UnifiedName != "linkin park in the end" {
|
||||||
|
t.Errorf(`didn't correctly parse the name`)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestFetchAlbum(t *testing.T) {
|
||||||
|
RegisterPlugin(MusifyTest{})
|
||||||
|
|
||||||
|
a, err := Fetch(data.Source{
|
||||||
|
Url: "https://musify.club/release/linkin-park-hybrid-theory-2000-188",
|
||||||
|
})
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
album, ok := a.(data.Album)
|
||||||
|
if !ok {
|
||||||
|
t.Errorf(`fetch should have returned song`)
|
||||||
|
}
|
||||||
|
|
||||||
|
if album.UnifiedName != "linkin park hybrid theory 2000" {
|
||||||
|
t.Errorf(`didn't correctly parse the name`)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestFetchArtist(t *testing.T) {
|
||||||
|
RegisterPlugin(MusifyTest{})
|
||||||
|
|
||||||
|
a, err := Fetch(data.Source{
|
||||||
|
Url: "https://musify.club/artist/linkin-park-5",
|
||||||
|
})
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
artist, ok := a.(data.Artist)
|
||||||
|
if !ok {
|
||||||
|
t.Errorf(`fetch should have returned song`)
|
||||||
|
}
|
||||||
|
|
||||||
|
if artist.UnifiedName != "linkin park" {
|
||||||
|
t.Errorf(`didn't correctly parse the name`)
|
||||||
|
}
|
||||||
|
}
|
9
main.go
9
main.go
@ -11,7 +11,10 @@ func main() {
|
|||||||
fmt.Println("music kraken")
|
fmt.Println("music kraken")
|
||||||
|
|
||||||
plugin.RegisterPlugin(plugin.Musify{})
|
plugin.RegisterPlugin(plugin.Musify{})
|
||||||
fmt.Println(plugin.Fetch(data.Source{
|
|
||||||
Url: "https://musify.club/track/linkin-park-in-the-end-3058",
|
a, _ := plugin.Fetch(data.Source{
|
||||||
}))
|
Url: "https://musify.club/artist/linkin-park-5",
|
||||||
|
})
|
||||||
|
artist := a.(data.Artist)
|
||||||
|
fmt.Println(artist.UnifiedName)
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user