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