workflow/workflow_test.go

77 lines
1.5 KiB
Go
Raw Normal View History

2019-12-08 13:40:57 +03:00
package workflow
2020-07-15 15:26:24 +03:00
import (
"testing"
)
2019-12-08 13:40:57 +03:00
func getTestWorkflow() *Workflow {
2020-07-15 15:26:24 +03:00
w := NewWorkflow("Start")
w.AddTransition("Start", "A")
w.AddTransition("Start", "B")
w.AddTransition("A", "C")
w.AddTransition("B", "D")
w.AddTransition("C", "D")
w.AddTransition("C", "Finish")
w.AddTransition("D", "Finish")
2019-12-08 13:40:57 +03:00
return w
}
type testObject struct {
place Place
}
func (t *testObject) GetPlace() Place {
return t.place
}
func (t *testObject) SetPlace(p Place) error {
t.place = p
return nil
}
func TestWorkflow_Can(t *testing.T) {
o := new(testObject)
w := getTestWorkflow()
2020-07-15 15:26:24 +03:00
if err := w.Can(o, "A"); err != nil {
2019-12-08 13:40:57 +03:00
t.Error("Must has transition")
}
2020-07-15 15:26:24 +03:00
if err := w.Can(o, "C"); err == nil {
2019-12-08 13:40:57 +03:00
t.Error("Must has no transition")
}
}
func TestWorkflow_GetEnabledTransitions(t *testing.T) {
2020-07-15 15:26:24 +03:00
w := getTestWorkflow()
2019-12-08 13:40:57 +03:00
o := new(testObject)
if len(w.GetEnabledTransitions(o)) != 2 {
t.Error("Must be exactly 2 transitions from initial")
}
}
func TestWorkflow_Apply(t *testing.T) {
o := new(testObject)
w := getTestWorkflow()
2020-07-15 15:26:24 +03:00
if err := w.Apply(o, "A"); err != nil {
2019-12-08 13:40:57 +03:00
t.Error(err)
}
if o.GetPlace() != "A" {
t.Error("Must be at A place")
}
2020-07-15 15:26:24 +03:00
if err := w.Apply(o, "Finish"); err != ErrTransitionNotFound {
2019-12-08 13:40:57 +03:00
t.Error("Must be transition not found")
}
2020-07-15 15:26:24 +03:00
if err := w.Apply(o, "C"); err != nil {
2019-12-08 13:40:57 +03:00
t.Error(err)
}
if o.GetPlace() != "C" {
t.Error("Must be at C place")
}
}
func TestWorkflow_DumpToDot(t *testing.T) {
dump := getTestWorkflow().DumpToDot()
2020-07-15 15:26:24 +03:00
if len(dump) != 242 {
t.Errorf("Len must be 242, got %d", len(dump))
2019-12-08 13:40:57 +03:00
}
}