Files

81 lines
2.2 KiB
Go
Raw Permalink Normal View History

2026-10-19 15:08:00 +00:00
package commandpath
import "testing"
func TestParseDeclarationAndLookup(t *testing.T) {
declaration, err := ParseDeclaration(" config\tshow ")
if err != nil {
t.Fatal(err)
}
if got, want := declaration.String(), "config show"; got != want {
t.Fatalf("declaration string = %q, want %q", got, want)
}
if _, err := ParseDeclaration(" \t "); err == nil {
t.Fatal("ParseDeclaration accepted an empty path")
}
root, err := ParseLookup(" \t ")
if err != nil {
t.Fatal(err)
}
if len(root) != 0 || root.String() != "" {
t.Fatalf("empty lookup path = %#v, string %q", root, root.String())
}
lookup, err := ParseLookup("config show")
if err != nil || !lookup.Equal(declaration) {
t.Fatalf("lookup = %#v, err %v; want %#v", lookup, err, declaration)
}
quoted := `say"hello C:\tmp`
parsed, err := ParseDeclaration(quoted)
if err != nil {
t.Fatal(err)
}
if got := parsed.String(); got != quoted {
t.Fatalf("quoted path = %q, want literal %q", got, quoted)
}
}
func TestValidateComponent(t *testing.T) {
for _, value := range []string{"--help", "-x", "--", "name word", "name\tpart"} {
t.Run(value, func(t *testing.T) {
if err := ValidateComponent(value); err == nil {
t.Fatalf("ValidateComponent(%q) succeeded", value)
}
})
}
if err := ValidateComponent("under_score"); err != nil {
t.Fatalf("existing valid component rejected: %v", err)
}
}
func TestPathRelationsAndKey(t *testing.T) {
path := Path{"config", "show"}
if !path.Equal(Path{"config", "show"}) {
t.Fatal("equal paths did not compare equal")
}
if !path.HasPrefix(Path{"config"}) || !path.HasPrefix(nil) {
t.Fatal("path did not recognize component prefix")
}
if path.HasPrefix(Path{"show"}) || (Path{"config"}).HasPrefix(path) {
t.Fatal("path recognized a non-prefix")
}
if (Path{"a\x00b"}).Key() == (Path{"a", "b"}).Key() {
t.Fatal("normalized keys collided for distinct component sequences")
}
}
func TestTokenCopiesPreserveTokenContents(t *testing.T) {
input := []string{" show ", "", "--", "-x=value"}
path := FromTokens(input)
input[0] = "changed"
if path[0] != " show " {
t.Fatalf("FromTokens did not copy input: %#v", path)
}
copy := CloneTokens(path)
copy[1] = "changed"
if path[1] != "" {
t.Fatalf("CloneTokens did not copy input: %#v", path)
}
}