Merge pull request #2039 from vdemeester/add-match-support-to-reference

Add a new Match method to the reference package
This commit is contained in:
Stephen Day 2016-11-09 18:49:21 -08:00 committed by GitHub
commit 26c9a77535
2 changed files with 89 additions and 0 deletions

View file

@ -581,3 +581,81 @@ func TestWithDigest(t *testing.T) {
}
}
}
func TestMatchError(t *testing.T) {
named, err := Parse("foo")
if err != nil {
t.Fatal(err)
}
_, err = Match("[-x]", named)
if err == nil {
t.Fatalf("expected an error, got nothing")
}
}
func TestMatch(t *testing.T) {
matchCases := []struct {
reference string
pattern string
expected bool
}{
{
reference: "foo",
pattern: "foo/**/ba[rz]",
expected: false,
},
{
reference: "foo/any/bat",
pattern: "foo/**/ba[rz]",
expected: false,
},
{
reference: "foo/a/bar",
pattern: "foo/**/ba[rz]",
expected: true,
},
{
reference: "foo/b/baz",
pattern: "foo/**/ba[rz]",
expected: true,
},
{
reference: "foo/c/baz:tag",
pattern: "foo/**/ba[rz]",
expected: true,
},
{
reference: "foo/c/baz:tag",
pattern: "foo/*/baz:tag",
expected: true,
},
{
reference: "foo/c/baz:tag",
pattern: "foo/c/baz:tag",
expected: true,
},
{
reference: "example.com/foo/c/baz:tag",
pattern: "*/foo/c/baz",
expected: true,
},
{
reference: "example.com/foo/c/baz:tag",
pattern: "example.com/foo/c/baz",
expected: true,
},
}
for _, c := range matchCases {
named, err := Parse(c.reference)
if err != nil {
t.Fatal(err)
}
actual, err := Match(c.pattern, named)
if err != nil {
t.Fatal(err)
}
if actual != c.expected {
t.Fatalf("expected %s match %s to be %v, was %v", c.reference, c.pattern, c.expected, actual)
}
}
}