Skip to content

Commit

Permalink
test: improve coverage of side cases
Browse files Browse the repository at this point in the history
  • Loading branch information
wI2L committed Jun 2, 2023
1 parent d148a82 commit 7130e0c
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 3 deletions.
38 changes: 38 additions & 0 deletions equal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,44 @@ func Test_jsonTypeSwitch(t *testing.T) {
}
}

func Test_deepEqualValue(t *testing.T) {
for _, tt := range []struct {
src, tgt interface{}
equal bool
}{
{
nil,
nil,
true,
},
{
"foobar",
"foobar",
true,
},
{
true,
false,
false,
},
{
3.14,
3.14,
true,
},
{
json.Number("42.69"),
json.Number("69.42"),
false,
},
} {
ok := deepEqualValue(tt.src, tt.tgt)
if ok != tt.equal {
t.Errorf("got %t, want %t", ok, tt.equal)
}
}
}

func Test_deepEqual_invalid_type(t *testing.T) {
defer func() {
if err := recover(); err == nil {
Expand Down
23 changes: 20 additions & 3 deletions operation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package jsondiff

import "testing"

func TestOperationMarshalJSON(t *testing.T) {
func TestOperation_MarshalJSON(t *testing.T) {
for _, tc := range []struct {
Op Operation
Out string // marshaled output
Expand Down Expand Up @@ -101,7 +101,7 @@ func TestOperationMarshalJSON(t *testing.T) {
}
}

func TestPatchString(t *testing.T) {
func TestPatch_String(t *testing.T) {
patch := Patch{
{
Type: OperationReplace,
Expand Down Expand Up @@ -129,7 +129,7 @@ func TestPatchString(t *testing.T) {
}
}

func TestNilPatchString(t *testing.T) {
func TestPatch_String_nil(t *testing.T) {
p := new(Patch)
s := p.String()

Expand All @@ -138,6 +138,23 @@ func TestNilPatchString(t *testing.T) {
}
}

func TestPatch_jsonLength(t *testing.T) {
t.Run("empty", func(t *testing.T) {
p := new(Patch)
l := p.jsonLength()
if l != 0 {
t.Error("expected zero length for empty patch")
}
})
t.Run("nil", func(t *testing.T) {
var p *Patch
l := p.jsonLength()
if l != 0 {
t.Error("expected zero length for nil patch")
}
})
}

func typeNilIface() interface{} {
var i *int
var p interface{}
Expand Down

0 comments on commit 7130e0c

Please sign in to comment.