Skip to content

Commit

Permalink
test: improve coverage for Operation and Patch types methods
Browse files Browse the repository at this point in the history
  • Loading branch information
wI2L committed Oct 20, 2022
1 parent e80bf39 commit 5fbf127
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
2 changes: 1 addition & 1 deletion operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ func (o Operation) marshalWithValue() bool {

// String implements the fmt.Stringer interface.
func (p *Patch) String() string {
if p == nil {
if p == nil || len(*p) == 0 {
return ""
}
sb := strings.Builder{}
Expand Down
37 changes: 37 additions & 0 deletions operation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,40 @@ func TestOperationMarshalJSON(t *testing.T) {
}
}
}

func TestPatchString(t *testing.T) {
patch := Patch{
{
Type: OperationReplace,
Path: "/foo/baz",
Value: 42,
},
{
Type: OperationRemove,
Path: "/xxx",
},
{
Type: OperationAdd,
Path: "/zzz",
Value: make(chan<- string), // UnsupportedTypeError
},
}
s := patch.String()

const expected = `{"op":"replace","path":"/foo/baz","value":42}
{"op":"remove","path":"/xxx"}
<invalid operation>`

if s != expected {
t.Errorf("stringified operation mismatch, got %q, want %q", s, expected)
}
}

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

if s != "" {
t.Errorf("stringified operation mismatch, got %q, wantempty string", s)
}
}

0 comments on commit 5fbf127

Please sign in to comment.