github.com/onsi/gomega@v1.32.0/matchers/type_support.go (about)

     1  /*
     2  Gomega matchers
     3  
     4  This package implements the Gomega matchers and does not typically need to be imported.
     5  See the docs for Gomega for documentation on the matchers
     6  
     7  http://onsi.github.io/gomega/
     8  */
     9  
    10  // untested sections: 11
    11  
    12  package matchers
    13  
    14  import (
    15  	"encoding/json"
    16  	"fmt"
    17  	"reflect"
    18  )
    19  
    20  type omegaMatcher interface {
    21  	Match(actual interface{}) (success bool, err error)
    22  	FailureMessage(actual interface{}) (message string)
    23  	NegatedFailureMessage(actual interface{}) (message string)
    24  }
    25  
    26  func isBool(a interface{}) bool {
    27  	return reflect.TypeOf(a).Kind() == reflect.Bool
    28  }
    29  
    30  func isNumber(a interface{}) bool {
    31  	if a == nil {
    32  		return false
    33  	}
    34  	kind := reflect.TypeOf(a).Kind()
    35  	return reflect.Int <= kind && kind <= reflect.Float64
    36  }
    37  
    38  func isInteger(a interface{}) bool {
    39  	kind := reflect.TypeOf(a).Kind()
    40  	return reflect.Int <= kind && kind <= reflect.Int64
    41  }
    42  
    43  func isUnsignedInteger(a interface{}) bool {
    44  	kind := reflect.TypeOf(a).Kind()
    45  	return reflect.Uint <= kind && kind <= reflect.Uint64
    46  }
    47  
    48  func isFloat(a interface{}) bool {
    49  	kind := reflect.TypeOf(a).Kind()
    50  	return reflect.Float32 <= kind && kind <= reflect.Float64
    51  }
    52  
    53  func toInteger(a interface{}) int64 {
    54  	if isInteger(a) {
    55  		return reflect.ValueOf(a).Int()
    56  	} else if isUnsignedInteger(a) {
    57  		return int64(reflect.ValueOf(a).Uint())
    58  	} else if isFloat(a) {
    59  		return int64(reflect.ValueOf(a).Float())
    60  	}
    61  	panic(fmt.Sprintf("Expected a number!  Got <%T> %#v", a, a))
    62  }
    63  
    64  func toUnsignedInteger(a interface{}) uint64 {
    65  	if isInteger(a) {
    66  		return uint64(reflect.ValueOf(a).Int())
    67  	} else if isUnsignedInteger(a) {
    68  		return reflect.ValueOf(a).Uint()
    69  	} else if isFloat(a) {
    70  		return uint64(reflect.ValueOf(a).Float())
    71  	}
    72  	panic(fmt.Sprintf("Expected a number!  Got <%T> %#v", a, a))
    73  }
    74  
    75  func toFloat(a interface{}) float64 {
    76  	if isInteger(a) {
    77  		return float64(reflect.ValueOf(a).Int())
    78  	} else if isUnsignedInteger(a) {
    79  		return float64(reflect.ValueOf(a).Uint())
    80  	} else if isFloat(a) {
    81  		return reflect.ValueOf(a).Float()
    82  	}
    83  	panic(fmt.Sprintf("Expected a number!  Got <%T> %#v", a, a))
    84  }
    85  
    86  func isError(a interface{}) bool {
    87  	_, ok := a.(error)
    88  	return ok
    89  }
    90  
    91  func isChan(a interface{}) bool {
    92  	if isNil(a) {
    93  		return false
    94  	}
    95  	return reflect.TypeOf(a).Kind() == reflect.Chan
    96  }
    97  
    98  func isMap(a interface{}) bool {
    99  	if a == nil {
   100  		return false
   101  	}
   102  	return reflect.TypeOf(a).Kind() == reflect.Map
   103  }
   104  
   105  func isArrayOrSlice(a interface{}) bool {
   106  	if a == nil {
   107  		return false
   108  	}
   109  	switch reflect.TypeOf(a).Kind() {
   110  	case reflect.Array, reflect.Slice:
   111  		return true
   112  	default:
   113  		return false
   114  	}
   115  }
   116  
   117  func isString(a interface{}) bool {
   118  	if a == nil {
   119  		return false
   120  	}
   121  	return reflect.TypeOf(a).Kind() == reflect.String
   122  }
   123  
   124  func toString(a interface{}) (string, bool) {
   125  	aString, isString := a.(string)
   126  	if isString {
   127  		return aString, true
   128  	}
   129  
   130  	aBytes, isBytes := a.([]byte)
   131  	if isBytes {
   132  		return string(aBytes), true
   133  	}
   134  
   135  	aStringer, isStringer := a.(fmt.Stringer)
   136  	if isStringer {
   137  		return aStringer.String(), true
   138  	}
   139  
   140  	aJSONRawMessage, isJSONRawMessage := a.(json.RawMessage)
   141  	if isJSONRawMessage {
   142  		return string(aJSONRawMessage), true
   143  	}
   144  
   145  	return "", false
   146  }
   147  
   148  func lengthOf(a interface{}) (int, bool) {
   149  	if a == nil {
   150  		return 0, false
   151  	}
   152  	switch reflect.TypeOf(a).Kind() {
   153  	case reflect.Map, reflect.Array, reflect.String, reflect.Chan, reflect.Slice:
   154  		return reflect.ValueOf(a).Len(), true
   155  	default:
   156  		return 0, false
   157  	}
   158  }
   159  func capOf(a interface{}) (int, bool) {
   160  	if a == nil {
   161  		return 0, false
   162  	}
   163  	switch reflect.TypeOf(a).Kind() {
   164  	case reflect.Array, reflect.Chan, reflect.Slice:
   165  		return reflect.ValueOf(a).Cap(), true
   166  	default:
   167  		return 0, false
   168  	}
   169  }
   170  
   171  func isNil(a interface{}) bool {
   172  	if a == nil {
   173  		return true
   174  	}
   175  
   176  	switch reflect.TypeOf(a).Kind() {
   177  	case reflect.Chan, reflect.Func, reflect.Interface, reflect.Map, reflect.Ptr, reflect.Slice:
   178  		return reflect.ValueOf(a).IsNil()
   179  	}
   180  
   181  	return false
   182  }