Skip to content

Commit

Permalink
Merge pull request #1 from davidae/fix-mismatch-on-integer
Browse files Browse the repository at this point in the history
Check matches that are not string
  • Loading branch information
davidae authored Jul 6, 2020
2 parents bbd3c82 + bb14162 commit bec3398
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
2 changes: 1 addition & 1 deletion match.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func isEqualValue(expected, actual interface{}, ph []Placeholder) error {
for _, p := range ph {
expectedStr, ok := expected.(string)
if !ok {
return nil
continue
}

if key, fn := p(); key == expectedStr {
Expand Down
29 changes: 29 additions & 0 deletions match_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package jm
import (
"io/ioutil"
"testing"
"time"
)

func TestEqualJSON(t *testing.T) {
Expand Down Expand Up @@ -83,6 +84,34 @@ func TestNotEqualJSONWithUnexpectedKey(t *testing.T) {
}
}

func TestEqualWithIntegerMismatch(t *testing.T) {
var (
expected = `{"id": "1","count":2}`
actual = `{"id": "1","count":4}`
expectedErrMsg = "value 2 and 4: values are not equal"
)

if err := Match([]byte(expected), []byte(actual), WithTimeLayout("$TIME_RFC3339", time.RFC3339)); err == nil {
t.Error("expected a mismatch on 'count'")
} else if err.Error() != expectedErrMsg {
t.Errorf("expected error message %s to be, but got %s", expectedErrMsg, err)
}
}

func TestEqualWithArrayIntegerMismatch(t *testing.T) {
var (
expected = `{"id": "1","count":[1,2,3,4,5,6]}`
actual = `{"id": "1","count":[1,2,3,4,"s",6]}`
expectedErrMsg = `value 5 and "s": values are not equal`
)

if err := Match([]byte(expected), []byte(actual), WithTimeLayout("$TIME_RFC3339", time.RFC3339)); err == nil {
t.Error("expected a mismatch on 'count'")
} else if err.Error() != expectedErrMsg {
t.Errorf("expected error message %s to be, but got %s", expectedErrMsg, err)
}
}

func TestEmptyBytes(t *testing.T) {
expectedErrMsg := "unexpected end of JSON input"

Expand Down

0 comments on commit bec3398

Please sign in to comment.