You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

indexer_test.go 1.7KB

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