Skip to content

Commit

Permalink
chore: add a few unit tests (#232)
Browse files Browse the repository at this point in the history
Signed-off-by: Charles-Edouard Brétéché <[email protected]>
  • Loading branch information
eddycharly authored Nov 28, 2023
1 parent 1094cc3 commit 5ba1828
Showing 1 changed file with 94 additions and 0 deletions.
94 changes: 94 additions & 0 deletions pkg/apis/v1alpha1/any_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
})
}
}

0 comments on commit 5ba1828

Please sign in to comment.