testing for user input
This commit is contained in:
parent
e6e8f7703a
commit
b2a0fb6952
@ -102,13 +102,15 @@ func NewQuery(search string) (Query, error) {
|
||||
if query.Song != "" {
|
||||
elements = append(elements, query.Song)
|
||||
}
|
||||
if query.Album != "" && !(query.Song != "" && query.Album != "") {
|
||||
elements = append(elements, query.Album)
|
||||
}
|
||||
|
||||
if query.Artist != "" {
|
||||
elements = append(elements, query.Artist)
|
||||
}
|
||||
|
||||
if query.Album != "" && len(elements) <= 1 {
|
||||
elements = append(elements, query.Album)
|
||||
}
|
||||
|
||||
query.Search = strings.Join(elements, " - ")
|
||||
}
|
||||
|
||||
|
118
internal/common/user_input_test.go
Normal file
118
internal/common/user_input_test.go
Normal file
@ -0,0 +1,118 @@
|
||||
package common
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestNewQuery(t *testing.T) {
|
||||
tests := []struct {
|
||||
input string
|
||||
expectedQuery Query
|
||||
expectError bool
|
||||
errorMessage string
|
||||
}{
|
||||
{
|
||||
input: "hello",
|
||||
expectedQuery: Query{
|
||||
Search: "hello",
|
||||
},
|
||||
expectError: false,
|
||||
},
|
||||
{
|
||||
input: "#s songName",
|
||||
expectedQuery: Query{
|
||||
Song: "songName",
|
||||
Search: "songName",
|
||||
},
|
||||
expectError: false,
|
||||
},
|
||||
{
|
||||
input: "#r albumName",
|
||||
expectedQuery: Query{
|
||||
Album: "albumName",
|
||||
Search: "albumName",
|
||||
},
|
||||
expectError: false,
|
||||
},
|
||||
{
|
||||
input: "#a artistName",
|
||||
expectedQuery: Query{
|
||||
Artist: "artistName",
|
||||
Search: "artistName",
|
||||
},
|
||||
expectError: false,
|
||||
},
|
||||
{
|
||||
input: "#s songName #r albumName",
|
||||
expectedQuery: Query{
|
||||
Song: "songName",
|
||||
Album: "albumName",
|
||||
Search: "songName - albumName",
|
||||
},
|
||||
expectError: false,
|
||||
},
|
||||
{
|
||||
input: "#a artistName #s songName",
|
||||
expectedQuery: Query{
|
||||
Song: "songName",
|
||||
Artist: "artistName",
|
||||
Search: "songName - artistName",
|
||||
},
|
||||
expectError: false,
|
||||
},
|
||||
{
|
||||
input: "#s songName #a artistName #r albumName",
|
||||
expectedQuery: Query{
|
||||
Song: "songName",
|
||||
Album: "albumName",
|
||||
Artist: "artistName",
|
||||
Search: "songName - artistName",
|
||||
},
|
||||
expectError: false,
|
||||
},
|
||||
{
|
||||
input: " ",
|
||||
expectError: true,
|
||||
errorMessage: "search should not be empty",
|
||||
},
|
||||
{
|
||||
input: "#x invalidType",
|
||||
expectError: true,
|
||||
errorMessage: "the char after # has to be s, r or a if it isn't escaped with another #",
|
||||
},
|
||||
{
|
||||
input: "#s songName #s anotherSong",
|
||||
expectError: true,
|
||||
errorMessage: "you can use #s only once",
|
||||
},
|
||||
{
|
||||
input: "#s songName #r albumName #",
|
||||
expectError: true,
|
||||
errorMessage: "there cant be an unescaped # at the end, it has to be escaped with another #",
|
||||
},
|
||||
{
|
||||
input: "invalidText #r albumName",
|
||||
expectError: true,
|
||||
errorMessage: "if you use advanced search with # you cant put anything before the first #",
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
query, err := NewQuery(test.input)
|
||||
|
||||
if test.expectError {
|
||||
if err == nil {
|
||||
t.Errorf("expected error for input '%s', got none", test.input)
|
||||
} else if err.Error() != test.errorMessage {
|
||||
t.Errorf("expected error message '%s', got '%s'", test.errorMessage, err.Error())
|
||||
}
|
||||
} else {
|
||||
if err != nil {
|
||||
t.Errorf("expected no error for input '%s', got '%v'", test.input, err)
|
||||
}
|
||||
if query != test.expectedQuery {
|
||||
t.Errorf("for input '%s', expected %v, got %v", test.input, test.expectedQuery, query)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user