code.gitea.io/gitea@v1.19.3/modules/setting/indexer_test.go (about)

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