From 425df2ab257b942cf68fc110fd2cb96d6120d0eb Mon Sep 17 00:00:00 2001 From: JK <3783092+masterjk@users.noreply.github.com> Date: Sat, 24 Oct 2020 15:36:12 -0500 Subject: [PATCH] Added Tests. (#3) * Limit trigger on release pipeline to pushes for tags with specific pattern. * Added some unit tests. * Added coverage-html as makefile target. --- Makefile | 4 ++++ cbor/array_test.go | 3 +++ cbor/positive.go | 20 ++++++++++++++++++++ cbor/positive_test.go | 26 ++++++++++++++++++++++++++ multiplex/container.go | 8 -------- multiplex/header_test.go | 10 ++++++++++ multiplex/mini_protocol_test.go | 18 ++++++++++++++++++ 7 files changed, 81 insertions(+), 8 deletions(-) create mode 100644 multiplex/mini_protocol_test.go diff --git a/Makefile b/Makefile index 8b0ca1e..caa820e 100644 --- a/Makefile +++ b/Makefile @@ -33,6 +33,10 @@ coverage: -v `pwd`:/go/src/${PROJECT_SRCDIR} \ -w /go/src/${PROJECT_SRCDIR} ${GOLANG_IMAGE} go test -coverprofile=.cov ./...; go tool cover -func .cov +coverage-html: + @go test -coverprofile=.cov ./...; go tool cover -func .cov + @go tool cover -html=.cov + build: @echo ➭ Building ${PROJECT_NAME} @docker run "${DOCKER_ARGS}" \ diff --git a/cbor/array_test.go b/cbor/array_test.go index 1ea3b05..d23eb86 100644 --- a/cbor/array_test.go +++ b/cbor/array_test.go @@ -89,6 +89,9 @@ func TestArray(t *testing.T) { } assert.Equal(t, testCase.input, c[0].(*Array).doEncodeCBOR(testCase.fixedLength)) + if c[0].(*Array).Length() > 0 { + assert.True(t, len(c[0].(*Array).ValuesAsString()) > 0) + } } } diff --git a/cbor/positive.go b/cbor/positive.go index a8fe39e..e04ad64 100644 --- a/cbor/positive.go +++ b/cbor/positive.go @@ -71,6 +71,16 @@ func (p *PositiveInteger16) ValueAsUint16() uint16 { return uint16(p.V) } +// ValueAsUint32 return value as uint32 +func (p *PositiveInteger16) ValueAsUint32() uint32 { + return uint32(p.V) +} + +// ValueAsUint64 return value as uint64 +func (p *PositiveInteger16) ValueAsUint64() uint64 { + return uint64(p.V) +} + // NewPositiveInteger32 returns a positive integer (32 bits) instance func NewPositiveInteger32(value uint32) *PositiveInteger32 { return &PositiveInteger32{ @@ -83,6 +93,11 @@ func (p *PositiveInteger32) ValueAsUint32() uint32 { return uint32(p.V) } +// ValueAsUint64 return value as uint64 +func (p *PositiveInteger32) ValueAsUint64() uint64 { + return uint64(p.V) +} + // NewPositiveInteger64 returns a positive integer (64 bits) instance func NewPositiveInteger64(value uint64) *PositiveInteger64 { return &PositiveInteger64{ @@ -90,6 +105,11 @@ func NewPositiveInteger64(value uint64) *PositiveInteger64 { } } +// ValueAsUint64 return value as uint64 +func (p *PositiveInteger64) ValueAsUint64() uint64 { + return uint64(p.V) +} + // NewPositiveInteger returns a positive integer using the most compact // struct that would fit the value func NewPositiveInteger(value uint64) DataItem { diff --git a/cbor/positive_test.go b/cbor/positive_test.go index 488cb59..afa19bd 100644 --- a/cbor/positive_test.go +++ b/cbor/positive_test.go @@ -1,6 +1,7 @@ package cbor import ( + "math" "testing" "github.com/stretchr/testify/assert" @@ -62,3 +63,28 @@ func TestPositiveUnsignedInt(t *testing.T) { assert.Equal(t, testCase.input, c[0].EncodeCBOR()) } } + +func TestNewPositiveUnsignedInt(t *testing.T) { + + // PositiveInteger8 + value8 := NewPositiveInteger(1) + assert.Equal(t, uint8(1), value8.(*PositiveInteger8).ValueAsUint8()) + assert.Equal(t, uint16(1), value8.(*PositiveInteger8).ValueAsUint16()) + assert.Equal(t, uint32(1), value8.(*PositiveInteger8).ValueAsUint32()) + assert.Equal(t, uint64(1), value8.(*PositiveInteger8).ValueAsUint64()) + + // PositiveInteger16 + value16 := NewPositiveInteger(math.MaxUint8 + 1) + assert.Equal(t, uint16(math.MaxUint8+1), value16.(*PositiveInteger16).ValueAsUint16()) + assert.Equal(t, uint32(math.MaxUint8+1), value16.(*PositiveInteger16).ValueAsUint32()) + assert.Equal(t, uint64(math.MaxUint8+1), value16.(*PositiveInteger16).ValueAsUint64()) + + // PositiveInteger32 + value32 := NewPositiveInteger(math.MaxUint16 + 1) + assert.Equal(t, uint32(math.MaxUint16+1), value32.(*PositiveInteger32).ValueAsUint32()) + assert.Equal(t, uint64(math.MaxUint16+1), value32.(*PositiveInteger32).ValueAsUint64()) + + // PositiveInteger64 + value64 := NewPositiveInteger(math.MaxUint32 + 1) + assert.Equal(t, uint64(math.MaxUint32+1), value64.(*PositiveInteger64).ValueAsUint64()) +} diff --git a/multiplex/container.go b/multiplex/container.go index 5f44b41..46e4181 100644 --- a/multiplex/container.go +++ b/multiplex/container.go @@ -58,14 +58,6 @@ func NewContainer(miniProtocol MiniProtocol, containerMode ContainerMode, array } } -// NewContainerMuxControl returns a new container for mux control -func NewContainerMuxControl(containerMode ContainerMode, array *cbor.Array) *Container { - return &Container{ - header: NewHeader(MiniProtocolIDMuxControl, containerMode, 0), - dataItems: []cbor.DataItem{array}, - } -} - // DataItems returns the dataItems associated with this container func (c *Container) DataItems() []cbor.DataItem { return c.dataItems diff --git a/multiplex/header_test.go b/multiplex/header_test.go index c0d1dc3..7430379 100644 --- a/multiplex/header_test.go +++ b/multiplex/header_test.go @@ -13,12 +13,22 @@ func TestNewHeader(t *testing.T) { assert.Equal(t, uint16(1), h.miniProtocol.Value()) assert.Equal(t, ContainerModeInitiator, h.ContainerMode()) assert.Equal(t, uint16(2), h.payloadLength) + assert.Equal(t, h.IsFromInitiator(), true) + assert.Equal(t, h.IsFromResponder(), false) + assert.True(t, len(h.String()) > 0) h = NewHeader(miniProtocolFromBytes(0x01), ContainerModeResponder, 2) assert.True(t, h.transmissionTime > 0) assert.Equal(t, uint16(1), h.miniProtocol.Value()) assert.Equal(t, ContainerModeResponder, h.ContainerMode()) assert.Equal(t, uint16(2), h.payloadLength) + assert.Equal(t, h.IsFromInitiator(), false) + assert.Equal(t, h.IsFromResponder(), true) + assert.True(t, len(h.String()) > 0) + + // Test update payload length + h.update(uint16(88)) + assert.Equal(t, uint16(88), h.payloadLength) } func TestNewMessageHeaderFromBytes(t *testing.T) { diff --git a/multiplex/mini_protocol_test.go b/multiplex/mini_protocol_test.go new file mode 100644 index 0000000..c058dd3 --- /dev/null +++ b/multiplex/mini_protocol_test.go @@ -0,0 +1,18 @@ +package multiplex + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestMiniProtocolValues(t *testing.T) { + for _, m := range miniProtocols { + assert.True(t, len(m.String()) > 0) + assert.True(t, m.Value() >= 0) + } + + // Test Unknown MiniProtocol + testMiniProtocol := uint16(9999) + assert.Equal(t, "unknown", MiniProtocol(testMiniProtocol).String()) +}