From 5ba18286e93ffe3a903d45846ccd0f33c9915186 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Charles-Edouard=20Br=C3=A9t=C3=A9ch=C3=A9?= Date: Tue, 28 Nov 2023 22:05:04 +0100 Subject: [PATCH] chore: add a few unit tests (#232) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Charles-Edouard Brétéché --- pkg/apis/v1alpha1/any_test.go | 94 +++++++++++++++++++++++++++++++++++ 1 file changed, 94 insertions(+) diff --git a/pkg/apis/v1alpha1/any_test.go b/pkg/apis/v1alpha1/any_test.go index f76df421..95b85a5c 100644 --- a/pkg/apis/v1alpha1/any_test.go +++ b/pkg/apis/v1alpha1/any_test.go @@ -35,3 +35,97 @@ func TestAny_DeepCopyInto(t *testing.T) { }) } } + +func TestAny_MarshalJSON(t *testing.T) { + tests := []struct { + name string + value interface{} + want []byte + wantErr bool + }{{ + name: "nil", + value: nil, + want: []byte("null"), + wantErr: false, + }, { + name: "int", + value: 42, + want: []byte("42"), + wantErr: false, + }, { + name: "string", + value: "foo", + want: []byte(`"foo"`), + wantErr: false, + }, { + name: "map", + value: map[string]interface{}{"foo": 42}, + want: []byte(`{"foo":42}`), + wantErr: false, + }, { + name: "error", + value: func() {}, + want: nil, + wantErr: true, + }} + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + a := &Any{ + Value: tt.value, + } + got, err := a.MarshalJSON() + if tt.wantErr { + assert.Error(t, err) + } else { + assert.NoError(t, err) + } + assert.Equal(t, tt.want, got) + }) + } +} + +func TestAny_UnmarshalJSON(t *testing.T) { + tests := []struct { + name string + data []byte + want *Any + wantErr bool + }{{ + name: "nil", + data: []byte("null"), + want: &Any{}, + wantErr: false, + }, { + name: "int", + data: []byte("42"), + want: &Any{Value: 42.0}, + wantErr: false, + }, { + name: "string", + data: []byte(`"foo"`), + want: &Any{Value: "foo"}, + wantErr: false, + }, { + name: "map", + data: []byte(`{"foo":42}`), + want: &Any{Value: map[string]interface{}{"foo": 42.0}}, + wantErr: false, + }, { + name: "error", + data: []byte(`{"foo":`), + want: nil, + wantErr: true, + }} + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + a := &Any{} + err := a.UnmarshalJSON(tt.data) + if tt.wantErr { + assert.Error(t, err) + } else { + assert.NoError(t, err) + assert.Equal(t, tt.want, a) + } + }) + } +}