diff --git a/internal/format/format.go b/internal/format/format.go index bf822d8..6343a95 100644 --- a/internal/format/format.go +++ b/internal/format/format.go @@ -107,7 +107,16 @@ func Slice[T any](s []T) string { typ := reflect.TypeFor[T]().Kind() - for index, element := range s { + first := fmt.Sprintf("%v", s[0]) + if typ == reflect.String { + first = strconv.Quote(first) + } + + builder.WriteString(first) + + for _, element := range s[1:] { + builder.WriteString(", ") + str := fmt.Sprintf("%v", element) if typ == reflect.String { // If it's a string, quote it @@ -115,10 +124,6 @@ func Slice[T any](s []T) string { } builder.WriteString(str) - - if index < length-1 { - builder.WriteString(", ") - } } builder.WriteByte(']') diff --git a/internal/format/format_test.go b/internal/format/format_test.go index 90a0a88..4b23456 100644 --- a/internal/format/format_test.go +++ b/internal/format/format_test.go @@ -63,11 +63,15 @@ func TestFloat64(t *testing.T) { } func TestSlice(t *testing.T) { + oneString := []string{"one"} + twoStrings := []string{"one", "two"} strings := []string{"one", "two", "three"} ints := []int{1, 2, 3} floats := []float64{1.0, 2.0, 3.0} bools := []bool{true, true, false} + test.Equal(t, Slice(oneString), `["one"]`) + test.Equal(t, Slice(twoStrings), `["one", "two"]`) test.Equal(t, Slice(strings), `["one", "two", "three"]`, test.Context("strings")) test.Equal(t, Slice(ints), "[1, 2, 3]", test.Context("ints")) test.Equal(t, Slice(floats), "[1, 2, 3]", test.Context("floats"))