diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index 39922910..5fcde733 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -1,6 +1,11 @@ # 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) + - fix: failed to build debugtalk.go without go.mod - 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 diff --git a/hrp/internal/builtin/assertion.go b/hrp/internal/builtin/assertion.go index 96fd4310..20473727 100644 --- a/hrp/internal/builtin/assertion.go +++ b/hrp/internal/builtin/assertion.go @@ -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 } diff --git a/hrp/internal/builtin/assertion_test.go b/hrp/internal/builtin/assertion_test.go index aa7d4bde..3a2ebc89 100644 --- a/hrp/internal/builtin/assertion_test.go +++ b/hrp/internal/builtin/assertion_test.go @@ -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() } } diff --git a/hrp/step_request.go b/hrp/step_request.go index 28b174ce..f890a93e 100644 --- a/hrp/step_request.go +++ b/hrp/step_request.go @@ -1065,6 +1065,17 @@ func (s *StepRequestValidation) AssertStringEqual(jmesPath string, expected inte 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 { v := Validator{ Check: jmesPath, diff --git a/hrp/tests/function_test.go b/hrp/tests/function_test.go index c6acc493..a4cc5d01 100644 --- a/hrp/tests/function_test.go +++ b/hrp/tests/function_test.go @@ -28,7 +28,7 @@ func TestCaseCallFunction(t *testing.T) { AssertLengthEqual("body.args.foo1", 5, "check args foo1"). AssertEqual("body.args.foo2", "12.3", "check args foo2"). 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 "). 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").