You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
85 lines
1.8 KiB
Go
85 lines
1.8 KiB
Go
package finder_test
|
|
|
|
import (
|
|
"git.noahlan.cn/noahlan/ntool/nfs/finder"
|
|
"git.noahlan.cn/noahlan/ntool/ntest/assert"
|
|
"git.noahlan.cn/noahlan/ntool/ntest/mock"
|
|
"testing"
|
|
)
|
|
|
|
func newMockElem(fp string, isDir ...bool) finder.Elem {
|
|
return finder.NewElem(fp, mock.NewDirEnt(fp, isDir...))
|
|
}
|
|
|
|
func TestFilters_simple(t *testing.T) {
|
|
el := newMockElem("path/some.txt")
|
|
fn := finder.MatcherFunc(func(el finder.Elem) bool {
|
|
return false
|
|
})
|
|
|
|
assert.False(t, fn(el))
|
|
|
|
// match name
|
|
fn = finder.MatchName("some.txt")
|
|
assert.True(t, fn(el))
|
|
fn = finder.MatchName("not-exist.txt")
|
|
assert.False(t, fn(el))
|
|
|
|
// MatchExt
|
|
fn = finder.MatchExt(".txt")
|
|
assert.True(t, fn(el))
|
|
fn = finder.MatchExt(".js")
|
|
assert.False(t, fn(el))
|
|
|
|
// MatchSuffix
|
|
fn = finder.MatchSuffix("me.txt")
|
|
assert.True(t, fn(el))
|
|
fn = finder.MatchSuffix("not-exist.txt")
|
|
assert.False(t, fn(el))
|
|
}
|
|
|
|
func TestRegexMatch(t *testing.T) {
|
|
tests := []struct {
|
|
filePath string
|
|
pattern string
|
|
match bool
|
|
}{
|
|
{"path/to/util.go", `\.go$`, true},
|
|
{"path/to/util.go", `\.md$`, false},
|
|
{"path/to/util.md", `\.md$`, true},
|
|
{"path/to/util.md", `\.go$`, false},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
el := newMockElem(tt.filePath)
|
|
fn := finder.RegexMatch(tt.pattern)
|
|
assert.Eq(t, tt.match, fn(el))
|
|
}
|
|
}
|
|
|
|
func TestMatchDotDir(t *testing.T) {
|
|
f := finder.EmptyFinder().
|
|
WithFlags(finder.FlagBoth).
|
|
ScanDir("./testdata")
|
|
|
|
dirName := ".dotdir"
|
|
assert.Contains(t, f.FindNames(), dirName)
|
|
|
|
t.Run("NoDotDir", func(t *testing.T) {
|
|
f = finder.EmptyFinder().
|
|
ScanDir("./testdata").
|
|
NoDotDir()
|
|
|
|
assert.NotContains(t, f.FindNames(), dirName)
|
|
})
|
|
|
|
t.Run("Exclude false", func(t *testing.T) {
|
|
f = finder.NewEmpty().
|
|
WithStrFlag("dir").
|
|
ScanDir("./testdata").
|
|
ExcludeDotDir(false)
|
|
|
|
assert.Contains(t, f.FindNames(), dirName)
|
|
})
|
|
}
|