Add 'capability/command/' from commit 'f37a43ac38af8c3710fee4e6a923070f6af4f74c'
git-subtree-dir: capability/command git-subtree-mainline:c2cc0ca0a4git-subtree-split:f37a43ac38
This commit is contained in:
159
capability/command/command_test.go
Normal file
159
capability/command/command_test.go
Normal file
@@ -0,0 +1,159 @@
|
||||
package command_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
"github.com/ucan-wg/go-ucan/pkg/command"
|
||||
)
|
||||
|
||||
func TestCommand_String(t *testing.T) {
|
||||
require.Equal(t, "/", command.Top().String())
|
||||
}
|
||||
|
||||
func TestIsValidCommand(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
t.Run("succeeds when", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
for _, testcase := range validTestcases(t) {
|
||||
testcase := testcase
|
||||
|
||||
t.Run(testcase.name, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
require.True(t, command.IsValid(testcase.inp))
|
||||
})
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("fails when", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
for _, testcase := range invalidTestcases(t) {
|
||||
testcase := testcase
|
||||
|
||||
t.Run(testcase.name, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
require.False(t, command.IsValid(testcase.inp))
|
||||
})
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func TestParseCommand(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
t.Run("succeeds when", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
for _, testcase := range validTestcases(t) {
|
||||
testcase := testcase
|
||||
|
||||
t.Run(testcase.name, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
cmd, err := command.Parse("/elem0/elem1/elem2")
|
||||
require.NoError(t, err)
|
||||
require.NotNil(t, cmd)
|
||||
})
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("fails when", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
for _, testcase := range invalidTestcases(t) {
|
||||
testcase := testcase
|
||||
|
||||
t.Run(testcase.name, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
cmd, err := command.Parse(testcase.inp)
|
||||
assert.ErrorIs(t, err, command.ErrParse)
|
||||
assert.ErrorIs(t, err, testcase.err)
|
||||
assert.Nil(t, cmd)
|
||||
})
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
type testcase struct {
|
||||
name string
|
||||
inp string
|
||||
}
|
||||
|
||||
func validTestcases(t *testing.T) []testcase {
|
||||
t.Helper()
|
||||
|
||||
cmds := []string{
|
||||
"/",
|
||||
"/crud",
|
||||
"/crud/create",
|
||||
"/stack/pop",
|
||||
"/crypto/sign",
|
||||
"/foo/bar/baz/qux/quux",
|
||||
"/ほげ/ふが",
|
||||
}
|
||||
|
||||
testcases := make([]testcase, len(cmds))
|
||||
|
||||
for i, inp := range cmds {
|
||||
testcases[i] = testcase{
|
||||
name: "Command is " + inp,
|
||||
inp: inp,
|
||||
}
|
||||
}
|
||||
|
||||
return testcases
|
||||
}
|
||||
|
||||
type errorTestcase struct {
|
||||
testcase
|
||||
err error
|
||||
}
|
||||
|
||||
func invalidTestcases(t *testing.T) []errorTestcase {
|
||||
t.Helper()
|
||||
|
||||
return []errorTestcase{
|
||||
{
|
||||
testcase: testcase{
|
||||
name: "leading slash is missing",
|
||||
inp: "elem0/elem1/elem2",
|
||||
},
|
||||
err: command.ErrRequiresLeadingSlash,
|
||||
},
|
||||
{
|
||||
testcase: testcase{
|
||||
name: "trailing slash is present",
|
||||
inp: "/elem0/elem1/elem2/",
|
||||
},
|
||||
err: command.ErrDisallowsTrailingSlash,
|
||||
},
|
||||
{
|
||||
testcase: testcase{
|
||||
name: "only reserved ucan namespace",
|
||||
inp: "/ucan",
|
||||
},
|
||||
err: command.ErrUCANNamespaceReserved,
|
||||
},
|
||||
{
|
||||
testcase: testcase{
|
||||
name: "reserved ucan namespace prefix",
|
||||
inp: "/ucan/elem0/elem1/elem2",
|
||||
},
|
||||
err: command.ErrUCANNamespaceReserved,
|
||||
},
|
||||
{
|
||||
testcase: testcase{
|
||||
name: "uppercase character are present",
|
||||
inp: "/elem0/Elem1/elem2",
|
||||
},
|
||||
err: command.ErrRequiresLowercase,
|
||||
},
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user