Files
commandkit/parser_test.go

51 lines
1.7 KiB
Go
Raw Normal View History

2026-10-07 13:27:00 +00:00
package commandkit
import (
"context"
"reflect"
"testing"
internalcobra "gitea.maximumdirect.net/eric/CommandKit/internal/cobra"
)
func TestAppParseUsesRegisteredBindingsWithTheAdapter(t *testing.T) {
type input struct {
scope Supplied[string]
id Supplied[string]
}
show, err := NewCommand(CommandSpec[input]{
Name: "show",
Flags: []Flag[input]{ValueFlag(FlagMeta{Name: "scope"}, (*string)(nil), func(value string) (string, error) {
return value, nil
}, func(value *input, supplied Supplied[string]) { value.scope = supplied })},
Arguments: []Argument[input]{ValueArgument(ArgumentMeta{Name: "id"}, true, func(value string) (string, error) {
return value, nil
}, func(value *input, supplied Supplied[string]) { value.id = supplied })},
Run: func(Invocation, input) Outcome { return Success() },
})
if err != nil {
t.Fatal(err)
}
config, err := Group("config", "configuration", show)
if err != nil {
t.Fatal(err)
}
app, err := New(Identity{Name: "example"}, config)
if err != nil {
t.Fatal(err)
}
options := internalcobra.ParseOptions{Context: context.Background()}
result := app.parse([]string{"config", "show", "one", "--scope", "app"}, options)
if result.Failure != nil {
t.Fatalf("parse failed: %s", result.Failure.Message)
}
if !reflect.DeepEqual(result.Path, []string{"config", "show"}) || !reflect.DeepEqual(result.FlagValues, [][]string{{"app"}}) || !reflect.DeepEqual(result.Operands, []string{"one"}) {
t.Fatalf("parse result = %#v", result)
}
result = app.parse([]string{"config", "--scope", "app", "show"}, options)
if result.Failure == nil {
t.Fatal("accepted a child flag before the complete command path")
}
}