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.

208 lines
4.7 KiB
Go

package path_test
import (
"testing"
"unicode"
"github.com/aziis98/cabret/path"
"gotest.tools/assert"
)
func TestPath(t *testing.T) {
p, err := path.ParsePattern("/foo/bar")
assert.NilError(t, err)
assert.DeepEqual(t, p, &path.Pattern{
Parts: []path.PathPart{
&path.PathLiteral{Slash: true},
&path.PathString{"foo"},
&path.PathLiteral{Slash: true},
&path.PathString{"bar"},
},
})
}
func TestSimplePattern(t *testing.T) {
p, err := path.ParsePattern("posts/{name}.md")
assert.NilError(t, err)
assert.DeepEqual(t, p, &path.Pattern{
Parts: []path.PathPart{
&path.PathString{"posts"},
&path.PathLiteral{Slash: true},
&path.PathShortPattern{"name"},
&path.PathString{".md"},
},
})
}
func TestSimplePatternWithAny(t *testing.T) {
p, err := path.ParsePattern("posts/*/{name}.md")
assert.NilError(t, err)
assert.DeepEqual(t, p, &path.Pattern{
Parts: []path.PathPart{
&path.PathString{"posts"},
&path.PathLiteral{Slash: true},
&path.PathLiteral{ShortAny: true},
&path.PathLiteral{Slash: true},
&path.PathShortPattern{"name"},
&path.PathString{".md"},
},
})
t.Run("MatchGood", func(t *testing.T) {
m, ctx, err := p.Match("posts/2022/post-1.md")
assert.NilError(t, err, nil)
assert.Equal(t, m, true)
assert.DeepEqual(t, ctx, map[string]string{
path.FullMatch: "posts/2022/post-1.md",
"name": "post-1",
})
})
t.Run("MatchBad", func(t *testing.T) {
m, ctx, err := p.Match("posts/2022/12/post-1.md")
assert.NilError(t, err)
assert.Assert(t, m == false)
assert.Assert(t, ctx == nil)
})
}
func TestSimplePatternCombo(t *testing.T) {
p, err := path.ParsePattern("posts/{{path}}/{name}.md")
assert.NilError(t, err)
assert.DeepEqual(t, p, &path.Pattern{
Parts: []path.PathPart{
&path.PathString{"posts"},
&path.PathLiteral{Slash: true},
&path.PathLongPattern{"path"},
&path.PathLiteral{Slash: true},
&path.PathShortPattern{"name"},
&path.PathString{".md"},
},
})
}
func TestPatternCombo(t *testing.T) {
p, err := path.ParsePattern("posts/**/{date}_{slug}.md")
assert.NilError(t, err)
assert.DeepEqual(t, p, &path.Pattern{
Parts: []path.PathPart{
&path.PathString{"posts"},
&path.PathLiteral{Slash: true},
&path.PathLiteral{LongAny: true},
&path.PathLiteral{Slash: true},
&path.PathShortPattern{"date"},
&path.PathString{"_"},
&path.PathShortPattern{"slug"},
&path.PathString{".md"},
},
})
t.Run("MatchGood", func(t *testing.T) {
ok, ctx, err := p.Match("posts/a/b/c/2022-12-25_example.md")
assert.Assert(t, ok)
assert.NilError(t, err)
assert.DeepEqual(t, ctx, map[string]string{
path.FullMatch: "posts/a/b/c/2022-12-25_example.md",
"date": "2022-12-25",
"slug": "example",
})
})
}
func TestStrangePatternCombo(t *testing.T) {
p, err := path.ParsePattern("foo{{path}}/{name}.md")
assert.NilError(t, err)
assert.DeepEqual(t, p, &path.Pattern{
Parts: []path.PathPart{
&path.PathString{"foo"},
&path.PathLongPattern{"path"},
&path.PathLiteral{Slash: true},
&path.PathShortPattern{"name"},
&path.PathString{".md"},
},
})
t.Run("match", func(t *testing.T) {
m, ctx, err := p.Match("foo-1/bar/baz/post-1.md")
assert.NilError(t, err, nil)
assert.Equal(t, m, true)
assert.DeepEqual(t, ctx, map[string]string{
path.FullMatch: "foo-1/bar/baz/post-1.md",
"name": "post-1",
"path": "-1/bar/baz",
})
})
}
func TestMultipleGroups(t *testing.T) {
p, err := path.ParsePattern("{a}/{b}/{c}")
assert.NilError(t, err)
assert.DeepEqual(t, p, &path.Pattern{
Parts: []path.PathPart{
&path.PathShortPattern{"a"},
&path.PathLiteral{Slash: true},
&path.PathShortPattern{"b"},
&path.PathLiteral{Slash: true},
&path.PathShortPattern{"c"},
},
})
t.Run("match", func(t *testing.T) {
m, ctx, err := p.Match("foo/bar/baz")
assert.NilError(t, err, nil)
assert.Equal(t, m, true)
assert.DeepEqual(t, ctx, map[string]string{
path.FullMatch: "foo/bar/baz",
"a": "foo",
"b": "bar",
"c": "baz",
})
})
}
func FuzzMatch(f *testing.F) {
f.Add(`{a}.txt`)
f.Add(`{a}/{b}.txt`)
f.Add(`src/{{a}}/index.html`)
f.Add(`/a/{b}/c-{d}.go`)
path.MustParsePattern(`{a}.txt`)
path.MustParsePattern(`{a}/{b}.txt`)
path.MustParsePattern(`src/{{a}}/index.html`)
path.MustParsePattern(`/a/{b}/c-{d}.go`)
f.Fuzz(func(t *testing.T, pattern string) {
goodPattern := make([]rune, 0, len(pattern))
// filter in only ascii runes
for _, c := range pattern {
if c <= unicode.MaxASCII {
goodPattern = append(goodPattern, c)
}
}
pattern = string(goodPattern)
p, err := path.ParsePattern(pattern)
if err != nil {
return
}
if _, _, err := p.Match(""); err != nil {
t.Errorf(`invalid regexp generated by pattern %q`, pattern)
}
})
}