fix lint errors
This commit is contained in:
parent
8b92e9e143
commit
85e39191b8
@ -25,7 +25,7 @@ type Definition struct {
|
||||
}
|
||||
|
||||
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) {
|
||||
@ -38,7 +38,7 @@ func (d *Definition) AddTransition(trigger string, source State, dest State) {
|
||||
|
||||
func (d *Definition) GetState(name string) State {
|
||||
var r State
|
||||
for _,s := range(d.states) {
|
||||
for _, s := range d.states {
|
||||
if string(s) == name {
|
||||
return s
|
||||
}
|
||||
@ -59,7 +59,7 @@ func (d *Definition) CurrentState() State {
|
||||
}
|
||||
|
||||
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)
|
||||
return nil
|
||||
}
|
||||
|
@ -2,10 +2,10 @@
|
||||
|
||||
package machine
|
||||
|
||||
import(
|
||||
import (
|
||||
"github.com/stretchr/testify/assert"
|
||||
"log"
|
||||
"testing"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func setupStater(initial State) Stater {
|
||||
@ -58,8 +58,8 @@ func TestMachineAddSubscription(t *testing.T) {
|
||||
assert.Nil(t, s.AddSubscription("connect", x))
|
||||
s.AddModel(setupModel("disconnected"))
|
||||
s.Trigger("connect")
|
||||
exitMessage := <- *x.(*EventChannel)
|
||||
enterMessage := <- *x.(*EventChannel)
|
||||
exitMessage := <-*x.(*EventChannel)
|
||||
enterMessage := <-*x.(*EventChannel)
|
||||
if exitMessage.on == EXITSTATEEVENT && exitMessage.source == "disconnected" {
|
||||
if enterMessage.on == ENTERSTATEEVENT && enterMessage.dest == "start_connection" {
|
||||
return
|
||||
|
2
model.go
2
model.go
@ -13,7 +13,7 @@ type Model struct {
|
||||
}
|
||||
|
||||
func NewModel(initial State) Modeler {
|
||||
return &Model{ state: initial }
|
||||
return &Model{state: initial}
|
||||
}
|
||||
|
||||
func (m *Model) Trigger(transition Transitioner) error {
|
||||
|
3
state.go
3
state.go
@ -7,6 +7,5 @@ type mState struct {
|
||||
}
|
||||
|
||||
func NewState(name string) *mState {
|
||||
return &mState{ name: name }
|
||||
return &mState{name: name}
|
||||
}
|
||||
|
||||
|
@ -3,8 +3,8 @@
|
||||
package machine
|
||||
|
||||
import (
|
||||
_ "errors"
|
||||
"fmt"
|
||||
"errors"
|
||||
)
|
||||
|
||||
type Transitioner interface {
|
||||
@ -20,7 +20,7 @@ type Transition struct {
|
||||
}
|
||||
|
||||
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 {
|
||||
@ -33,7 +33,7 @@ func (r *Transition) Run(m Modeler) error {
|
||||
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) {
|
||||
@ -41,7 +41,7 @@ func (r *Transition) Subscribe(s Subscriber) {
|
||||
}
|
||||
|
||||
func (r *Transition) Notify(on Eventtype, source State, dest State) {
|
||||
for _,s := range r.subscriptions {
|
||||
s.Notify(&EventMessage{ on: on, source: source, dest: dest})
|
||||
for _, s := range r.subscriptions {
|
||||
s.Notify(&EventMessage{on: on, source: source, dest: dest})
|
||||
}
|
||||
}
|
||||
|
@ -3,9 +3,9 @@
|
||||
package machine
|
||||
|
||||
import (
|
||||
"github.com/stretchr/testify/assert"
|
||||
"log"
|
||||
"testing"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func setupTransition() Transitioner {
|
||||
@ -44,8 +44,8 @@ func TestTransitionSubscribe(t *testing.T) {
|
||||
s.Subscribe(c)
|
||||
m := setupModel("closed")
|
||||
assert.Nil(t, s.Run(m))
|
||||
exitEvent := <- *c.(*EventChannel)
|
||||
enterEvent := <- *c.(*EventChannel)
|
||||
exitEvent := <-*c.(*EventChannel)
|
||||
enterEvent := <-*c.(*EventChannel)
|
||||
if exitEvent.on != EXITSTATEEVENT {
|
||||
t.Errorf("Invalid exit event")
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user