fix #1331: use str_eq to assert string and digit equality

This commit is contained in:
debugtalk
2022-06-03 23:18:58 +08:00
parent 174f07bac0
commit f1fd4a518d
5 changed files with 46 additions and 2 deletions

View File

@@ -45,6 +45,7 @@ var Assertions = map[string]func(t assert.TestingT, actual interface{}, expected
"contained_by": ContainedBy,
"str_eq": StringEqual,
"string_equals": StringEqual,
"equal_fold": EqualFold,
"regex_match": RegexMatch,
}
@@ -157,6 +158,12 @@ func ContainedBy(t assert.TestingT, actual, expected interface{}, msgAndArgs ...
}
func StringEqual(t assert.TestingT, actual, expected interface{}, msgAndArgs ...interface{}) bool {
a := fmt.Sprintf("%v", actual)
e := fmt.Sprintf("%v", expected)
return assert.True(t, a == e, msgAndArgs)
}
func EqualFold(t assert.TestingT, actual, expected interface{}, msgAndArgs ...interface{}) bool {
if !assert.IsType(t, "string", actual, msgAndArgs) {
return false
}

View File

@@ -158,6 +158,27 @@ func TestContainedBy(t *testing.T) {
}
func TestStringEqual(t *testing.T) {
testData := []struct {
raw interface{}
expected interface{}
}{
{"abcd", "abcd"},
{"0", 0},
{"123", 123},
// {"123.0", 123.0}, // FIXME
{"12.3", 12.3},
{"-12.3", -12.3},
{"-123", -123},
}
for _, data := range testData {
if !assert.True(t, StringEqual(t, data.raw, data.expected)) {
t.Fatal()
}
}
}
func TestEqualFold(t *testing.T) {
testData := []struct {
raw interface{}
expected interface{}
@@ -168,7 +189,7 @@ func TestStringEqual(t *testing.T) {
}
for _, data := range testData {
if !assert.True(t, StringEqual(t, data.raw, data.expected)) {
if !assert.True(t, EqualFold(t, data.raw, data.expected)) {
t.Fatal()
}
}