From 31ca5c14d33addfd6d8bb86e2c29861bb70d3089 Mon Sep 17 00:00:00 2001 From: Pavel Karpy Date: Tue, 8 Jun 2021 15:15:45 +0300 Subject: [PATCH] [#302] pkg/container/id: Document default values set in `New` Document field values of instance constructed via `New`. Assert the values in corresponding unit test. Signed-off-by: Pavel Karpy --- pkg/container/id/id.go | 5 ++++- pkg/container/id/id_test.go | 24 ++++++++++++++++++++---- 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/pkg/container/id/id.go b/pkg/container/id/id.go index b09c7b33..351c9b31 100644 --- a/pkg/container/id/id.go +++ b/pkg/container/id/id.go @@ -20,6 +20,9 @@ func NewFromV2(idV2 *refs.ContainerID) *ID { } // New creates and initializes blank ID. +// +// Defaults: +// - value: nil. func New() *ID { return NewFromV2(new(refs.ContainerID)) } @@ -31,7 +34,7 @@ func (id *ID) SetSHA256(v [sha256.Size]byte) { // ToV2 returns the v2 container ID message. // -// Nil Result converts to nil. +// Nil ID converts to nil. func (id *ID) ToV2() *refs.ContainerID { return (*refs.ContainerID)(id) } diff --git a/pkg/container/id/id_test.go b/pkg/container/id/id_test.go index e0a266ee..b633cb4b 100644 --- a/pkg/container/id/id_test.go +++ b/pkg/container/id/id_test.go @@ -33,6 +33,14 @@ func TestID_ToV2(t *testing.T) { require.Nil(t, x.ToV2()) }) + + t.Run("default values", func(t *testing.T) { + cid := cid.New() + + // convert to v2 message + cidV2 := cid.ToV2() + require.Nil(t, cidV2.GetValue()) + }) } func TestID_Equal(t *testing.T) { @@ -49,11 +57,19 @@ func TestID_Equal(t *testing.T) { } func TestID_String(t *testing.T) { - id := cidtest.Generate() - id2 := cid.New() + t.Run("Parse/String", func(t *testing.T) { + id := cidtest.Generate() + id2 := cid.New() - require.NoError(t, id2.Parse(id.String())) - require.Equal(t, id, id2) + require.NoError(t, id2.Parse(id.String())) + require.Equal(t, id, id2) + }) + + t.Run("nil", func(t *testing.T) { + id := cid.New() + + require.Empty(t, id.String()) + }) } func TestContainerIDEncoding(t *testing.T) {