From f76290f29983f719f4515507ea0e45c4d5629fba Mon Sep 17 00:00:00 2001 From: Henrik Hautakoski Date: Thu, 30 Mar 2023 03:35:44 +0200 Subject: [PATCH] transport/channel.go: Add Format() --- transport/channel.go | 6 +++++- transport/channel_test.go | 20 ++++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/transport/channel.go b/transport/channel.go index 59e048a..353d0c1 100644 --- a/transport/channel.go +++ b/transport/channel.go @@ -11,8 +11,12 @@ func (c *Channel) Append(name ...string) { *c = append(*c, name...) } +func (c Channel) Format(delimiter string) string { + return strings.Join(c, delimiter) +} + func (c Channel) String() string { - return strings.Join(c, "/") + return c.Format("/") } // Check if two channels are equal diff --git a/transport/channel_test.go b/transport/channel_test.go index b326ffb..81c3f5a 100644 --- a/transport/channel_test.go +++ b/transport/channel_test.go @@ -52,6 +52,26 @@ func TestChannel_Is(t *testing.T) { } } +func TestChannel_Format(t *testing.T) { + tests := []struct { + name string + c Channel + delim string + want string + }{ + {"Empty", Channel{}, ":", ""}, + {"Alot#1", Channel{"one", "two", "three"}, "-", "one-two-three"}, + {"Alot#2", Channel{"first", "second"}, ":", "first:second"}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := tt.c.Format(tt.delim); got != tt.want { + t.Errorf("Channel.Format() = %v, want %v", got, tt.want) + } + }) + } +} + func TestChannel_String(t *testing.T) { tests := []struct { name string