github.com/joomcode/pegomock@v2.9.2-0.20220414140958-14f53b6b2a6c+incompatible/matcher.go (about)

     1  package pegomock
     2  
     3  import (
     4  	"fmt"
     5  	"reflect"
     6  
     7  	"github.com/petergtz/pegomock/internal/verify"
     8  	"sync"
     9  )
    10  
    11  type EqMatcher struct {
    12  	Value  Param
    13  	actual Param
    14  	sync.Mutex
    15  }
    16  
    17  func (matcher *EqMatcher) Matches(param Param) bool {
    18  	matcher.Lock()
    19  	defer matcher.Unlock()
    20  
    21  	matcher.actual = param
    22  	return reflect.DeepEqual(matcher.Value, param)
    23  }
    24  
    25  func (matcher *EqMatcher) FailureMessage() string {
    26  	return fmt.Sprintf("Expected: %v; but got: %v", matcher.Value, matcher.actual)
    27  }
    28  
    29  func (matcher *EqMatcher) String() string {
    30  	return fmt.Sprintf("Eq(%#v)", matcher.Value)
    31  }
    32  
    33  type NotEqMatcher struct {
    34  	Value Param
    35  }
    36  
    37  func (matcher *NotEqMatcher) Matches(param Param) bool {
    38  	return !reflect.DeepEqual(matcher.Value, param)
    39  }
    40  
    41  func (matcher *NotEqMatcher) FailureMessage() string {
    42  	return fmt.Sprintf("Expected: not %v; but got: %v", matcher.Value, matcher.Value)
    43  }
    44  
    45  func (matcher *NotEqMatcher) String() string {
    46  	return fmt.Sprintf("NotEq(%#v)", matcher.Value)
    47  }
    48  
    49  type AnyMatcher struct {
    50  	Type   reflect.Type
    51  	actual reflect.Type
    52  	sync.Mutex
    53  }
    54  
    55  func NewAnyMatcher(typ reflect.Type) *AnyMatcher {
    56  	verify.Argument(typ != nil, "Must provide a non-nil type")
    57  	return &AnyMatcher{Type: typ}
    58  }
    59  
    60  func (matcher *AnyMatcher) Matches(param Param) bool {
    61  	matcher.Lock()
    62  	defer matcher.Unlock()
    63  
    64  	matcher.actual = reflect.TypeOf(param)
    65  	if matcher.actual == nil {
    66  		switch matcher.Type.Kind() {
    67  		case reflect.Chan, reflect.Func, reflect.Interface, reflect.Map,
    68  			reflect.Ptr, reflect.Slice, reflect.UnsafePointer:
    69  			return true
    70  		default:
    71  			return false
    72  		}
    73  	}
    74  	return matcher.actual.AssignableTo(matcher.Type)
    75  }
    76  
    77  func (matcher *AnyMatcher) FailureMessage() string {
    78  	return fmt.Sprintf("Expected: %v; but got: %v", matcher.Type, matcher.actual)
    79  }
    80  
    81  func (matcher *AnyMatcher) String() string {
    82  	return fmt.Sprintf("Any(%v)", matcher.Type)
    83  }
    84  
    85  type AtLeastIntMatcher struct {
    86  	Value  int
    87  	actual int
    88  }
    89  
    90  func (matcher *AtLeastIntMatcher) Matches(param Param) bool {
    91  	matcher.actual = param.(int)
    92  	return param.(int) >= matcher.Value
    93  }
    94  
    95  func (matcher *AtLeastIntMatcher) FailureMessage() string {
    96  	return fmt.Sprintf("Expected: at least %v; but got: %v", matcher.Value, matcher.actual)
    97  }
    98  
    99  func (matcher *AtLeastIntMatcher) String() string {
   100  	return fmt.Sprintf("AtLeast(%v)", matcher.Value)
   101  }
   102  
   103  type AtMostIntMatcher struct {
   104  	Value  int
   105  	actual int
   106  }
   107  
   108  func (matcher *AtMostIntMatcher) Matches(param Param) bool {
   109  	matcher.actual = param.(int)
   110  	return param.(int) <= matcher.Value
   111  }
   112  
   113  func (matcher *AtMostIntMatcher) FailureMessage() string {
   114  	return fmt.Sprintf("Expected: at most %v; but got: %v", matcher.Value, matcher.actual)
   115  }
   116  
   117  func (matcher *AtMostIntMatcher) String() string {
   118  	return fmt.Sprintf("AtMost(%v)", matcher.Value)
   119  }