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

This commit is contained in:
debugtalk
2022-06-03 23:29:03 +08:00
parent 174f07bac0
commit f1fd4a518d
5 changed files with 46 additions and 2 deletions
+5
View File
@@ -1,6 +1,11 @@
# Release History # Release History
## v4.1.2 (2022-06-03)
- fix #1331: use `str_eq` to assert string and digit equality
## v4.1.1 (2022-05-31) ## v4.1.1 (2022-05-31)
- fix: failed to build debugtalk.go without go.mod - fix: failed to build debugtalk.go without go.mod
- fix: avoid to escape from html special characters like '&' in converted JSON testcase - fix: avoid to escape from html special characters like '&' in converted JSON testcase
- fix: display the full step name when referencing testcase in html report - fix: display the full step name when referencing testcase in html report
+7
View File
@@ -45,6 +45,7 @@ var Assertions = map[string]func(t assert.TestingT, actual interface{}, expected
"contained_by": ContainedBy, "contained_by": ContainedBy,
"str_eq": StringEqual, "str_eq": StringEqual,
"string_equals": StringEqual, "string_equals": StringEqual,
"equal_fold": EqualFold,
"regex_match": RegexMatch, "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 { 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) { if !assert.IsType(t, "string", actual, msgAndArgs) {
return false return false
} }
+22 -1
View File
@@ -158,6 +158,27 @@ func TestContainedBy(t *testing.T) {
} }
func TestStringEqual(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 { testData := []struct {
raw interface{} raw interface{}
expected interface{} expected interface{}
@@ -168,7 +189,7 @@ func TestStringEqual(t *testing.T) {
} }
for _, data := range testData { 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() t.Fatal()
} }
} }
+11
View File
@@ -1065,6 +1065,17 @@ func (s *StepRequestValidation) AssertStringEqual(jmesPath string, expected inte
return s return s
} }
func (s *StepRequestValidation) AssertEqualFold(jmesPath string, expected interface{}, msg string) *StepRequestValidation {
v := Validator{
Check: jmesPath,
Assert: "equal_fold",
Expect: expected,
Message: msg,
}
s.step.Validators = append(s.step.Validators, v)
return s
}
func (s *StepRequestValidation) AssertLengthLessOrEquals(jmesPath string, expected interface{}, msg string) *StepRequestValidation { func (s *StepRequestValidation) AssertLengthLessOrEquals(jmesPath string, expected interface{}, msg string) *StepRequestValidation {
v := Validator{ v := Validator{
Check: jmesPath, Check: jmesPath,
+1 -1
View File
@@ -28,7 +28,7 @@ func TestCaseCallFunction(t *testing.T) {
AssertLengthEqual("body.args.foo1", 5, "check args foo1"). AssertLengthEqual("body.args.foo1", 5, "check args foo1").
AssertEqual("body.args.foo2", "12.3", "check args foo2"). AssertEqual("body.args.foo2", "12.3", "check args foo2").
AssertTypeMatch("body.args.foo3", "str", "check args foo3 is type string"). AssertTypeMatch("body.args.foo3", "str", "check args foo3 is type string").
AssertStringEqual("body.args.foo3", "foo3", "check args foo3 case-insensitivity"). AssertEqualFold("body.args.foo3", "foo3", "check args foo3 case-insensitivity").
AssertContains("body.args.foo3", "Foo", "check contains "). AssertContains("body.args.foo3", "Foo", "check contains ").
AssertContainedBy("body.args.foo3", "this is Foo3 test", "check contained by"), // notice: request params value will be converted to string AssertContainedBy("body.args.foo3", "this is Foo3 test", "check contained by"), // notice: request params value will be converted to string
hrp.NewStep("post json data with functions"). hrp.NewStep("post json data with functions").