119 lines
		
	
	
		
			2.6 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			119 lines
		
	
	
		
			2.6 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
| 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)
 | |
| 			}
 | |
| 		}
 | |
| 	}
 | |
| }
 |