Skip to content

Commit

Permalink
fix: handle json number is assertions (#460)
Browse files Browse the repository at this point in the history
close #449

Signed-off-by: francois  samin <[email protected]>
  • Loading branch information
fsamin authored Dec 9, 2021
1 parent fe3d592 commit 4d549ad
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
32 changes: 32 additions & 0 deletions assertions/helper.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package assertions

import (
"encoding/json"
"fmt"
"reflect"
)
Expand Down Expand Up @@ -49,8 +50,39 @@ func isNil(i interface{}) bool {
}

func areSameTypes(i, j interface{}) bool {
var err error
i, j, err = handleJSONNumber(i, j)
if err != nil {
return false
}
return reflect.DeepEqual(
reflect.Zero(reflect.TypeOf(i)).Interface(),
reflect.Zero(reflect.TypeOf(j)).Interface(),
)
}

func handleJSONNumber(actual interface{}, expected interface{}) (interface{}, interface{}, error) {
jsNumber, is := actual.(json.Number)
if !is {
return actual, expected, nil
}

switch expected.(type) {
case string:
return jsNumber.String(), expected, nil
case int64:
i, err := jsNumber.Int64()
if err != nil {
return actual, expected, err
}
return i, expected, nil
case float64:
f, err := jsNumber.Float64()
if err != nil {
return actual, expected, err
}
return f, expected, nil
}

return jsNumber, expected, nil
}
12 changes: 11 additions & 1 deletion tests/assertions/ShouldBeLessThan.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,14 @@ testcases:
steps:
- script: echo 2.4
assertions:
- result.systemout ShouldBeLessThan 2.5
- result.systemout ShouldBeLessThan 2.5

- name: bodyjson assert comparator test
steps:
- type: http
method: GET
url: https://eu.api.ovh.com/1.0/sms/rates/destinations?billingCountry=fr&country=fr
headers:
Content-type: application/json
assertions:
- result.bodyjson.credit ShouldBeLessThan 2

0 comments on commit 4d549ad

Please sign in to comment.