diff --git a/internal/common/user_input.go b/internal/common/user_input.go index 35b5d42..a8e41c2 100644 --- a/internal/common/user_input.go +++ b/internal/common/user_input.go @@ -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, " - ") } diff --git a/internal/common/user_input_test.go b/internal/common/user_input_test.go new file mode 100644 index 0000000..78b90c2 --- /dev/null +++ b/internal/common/user_input_test.go @@ -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) + } + } + } +}