2019-06-23 04:54:06 +00:00
|
|
|
package pathmatch_test
|
2016-02-25 22:59:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
import (
|
2023-10-01 04:49:20 +00:00
|
|
|
"sourcecode.social/reiver/go-pathmatch"
|
2019-06-23 04:54:06 +00:00
|
|
|
|
2016-02-25 22:59:38 +00:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2019-06-21 04:51:32 +00:00
|
|
|
func TestFind(t *testing.T) {
|
2016-02-25 22:59:38 +00:00
|
|
|
|
|
|
|
tests := []struct{
|
2019-06-23 04:54:06 +00:00
|
|
|
Pattern *pathmatch.Pattern
|
2016-02-25 22:59:38 +00:00
|
|
|
Args []interface{}
|
|
|
|
Path string
|
|
|
|
ExpectedArgs []string
|
|
|
|
}{
|
|
|
|
{
|
2019-06-23 04:54:06 +00:00
|
|
|
Pattern: pathmatch.MustCompile("/{this}/{that}/{these}/{those}"),
|
2016-02-25 22:59:38 +00:00
|
|
|
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}"),
|
2016-02-25 22:59:38 +00:00
|
|
|
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}/"),
|
2016-02-25 22:59:38 +00:00
|
|
|
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"),
|
2016-02-25 22:59:38 +00:00
|
|
|
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/"),
|
2016-02-25 22:59:38 +00:00
|
|
|
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"),
|
2016-02-25 22:59:38 +00:00
|
|
|
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/"),
|
2016-02-25 22:59:38 +00:00
|
|
|
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}"),
|
2016-02-25 22:59:38 +00:00
|
|
|
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}/"),
|
2016-02-25 22:59:38 +00:00
|
|
|
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}"),
|
2016-02-25 22:59:38 +00:00
|
|
|
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}/"),
|
2016-02-25 22:59:38 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-21 04:51:32 +00:00
|
|
|
if didMatch, err := test.Pattern.Find(test.Path, test.Args...); nil != err {
|
2016-02-25 22:59:38 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|