fix lint errors
Some checks failed
Lint / golangci-lint (push) Failing after 9m53s
Machine Tests / test (push) Successful in 40s

This commit is contained in:
Matthew Rich 2024-04-04 13:08:50 -07:00
parent 8b92e9e143
commit 85e39191b8
10 changed files with 180 additions and 181 deletions

View File

@ -25,7 +25,7 @@ type Definition struct {
} }
func New(initial State) Stater { func New(initial State) Stater {
return &Definition{ model: NewModel(initial), triggers: make(map[string]Transitioner) } return &Definition{model: NewModel(initial), triggers: make(map[string]Transitioner)}
} }
func (d *Definition) AddStates(name ...State) { func (d *Definition) AddStates(name ...State) {
@ -38,7 +38,7 @@ func (d *Definition) AddTransition(trigger string, source State, dest State) {
func (d *Definition) GetState(name string) State { func (d *Definition) GetState(name string) State {
var r State var r State
for _,s := range(d.states) { for _, s := range d.states {
if string(s) == name { if string(s) == name {
return s return s
} }
@ -59,7 +59,7 @@ func (d *Definition) CurrentState() State {
} }
func (d *Definition) AddSubscription(transition string, subscription Subscriber) error { func (d *Definition) AddSubscription(transition string, subscription Subscriber) error {
if t,ok := d.triggers[transition]; ok { if t, ok := d.triggers[transition]; ok {
t.Subscribe(subscription) t.Subscribe(subscription)
return nil return nil
} }

View File

@ -2,10 +2,10 @@
package machine package machine
import( import (
"github.com/stretchr/testify/assert"
"log" "log"
"testing" "testing"
"github.com/stretchr/testify/assert"
) )
func setupStater(initial State) Stater { func setupStater(initial State) Stater {
@ -58,8 +58,8 @@ func TestMachineAddSubscription(t *testing.T) {
assert.Nil(t, s.AddSubscription("connect", x)) assert.Nil(t, s.AddSubscription("connect", x))
s.AddModel(setupModel("disconnected")) s.AddModel(setupModel("disconnected"))
s.Trigger("connect") s.Trigger("connect")
exitMessage := <- *x.(*EventChannel) exitMessage := <-*x.(*EventChannel)
enterMessage := <- *x.(*EventChannel) enterMessage := <-*x.(*EventChannel)
if exitMessage.on == EXITSTATEEVENT && exitMessage.source == "disconnected" { if exitMessage.on == EXITSTATEEVENT && exitMessage.source == "disconnected" {
if enterMessage.on == ENTERSTATEEVENT && enterMessage.dest == "start_connection" { if enterMessage.on == ENTERSTATEEVENT && enterMessage.dest == "start_connection" {
return return

View File

@ -13,7 +13,7 @@ type Model struct {
} }
func NewModel(initial State) Modeler { func NewModel(initial State) Modeler {
return &Model{ state: initial } return &Model{state: initial}
} }
func (m *Model) Trigger(transition Transitioner) error { func (m *Model) Trigger(transition Transitioner) error {

View File

@ -7,6 +7,5 @@ type mState struct {
} }
func NewState(name string) *mState { func NewState(name string) *mState {
return &mState{ name: name } return &mState{name: name}
} }

View File

@ -3,8 +3,8 @@
package machine package machine
import ( import (
_ "errors"
"fmt" "fmt"
"errors"
) )
type Transitioner interface { type Transitioner interface {
@ -20,7 +20,7 @@ type Transition struct {
} }
func NewTransition(trigger string, source State, dest State) Transitioner { func NewTransition(trigger string, source State, dest State) Transitioner {
return &Transition{ trigger: trigger, source: source, dest: dest } return &Transition{trigger: trigger, source: source, dest: dest}
} }
func (r *Transition) Run(m Modeler) error { func (r *Transition) Run(m Modeler) error {
@ -33,7 +33,7 @@ func (r *Transition) Run(m Modeler) error {
return nil return nil
} }
} }
return errors.New(fmt.Sprintf("Transition from %s to %s failed on model state %s", r.source, r.dest, currentState)) return fmt.Errorf("Transition from %s to %s failed on model state %s", r.source, r.dest, currentState)
} }
func (r *Transition) Subscribe(s Subscriber) { func (r *Transition) Subscribe(s Subscriber) {
@ -41,7 +41,7 @@ func (r *Transition) Subscribe(s Subscriber) {
} }
func (r *Transition) Notify(on Eventtype, source State, dest State) { func (r *Transition) Notify(on Eventtype, source State, dest State) {
for _,s := range r.subscriptions { for _, s := range r.subscriptions {
s.Notify(&EventMessage{ on: on, source: source, dest: dest}) s.Notify(&EventMessage{on: on, source: source, dest: dest})
} }
} }

View File

@ -3,9 +3,9 @@
package machine package machine
import ( import (
"github.com/stretchr/testify/assert"
"log" "log"
"testing" "testing"
"github.com/stretchr/testify/assert"
) )
func setupTransition() Transitioner { func setupTransition() Transitioner {
@ -44,8 +44,8 @@ func TestTransitionSubscribe(t *testing.T) {
s.Subscribe(c) s.Subscribe(c)
m := setupModel("closed") m := setupModel("closed")
assert.Nil(t, s.Run(m)) assert.Nil(t, s.Run(m))
exitEvent := <- *c.(*EventChannel) exitEvent := <-*c.(*EventChannel)
enterEvent := <- *c.(*EventChannel) enterEvent := <-*c.(*EventChannel)
if exitEvent.on != EXITSTATEEVENT { if exitEvent.on != EXITSTATEEVENT {
t.Errorf("Invalid exit event") t.Errorf("Invalid exit event")
} }