Skip to content

Commit

Permalink
fix: shouldEqual with int value (#341)
Browse files Browse the repository at this point in the history
Signed-off-by: Yvonnick Esnault <[email protected]>
  • Loading branch information
yesnault authored Nov 30, 2020
1 parent d307b52 commit 3adefcc
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 8 deletions.
21 changes: 14 additions & 7 deletions assertions/assertions.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,23 +63,30 @@ func Get(s string) (AssertFunc, bool) {
return f, ok
}

func deepEqual(x, y interface{}) bool {
if !reflect.DeepEqual(x, y) {
return fmt.Sprintf("%v", x) == fmt.Sprintf("%v", y)
}
return true
}

// ShouldEqual receives exactly two parameters and does an equality check.
func ShouldEqual(actual interface{}, expected ...interface{}) error {
if err := need(1, expected); err != nil {
return err
}
if reflect.DeepEqual(actual, expected[0]) {
if deepEqual(actual, expected[0]) {
return nil
}
return fmt.Errorf("expected: %v got: %v", expected[0], actual)
}

// ShouldEqual receives exactly two parameters and does an inequality check.
// ShouldNotEqual receives exactly two parameters and does an inequality check.
func ShouldNotEqual(actual interface{}, expected ...interface{}) error {
if err := need(1, expected); err != nil {
return err
}
if !reflect.DeepEqual(actual, expected[0]) {
if !deepEqual(actual, expected[0]) {
return nil
}
return fmt.Errorf("not expected: %v got: %v", expected[0], actual)
Expand Down Expand Up @@ -154,7 +161,7 @@ func ShouldBeNil(actual interface{}, expected ...interface{}) error {
return fmt.Errorf("expected: Nil but is wasn't")
}

// ShouldBeNil receives a single parameter and ensures that it is nil, blank or zero value
// ShouldNotExist receives a single parameter and ensures that it is nil, blank or zero value
func ShouldNotExist(actual interface{}, expected ...interface{}) error {
if ShouldBeNil(actual) != nil ||
ShouldBeBlank(actual) != nil ||
Expand Down Expand Up @@ -343,7 +350,7 @@ func ShouldBeLessThan(actual interface{}, expected ...interface{}) error {
return fmt.Errorf("expected: %v less than %v but it wasn't", actual, expected[0])
}

// ShouldBeLessThan receives exactly two parameters and ensures that the first is less than or equal to the second.
// ShouldBeLessThanOrEqualTo receives exactly two parameters and ensures that the first is less than or equal to the second.
func ShouldBeLessThanOrEqualTo(actual interface{}, expected ...interface{}) error {
if err := need(1, expected); err != nil {
return err
Expand Down Expand Up @@ -384,7 +391,7 @@ func ShouldBeLessThanOrEqualTo(actual interface{}, expected ...interface{}) erro
return fmt.Errorf("expected '%v' less than or equals to %v but it wasn't", actual, expected[0])
}

// ShouldBeLessThan receives exactly two parameters and ensures that the first is less than the second.
// ShouldBeBetween receives exactly two parameters and ensures that the first is less than the second.
func ShouldBeBetween(actual interface{}, expected ...interface{}) error {
if err := need(2, expected); err != nil {
return err
Expand Down Expand Up @@ -693,7 +700,7 @@ func ShouldEndWith(actual interface{}, expected ...interface{}) error {
return fmt.Errorf("expected '%v' have suffix %q but it wasn't", s, suffix)
}

// ShouldEndWith receives exactly 2 string parameters and ensures that the first does not end with the second.
// ShouldNotEndWith receives exactly 2 string parameters and ensures that the first does not end with the second.
func ShouldNotEndWith(actual interface{}, expected ...interface{}) error {
if err := need(1, expected); err != nil {
return err
Expand Down
7 changes: 7 additions & 0 deletions assertions/assertions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,13 @@ func TestShouldEqual(t *testing.T) {
expected: []interface{}{1.0},
},
},
{
name: "different types",
args: args{
actual: 42,
expected: []interface{}{"42"},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down
7 changes: 7 additions & 0 deletions tests/readfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,10 @@ testcases:
- result.err ShouldEqual ""
- result.md5sum.readfile_testa.json ShouldEqual "{{.testcase-readfile2.md5sum_foo}}"

- name: testcase-readfile-with-integer
steps:
- type: readfile
path: readfile/testa.json
info: "result.contentjson.total: {{.result.contentjson.total}}"
assertions:
- result.contentjson.total ShouldEqual 42
3 changes: 2 additions & 1 deletion tests/readfile/testa.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{
"foo": "bar"
"foo": "bar",
"total": 42
}

0 comments on commit 3adefcc

Please sign in to comment.