Add captured alias declaration types
This commit is contained in:
@@ -40,6 +40,83 @@ func TestCapturedDeclarationsRetainCallbacksWithoutCallingThem(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestRoutingRuleFactoriesCaptureNormalizedValues(t *testing.T) {
|
||||
commandRule := Alias(" old\tpath ", "new path")
|
||||
if got, want := commandRule.record.sourcePath.String(), "old path"; got != want {
|
||||
t.Fatalf("source path = %q, want %q", got, want)
|
||||
}
|
||||
if got, want := commandRule.record.targetPath.String(), "new path"; got != want {
|
||||
t.Fatalf("target path = %q, want %q", got, want)
|
||||
}
|
||||
globalRule := FlagAlias("--colour", "-c")
|
||||
if globalRule.record.sourceFlag != (capturedFlagSpelling{name: "colour"}) {
|
||||
t.Fatalf("source spelling = %#v", globalRule.record.sourceFlag)
|
||||
}
|
||||
if globalRule.record.targetFlag != (capturedFlagSpelling{name: "c", short: true}) {
|
||||
t.Fatalf("target spelling = %#v", globalRule.record.targetFlag)
|
||||
}
|
||||
scopedRule := FlagAliasFor("group show", "-f", "--format")
|
||||
if got, want := scopedRule.record.scopePath.String(), "group show"; got != want {
|
||||
t.Fatalf("scope path = %q, want %q", got, want)
|
||||
}
|
||||
|
||||
command := NewCommand(CommandSpec[struct{}]{Name: "new", Summary: "new"})
|
||||
app, err := New(Identity{Name: "Example"}, &command, &commandRule, globalRule, scopedRule)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(app.routingRules) != 3 {
|
||||
t.Fatalf("captured routing rules = %d, want 3", len(app.routingRules))
|
||||
}
|
||||
registeredCommand := NewCommand(CommandSpec[struct{}]{Name: "registered", Summary: "registered"})
|
||||
registeredRule := Alias("another", "registered")
|
||||
if err := app.Register(®isteredCommand, ®isteredRule); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(app.routingRules) != 4 || app.commands["registered"] == nil {
|
||||
t.Fatalf("pointer declarations were not normalized during Register: rules=%d command=%v", len(app.routingRules), app.commands["registered"])
|
||||
}
|
||||
}
|
||||
|
||||
func TestInvalidRoutingRuleValuesFailAtInstallation(t *testing.T) {
|
||||
for name, rule := range map[string]RoutingRule{
|
||||
"empty command source": Alias(" \t ", "target"),
|
||||
"invalid command target": Alias("source", "--help"),
|
||||
"flag assignment": FlagAlias("--format=yaml", "--format"),
|
||||
"short cluster": FlagAlias("-xy", "--format"),
|
||||
"invalid scope": FlagAliasFor("", "-f", "--format"),
|
||||
} {
|
||||
t.Run(name, func(t *testing.T) {
|
||||
if _, err := New(Identity{Name: "Example"}, rule); err == nil {
|
||||
t.Fatal("New() succeeded with an invalid routing rule")
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestNilAndZeroDeclarationsFailWithoutMutation(t *testing.T) {
|
||||
app, err := New(Identity{Name: "Example"})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var nilCommand *Command
|
||||
var nilRule *RoutingRule
|
||||
declarations := []Declaration{nil, nilCommand, nilRule, Command{}, RoutingRule{}}
|
||||
for index, declaration := range declarations {
|
||||
if _, err := New(Identity{Name: "Example"}, declaration); err == nil {
|
||||
t.Errorf("New() accepted invalid declaration %d", index)
|
||||
}
|
||||
beforeCommands := len(app.commands)
|
||||
beforeRules := len(app.routingRules)
|
||||
if err := app.Register(declaration); err == nil {
|
||||
t.Errorf("Register() accepted invalid declaration %d", index)
|
||||
}
|
||||
if len(app.commands) != beforeCommands || len(app.routingRules) != beforeRules {
|
||||
t.Errorf("invalid declaration %d mutated the app", index)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestAppsHaveIndependentRegistries(t *testing.T) {
|
||||
first, err := New(Identity{Name: "First"})
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user