You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

127 lines
2.9 KiB

4 years ago
package console_test
import (
"context"
"strings"
"sync/atomic"
"testing"
"time"
"gitoa.ru/go-4devs/console"
"gitoa.ru/go-4devs/console/input"
4 years ago
"gitoa.ru/go-4devs/console/input/argument"
4 years ago
"gitoa.ru/go-4devs/console/input/option"
"gitoa.ru/go-4devs/console/output"
)
//nolint:gochecknoinits
4 years ago
func init() {
console.MustRegister(Command().With(console.WithName("fdevs:console:test")))
console.MustRegister(Command().With(console.WithName("fdevs:console:arg")))
4 years ago
}
func Command() *console.Command {
return &console.Command{
Name: "test:command",
Description: "test command",
Execute: func(ctx context.Context, in input.Input, out output.Output) error {
out.Print(ctx,
"test argument:", in.Argument(ctx, "test_argument").String(), "\n",
"bool option:", in.Option(ctx, "bool").Bool(), "\n",
"duration option with default:", in.Option(ctx, "duration").Duration(), "\n",
"array string:[", strings.Join(in.Option(ctx, "string").Strings(), ","), "]\n",
)
return nil
},
Configure: func(ctx context.Context, def *input.Definition) error {
def.
SetArguments(
argument.String("test_argument", "test argument"),
4 years ago
).
SetOptions(
option.String("string", "array string", option.Array),
4 years ago
option.Bool("bool", "test bool option"),
option.Duration("duration", "test duration with default", option.Default(time.Second)),
)
return nil
},
}
}
func TestChainPrepare(t *testing.T) {
t.Parallel()
4 years ago
var cnt int32
ctx := context.Background()
def := input.NewDefinition()
prepare := func(ctx context.Context, def *input.Definition, n console.Configure) error {
atomic.AddInt32(&cnt, 1)
return n(ctx, def)
}
configure := func(context.Context, *input.Definition) error {
return nil
}
for i := range []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10} {
prepares := make([]console.Prepare, i)
for p := 0; p < i; p++ {
prepares[p] = prepare
}
cnt = 0
chain := console.ChainPrepare(prepares...)
if err := chain(ctx, def, configure); err != nil {
t.Errorf("expected nil err, got: %s", err)
}
if cnt != int32(i) {
t.Fatalf("expected: call prepare 1, got: %d ", cnt)
}
}
}
func TestChainHandle(t *testing.T) {
t.Parallel()
4 years ago
var cnt int32
ctx := context.Background()
in := &input.Array{
Map: input.Map{},
}
out := output.Stdout()
4 years ago
handle := func(ctx context.Context, in input.Input, out output.Output, next console.Action) error {
atomic.AddInt32(&cnt, 1)
return next(ctx, in, out)
}
action := func(context.Context, input.Input, output.Output) error {
return nil
}
for i := range []int{0, 1, 2, 30, 40, 50} {
handles := make([]console.Handle, i)
for p := 0; p < i; p++ {
handles[p] = handle
}
cnt = 0
chain := console.ChainHandle(handles...)
if err := chain(ctx, in, out, action); err != nil {
t.Errorf("expected nil err, got: %s", err)
}
if cnt != int32(i) {
t.Fatalf("expected: call prepare 1, got: %d ", cnt)
}
}
}