1
0
Fork 0
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:
Charles-Edouard Brétéché 2023-06-07 13:48:50 +02:00 committed by GitHub
parent 1d2b50bc03
commit f20c0ed417
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 59 additions and 2 deletions

View file

@ -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
}

View 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)
}
})
}
}