From 9bbbc455e35f046a33301fbe8f973f2b2a288e93 Mon Sep 17 00:00:00 2001 From: William Poussier Date: Mon, 17 Jun 2024 15:35:47 +0200 Subject: [PATCH] test: small improvements to coverage --- .golangci.yaml | 2 +- apply_test.go | 13 ++++++++++++- compare.go | 7 ++----- compare_test.go | 49 ++++++++++++++++++++++++++++++++++++++++++++++++- equal.go | 8 ++------ 5 files changed, 65 insertions(+), 14 deletions(-) diff --git a/.golangci.yaml b/.golangci.yaml index 9c6e21c..bf977ae 100644 --- a/.golangci.yaml +++ b/.golangci.yaml @@ -1,5 +1,5 @@ run: - go: "1.21" + go: "1.22" timeout: 10m linters: disable-all: true diff --git a/apply_test.go b/apply_test.go index f31de3c..5015895 100644 --- a/apply_test.go +++ b/apply_test.go @@ -1,6 +1,8 @@ package jsondiff -import "testing" +import ( + "testing" +) func Test_toDotPath(t *testing.T) { for _, tc := range []struct { @@ -80,3 +82,12 @@ func Test_isArrayIndex(t *testing.T) { } } } + +func TestPatch_apply_invalidJSON(t *testing.T) { + badJSON := []byte(`{"example":2:]}}`) + + var p Patch + if _, err := p.apply(badJSON, true); err == nil { + t.Errorf("expected non-nil error") + } +} diff --git a/compare.go b/compare.go index debc3b0..19a7c98 100644 --- a/compare.go +++ b/compare.go @@ -34,11 +34,8 @@ func CompareWithoutMarshal(source, target interface{}, opts ...Option) (patch Pa defer func() { if r := recover(); r != nil { - if e, ok := r.(invalidJSONTypeError); ok { - err = e - } else { - err = fmt.Errorf("recovered from panic: %q", r) - } + e := r.(invalidJSONTypeError) + err = fmt.Errorf("jsondiff: invalid json type: %T", e.t) patch = nil } }() diff --git a/compare_test.go b/compare_test.go index 0dabe39..13b67e3 100644 --- a/compare_test.go +++ b/compare_test.go @@ -1,6 +1,7 @@ package jsondiff import ( + "bytes" "encoding/json" "errors" "testing" @@ -31,7 +32,7 @@ func Test_marshalUnmarshal_invalid_JSON(t *testing.T) { }) } -func TestCompareWithoutMarshal(t *testing.T) { +func TestCompareWithoutMarshal_error(t *testing.T) { type custom struct { foo string bar int @@ -42,3 +43,49 @@ func TestCompareWithoutMarshal(t *testing.T) { } t.Log(err) } + +type skip struct{} + +var skipBytes = []byte("skip") + +func Test_compare_marshaling_error(t *testing.T) { + e := errors.New("") + + d := Differ{opts: options{ + marshal: func(a any) ([]byte, error) { + if _, ok := a.(skip); ok { + return skipBytes, nil + } + return nil, e + }, + unmarshal: func(b []byte, _ any) error { + if bytes.Equal(b, skipBytes) { + return nil + } + return e + }, + }} + if _, err := compare(&d, nil, nil); !errors.Is(err, e) { + t.Errorf("expected non-nil error") + } + if _, err := compare(&d, skip{}, nil); !errors.Is(err, e) { + t.Errorf("expected non-nil error") + } +} + +func Test_compareJSON_marshaling_error(t *testing.T) { + e := errors.New("") + + unmarshalFn := func(b []byte, _ any) error { + if bytes.Equal(b, skipBytes) { + return nil + } + return e + } + if _, err := compareJSON(nil, nil, nil, unmarshalFn); !errors.Is(err, e) { + t.Errorf("expected non-nil error") + } + if _, err := compareJSON(nil, []byte("skip"), nil, unmarshalFn); !errors.Is(err, e) { + t.Errorf("expected non-nil error") + } +} diff --git a/equal.go b/equal.go index 9bcce55..21d16f5 100644 --- a/equal.go +++ b/equal.go @@ -2,7 +2,6 @@ package jsondiff import ( "encoding/json" - "fmt" "strconv" ) @@ -10,10 +9,6 @@ type invalidJSONTypeError struct { t any } -func (e invalidJSONTypeError) Error() string { - return fmt.Sprintf("jsondiff: invalid json type: %T", e.t) -} - // jsonValueType represents the type of JSON value. // It follows the types of values stored by json.Unmarshal // in interface values. @@ -122,8 +117,9 @@ func deepEqualValue(src, tgt interface{}) bool { } } return true + default: + panic("unexpected json type") } - return false } var jsonTypeNames = []string{