From cfed097c911e22133866bf99d739715d28427018 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=ABl=20G=C3=A4hwiler?= Date: Sun, 18 Oct 2020 19:20:08 +0200 Subject: [PATCH] added types function --- packet/type.go | 6 ++++++ packet/type_test.go | 26 +++++++------------------- 2 files changed, 13 insertions(+), 19 deletions(-) diff --git a/packet/type.go b/packet/type.go index b2abbd0..d918afb 100644 --- a/packet/type.go +++ b/packet/type.go @@ -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 { diff --git a/packet/type_test.go b/packet/type_test.go index 2ff6ee8..1c6463e 100644 --- a/packet/type_test.go +++ b/packet/type_test.go @@ -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()) } @@ -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)