Skip to content

Commit

Permalink
Added Tests. (#3)
Browse files Browse the repository at this point in the history
* Limit trigger on release pipeline to pushes for tags with specific pattern.
* Added some unit tests.
* Added coverage-html as makefile target.
  • Loading branch information
masterjk authored Oct 24, 2020
1 parent 39d2736 commit 425df2a
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 8 deletions.
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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}" \
Expand Down
3 changes: 3 additions & 0 deletions cbor/array_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}

Expand Down
20 changes: 20 additions & 0 deletions cbor/positive.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -83,13 +93,23 @@ 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{
basePositiveInteger: newBasePositiveInteger(uint64(value), additionalType64Bits),
}
}

// 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 {
Expand Down
26 changes: 26 additions & 0 deletions cbor/positive_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cbor

import (
"math"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -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())
}
8 changes: 0 additions & 8 deletions multiplex/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 10 additions & 0 deletions multiplex/header_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
18 changes: 18 additions & 0 deletions multiplex/mini_protocol_test.go
Original file line number Diff line number Diff line change
@@ -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())
}

0 comments on commit 425df2a

Please sign in to comment.