Merge pull request #59 from ucan-wg/command-covers
command: add Covers() for attenuation test, fix incorrect Segments()
This commit is contained in:
@@ -98,7 +98,34 @@ func (c Command) Join(segments ...string) Command {
|
|||||||
// Segments returns the ordered segments that comprise the Command as a
|
// Segments returns the ordered segments that comprise the Command as a
|
||||||
// slice of strings.
|
// slice of strings.
|
||||||
func (c Command) Segments() []string {
|
func (c Command) Segments() []string {
|
||||||
return strings.Split(string(c), separator)
|
if c == separator {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return strings.Split(string(c), separator)[1:]
|
||||||
|
}
|
||||||
|
|
||||||
|
// Covers returns true if the command is identical or a parent of the given other command.
|
||||||
|
func (c Command) Covers(other Command) bool {
|
||||||
|
// fast-path, equivalent to the code below (verified with fuzzing)
|
||||||
|
if !strings.HasPrefix(string(other), string(c)) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return c == separator || len(c) == len(other) || other[len(c)] == separator[0]
|
||||||
|
|
||||||
|
/* -------
|
||||||
|
|
||||||
|
otherSegments := other.Segments()
|
||||||
|
if len(otherSegments) < len(c.Segments()) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
for i, s := range c.Segments() {
|
||||||
|
if otherSegments[i] != s {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
|
||||||
|
*/
|
||||||
}
|
}
|
||||||
|
|
||||||
// String returns the composed representation the command. This is also
|
// String returns the composed representation the command. This is also
|
||||||
|
|||||||
@@ -73,6 +73,21 @@ func TestJoin(t *testing.T) {
|
|||||||
require.Equal(t, "/faz/boz/foo/bar", command.MustParse("/faz/boz").Join("foo", "bar").String())
|
require.Equal(t, "/faz/boz/foo/bar", command.MustParse("/faz/boz").Join("foo", "bar").String())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestSegments(t *testing.T) {
|
||||||
|
require.Empty(t, command.Top().Segments())
|
||||||
|
require.Equal(t, []string{"foo", "bar", "baz"}, command.MustParse("/foo/bar/baz").Segments())
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCovers(t *testing.T) {
|
||||||
|
require.True(t, command.MustParse("/foo/bar/baz").Covers(command.MustParse("/foo/bar/baz")))
|
||||||
|
require.True(t, command.MustParse("/foo/bar").Covers(command.MustParse("/foo/bar/baz")))
|
||||||
|
require.False(t, command.MustParse("/foo/bar/baz").Covers(command.MustParse("/foo/bar")))
|
||||||
|
require.True(t, command.MustParse("/").Covers(command.MustParse("/foo")))
|
||||||
|
require.True(t, command.MustParse("/").Covers(command.MustParse("/foo/bar/baz")))
|
||||||
|
require.False(t, command.MustParse("/foo").Covers(command.MustParse("/foo00")))
|
||||||
|
require.False(t, command.MustParse("/foo/bar").Covers(command.MustParse("/foo/bar00")))
|
||||||
|
}
|
||||||
|
|
||||||
type testcase struct {
|
type testcase struct {
|
||||||
name string
|
name string
|
||||||
inp string
|
inp string
|
||||||
|
|||||||
Reference in New Issue
Block a user