147 lines
3.1 KiB
Go
147 lines
3.1 KiB
Go
|
package stepper
|
||
|
|
||
|
import (
|
||
|
"context"
|
||
|
"errors"
|
||
|
"testing"
|
||
|
)
|
||
|
|
||
|
func TestBasicSteps(t *testing.T) {
|
||
|
|
||
|
ctx := context.Background()
|
||
|
state := make(map[string]bool)
|
||
|
|
||
|
step1 := func(ctx context.Context, cancel context.CancelFunc, state interface{}) error {
|
||
|
st, ok := state.(map[string]bool)
|
||
|
if !ok {
|
||
|
t.Error(ErrUnsupportedState)
|
||
|
}
|
||
|
st["step1"] = true
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
step2 := func(ctx context.Context, cancel context.CancelFunc, state interface{}) error {
|
||
|
st, ok := state.(map[string]bool)
|
||
|
if !ok {
|
||
|
t.Error(ErrUnsupportedState)
|
||
|
}
|
||
|
st["step2"] = true
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
status, err := Run(ctx, state, step1, step2)
|
||
|
if err != nil {
|
||
|
t.Error(err)
|
||
|
}
|
||
|
|
||
|
if g, e := status, Completed; g != e {
|
||
|
t.Errorf("status: expected '%v', got '%v'", e, g)
|
||
|
}
|
||
|
|
||
|
if g, e := state["step1"], true; g != e {
|
||
|
t.Errorf("state[\"step1\"]: expected '%v', got '%v'", e, g)
|
||
|
}
|
||
|
|
||
|
if g, e := state["step2"], true; g != e {
|
||
|
t.Errorf("state[\"step2\"]: expected '%v', got '%v'", e, g)
|
||
|
}
|
||
|
|
||
|
}
|
||
|
|
||
|
func TestStepsCancel(t *testing.T) {
|
||
|
|
||
|
ctx := context.Background()
|
||
|
state := make(map[string]bool)
|
||
|
|
||
|
step1 := func(ctx context.Context, cancel context.CancelFunc, state interface{}) error {
|
||
|
st, ok := state.(map[string]bool)
|
||
|
if !ok {
|
||
|
t.Error(ErrUnsupportedState)
|
||
|
}
|
||
|
st["step1"] = true
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
step2 := func(ctx context.Context, cancel context.CancelFunc, state interface{}) error {
|
||
|
cancel()
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
step3 := func(ctx context.Context, cancel context.CancelFunc, state interface{}) error {
|
||
|
st, ok := state.(map[string]bool)
|
||
|
if !ok {
|
||
|
t.Error(ErrUnsupportedState)
|
||
|
}
|
||
|
st["step3"] = true
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
status, err := Run(ctx, state, step1, step2, step3)
|
||
|
if err != nil {
|
||
|
t.Error(err)
|
||
|
}
|
||
|
|
||
|
if g, e := status, Canceled; g != e {
|
||
|
t.Errorf("status: expected '%v', got '%v'", e, g)
|
||
|
}
|
||
|
|
||
|
if g, e := state["step1"], true; g != e {
|
||
|
t.Errorf("state[\"step1\"]: expected '%v', got '%v'", e, g)
|
||
|
}
|
||
|
|
||
|
if g, e := state["step3"], false; g != e {
|
||
|
t.Errorf("state[\"step3\"]: expected '%v', got '%v'", e, g)
|
||
|
}
|
||
|
|
||
|
}
|
||
|
|
||
|
func TestStepsError(t *testing.T) {
|
||
|
|
||
|
stepErr := errors.New("test error")
|
||
|
ctx := context.Background()
|
||
|
state := make(map[string]bool)
|
||
|
|
||
|
step1 := func(ctx context.Context, cancel context.CancelFunc, state interface{}) error {
|
||
|
st, ok := state.(map[string]bool)
|
||
|
if !ok {
|
||
|
t.Error(ErrUnsupportedState)
|
||
|
}
|
||
|
st["step1"] = true
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
step2 := func(ctx context.Context, cancel context.CancelFunc, state interface{}) error {
|
||
|
return stepErr
|
||
|
}
|
||
|
|
||
|
step3 := func(ctx context.Context, cancel context.CancelFunc, state interface{}) error {
|
||
|
st, ok := state.(map[string]bool)
|
||
|
if !ok {
|
||
|
t.Error(ErrUnsupportedState)
|
||
|
}
|
||
|
st["step3"] = true
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
status, err := Run(ctx, state, step1, step2, step3)
|
||
|
if err == nil {
|
||
|
t.Errorf("err should not be nil")
|
||
|
}
|
||
|
if g, e := err, stepErr; g != e {
|
||
|
t.Errorf("err: expected '%v', got '%v'", e, g)
|
||
|
}
|
||
|
|
||
|
if g, e := status, Error; g != e {
|
||
|
t.Errorf("status: expected '%v', got '%v'", e, g)
|
||
|
}
|
||
|
|
||
|
if g, e := state["step1"], true; g != e {
|
||
|
t.Errorf("state[\"step1\"]: expected '%v', got '%v'", e, g)
|
||
|
}
|
||
|
|
||
|
if g, e := state["step3"], false; g != e {
|
||
|
t.Errorf("state[\"step3\"]: expected '%v', got '%v'", e, g)
|
||
|
}
|
||
|
|
||
|
}
|