From cd3113118823f89194f0edf758aae0d9923436ea Mon Sep 17 00:00:00 2001 From: Christopher Speller Date: Fri, 6 Nov 2015 09:46:36 -0500 Subject: Fixing searching for quotations --- model/search_params.go | 30 +++++++++- model/search_params_test.go | 142 +++++++++++++++++++++++++++++++++++++++++++- model/utils.go | 4 +- 3 files changed, 170 insertions(+), 6 deletions(-) (limited to 'model') diff --git a/model/search_params.go b/model/search_params.go index 144e8e461..17a64d980 100644 --- a/model/search_params.go +++ b/model/search_params.go @@ -16,7 +16,7 @@ type SearchParams struct { var searchFlags = [...]string{"from", "channel", "in"} -func splitWords(text string) []string { +func splitWordsNoQuotes(text string) []string { words := []string{} for _, word := range strings.Fields(text) { @@ -31,6 +31,32 @@ func splitWords(text string) []string { return words } +func splitWords(text string) []string { + words := []string{} + + foundQuote := false + location := 0 + for i, char := range text { + if char == '"' { + if foundQuote { + // Grab the quoted section + word := text[location : i+1] + words = append(words, word) + foundQuote = false + location = i + 1 + } else { + words = append(words, splitWordsNoQuotes(text[location:i])...) + foundQuote = true + location = i + } + } + } + + words = append(words, splitWordsNoQuotes(text[location:])...) + + return words +} + func parseSearchFlags(input []string) ([]string, [][2]string) { words := []string{} flags := [][2]string{} @@ -127,7 +153,7 @@ func ParseSearchParams(text string) []*SearchParams { } // special case for when no terms are specified but we still have a filter - if len(plainTerms) == 0 && len(hashtagTerms) == 0 { + if len(plainTerms) == 0 && len(hashtagTerms) == 0 && (len(inChannels) != 0 || len(fromUsers) != 0) { paramsList = append(paramsList, &SearchParams{ Terms: "", IsHashtag: true, diff --git a/model/search_params_test.go b/model/search_params_test.go index e03e82c5a..af4cbe595 100644 --- a/model/search_params_test.go +++ b/model/search_params_test.go @@ -7,11 +7,73 @@ 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.Fatal("got words from empty input") + t.Fatalf("got words from empty input") } else if len(flags) != 0 { - t.Fatal("got flags from empty input") + t.Fatalf("got flags from empty input") } if words, flags := parseSearchFlags(splitWords("word")); len(words) != 1 || words[0] != "word" { @@ -32,6 +94,12 @@ func TestParseSearchFlags(t *testing.T) { 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" { @@ -74,4 +142,74 @@ func TestParseSearchFlags(t *testing.T) { 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 != false || 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 != false || 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 != true || 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 != true || len(sp[1].InChannels) != 0 || len(sp[1].FromUsers) != 0 || sp[0].Terms != "words" || sp[0].IsHashtag != false || 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]) + } } diff --git a/model/utils.go b/model/utils.go index 681ade870..75c982eec 100644 --- a/model/utils.go +++ b/model/utils.go @@ -242,8 +242,8 @@ func Etag(parts ...interface{}) string { } var validHashtag = regexp.MustCompile(`^(#[A-Za-z]+[A-Za-z0-9_\-]*[A-Za-z0-9])$`) -var puncStart = regexp.MustCompile(`^[.,()&$!\[\]{}"':;\\]+`) -var puncEnd = regexp.MustCompile(`[.,()&$#!\[\]{}"';\\]+$`) +var puncStart = regexp.MustCompile(`^[.,()&$!\[\]{}':;\\]+`) +var puncEnd = regexp.MustCompile(`[.,()&$#!\[\]{}';\\]+$`) func ParseHashtags(text string) (string, string) { words := strings.Fields(text) -- cgit v1.2.3-1-g7c22