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

     1  package gopter
     2  
     3  type propStatus int
     4  
     5  const (
     6  	// PropProof THe property was proved (i.e. it is known to be correct and will be always true)
     7  	PropProof propStatus = iota
     8  	// PropTrue The property was true this time
     9  	PropTrue
    10  	// PropFalse The property was false this time
    11  	PropFalse
    12  	// PropUndecided The property has no clear outcome this time
    13  	PropUndecided
    14  	// PropError The property has generated an error
    15  	PropError
    16  )
    17  
    18  func (s propStatus) String() string {
    19  	switch s {
    20  	case PropProof:
    21  		return "PROOF"
    22  	case PropTrue:
    23  		return "TRUE"
    24  	case PropFalse:
    25  		return "FALSE"
    26  	case PropUndecided:
    27  		return "UNDECIDED"
    28  	case PropError:
    29  		return "ERROR"
    30  	}
    31  	return ""
    32  }
    33  
    34  // PropResult contains the result of a property
    35  type PropResult struct {
    36  	Status     propStatus
    37  	Error      error
    38  	ErrorStack []byte
    39  	Args       []*PropArg
    40  	Labels     []string
    41  }
    42  
    43  // NewPropResult create a PropResult with label
    44  func NewPropResult(success bool, label string) *PropResult {
    45  	if success {
    46  		return &PropResult{
    47  			Status: PropTrue,
    48  			Labels: []string{label},
    49  			Args:   make([]*PropArg, 0),
    50  		}
    51  	}
    52  	return &PropResult{
    53  		Status: PropFalse,
    54  		Labels: []string{label},
    55  		Args:   make([]*PropArg, 0),
    56  	}
    57  }
    58  
    59  // Success checks if the result was successful
    60  func (r *PropResult) Success() bool {
    61  	return r.Status == PropTrue || r.Status == PropProof
    62  }
    63  
    64  // WithArgs sets argument descriptors to the PropResult for reporting
    65  func (r *PropResult) WithArgs(args []*PropArg) *PropResult {
    66  	r.Args = args
    67  	return r
    68  }
    69  
    70  // AddArgs add argument descriptors to the PropResult for reporting
    71  func (r *PropResult) AddArgs(args ...*PropArg) *PropResult {
    72  	r.Args = append(r.Args, args...)
    73  	return r
    74  }
    75  
    76  // And combines two PropResult by an and operation.
    77  // The resulting PropResult will be only true if both PropResults are true.
    78  func (r *PropResult) And(other *PropResult) *PropResult {
    79  	switch {
    80  	case r.Status == PropError:
    81  		return r
    82  	case other.Status == PropError:
    83  		return other
    84  	case r.Status == PropFalse:
    85  		return r
    86  	case other.Status == PropFalse:
    87  		return other
    88  	case r.Status == PropUndecided:
    89  		return r
    90  	case other.Status == PropUndecided:
    91  		return other
    92  	case r.Status == PropProof:
    93  		return r.mergeWith(other, other.Status)
    94  	case other.Status == PropProof:
    95  		return r.mergeWith(other, r.Status)
    96  	case r.Status == PropTrue && other.Status == PropTrue:
    97  		return r.mergeWith(other, PropTrue)
    98  	default:
    99  		return r
   100  	}
   101  }
   102  
   103  func (r *PropResult) mergeWith(other *PropResult, status propStatus) *PropResult {
   104  	return &PropResult{
   105  		Status: status,
   106  		Args:   append(append(make([]*PropArg, 0, len(r.Args)+len(other.Args)), r.Args...), other.Args...),
   107  		Labels: append(append(make([]string, 0, len(r.Labels)+len(other.Labels)), r.Labels...), other.Labels...),
   108  	}
   109  }