vendor/github.com/stretchr/testify/assert/assertions.go
changeset 251 1c52a0eeb952
parent 242 2a9ec03fe5a1
child 256 6d9efbef00a9
equal deleted inserted replaced
250:c040f992052f 251:1c52a0eeb952
    37 
    37 
    38 // BoolAssertionFunc is a common function prototype when validating a bool value.  Can be useful
    38 // BoolAssertionFunc is a common function prototype when validating a bool value.  Can be useful
    39 // for table driven tests.
    39 // for table driven tests.
    40 type BoolAssertionFunc func(TestingT, bool, ...interface{}) bool
    40 type BoolAssertionFunc func(TestingT, bool, ...interface{}) bool
    41 
    41 
    42 // ValuesAssertionFunc is a common function prototype when validating an error value.  Can be useful
    42 // ErrorAssertionFunc is a common function prototype when validating an error value.  Can be useful
    43 // for table driven tests.
    43 // for table driven tests.
    44 type ErrorAssertionFunc func(TestingT, error, ...interface{}) bool
    44 type ErrorAssertionFunc func(TestingT, error, ...interface{}) bool
    45 
    45 
    46 // Comparison a custom function that returns true on success and false on failure
    46 // Comparison a custom function that returns true on success and false on failure
    47 type Comparison func() (success bool)
    47 type Comparison func() (success bool)
   177 func messageFromMsgAndArgs(msgAndArgs ...interface{}) string {
   177 func messageFromMsgAndArgs(msgAndArgs ...interface{}) string {
   178 	if len(msgAndArgs) == 0 || msgAndArgs == nil {
   178 	if len(msgAndArgs) == 0 || msgAndArgs == nil {
   179 		return ""
   179 		return ""
   180 	}
   180 	}
   181 	if len(msgAndArgs) == 1 {
   181 	if len(msgAndArgs) == 1 {
   182 		return msgAndArgs[0].(string)
   182 		msg := msgAndArgs[0]
       
   183 		if msgAsStr, ok := msg.(string); ok {
       
   184 			return msgAsStr
       
   185 		}
       
   186 		return fmt.Sprintf("%+v", msg)
   183 	}
   187 	}
   184 	if len(msgAndArgs) > 1 {
   188 	if len(msgAndArgs) > 1 {
   185 		return fmt.Sprintf(msgAndArgs[0].(string), msgAndArgs[1:]...)
   189 		return fmt.Sprintf(msgAndArgs[0].(string), msgAndArgs[1:]...)
   186 	}
   190 	}
   187 	return ""
   191 	return ""
   413 		return true
   417 		return true
   414 	}
   418 	}
   415 	return Fail(t, "Expected value not to be nil.", msgAndArgs...)
   419 	return Fail(t, "Expected value not to be nil.", msgAndArgs...)
   416 }
   420 }
   417 
   421 
       
   422 // containsKind checks if a specified kind in the slice of kinds.
       
   423 func containsKind(kinds []reflect.Kind, kind reflect.Kind) bool {
       
   424 	for i := 0; i < len(kinds); i++ {
       
   425 		if kind == kinds[i] {
       
   426 			return true
       
   427 		}
       
   428 	}
       
   429 
       
   430 	return false
       
   431 }
       
   432 
   418 // isNil checks if a specified object is nil or not, without Failing.
   433 // isNil checks if a specified object is nil or not, without Failing.
   419 func isNil(object interface{}) bool {
   434 func isNil(object interface{}) bool {
   420 	if object == nil {
   435 	if object == nil {
   421 		return true
   436 		return true
   422 	}
   437 	}
   423 
   438 
   424 	value := reflect.ValueOf(object)
   439 	value := reflect.ValueOf(object)
   425 	kind := value.Kind()
   440 	kind := value.Kind()
   426 	if kind >= reflect.Chan && kind <= reflect.Slice && value.IsNil() {
   441 	isNilableKind := containsKind(
       
   442 		[]reflect.Kind{
       
   443 			reflect.Chan, reflect.Func,
       
   444 			reflect.Interface, reflect.Map,
       
   445 			reflect.Ptr, reflect.Slice},
       
   446 		kind)
       
   447 
       
   448 	if isNilableKind && value.IsNil() {
   427 		return true
   449 		return true
   428 	}
   450 	}
   429 
   451 
   430 	return false
   452 	return false
   431 }
   453 }
  1325 	}
  1347 	}
  1326 	return t, k
  1348 	return t, k
  1327 }
  1349 }
  1328 
  1350 
  1329 // diff returns a diff of both values as long as both are of the same type and
  1351 // diff returns a diff of both values as long as both are of the same type and
  1330 // are a struct, map, slice or array. Otherwise it returns an empty string.
  1352 // are a struct, map, slice, array or string. Otherwise it returns an empty string.
  1331 func diff(expected interface{}, actual interface{}) string {
  1353 func diff(expected interface{}, actual interface{}) string {
  1332 	if expected == nil || actual == nil {
  1354 	if expected == nil || actual == nil {
  1333 		return ""
  1355 		return ""
  1334 	}
  1356 	}
  1335 
  1357 
  1343 	if ek != reflect.Struct && ek != reflect.Map && ek != reflect.Slice && ek != reflect.Array && ek != reflect.String {
  1365 	if ek != reflect.Struct && ek != reflect.Map && ek != reflect.Slice && ek != reflect.Array && ek != reflect.String {
  1344 		return ""
  1366 		return ""
  1345 	}
  1367 	}
  1346 
  1368 
  1347 	var e, a string
  1369 	var e, a string
  1348 	if ek != reflect.String {
  1370 	if et != reflect.TypeOf("") {
  1349 		e = spewConfig.Sdump(expected)
  1371 		e = spewConfig.Sdump(expected)
  1350 		a = spewConfig.Sdump(actual)
  1372 		a = spewConfig.Sdump(actual)
  1351 	} else {
  1373 	} else {
  1352 		e = expected.(string)
  1374 		e = expected.(string)
  1353 		a = actual.(string)
  1375 		a = actual.(string)