Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
wxiaoguang committed Feb 14, 2025
1 parent fca84ac commit d51d736
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 12 deletions.
2 changes: 1 addition & 1 deletion modules/indexer/code/internal/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func FilenameMatchIndexPos(content string) (int, int) {
}

func ParseKeywordAsPhrase(keyword string) (string, bool) {
if strings.HasPrefix(keyword, `"`) && strings.HasSuffix(keyword, `"`) {
if strings.HasPrefix(keyword, `"`) && strings.HasSuffix(keyword, `"`) && len(keyword) > 1 {
// only remove the prefix and suffix quotes, no need to decode the content at the moment
return keyword[1 : len(keyword)-1], true
}
Expand Down
28 changes: 17 additions & 11 deletions modules/indexer/code/internal/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,21 @@ import (
)

func TestParseKeywordAsPhrase(t *testing.T) {
phrase, isPhrase := ParseKeywordAsPhrase(`a`)
assert.Empty(t, phrase)
assert.False(t, isPhrase)

phrase, isPhrase = ParseKeywordAsPhrase(`"a"`)
assert.Equal(t, "a", phrase)
assert.True(t, isPhrase)

phrase, isPhrase = ParseKeywordAsPhrase(`""\"""`)
assert.Equal(t, `"\""`, phrase)
assert.True(t, isPhrase)
cases := []struct {
keyword string
phrase string
isPhrase bool
}{
{``, "", false},
{`a`, "", false},
{`"`, "", false},
{`"a`, "", false},
{`"a"`, "a", true},
{`""\"""`, `"\""`, true},
}
for _, c := range cases {
phrase, isPhrase := ParseKeywordAsPhrase(c.keyword)
assert.Equal(t, c.phrase, phrase, "keyword=%q", c.keyword)
assert.Equal(t, c.isPhrase, isPhrase, "keyword=%q", c.keyword)
}
}

0 comments on commit d51d736

Please sign in to comment.