summaryrefslogtreecommitdiffstats
path: root/model/search_params_test.go
blob: 3de4e0b523b70d745ee400d868939940b12bb63b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.

package model

import (
	"testing"
)

func TestSplitWords(t *testing.T) {
	if words := splitWords(""); len(words) != 0 {
		t.Fatalf("Incorrect output splitWords: %v", words)
	}

	if words := splitWords("   "); len(words) != 0 {
		t.Fatalf("Incorrect output splitWords: %v", words)
	}

	if words := splitWords("word"); len(words) != 1 || words[0] != "word" {
		t.Fatalf("Incorrect output splitWords: %v", words)
	}

	if words := splitWords("wo\"rd"); len(words) != 2 || words[0] != "wo" || words[1] != "\"rd" {
		t.Fatalf("Incorrect output splitWords: %v", words)
	}

	if words := splitWords("wo\"rd\""); len(words) != 2 || words[0] != "wo" || words[1] != "\"rd\"" {
		t.Fatalf("Incorrect output splitWords: %v", words)
	}

	if words := splitWords("word1 word2 word3"); len(words) != 3 || words[0] != "word1" || words[1] != "word2" || words[2] != "word3" {
		t.Fatalf("Incorrect output splitWords: %v", words)
	}

	if words := splitWords("word1 \"word2 word3"); len(words) != 3 || words[0] != "word1" || words[1] != "\"word2" || words[2] != "word3" {
		t.Fatalf("Incorrect output splitWords: %v", words)
	}

	if words := splitWords("\"word1 word2 word3"); len(words) != 3 || words[0] != "\"word1" || words[1] != "word2" || words[2] != "word3" {
		t.Fatalf("Incorrect output splitWords: %v", words)
	}

	if words := splitWords("word1 word2 word3\""); len(words) != 4 || words[0] != "word1" || words[1] != "word2" || words[2] != "word3" || words[3] != "\"" {
		t.Fatalf("Incorrect output splitWords: %v", words)
	}

	if words := splitWords("word1 #word2 ##word3"); len(words) != 3 || words[0] != "word1" || words[1] != "#word2" || words[2] != "##word3" {
		t.Fatalf("Incorrect output splitWords: %v", words)
	}

	if words := splitWords("    word1 word2     word3  "); len(words) != 3 || words[0] != "word1" || words[1] != "word2" || words[2] != "word3" {
		t.Fatalf("Incorrect output splitWords: %v", words)
	}

	if words := splitWords("\"quoted\""); len(words) != 1 || words[0] != "\"quoted\"" {
		t.Fatalf("Incorrect output splitWords: %v", words)
	}

	if words := splitWords("\"quoted multiple words\""); len(words) != 1 || words[0] != "\"quoted multiple words\"" {
		t.Fatalf("Incorrect output splitWords: %v", words)
	}

	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" {
		t.Fatalf("Incorrect output splitWords: %v", words)
	}

	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\"" {
		t.Fatalf("Incorrect output splitWords: %v", words)
	}
}

