本站源代码
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

74 lines
1.8KB

  1. // Copyright 2019 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package setting
  5. import (
  6. "testing"
  7. "github.com/stretchr/testify/assert"
  8. )
  9. type indexerMatchList struct {
  10. value string
  11. position int
  12. }
  13. func Test_newIndexerGlobSettings(t *testing.T) {
  14. checkGlobMatch(t, "", []indexerMatchList{})
  15. checkGlobMatch(t, " ", []indexerMatchList{})
  16. checkGlobMatch(t, "data, */data, */data/*, **/data/*, **/data/**", []indexerMatchList{
  17. {"", -1},
  18. {"don't", -1},
  19. {"data", 0},
  20. {"/data", 1},
  21. {"x/data", 1},
  22. {"x/data/y", 2},
  23. {"a/b/c/data/z", 3},
  24. {"a/b/c/data/x/y/z", 4},
  25. })
  26. checkGlobMatch(t, "*.txt, txt, **.txt, **txt, **txt*", []indexerMatchList{
  27. {"my.txt", 0},
  28. {"don't", -1},
  29. {"mytxt", 3},
  30. {"/data/my.txt", 2},
  31. {"data/my.txt", 2},
  32. {"data/txt", 3},
  33. {"data/thistxtfile", 4},
  34. {"/data/thistxtfile", 4},
  35. })
  36. checkGlobMatch(t, "data/**/*.txt, data/**.txt", []indexerMatchList{
  37. {"data/a/b/c/d.txt", 0},
  38. {"data/a.txt", 1},
  39. })
  40. checkGlobMatch(t, "**/*.txt, data/**.txt", []indexerMatchList{
  41. {"data/a/b/c/d.txt", 0},
  42. {"data/a.txt", 0},
  43. {"a.txt", -1},
  44. })
  45. }
  46. func checkGlobMatch(t *testing.T, globstr string, list []indexerMatchList) {
  47. glist := IndexerGlobFromString(globstr)
  48. if len(list) == 0 {
  49. assert.Empty(t, glist)
  50. return
  51. }
  52. assert.NotEmpty(t, glist)
  53. for _, m := range list {
  54. found := false
  55. for pos, g := range glist {
  56. if g.Match(m.value) {
  57. assert.Equal(t, m.position, pos, "Test string `%s` doesn't match `%s`@%d, but matches @%d", m.value, globstr, m.position, pos)
  58. found = true
  59. break
  60. }
  61. }
  62. if !found {
  63. assert.Equal(t, m.position, -1, "Test string `%s` doesn't match `%s` anywhere; expected @%d", m.value, globstr, m.position)
  64. }
  65. }
  66. }
上海开阖软件有限公司 沪ICP备12045867号-1