mirror of
https://github.com/kyverno/kyverno.git
synced 2025-03-30 19:35:06 +00:00
chore: add buffer unit tests (#7453)
Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>
This commit is contained in:
parent
1d2b50bc03
commit
f20c0ed417
2 changed files with 59 additions and 2 deletions
|
@ -1,6 +1,8 @@
|
|||
package patch
|
||||
|
||||
import "bytes"
|
||||
import (
|
||||
"bytes"
|
||||
)
|
||||
|
||||
// buffer is a wrapper around a slice of bytes used for JSON
|
||||
// marshal and unmarshal operations for a strategic merge patch
|
||||
|
@ -11,7 +13,6 @@ type buffer struct {
|
|||
// UnmarshalJSON writes the slice of bytes to an internal buffer
|
||||
func (buff buffer) UnmarshalJSON(b []byte) error {
|
||||
buff.Reset()
|
||||
|
||||
if _, err := buff.Write(b); err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
56
pkg/engine/mutate/patch/buffer_test.go
Normal file
56
pkg/engine/mutate/patch/buffer_test.go
Normal file
|
@ -0,0 +1,56 @@
|
|||
package patch
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func Test_buffer_UnmarshalJSON(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
Buffer *bytes.Buffer
|
||||
b []byte
|
||||
wantErr bool
|
||||
}{{
|
||||
Buffer: bytes.NewBufferString(""),
|
||||
b: []byte("aaa"),
|
||||
}}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
buff := buffer{
|
||||
Buffer: tt.Buffer,
|
||||
}
|
||||
if err := buff.UnmarshalJSON(tt.b); (err != nil) != tt.wantErr {
|
||||
t.Errorf("buffer.UnmarshalJSON() error = %v, wantErr %v", err, tt.wantErr)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func Test_buffer_MarshalJSON(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
Buffer *bytes.Buffer
|
||||
want []byte
|
||||
wantErr bool
|
||||
}{{
|
||||
Buffer: bytes.NewBufferString("aaa"),
|
||||
want: []byte("aaa"),
|
||||
}}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
buff := buffer{
|
||||
Buffer: tt.Buffer,
|
||||
}
|
||||
got, err := buff.MarshalJSON()
|
||||
if (err != nil) != tt.wantErr {
|
||||
t.Errorf("buffer.MarshalJSON() error = %v, wantErr %v", err, tt.wantErr)
|
||||
return
|
||||
}
|
||||
if !reflect.DeepEqual(got, tt.want) {
|
||||
t.Errorf("buffer.MarshalJSON() = %v, want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
Loading…
Add table
Reference in a new issue