diff --git a/go.mod b/go.mod index be8d218..f90955c 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,10 @@ module gitea.rosskeen.house/rosskeen.house/machine go 1.21.1 + +require ( + github.com/davecgh/go-spew v1.1.1 // indirect + github.com/pmezard/go-difflib v1.0.0 // indirect + github.com/stretchr/testify v1.9.0 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..e20fa14 --- /dev/null +++ b/go.sum @@ -0,0 +1,9 @@ +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= +github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/machine.go b/machine.go index f187a84..fa05c97 100644 --- a/machine.go +++ b/machine.go @@ -14,7 +14,7 @@ type Stater interface { AddTransition(trigger string, source State, dest State) AddSubscription(transition string, subscription Subscriber) error AddModel(m Modeler) - Trigger(transition string) + Trigger(transition string) error CurrentState() State } @@ -50,8 +50,8 @@ func (d *Definition) AddModel(m Modeler) { d.model = m } -func (d *Definition) Trigger(transition string) { - d.triggers[transition].Run(d.model) +func (d *Definition) Trigger(transition string) error { + return d.triggers[transition].Run(d.model) } func (d *Definition) CurrentState() State { @@ -61,6 +61,7 @@ func (d *Definition) CurrentState() State { func (d *Definition) AddSubscription(transition string, subscription Subscriber) error { if t,ok := d.triggers[transition]; ok { t.Subscribe(subscription) + return nil } return errors.New("Transition does not exist") } diff --git a/machine_test.go b/machine_test.go index cd8f1c0..821faa9 100644 --- a/machine_test.go +++ b/machine_test.go @@ -5,6 +5,7 @@ package machine import( "log" "testing" + "github.com/stretchr/testify/assert" ) func setupStater(initial State) Stater { @@ -41,7 +42,8 @@ func TestMachineAddTransition(t *testing.T) { s.AddTransition("connect", "disconnected", "start_connection") s.AddModel(setupModel("disconnected")) // worker gets a trigger message - s.Trigger("connect") + assert.Nil(t, s.Trigger("connect")) + // machine generates transition event mesages if s.CurrentState() != "start_connection" { t.Errorf("State transition failed for: connect - %s", s.CurrentState()) @@ -53,7 +55,7 @@ func TestMachineAddSubscription(t *testing.T) { s := setupStater("disconnected") s.AddStates("disconnected", "start_connection", "connected") s.AddTransition("connect", "disconnected", "start_connection") - s.AddSubscription("connect", x) + assert.Nil(t, s.AddSubscription("connect", x)) s.AddModel(setupModel("disconnected")) s.Trigger("connect") exitMessage := <- *x.(*EventChannel) diff --git a/model.go b/model.go index 2ac49fa..00176cb 100644 --- a/model.go +++ b/model.go @@ -3,7 +3,7 @@ package machine type Modeler interface { - Trigger(transition Transitioner) + Trigger(transition Transitioner) error ChangeState(target State) State InspectState() State } @@ -16,8 +16,8 @@ func NewModel(initial State) Modeler { return &Model{ state: initial } } -func (m *Model) Trigger(transition Transitioner) { - transition.Run(m) +func (m *Model) Trigger(transition Transitioner) error { + return transition.Run(m) } func (m *Model) ChangeState(target State) State { diff --git a/transition_test.go b/transition_test.go index 0f57b92..015e4a2 100644 --- a/transition_test.go +++ b/transition_test.go @@ -5,6 +5,7 @@ package machine import ( "log" "testing" + "github.com/stretchr/testify/assert" ) func setupTransition() Transitioner { @@ -30,7 +31,7 @@ func TestNewTransition(t *testing.T) { func TestTransitionExecution(t *testing.T) { s := setupTransition() m := setupModel("closed") - s.Run(m) + assert.Nil(t, s.Run(m)) state := m.InspectState() if state != "open" { t.Errorf("Failed to transition state: %s", state) @@ -42,7 +43,7 @@ func TestTransitionSubscribe(t *testing.T) { s := setupTransition() s.Subscribe(c) m := setupModel("closed") - s.Run(m) + assert.Nil(t, s.Run(m)) exitEvent := <- *c.(*EventChannel) enterEvent := <- *c.(*EventChannel) if exitEvent.on != EXITSTATEEVENT {