Skip to content

Commit

Permalink
packet/channel/field: add entries
Browse files Browse the repository at this point in the history
  • Loading branch information
johnlettman committed Jul 18, 2024
1 parent 7778aca commit f4254bd
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 0 deletions.
35 changes: 35 additions & 0 deletions packet/channel/field/entries.go
Original file line number Diff line number Diff line change
@@ -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()
}
48 changes: 48 additions & 0 deletions packet/channel/field/entries_test.go
Original file line number Diff line number Diff line change
@@ -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")
}

0 comments on commit f4254bd

Please sign in to comment.