Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat!: hex encode id.String() output #202

Merged
merged 3 commits into from
Jun 6, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 2 additions & 6 deletions namespace/id.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,8 @@ func (nid ID) Size() IDSize {
return IDSize(len(nid))
}

// String stringifies the nid.
// String returns the hexadecimal encoding of the nid. The output of
// nid.String() is not equivalant to string(nid).
func (nid ID) String() string {
return string(nid)
}

// HexString returns hexadecimal encoding of nid.
func (nid ID) HexString() string {
return hex.EncodeToString(nid)
}
39 changes: 35 additions & 4 deletions namespace/id_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,41 @@ package namespace
import (
"testing"

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

func TestID_HexString(t *testing.T) {
nID := ID("12345678")
require.Equal(t, "3132333435363738", nID.HexString())
// TestString verifies that id.String() returns the hexadecimal encoding of id.
func TestString(t *testing.T) {
type testCase struct {
id ID
want string
}
testCases := []testCase{
{ID(""), ""},
{ID("12345678"), "3132333435363738"},
{[]byte{0, 0, 0, 0, 0, 0, 0, 0}, "0000000000000000"},
{[]byte{0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa}, "aaaaaaaaaaaaaaaa"},
{[]byte{1, 2, 3, 4, 5, 6, 7, 8}, "0102030405060708"},
}
for _, tc := range testCases {
assert.Equal(t, tc.want, tc.id.String())
}
}

// Test_string verifies that string(id) returns the native string representation of id.
func Test_string(t *testing.T) {
type testCase struct {
id ID
want string
}
testCases := []testCase{
{ID(""), ""},
{ID("12345678"), "12345678"},
{[]byte{0, 0, 0, 0, 0, 0, 0, 0}, "\x00\x00\x00\x00\x00\x00\x00\x00"},
{[]byte{0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa}, "\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa"},
{[]byte{1, 2, 3, 4, 5, 6, 7, 8}, "\x01\x02\x03\x04\x05\x06\a\b"},
}
for _, tc := range testCases {
assert.Equal(t, tc.want, string(tc.id))
}
}