github.com/gitbundle/modules@v0.0.0-20231025071548-85b91c5c3b01/setting/indexer_test.go (about)

     1  // Copyright 2023 The GitBundle Inc. All rights reserved.
     2  // Copyright 2017 The Gitea Authors. All rights reserved.
     3  // Use of this source code is governed by a MIT-style
     4  // license that can be found in the LICENSE file.
     5  
     6  package setting
     7  
     8  import (
     9  	"testing"
    10  
    11  	"github.com/stretchr/testify/assert"
    12  )
    13  
    14  type indexerMatchList struct {
    15  	value    string
    16  	position int
    17  }
    18  
    19  func Test_newIndexerGlobSettings(t *testing.T) {
    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  
    53  func 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  }