Skip to content

Commit

Permalink
Merge pull request #1 from cocoonspace/chore/tests
Browse files Browse the repository at this point in the history
add tests
  • Loading branch information
Jean-François Bustarret authored Oct 12, 2021
2 parents 6d6f210 + 9c36c9b commit dbe1e74
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 59 deletions.
52 changes: 0 additions & 52 deletions fms_test.go

This file was deleted.

9 changes: 2 additions & 7 deletions fsm.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
package fsm

import (
"log"
)

// Event is the event type.
// You can define your own values as
// const (
Expand Down Expand Up @@ -114,7 +110,6 @@ func On(e Event) Option {
func Dst(s State) Option {
return func(t *transition) {
t.actions = append(t.actions, func(fsm *FSM) {
log.Println("[fsm] changing state from", fsm.current, "to", s)
fsm.current = s
})
}
Expand Down Expand Up @@ -183,15 +178,15 @@ 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, f); res != resultNOK {
if res := f.transitions[i].match(e, f.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 = 1
f.times = 0
}
return res == resultOK
}
Expand Down
115 changes: 115 additions & 0 deletions fsm_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
package fsm_test

import (
"fmt"
"testing"

"github.com/cocoonspace/fsm"
)

const (
StateFoo fsm.State = iota
StateBar
)

const (
EventFoo fsm.Event = iota
)

func TestFSM(t *testing.T) {
f := fsm.New(StateFoo)
f.Transition(
fsm.On(EventFoo), fsm.Src(StateFoo),
fsm.Dst(StateBar),
)
res := f.Event(EventFoo)
if !res {
t.Error("Event returned false")
}
if f.Current() != StateBar {
t.Error("Bad destination state")
}
}

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

func ExampleCheck() {
f := fsm.New(StateFoo)
f.Transition(
fsm.On(EventFoo), fsm.Src(StateFoo), fsm.Check(func() bool {
return true
}),
fsm.Dst(StateBar),
)
}

func TestCall(t *testing.T) {
call := false
f := fsm.New(StateFoo)
f.Transition(
fsm.On(EventFoo), fsm.Src(StateFoo),
fsm.Call(func() {
call = true
}),
)
_ = f.Event(EventFoo)
if !call {
t.Error("Call should have been called")
}
}

func ExampleCall() {
f := fsm.New(StateFoo)
f.Transition(
fsm.On(EventFoo), fsm.Src(StateFoo),
fsm.Dst(StateBar), fsm.Call(func() {
fmt.Println("Call called")
}),
)
}

func TestTimes(t *testing.T) {
f := fsm.New(StateFoo)
f.Transition(
fsm.On(EventFoo), fsm.Src(StateFoo), fsm.Times(2),
fsm.Dst(StateBar),
)

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
if !res || f.Current() != StateBar {
t.Error("Transition should happen the second time")
}
}

func ExampleTimes() {
f := fsm.New(StateFoo)
f.Transition(
fsm.On(EventFoo), fsm.Src(StateFoo), fsm.Times(2),
fsm.Dst(StateBar),
)

_ = f.Event(EventFoo) // no transition
_ = f.Event(EventFoo) // transition to StateBar
}

0 comments on commit dbe1e74

Please sign in to comment.