Skip to content

Commit

Permalink
[refactor] use action.run in Replay
Browse files Browse the repository at this point in the history
  • Loading branch information
untoldwind committed May 18, 2020
1 parent 4977b4b commit 8de0d74
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 13 deletions.
6 changes: 3 additions & 3 deletions commands/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,18 @@ func (a *actions) String() string {
return fmt.Sprintf("initialState=%v sequential=%s", a.initialStateProvider(), a.sequentialCommands)
}

func (a *actions) run(systemUnderTest SystemUnderTest) (*gopter.PropResult, error) {
func (a *actions) run(systemUnderTest SystemUnderTest) *gopter.PropResult {
state := a.initialStateProvider()
propResult := &gopter.PropResult{Status: gopter.PropTrue}
for _, shrinkableCommand := range a.sequentialCommands {
if !shrinkableCommand.command.PreCondition(state) {
return &gopter.PropResult{Status: gopter.PropFalse}, nil
return &gopter.PropResult{Status: gopter.PropFalse}
}
result := shrinkableCommand.command.Run(systemUnderTest)
state = shrinkableCommand.command.NextState(state)
propResult = propResult.And(shrinkableCommand.command.PostCondition(state, result))
}
return propResult, nil
return propResult
}

type sizedCommands struct {
Expand Down
2 changes: 1 addition & 1 deletion commands/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func (p *ProtoCommands) InitialPreCondition(state State) bool {

// Prop creates a gopter.Prop from Commands
func Prop(commands Commands) gopter.Prop {
return prop.ForAll(func(actions *actions) (*gopter.PropResult, error) {
return prop.ForAll(func(actions *actions) *gopter.PropResult {
systemUnderTest := commands.NewSystemUnderTest(actions.initialStateProvider())
defer commands.DestroySystemUnderTest(systemUnderTest)

Expand Down
16 changes: 7 additions & 9 deletions commands/replay.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,13 @@ import (

// Replay a sequence of commands on a system for regression testing
func Replay(systemUnderTest SystemUnderTest, initialState State, commands ...Command) *gopter.PropResult {
state := initialState
propResult := &gopter.PropResult{Status: gopter.PropTrue}
sequentialCommands := make([]shrinkableCommand, 0, len(commands))
for _, command := range commands {
if !command.PreCondition(state) {
return &gopter.PropResult{Status: gopter.PropFalse}
}
result := command.Run(systemUnderTest)
state = command.NextState(state)
propResult = propResult.And(command.PostCondition(state, result))
sequentialCommands = append(sequentialCommands, shrinkableCommand{command: command, shrinker: gopter.NoShrinker})
}
return propResult
actions := actions{
initialStateProvider: func() State { return initialState },
sequentialCommands: sequentialCommands,
}
return actions.run(systemUnderTest)
}

0 comments on commit 8de0d74

Please sign in to comment.