github.com/leanovate/gopter@v0.2.9/commands/command.go (about)

     1  package commands
     2  
     3  import "github.com/leanovate/gopter"
     4  
     5  // SystemUnderTest resembles the system under test, which may be any kind
     6  // of stateful unit of code
     7  type SystemUnderTest interface{}
     8  
     9  // State resembles the state the system under test is expected to be in
    10  type State interface{}
    11  
    12  // Result resembles the result of a command that may or may not be checked
    13  type Result interface{}
    14  
    15  // Command is any kind of command that may be applied to the system under test
    16  type Command interface {
    17  	// Run applies the command to the system under test
    18  	Run(systemUnderTest SystemUnderTest) Result
    19  	// NextState calculates the next expected state if the command is applied
    20  	NextState(state State) State
    21  	// PreCondition checks if the state is valid before the command is applied
    22  	PreCondition(state State) bool
    23  	// PostCondition checks if the state is valid after the command is applied
    24  	PostCondition(state State, result Result) *gopter.PropResult
    25  	// String gets a (short) string representation of the command
    26  	String() string
    27  }
    28  
    29  // ProtoCommand is a prototype implementation of the Command interface
    30  type ProtoCommand struct {
    31  	Name              string
    32  	RunFunc           func(systemUnderTest SystemUnderTest) Result
    33  	NextStateFunc     func(state State) State
    34  	PreConditionFunc  func(state State) bool
    35  	PostConditionFunc func(state State, result Result) *gopter.PropResult
    36  }
    37  
    38  // Run applies the command to the system under test
    39  func (p *ProtoCommand) Run(systemUnderTest SystemUnderTest) Result {
    40  	if p.RunFunc != nil {
    41  		return p.RunFunc(systemUnderTest)
    42  	}
    43  	return nil
    44  }
    45  
    46  // NextState calculates the next expected state if the command is applied
    47  func (p *ProtoCommand) NextState(state State) State {
    48  	if p.NextStateFunc != nil {
    49  		return p.NextStateFunc(state)
    50  	}
    51  	return state
    52  }
    53  
    54  // PreCondition checks if the state is valid before the command is applied
    55  func (p *ProtoCommand) PreCondition(state State) bool {
    56  	if p.PreConditionFunc != nil {
    57  		return p.PreConditionFunc(state)
    58  	}
    59  	return true
    60  }
    61  
    62  // PostCondition checks if the state is valid after the command is applied
    63  func (p *ProtoCommand) PostCondition(state State, result Result) *gopter.PropResult {
    64  	if p.PostConditionFunc != nil {
    65  		return p.PostConditionFunc(state, result)
    66  	}
    67  	return &gopter.PropResult{Status: gopter.PropTrue}
    68  }
    69  
    70  func (p *ProtoCommand) String() string {
    71  	return p.Name
    72  }