Skip to content

Commit

Permalink
test: small improvements to coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
wI2L committed Jun 17, 2024
1 parent 71ffe13 commit 9bbbc45
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 14 deletions.
2 changes: 1 addition & 1 deletion .golangci.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
run:
go: "1.21"
go: "1.22"
timeout: 10m
linters:
disable-all: true
Expand Down
13 changes: 12 additions & 1 deletion apply_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package jsondiff

import "testing"
import (
"testing"
)

func Test_toDotPath(t *testing.T) {
for _, tc := range []struct {
Expand Down Expand Up @@ -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")
}
}
7 changes: 2 additions & 5 deletions compare.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}()
Expand Down
49 changes: 48 additions & 1 deletion compare_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package jsondiff

import (
"bytes"
"encoding/json"
"errors"
"testing"
Expand Down Expand Up @@ -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
Expand All @@ -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")
}
}
8 changes: 2 additions & 6 deletions equal.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,13 @@ package jsondiff

import (
"encoding/json"
"fmt"
"strconv"
)

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.
Expand Down Expand Up @@ -122,8 +117,9 @@ func deepEqualValue(src, tgt interface{}) bool {
}
}
return true
default:
panic("unexpected json type")
}
return false
}

var jsonTypeNames = []string{
Expand Down

0 comments on commit 9bbbc45

Please sign in to comment.