search_test.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. //
  2. // Copyright (C) 2018-2018 Marcus Rohrmoser, http://purl.mro.name/ShaarliGo
  3. //
  4. // This program is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. //
  17. package main
  18. import (
  19. "golang.org/x/text/language"
  20. "golang.org/x/text/search"
  21. "strings"
  22. "github.com/stretchr/testify/assert"
  23. "testing"
  24. )
  25. func entry(title, content, tags string) *Entry {
  26. cs := strings.Fields(tags)
  27. cat := make([]Category, len(cs))
  28. for idx, txt := range cs {
  29. if strings.HasPrefix(txt, "#") {
  30. txt = txt[1:]
  31. }
  32. cat[idx].Term = txt
  33. }
  34. return &Entry{
  35. Title: HumanText{Body: title},
  36. Content: &HumanText{Body: content},
  37. Categories: cat,
  38. }
  39. }
  40. func TestRankEntryTerms(t *testing.T) {
  41. t.Parallel()
  42. lang := language.Make("DE-LU")
  43. assert.Equal(t, "de-LU", lang.String(), "aha")
  44. matcher := search.New(language.German, search.IgnoreDiacritics, search.IgnoreCase)
  45. assert.Equal(t, 0, rankEntryTerms(nil, nil, nil), "soso")
  46. assert.Equal(t, 0, rankEntryTerms(&Entry{}, nil, nil), "soso")
  47. assert.Equal(t, 2, rankEntryTerms(&Entry{Title: HumanText{Body: "my foo bar"}}, []string{"foo"}, matcher), "soso")
  48. assert.Equal(t, 2, rankEntryTerms(entry("my foo bar", "", ""), []string{"fòO"}, matcher), "ignores Diacritics")
  49. assert.Equal(t, 5, rankEntryTerms(entry("my foo bar", "", "#barfoobaz"), []string{"#fòO"}, matcher), "matches tag substrings")
  50. }
  51. //