Skip to content

Commit

Permalink
added types function
Browse files Browse the repository at this point in the history
  • Loading branch information
256dpi committed Oct 18, 2020
1 parent 7f6e7a0 commit cfed097
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 19 deletions.
6 changes: 6 additions & 0 deletions packet/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ const (
DISCONNECT
)

// Types returns a list of all known packet types.
func Types() []Type {
return []Type{CONNECT, CONNACK, PUBLISH, PUBACK, PUBREC, PUBREL, PUBCOMP,
SUBSCRIBE, SUBACK, UNSUBSCRIBE, UNSUBACK, PINGREQ, PINGRESP, DISCONNECT}
}

// String returns the type as a string.
func (t Type) String() string {
switch t {
Expand Down
26 changes: 7 additions & 19 deletions packet/type_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
package packet

import (
"github.com/stretchr/testify/assert"
"testing"

"github.com/stretchr/testify/assert"
)

func TestTypes(t *testing.T) {
assert.Len(t, Types(), 14)
}

func TestTypeString(t *testing.T) {
assert.Equal(t, "Unknown", Type(99).String())
}
Expand All @@ -14,24 +19,7 @@ func TestTypeValid(t *testing.T) {
}

func TestTypeNew(t *testing.T) {
list := []Type{
CONNECT,
CONNACK,
PUBLISH,
PUBACK,
PUBREC,
PUBREL,
PUBCOMP,
SUBSCRIBE,
SUBACK,
UNSUBSCRIBE,
UNSUBACK,
PINGREQ,
PINGRESP,
DISCONNECT,
}

for _, tt := range list {
for _, tt := range Types() {
m, err := tt.New()
assert.NotNil(t, m)
assert.NoError(t, err)
Expand Down

0 comments on commit cfed097

Please sign in to comment.