From 0704d3064a943255e3025670c92a43c1894ebc4c Mon Sep 17 00:00:00 2001 From: Matt Date: Sat, 6 Nov 2021 00:51:35 -0400 Subject: [PATCH 1/3] convert LcFirst tests to table driven format --- stringy_test.go | 31 +++++++++++++++++++++---------- 1 file changed, 21 insertions(+), 10 deletions(-) diff --git a/stringy_test.go b/stringy_test.go index 800aff3..70646ae 100644 --- a/stringy_test.go +++ b/stringy_test.go @@ -123,18 +123,29 @@ func TestInput_KebabCase(t *testing.T) { } func TestInput_LcFirst(t *testing.T) { - str := New("This is an all lower") - against := "this is an all lower" - if val := str.LcFirst(); val != against { - t.Errorf("Expected: to be %s but got: %s", against, val) + tests := []struct { + name string + arg string + want string + }{ + { + name: "leading uppercase", + arg: "This is an all lower", + want: "this is an all lower", + }, + { + name: "empty string", + arg: "", + want: "", + }, } -} -func TestInput_LcFirstEmpty(t *testing.T) { - str := New("") - against := "" - if val := str.LcFirst(); val != against { - t.Errorf("Expected: to be %s but got: %s", against, val) + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := New(tt.arg).LcFirst(); got != tt.want { + t.Errorf("LcFirst(%v) = %v, want %v", tt.arg, got, tt.want) + } + }) } } From 2e5ad8d58b0d5088361298895fe8894665779570 Mon Sep 17 00:00:00 2001 From: Matt Date: Sat, 6 Nov 2021 00:54:21 -0400 Subject: [PATCH 2/3] add test for leading multi-byte character to demo bug --- stringy_test.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/stringy_test.go b/stringy_test.go index 70646ae..715a2fd 100644 --- a/stringy_test.go +++ b/stringy_test.go @@ -138,6 +138,11 @@ func TestInput_LcFirst(t *testing.T) { arg: "", want: "", }, + { + name: "multi-byte leading character", + arg: "ΔΔΔ", + want: "δΔΔ", + }, } for _, tt := range tests { From 571d4cc48e59e4c758c402877ab89b3eb7b48f9f Mon Sep 17 00:00:00 2001 From: Matt Date: Sat, 6 Nov 2021 00:59:33 -0400 Subject: [PATCH 3/3] fix lcFirst leading multi-byte character bug --- stringy.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/stringy.go b/stringy.go index cfcac33..046edbe 100644 --- a/stringy.go +++ b/stringy.go @@ -180,10 +180,10 @@ func (i *input) Last(length int) string { // function which return StringManipulation interface func (i *input) LcFirst() string { input := getInput(*i) - for i, v := range input { - return string(unicode.ToLower(v)) + input[i+1:] + for _, v := range input { + return string(unicode.ToLower(v)) + input[len(string(v)):] } - return input + return "" } // Lines returns slice of strings by removing white space characters