From f4254bd4714782b59c0071fd4cf29eec42d0dc6b Mon Sep 17 00:00:00 2001 From: John Lettman Date: Wed, 17 Jul 2024 22:38:59 -0400 Subject: [PATCH] packet/channel/field: add entries --- packet/channel/field/entries.go | 35 ++++++++++++++++++++ packet/channel/field/entries_test.go | 48 ++++++++++++++++++++++++++++ 2 files changed, 83 insertions(+) create mode 100644 packet/channel/field/entries.go create mode 100644 packet/channel/field/entries_test.go diff --git a/packet/channel/field/entries.go b/packet/channel/field/entries.go new file mode 100644 index 0000000..e3f103a --- /dev/null +++ b/packet/channel/field/entries.go @@ -0,0 +1,35 @@ +package field + +import ( + "fmt" + "strings" +) + +type Entries map[Field]Structure + +// String returns the string representation of an Entries value. +func (e Entries) String() string { + s := new(strings.Builder) + s.WriteString("Entries:\n") + + for k, v := range e { + ks := fmt.Sprintf("%s:", k.String()) + s.WriteString(fmt.Sprintf("\t%-14s %v,\n", ks, v)) + } + + return s.String() +} + +// GoString returns the Go syntax representation of an Entries value. +func (e Entries) GoString() string { + s := new(strings.Builder) + s.WriteString("field.Entries{\n") + + for k, v := range e { + ks := fmt.Sprintf("%#v:", k) + s.WriteString(fmt.Sprintf("\t%-14s %#v,\n", ks, v)) + } + + s.WriteRune('}') + return s.String() +} diff --git a/packet/channel/field/entries_test.go b/packet/channel/field/entries_test.go new file mode 100644 index 0000000..f0ae659 --- /dev/null +++ b/packet/channel/field/entries_test.go @@ -0,0 +1,48 @@ +package field + +import ( + "fmt" + "github.com/stretchr/testify/assert" + "testing" +) + +func TestEntries_StringInterfaces(t *testing.T) { + assert.Implements(t, (*fmt.Stringer)(nil), new(Entries)) + assert.Implements(t, (*fmt.GoStringer)(nil), new(Entries)) +} + +func TestEntries_String(t *testing.T) { + f := Reflectivity + fString := fmt.Sprintf("%s:", f) + + s := Structure{ + Type: TypeUint8, + Offset: 1, + ValueMask: 0xF, + Shift: 32, + } + + e := Entries{f: s} + want := fmt.Sprintf("Entries:\n\t%-14s %v,\n", fString, s) + got := e.String() + + assert.Equal(t, want, got, "it should return the correct representation") +} + +func TestEntries_GoString(t *testing.T) { + f := Reflectivity + fString := fmt.Sprintf("%#v:", f) + + s := Structure{ + Type: TypeUint8, + Offset: 1, + ValueMask: 0xF, + Shift: 32, + } + + e := Entries{f: s} + want := fmt.Sprintf("field.Entries{\n\t%-14s %#v,\n}", fString, s) + got := e.GoString() + + assert.Equal(t, want, got, "it should return the correct representation") +}