func TestParseSearchFlags(t *testing.T) {
	if words, flags := parseSearchFlags(splitWords("")); len(words) != 0 {
		t.Fatalf("got words from empty input")
	} else if len(flags) != 0 {
		t.Fatalf("got flags from empty input")
	}

	if words, flags := parseSearchFlags(splitWords("word")); len(words) != 1 || words[0] != "word" {
		t.Fatalf("got incorrect words %v", words)
	} else if len(flags) != 0 {
		t.Fatalf("got incorrect flags %v", flags)
	}

	if words, flags := parseSearchFlags(splitWords("apple banana cherry")); len(words) != 3 || words[0] != "apple" || words[1] != "banana" || words[2] != "cherry" {
		t.Fatalf("got incorrect words %v", words)
	} else if len(flags) != 0 {
		t.Fatalf("got incorrect flags %v", flags)
	}

	if words, flags := parseSearchFlags(splitWords("apple banana from:chan")); len(words) != 2 || words[0] != "apple" || words[1] != "banana" {
		t.Fatalf("got incorrect words %v", words)
	} else if len(flags) != 1 || flags[0][0] != "from" || flags[0][1] != "chan" {
		t.Fatalf("got incorrect flags %v", flags)
	}

	if words, flags := parseSearchFlags(splitWords("#apple #banana from:chan")); len(words) != 2 || words[0] != "#apple" || words[1] != "#banana" {
		t.Fatalf("got incorrect words %v", words)
	} else if len(flags) != 1 || flags[0][0] != "from" || flags[0][1] != "chan" {
		t.Fatalf("got incorrect flags %v", flags)
	}

	if words, flags := parseSearchFlags(splitWords("apple banana from: chan")); len(words) != 2 || words[0] != "apple" || words[1] != "banana" {
		t.Fatalf("got incorrect words %v", words)
	} else if len(flags) != 1 || flags[0][0] != "from" || flags[0][1] != "chan" {
		t.Fatalf("got incorrect flags %v", flags)
	}

	if words, flags := parseSearchFlags(splitWords("apple banana in: chan")); len(words) != 2 || words[0] != "apple" || words[1] != "banana" {
		t.Fatalf("got incorrect words %v", words)
	} else if len(flags) != 1 || flags[0][0] != "in" || flags[0][1] != "chan" {
		t.Fatalf("got incorrect flags %v", flags)
	}

	if words, flags := parseSearchFlags(splitWords("apple banana channel:chan")); len(words) != 2 || words[0] != "apple" || words[1] != "banana" {
		t.Fatalf("got incorrect words %v", words)
	} else if len(flags) != 1 || flags[0][0] != "channel" || flags[0][1] != "chan" {
		t.Fatalf("got incorrect flags %v", flags)
	}

	if words, flags := parseSearchFlags(splitWords("fruit: cherry")); len(words) != 2 || words[0] != "fruit" || words[1] != "cherry" {
		t.Fatalf("got incorrect words %v", words)
	} else if len(flags) != 0 {
		t.Fatalf("got incorrect flags %v", flags)
	}

	if words, flags := parseSearchFlags(splitWords("channel:")); len(words) != 1 || words[0] != "channel" {
		t.Fatalf("got incorrect words %v", words)
	} else if len(flags) != 0 {
		t.Fatalf("got incorrect flags %v", flags)
	}

	if words, flags := parseSearchFlags(splitWords("channel: first in: second from:")); len(words) != 1 || words[0] != "from" {
		t.Fatalf("got incorrect words %v", words)
	} else if len(flags) != 2 || flags[0][0] != "channel" || flags[0][1] != "first" || flags[1][0] != "in" || flags[1][1] != "second" {
		t.Fatalf("got incorrect flags %v", flags)
	}

	if words, flags := parseSearchFlags(splitWords("channel: first channel: second from: third from: fourth")); len(words) != 0 {
		t.Fatalf("got incorrect words %v", words)
	} else if len(flags) != 4 || flags[0][0] != "channel" || flags[0][1] != "first" || flags[1][0] != "channel" || flags[1][1] != "second" ||
		flags[2][0] != "from" || flags[2][1] != "third" || flags[3][0] != "from" || flags[3][1] != "fourth" {
		t.Fatalf("got incorrect flags %v", flags)
	}

	if words, flags := parseSearchFlags(splitWords("\"quoted\"")); len(words) != 1 || words[0] != "\"quoted\"" {
		t.Fatalf("got incorrect words %v", words)
	} else if len(flags) != 0 {
		t.Fatalf("got incorrect flags %v", flags)
	}

	if words, flags := parseSearchFlags(splitWords("\"quoted multiple words\"")); len(words) != 1 || words[0] != "\"quoted multiple words\"" {
		t.Fatalf("got incorrect words %v", words)
	} else if len(flags) != 0 {
		t.Fatalf("got incorrect flags %v", flags)
	}

	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\"" {
		t.Fatalf("Incorrect output splitWords: %v", words)
	} else if len(flags) != 0 {
		t.Fatalf("got incorrect flags %v", flags)
	}

	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\"" {
		t.Fatalf("Incorrect output splitWords: %v", words)
	} else if len(flags) != 2 || flags[0][0] != "in" || flags[0][1] != "here" || flags[1][0] != "from" || flags[1][1] != "someone" {
		t.Fatalf("got incorrect flags %v", flags)
	}
}

func TestParseSearchParams(t *testing.T) {
	if sp := ParseSearchParams(""); len(sp) != 0 {
		t.Fatalf("Incorrect output from parse search params: %v", sp)
	}

	if sp := ParseSearchParams("     "); len(sp) != 0 {
		t.Fatalf("Incorrect output from parse search params: %v", sp)
	}

	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 {
		t.Fatalf("Incorrect output from parse search params: %v", sp)
	}

	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 {
		t.Fatalf("Incorrect output from parse search params: %v", sp)
	}

	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 {
		t.Fatalf("Incorrect output from parse search params: %v", sp)
	}

	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 {
		t.Fatalf("Incorrect output from parse search params: %v", sp)
	}

	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 {
		t.Fatalf("Incorrect output from parse search params: %v", sp)
	}

	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 {
		t.Fatalf("Incorrect output from parse search params: %v", sp)
	}

	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 {
		t.Fatalf("Incorrect output from parse search params: %v", sp)
	}

	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 {
		t.Fatalf("Incorrect output from parse search params: %v", sp)
	}

	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" {
		t.Fatalf("Incorrect output from parse search params: %v", sp[0])
	}

	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 {
		t.Fatalf("Incorrect output from parse search params: %v", sp[0])
	}

	if sp := ParseSearchParams("wildcar*"); len(sp) != 1 || sp[0].Terms != "wildcar*" || sp[0].IsHashtag || len(sp[0].InChannels) != 0 || len(sp[0].FromUsers) != 0 {
		t.Fatalf("Incorrect output from parse search params: %v", sp[0])
	}
}