Skip to content

Commit

Permalink
rewrite TestStateMachineActions to use TB.Skip
Browse files Browse the repository at this point in the history
  • Loading branch information
prashantv authored and flyingmutant committed Apr 1, 2024
1 parent cee6e9b commit fec3f59
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 16 deletions.
3 changes: 2 additions & 1 deletion statemachine.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ type StateMachine interface {
// Check is ran after every action and should contain invariant checks.
//
// All other public methods should have a form ActionName(t *rapid.T)
// and are used as possible actions. At least one action has to be specified.
// or ActionName(t rapid.TB) and are used as possible actions.
// At least one action has to be specified.
Check(*T)
}

Expand Down
60 changes: 45 additions & 15 deletions statemachine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,26 +240,56 @@ type stateMachineTest struct {
run []string
}

func (sm *stateMachineTest) ActionA(t *T) {
sm.run = append(sm.run, "actionA")
func (sm *stateMachineTest) Check(t *T) {}

func (sm *stateMachineTest) ActionT(t *T) {
sm.run = append(sm.run, "ActionT")
}

func (sm *stateMachineTest) ActionTB(t TB) {
if len(sm.run) > 2 {
// Add a value to run that isn't expected to ensure the post-action check is skipped.
sm.run = append(sm.run, "Post-Skip")
t.Skip()
}

sm.run = append(sm.run, "ActionTB")
}
func (sm *stateMachineTest) ActionB(t TB) { sm.run = append(sm.run, "actionB") }
func (sm *stateMachineTest) ActionC(t *T) { sm.run = append(sm.run, "actionC") }
func (sm *stateMachineTest) Check(*T) {}

func TestStateMachineActions(t *testing.T) {
Check(t, func(t *T) {
t.Run("Check", MakeCheck(func(t *T) {
sm := &stateMachineTest{}
actions := StateMachineActions(sm)
actions["ActionA"](t)
actions["ActionB"](t)
actions["ActionC"](t)

if want := []string{
"actionA",
"actionB",
"actionC",
}; !reflect.DeepEqual(want, sm.run) {

actionT, ok := actions["ActionT"]
if !ok {
t.Fatalf("ActionA missing")
}
actionTB, ok := actions["ActionTB"]
if !ok {
t.Fatalf("ActionTB missing")
}

var want []string
for i := 0; i < Int().Draw(t, "ActionT acount"); i++ {
actionT(t)
want = append(want, "ActionT")
}

for i := 0; i < Int().Draw(t, "ActionTB acount"); i++ {
actionTB(t)
want = append(want, "ActionTB")
}

if !reflect.DeepEqual(want, sm.run) {
t.Fatalf("expected state %v, got %v", want, sm.run)
}
}))

t.Run("directly use action with testing.T", func(t *testing.T) {
sm := &stateMachineTest{}
sm.ActionTB(t)
if want := []string{"ActionTB"}; !reflect.DeepEqual(want, sm.run) {
t.Fatalf("expected state %v, got %v", want, sm.run)
}
})
Expand Down

0 comments on commit fec3f59

Please sign in to comment.