Skip to content

Commit

Permalink
more tests (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jean-François Bustarret authored Oct 12, 2021
1 parent dbe1e74 commit b0f4c04
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 4 deletions.
8 changes: 6 additions & 2 deletions fsm.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,15 +178,19 @@ func (f *FSM) Current() State {
// true is returned if a transition is applied.
func (f *FSM) Event(e Event) bool {
for i := range f.transitions {
if res := f.transitions[i].match(e, f.times+1, f); res != resultNOK {
times := f.times
if i != f.previous {
times = 0
}
if res := f.transitions[i].match(e, times+1, f); res != resultNOK {
if res == resultOK {
f.transitions[i].apply(f)
}
if i == f.previous {
f.times++
} else {
f.previous = i
f.times = 0
f.times = 1
}
return res == resultOK
}
Expand Down
45 changes: 43 additions & 2 deletions fsm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const (

const (
EventFoo fsm.Event = iota
EventBar
)

func TestFSM(t *testing.T) {
Expand All @@ -29,6 +30,10 @@ func TestFSM(t *testing.T) {
if f.Current() != StateBar {
t.Error("Bad destination state")
}
f.Reset()
if f.Current() != StateFoo {
t.Error("Bad state after Reset")
}
}

func TestCheck(t *testing.T) {
Expand All @@ -47,7 +52,7 @@ func TestCheck(t *testing.T) {
check = true
res = f.Event(EventFoo)
if !res && f.Current() != StateBar {
t.Error("Transition should happen because of Check")
t.Error("Transition should happen thanks to Check")
}
}

Expand All @@ -61,6 +66,26 @@ func ExampleCheck() {
)
}

func TestNotCheck(t *testing.T) {
check := true
f := fsm.New(StateFoo)
f.Transition(
fsm.On(EventFoo), fsm.Src(StateFoo), fsm.NotCheck(func() bool {
return check
}),
fsm.Dst(StateBar),
)
res := f.Event(EventFoo)
if res || f.Current() == StateBar {
t.Error("Transition should not happen because of NotCheck")
}
check = false
res = f.Event(EventFoo)
if !res && f.Current() != StateBar {
t.Error("Transition should happen thanks to NotCheck")
}
}

func TestCall(t *testing.T) {
call := false
f := fsm.New(StateFoo)
Expand Down Expand Up @@ -92,15 +117,31 @@ func TestTimes(t *testing.T) {
fsm.On(EventFoo), fsm.Src(StateFoo), fsm.Times(2),
fsm.Dst(StateBar),
)
f.Transition(
fsm.On(EventBar), fsm.Src(StateBar),
fsm.Dst(StateFoo),
)

res := f.Event(EventFoo)
if res || f.Current() == StateBar {
t.Error("Transition should not happen the first time")
}
res = f.Event(EventFoo) // transition to StateBar
res = f.Event(EventFoo)
if !res || f.Current() != StateBar {
t.Error("Transition should happen the second time")
}
res = f.Event(EventBar)
if !res || f.Current() != StateFoo {
t.Error("FSM should have returned to StateFoo")
}
res = f.Event(EventFoo)
if res || f.Current() == StateBar {
t.Error("Transition should not happen the first time of the second run")
}
res = f.Event(EventFoo)
if !res || f.Current() != StateBar {
t.Error("Transition should happen the second time of the second run")
}
}

func ExampleTimes() {
Expand Down

0 comments on commit b0f4c04

Please sign in to comment.