go-pathmatch/pattern_find_test.go

132 lines
4.7 KiB
Go
Raw Normal View History

2019-06-23 04:54:06 +00:00
package pathmatch_test
import (
"sourcecode.social/reiver/go-pathmatch"
2019-06-23 04:54:06 +00:00
"testing"
)
func TestFind(t *testing.T) {
tests := []struct{
2019-06-23 04:54:06 +00:00
Pattern *pathmatch.Pattern
Args []interface{}
Path string
ExpectedArgs []string
}{
{
2019-06-23 04:54:06 +00:00
Pattern: pathmatch.MustCompile("/{this}/{that}/{these}/{those}"),
Args: []interface{}{new(string), new(string), new(string), new(string), },
Path: "/apple/banana/cherry/grape",
ExpectedArgs: []string{"apple","banana","cherry","grape"},
},
{
2019-06-23 04:54:06 +00:00
Pattern: pathmatch.MustCompile("/user/{sessionKey}"),
Args: []interface{}{new(string), },
Path: "/user/76M6.mXQfgiGSC_YJ5uXSnWUmELbe8OgOm5n.iZ98Ij",
ExpectedArgs: []string{"76M6.mXQfgiGSC_YJ5uXSnWUmELbe8OgOm5n.iZ98Ij"},
},
{
2019-06-23 04:54:06 +00:00
Pattern: pathmatch.MustCompile("/user/{sessionKey}/"),
Args: []interface{}{new(string), },
Path: "/user/76M6.mXQfgiGSC_YJ5uXSnWUmELbe8OgOm5n.iZ98Ij/",
ExpectedArgs: []string{"76M6.mXQfgiGSC_YJ5uXSnWUmELbe8OgOm5n.iZ98Ij"},
},
{
2019-06-23 04:54:06 +00:00
Pattern: pathmatch.MustCompile("/user/{sessionKey}/vehicle"),
Args: []interface{}{new(string), },
Path: "/user/76M6.mXQfgiGSC_YJ5uXSnWUmELbe8OgOm5n.iZ98Ij/vehicle",
ExpectedArgs: []string{"76M6.mXQfgiGSC_YJ5uXSnWUmELbe8OgOm5n.iZ98Ij"},
},
{
2019-06-23 04:54:06 +00:00
Pattern: pathmatch.MustCompile("/user/{sessionKey}/vehicle/"),
Args: []interface{}{new(string), },
Path: "/user/76M6.mXQfgiGSC_YJ5uXSnWUmELbe8OgOm5n.iZ98Ij/vehicle/",
ExpectedArgs: []string{"76M6.mXQfgiGSC_YJ5uXSnWUmELbe8OgOm5n.iZ98Ij"},
},
{
2019-06-23 04:54:06 +00:00
Pattern: pathmatch.MustCompile("/user/{sessionKey}/vehicle/DEFAULT"),
Args: []interface{}{new(string), },
Path: "/user/76M6.mXQfgiGSC_YJ5uXSnWUmELbe8OgOm5n.iZ98Ij/vehicle/DEFAULT",
ExpectedArgs: []string{"76M6.mXQfgiGSC_YJ5uXSnWUmELbe8OgOm5n.iZ98Ij"},
},
{
2019-06-23 04:54:06 +00:00
Pattern: pathmatch.MustCompile("/user/{sessionKey}/vehicle/DEFAULT/"),
Args: []interface{}{new(string), },
Path: "/user/76M6.mXQfgiGSC_YJ5uXSnWUmELbe8OgOm5n.iZ98Ij/vehicle/DEFAULT/",
ExpectedArgs: []string{"76M6.mXQfgiGSC_YJ5uXSnWUmELbe8OgOm5n.iZ98Ij"},
},
{
2019-06-23 04:54:06 +00:00
Pattern: pathmatch.MustCompile("/user/{sessionKey}/vehicle/{vehicleIdcode}"),
Args: []interface{}{new(string), new(string), },
Path: "/user/76M6.mXQfgiGSC_YJ5uXSnWUmELbe8OgOm5n.iZ98Ij/vehicle/DEFAULT",
ExpectedArgs: []string{"76M6.mXQfgiGSC_YJ5uXSnWUmELbe8OgOm5n.iZ98Ij", "DEFAULT"},
},
{
2019-06-23 04:54:06 +00:00
Pattern: pathmatch.MustCompile("/user/{sessionKey}/vehicle/{vehicleIdcode}/"),
Args: []interface{}{new(string), new(string), },
Path: "/user/76M6.mXQfgiGSC_YJ5uXSnWUmELbe8OgOm5n.iZ98Ij/vehicle/DEFAULT/",
ExpectedArgs: []string{"76M6.mXQfgiGSC_YJ5uXSnWUmELbe8OgOm5n.iZ98Ij", "DEFAULT"},
},
{
2019-06-23 04:54:06 +00:00
Pattern: pathmatch.MustCompile("/user/{sessionKey}/vehicle/{vehicleIdcode}"),
Args: []interface{}{new(string), new(string), },
Path: "/user/76M6.mXQfgiGSC_YJ5uXSnWUmELbe8OgOm5n.iZ98Ij/vehicle/N9Z_tiv7",
ExpectedArgs: []string{"76M6.mXQfgiGSC_YJ5uXSnWUmELbe8OgOm5n.iZ98Ij", "N9Z_tiv7"},
},
{
2019-06-23 04:54:06 +00:00
Pattern: pathmatch.MustCompile("/user/{sessionKey}/vehicle/{vehicleIdcode}/"),
Args: []interface{}{new(string), new(string), },
Path: "/user/76M6.mXQfgiGSC_YJ5uXSnWUmELbe8OgOm5n.iZ98Ij/vehicle/N9Z_tiv7/",
ExpectedArgs: []string{"76M6.mXQfgiGSC_YJ5uXSnWUmELbe8OgOm5n.iZ98Ij", "N9Z_tiv7"},
},
}
for testNumber, test := range tests {
for argNumber, arg := range test.Args {
argStringPtr, ok := arg.(*string)
if !ok {
t.Errorf("For test #%d, expected test.Args[%d] to be of type *string, but actually was %T.", testNumber, argNumber, arg)
continue
}
if expected, actual := "", *argStringPtr; expected != actual {
t.Errorf("For test #%d, expected *test.Args[%d] to (initially) be %q, but actually was %q.", testNumber, argNumber, expected, actual)
continue
}
}
if didMatch, err := test.Pattern.Find(test.Path, test.Args...); nil != err {
t.Errorf("For test #%d, did not expected an error, but actually got one: %v", testNumber, err)
continue
} else if !didMatch {
t.Errorf("For test #%d, expected path to match pattern, but it didn't.", testNumber)
continue
}
for argNumber, arg := range test.Args {
argStringPtr, ok := arg.(*string)
if !ok {
t.Errorf("For test #%d, expected test.Args[%d] to be of type *string, but actually was %T.", testNumber, argNumber, arg)
continue
}
if expected, actual := test.ExpectedArgs[argNumber], *argStringPtr; expected != actual {
t.Errorf("For test #%d, expected *test.Args[%d] to be %q, but actually was %q.", testNumber, argNumber, expected, actual)
continue
}
}
}
}