fix: contains assertion bug; feat: add type_match, contained_by, string_equals assertion methods

Change-Id: Icb15fe33a58d1ff69991435bbe70c3cd53bb8dea
This commit is contained in:
buyuxiang
2021-12-30 14:47:10 +08:00
parent 10fbc7f5c4
commit b22a5d2af0
3 changed files with 71 additions and 3 deletions

View File

@@ -15,13 +15,16 @@ var Assertions = map[string]func(t assert.TestingT, expected interface{}, actual
"greater_or_equals": assert.GreaterOrEqual,
"less_or_equals": assert.LessOrEqual,
"not_equal": assert.NotEqual,
"contains": assert.Contains,
"contained_by": assert.Contains,
"regex_match": assert.Regexp,
"type_match": assert.IsType,
// custom assertions
"startswith": StartsWith, // check if string starts with substring
"endswith": EndsWith, // check if string ends with substring
"length_equals": EqualLength,
"length_equal": EqualLength, // alias for length_equals
"contains": Contains,
"string_equals": EqualString,
}
func StartsWith(t assert.TestingT, expected, actual interface{}, msgAndArgs ...interface{}) bool {
@@ -83,3 +86,20 @@ func convertInt(value interface{}) (int, error) {
return 0, fmt.Errorf("unsupported int convertion for %v(%T)", v, v)
}
}
// Contains assert whether actual
func Contains(t assert.TestingT, expected, actual interface{}, msgAndArgs ...interface{}) bool {
return assert.Contains(t, actual, expected, msgAndArgs)
}
func EqualString(t assert.TestingT, expected, actual interface{}, msgAndArgs ...interface{}) bool {
if !assert.IsType(t, "string", actual, msgAndArgs) {
return false
}
if !assert.IsType(t, "string", expected, msgAndArgs) {
return false
}
actualString := actual.(string)
expectedString := expected.(string)
return assert.True(t, strings.EqualFold(actualString, expectedString), msgAndArgs)
}