Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: assertion with json number #546

Merged
merged 3 commits into from
May 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
129 changes: 85 additions & 44 deletions assertions/assertions.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package assertions

import (
"encoding/json"
"fmt"
"math"
"reflect"
Expand Down Expand Up @@ -261,24 +262,33 @@ func ShouldBeGreaterThan(actual interface{}, expected ...interface{}) error {
return newAssertionError(needSameType)
}

actualF, err := cast.ToFloat64E(actual)
if err != nil {
actualS, err := cast.ToStringE(actual)
var actualF float64
var err error
switch x := actual.(type) {
case json.Number:
actualF, err = x.Float64()
if err != nil {
return err
}

expectedS, err := cast.ToStringE(expected[0])
default:
actualF, err = cast.ToFloat64E(actual)
if err != nil {
return err
}
actualS, err := cast.ToStringE(actual)
if err != nil {
return err
}

if actualS > expectedS {
return nil
}
expectedS, err := cast.ToStringE(expected[0])
if err != nil {
return err
}

return fmt.Errorf("expected: %v greater than %v but it wasn't", actual, expected[0])
if actualS > expectedS {
return nil
}

return fmt.Errorf("expected: %v greater than %v but it wasn't", actual, expected[0])
}
}

expectedF, err := cast.ToFloat64E(expected[0])
Expand All @@ -303,24 +313,34 @@ func ShouldBeGreaterThanOrEqualTo(actual interface{}, expected ...interface{}) e
return newAssertionError(needSameType)
}

actualF, err := cast.ToFloat64E(actual)
if err != nil {
actualS, err := cast.ToStringE(actual)
var actualF float64
var err error
switch x := actual.(type) {
case json.Number:
actualF, err = x.Float64()
if err != nil {
return err
}

expectedS, err := cast.ToStringE(expected[0])
default:
actualF, err = cast.ToFloat64E(actual)
if err != nil {
return err
}
actualS, err := cast.ToStringE(actual)
if err != nil {
return err
}

if actualS >= expectedS {
return nil
}
expectedS, err := cast.ToStringE(expected[0])
if err != nil {
return err
}

return fmt.Errorf("expected: %v greater than or equals to %v but it wasn't", actual, expected[0])
if actualS >= expectedS {
return nil
}

return fmt.Errorf("expected: %v greater than or equals to %v but it wasn't", actual, expected[0])

}
}

expectedF, err := cast.ToFloat64E(expected[0])
Expand All @@ -345,23 +365,34 @@ func ShouldBeLessThan(actual interface{}, expected ...interface{}) error {
return newAssertionError(needSameType)
}

actualF, err := cast.ToFloat64E(actual)
if err != nil {
actualS, err := cast.ToStringE(actual)
var actualF float64
var err error
switch x := actual.(type) {
case json.Number:
actualF, err = x.Float64()
if err != nil {
return err
}

expectedS, err := cast.ToStringE(expected[0])
default:
actualF, err = cast.ToFloat64E(actual)
if err != nil {
return err
}
actualS, err := cast.ToStringE(actual)
if err != nil {
return err
}

if actualS < expectedS {
return nil
}
expectedS, err := cast.ToStringE(expected[0])
if err != nil {
return err
}

return fmt.Errorf("expected: %v less than %v but it wasn't", actual, expected[0])
if actualS < expectedS {
return nil
}

return fmt.Errorf("expected: %v less than %v but it wasn't", actual, expected[0])

}
}

expectedF, err := cast.ToFloat64E(expected[0])
Expand All @@ -386,23 +417,33 @@ func ShouldBeLessThanOrEqualTo(actual interface{}, expected ...interface{}) erro
return newAssertionError(needSameType)
}

actualF, err := cast.ToFloat64E(actual)
if err != nil {
actualS, err := cast.ToStringE(actual)
var actualF float64
var err error
switch x := actual.(type) {
case json.Number:
actualF, err = x.Float64()
if err != nil {
return err
}

expectedS, err := cast.ToStringE(expected[0])
default:
actualF, err = cast.ToFloat64E(actual)
if err != nil {
return err
}
actualS, err := cast.ToStringE(actual)
if err != nil {
return err
}

if actualS <= expectedS {
return nil
}
expectedS, err := cast.ToStringE(expected[0])
if err != nil {
return err
}

if actualS <= expectedS {
return nil
}

return fmt.Errorf("expected: %v less than or equals to %v but it wasn't", actual, expected[0])
return fmt.Errorf("expected: %v less than or equals to %v but it wasn't", actual, expected[0])
}
}

expectedF, err := cast.ToFloat64E(expected[0])
Expand Down
18 changes: 17 additions & 1 deletion tests/assertions/ShouldBeGreaterThan.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,20 @@ testcases:
steps:
- script: echo '["foo", "bar", "foo2"]'
assertions:
- result.systemoutjson.__len__ ShouldBeGreaterThan 2
- result.systemoutjson.__len__ ShouldBeGreaterThan 2

- name: ShouldBeGreaterThan100
steps:
- type: exec
script: echo 100
assertions:
- result.code ShouldEqual 0
- result.systemout ShouldBeGreaterThan 29

- name: ShouldBeGreaterThan100FromJSON
steps:
- type: exec
script: echo '{"nb":100}'
assertions:
- result.code ShouldEqual 0
- result.systemoutjson.nb ShouldBeGreaterThan 29