github.com/wgh-/mattermost-server@v4.8.0-rc2+incompatible/model/search_params_test.go (about) 1 // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved. 2 // See License.txt for license information. 3 4 package model 5 6 import ( 7 "testing" 8 ) 9 10 func TestSplitWords(t *testing.T) { 11 if words := splitWords(""); len(words) != 0 { 12 t.Fatalf("Incorrect output splitWords: %v", words) 13 } 14 15 if words := splitWords(" "); len(words) != 0 { 16 t.Fatalf("Incorrect output splitWords: %v", words) 17 } 18 19 if words := splitWords("word"); len(words) != 1 || words[0] != "word" { 20 t.Fatalf("Incorrect output splitWords: %v", words) 21 } 22 23 if words := splitWords("wo\"rd"); len(words) != 2 || words[0] != "wo" || words[1] != "\"rd" { 24 t.Fatalf("Incorrect output splitWords: %v", words) 25 } 26 27 if words := splitWords("wo\"rd\""); len(words) != 2 || words[0] != "wo" || words[1] != "\"rd\"" { 28 t.Fatalf("Incorrect output splitWords: %v", words) 29 } 30 31 if words := splitWords("word1 word2 word3"); len(words) != 3 || words[0] != "word1" || words[1] != "word2" || words[2] != "word3" { 32 t.Fatalf("Incorrect output splitWords: %v", words) 33 } 34 35 if words := splitWords("word1 \"word2 word3"); len(words) != 3 || words[0] != "word1" || words[1] != "\"word2" || words[2] != "word3" { 36 t.Fatalf("Incorrect output splitWords: %v", words) 37 } 38 39 if words := splitWords("\"word1 word2 word3"); len(words) != 3 || words[0] != "\"word1" || words[1] != "word2" || words[2] != "word3" { 40 t.Fatalf("Incorrect output splitWords: %v", words) 41 } 42 43 if words := splitWords("word1 word2 word3\""); len(words) != 4 || words[0] != "word1" || words[1] != "word2" || words[2] != "word3" || words[3] != "\"" { 44 t.Fatalf("Incorrect output splitWords: %v", words) 45 } 46 47 if words := splitWords("word1 #word2 ##word3"); len(words) != 3 || words[0] != "word1" || words[1] != "#word2" || words[2] != "##word3" { 48 t.Fatalf("Incorrect output splitWords: %v", words) 49 } 50 51 if words := splitWords(" word1 word2 word3 "); len(words) != 3 || words[0] != "word1" || words[1] != "word2" || words[2] != "word3" { 52 t.Fatalf("Incorrect output splitWords: %v", words) 53 } 54 55 if words := splitWords("\"quoted\""); len(words) != 1 || words[0] != "\"quoted\"" { 56 t.Fatalf("Incorrect output splitWords: %v", words) 57 } 58 59 if words := splitWords("\"quoted multiple words\""); len(words) != 1 || words[0] != "\"quoted multiple words\"" { 60 t.Fatalf("Incorrect output splitWords: %v", words) 61 } 62 63 if words := splitWords("some stuff \"quoted multiple words\" more stuff"); len(words) != 5 || words[0] != "some" || words[1] != "stuff" || words[2] != "\"quoted multiple words\"" || words[3] != "more" || words[4] != "stuff" { 64 t.Fatalf("Incorrect output splitWords: %v", words) 65 } 66 67 if words := splitWords("some \"stuff\" \"quoted multiple words\" #some \"more stuff\""); len(words) != 5 || words[0] != "some" || words[1] != "\"stuff\"" || words[2] != "\"quoted multiple words\"" || words[3] != "#some" || words[4] != "\"more stuff\"" { 68 t.Fatalf("Incorrect output splitWords: %v", words) 69 } 70 } 71 72 func TestParseSearchFlags(t *testing.T) { 73 if words, flags := parseSearchFlags(splitWords("")); len(words) != 0 { 74 t.Fatalf("got words from empty input") 75 } else if len(flags) != 0 { 76 t.Fatalf("got flags from empty input") 77 } 78 79 if words, flags := parseSearchFlags(splitWords("word")); len(words) != 1 || words[0] != "word" { 80 t.Fatalf("got incorrect words %v", words) 81 } else if len(flags) != 0 { 82 t.Fatalf("got incorrect flags %v", flags) 83 } 84 85 if words, flags := parseSearchFlags(splitWords("apple banana cherry")); len(words) != 3 || words[0] != "apple" || words[1] != "banana" || words[2] != "cherry" { 86 t.Fatalf("got incorrect words %v", words) 87 } else if len(flags) != 0 { 88 t.Fatalf("got incorrect flags %v", flags) 89 } 90 91 if words, flags := parseSearchFlags(splitWords("apple banana from:chan")); len(words) != 2 || words[0] != "apple" || words[1] != "banana" { 92 t.Fatalf("got incorrect words %v", words) 93 } else if len(flags) != 1 || flags[0][0] != "from" || flags[0][1] != "chan" { 94 t.Fatalf("got incorrect flags %v", flags) 95 } 96 97 if words, flags := parseSearchFlags(splitWords("#apple #banana from:chan")); len(words) != 2 || words[0] != "#apple" || words[1] != "#banana" { 98 t.Fatalf("got incorrect words %v", words) 99 } else if len(flags) != 1 || flags[0][0] != "from" || flags[0][1] != "chan" { 100 t.Fatalf("got incorrect flags %v", flags) 101 } 102 103 if words, flags := parseSearchFlags(splitWords("apple banana from: chan")); len(words) != 2 || words[0] != "apple" || words[1] != "banana" { 104 t.Fatalf("got incorrect words %v", words) 105 } else if len(flags) != 1 || flags[0][0] != "from" || flags[0][1] != "chan" { 106 t.Fatalf("got incorrect flags %v", flags) 107 } 108 109 if words, flags := parseSearchFlags(splitWords("apple banana in: chan")); len(words) != 2 || words[0] != "apple" || words[1] != "banana" { 110 t.Fatalf("got incorrect words %v", words) 111 } else if len(flags) != 1 || flags[0][0] != "in" || flags[0][1] != "chan" { 112 t.Fatalf("got incorrect flags %v", flags) 113 } 114 115 if words, flags := parseSearchFlags(splitWords("apple banana channel:chan")); len(words) != 2 || words[0] != "apple" || words[1] != "banana" { 116 t.Fatalf("got incorrect words %v", words) 117 } else if len(flags) != 1 || flags[0][0] != "channel" || flags[0][1] != "chan" { 118 t.Fatalf("got incorrect flags %v", flags) 119 } 120 121 if words, flags := parseSearchFlags(splitWords("fruit: cherry")); len(words) != 2 || words[0] != "fruit" || words[1] != "cherry" { 122 t.Fatalf("got incorrect words %v", words) 123 } else if len(flags) != 0 { 124 t.Fatalf("got incorrect flags %v", flags) 125 } 126 127 if words, flags := parseSearchFlags(splitWords("channel:")); len(words) != 1 || words[0] != "channel" { 128 t.Fatalf("got incorrect words %v", words) 129 } else if len(flags) != 0 { 130 t.Fatalf("got incorrect flags %v", flags) 131 } 132 133 if words, flags := parseSearchFlags(splitWords("channel: first in: second from:")); len(words) != 1 || words[0] != "from" { 134 t.Fatalf("got incorrect words %v", words) 135 } else if len(flags) != 2 || flags[0][0] != "channel" || flags[0][1] != "first" || flags[1][0] != "in" || flags[1][1] != "second" { 136 t.Fatalf("got incorrect flags %v", flags) 137 } 138 139 if words, flags := parseSearchFlags(splitWords("channel: first channel: second from: third from: fourth")); len(words) != 0 { 140 t.Fatalf("got incorrect words %v", words) 141 } else if len(flags) != 4 || flags[0][0] != "channel" || flags[0][1] != "first" || flags[1][0] != "channel" || flags[1][1] != "second" || 142 flags[2][0] != "from" || flags[2][1] != "third" || flags[3][0] != "from" || flags[3][1] != "fourth" { 143 t.Fatalf("got incorrect flags %v", flags) 144 } 145 146 if words, flags := parseSearchFlags(splitWords("\"quoted\"")); len(words) != 1 || words[0] != "\"quoted\"" { 147 t.Fatalf("got incorrect words %v", words) 148 } else if len(flags) != 0 { 149 t.Fatalf("got incorrect flags %v", flags) 150 } 151 152 if words, flags := parseSearchFlags(splitWords("\"quoted multiple words\"")); len(words) != 1 || words[0] != "\"quoted multiple words\"" { 153 t.Fatalf("got incorrect words %v", words) 154 } else if len(flags) != 0 { 155 t.Fatalf("got incorrect flags %v", flags) 156 } 157 158 if words, flags := parseSearchFlags(splitWords("some \"stuff\" \"quoted multiple words\" some \"more stuff\"")); len(words) != 5 || words[0] != "some" || words[1] != "\"stuff\"" || words[2] != "\"quoted multiple words\"" || words[3] != "some" || words[4] != "\"more stuff\"" { 159 t.Fatalf("Incorrect output splitWords: %v", words) 160 } else if len(flags) != 0 { 161 t.Fatalf("got incorrect flags %v", flags) 162 } 163 164 if words, flags := parseSearchFlags(splitWords("some in:here \"stuff\" \"quoted multiple words\" from:someone \"more stuff\"")); len(words) != 4 || words[0] != "some" || words[1] != "\"stuff\"" || words[2] != "\"quoted multiple words\"" || words[3] != "\"more stuff\"" { 165 t.Fatalf("Incorrect output splitWords: %v", words) 166 } else if len(flags) != 2 || flags[0][0] != "in" || flags[0][1] != "here" || flags[1][0] != "from" || flags[1][1] != "someone" { 167 t.Fatalf("got incorrect flags %v", flags) 168 } 169 } 170 171 func TestParseSearchParams(t *testing.T) { 172 if sp := ParseSearchParams(""); len(sp) != 0 { 173 t.Fatalf("Incorrect output from parse search params: %v", sp) 174 } 175 176 if sp := ParseSearchParams(" "); len(sp) != 0 { 177 t.Fatalf("Incorrect output from parse search params: %v", sp) 178 } 179 180 if sp := ParseSearchParams("words words"); len(sp) != 1 || sp[0].Terms != "words words" || sp[0].IsHashtag || len(sp[0].InChannels) != 0 || len(sp[0].FromUsers) != 0 { 181 t.Fatalf("Incorrect output from parse search params: %v", sp) 182 } 183 184 if sp := ParseSearchParams("\"my stuff\""); len(sp) != 1 || sp[0].Terms != "\"my stuff\"" || sp[0].IsHashtag || len(sp[0].InChannels) != 0 || len(sp[0].FromUsers) != 0 { 185 t.Fatalf("Incorrect output from parse search params: %v", sp) 186 } 187 188 if sp := ParseSearchParams("#words #words"); len(sp) != 1 || sp[0].Terms != "#words #words" || !sp[0].IsHashtag || len(sp[0].InChannels) != 0 || len(sp[0].FromUsers) != 0 { 189 t.Fatalf("Incorrect output from parse search params: %v", sp) 190 } 191 192 if sp := ParseSearchParams("#words words"); len(sp) != 2 || sp[1].Terms != "#words" || !sp[1].IsHashtag || len(sp[1].InChannels) != 0 || len(sp[1].FromUsers) != 0 || sp[0].Terms != "words" || sp[0].IsHashtag || len(sp[0].InChannels) != 0 { 193 t.Fatalf("Incorrect output from parse search params: %v", sp) 194 } 195 196 if sp := ParseSearchParams("in:channel"); len(sp) != 1 || sp[0].Terms != "" || len(sp[0].InChannels) != 1 || sp[0].InChannels[0] != "channel" || len(sp[0].FromUsers) != 0 { 197 t.Fatalf("Incorrect output from parse search params: %v", sp) 198 } 199 200 if sp := ParseSearchParams("testing in:channel"); len(sp) != 1 || sp[0].Terms != "testing" || len(sp[0].InChannels) != 1 || sp[0].InChannels[0] != "channel" || len(sp[0].FromUsers) != 0 { 201 t.Fatalf("Incorrect output from parse search params: %v", sp) 202 } 203 204 if sp := ParseSearchParams("in:channel testing"); len(sp) != 1 || sp[0].Terms != "testing" || len(sp[0].InChannels) != 1 || sp[0].InChannels[0] != "channel" || len(sp[0].FromUsers) != 0 { 205 t.Fatalf("Incorrect output from parse search params: %v", sp) 206 } 207 208 if sp := ParseSearchParams("in:channel in:otherchannel"); len(sp) != 1 || sp[0].Terms != "" || len(sp[0].InChannels) != 2 || sp[0].InChannels[0] != "channel" || sp[0].InChannels[1] != "otherchannel" || len(sp[0].FromUsers) != 0 { 209 t.Fatalf("Incorrect output from parse search params: %v", sp) 210 } 211 212 if sp := ParseSearchParams("testing in:channel from:someone"); len(sp) != 1 || sp[0].Terms != "testing" || len(sp[0].InChannels) != 1 || sp[0].InChannels[0] != "channel" || len(sp[0].FromUsers) != 1 || sp[0].FromUsers[0] != "someone" { 213 t.Fatalf("Incorrect output from parse search params: %v", sp[0]) 214 } 215 216 if sp := ParseSearchParams("##hashtag +#plus+"); len(sp) != 1 || sp[0].Terms != "#hashtag #plus" || !sp[0].IsHashtag || len(sp[0].InChannels) != 0 || len(sp[0].FromUsers) != 0 { 217 t.Fatalf("Incorrect output from parse search params: %v", sp[0]) 218 } 219 220 if sp := ParseSearchParams("wildcar*"); len(sp) != 1 || sp[0].Terms != "wildcar*" || sp[0].IsHashtag || len(sp[0].InChannels) != 0 || len(sp[0].FromUsers) != 0 { 221 t.Fatalf("Incorrect output from parse search params: %v", sp[0]) 222 } 223 }