From 12032df45ddc2bccb69a67a5efa59bce28281e9e Mon Sep 17 00:00:00 2001 From: Matt Date: Mon, 8 Nov 2021 22:36:17 -0500 Subject: [PATCH] UcFirst accounts for multi-byte leading characters --- helper.go | 4 ++-- stringy.go | 2 +- stringy_test.go | 36 ++++++++++++++++++++++++++---------- 3 files changed, 29 insertions(+), 13 deletions(-) diff --git a/helper.go b/helper.go index 53311ae..16d4a61 100644 --- a/helper.go +++ b/helper.go @@ -59,8 +59,8 @@ func replaceStr(input, search, replace, types string) string { } func ucfirst(val string) string { - for i, v := range val { - return string(unicode.ToUpper(v)) + val[i+1:] + for _, v := range val { + return string(unicode.ToUpper(v)) + val[len(string(v)):] } return "" } diff --git a/stringy.go b/stringy.go index 046edbe..49040c9 100644 --- a/stringy.go +++ b/stringy.go @@ -10,7 +10,7 @@ import ( "unicode" ) -// input is struct that holds input form user and result +// input is struct that holds input from user and result type input struct { Input string Result string diff --git a/stringy_test.go b/stringy_test.go index 715a2fd..208cacb 100644 --- a/stringy_test.go +++ b/stringy_test.go @@ -276,18 +276,34 @@ func TestInput_TeaseEmpty(t *testing.T) { } func TestInput_UcFirst(t *testing.T) { - str := New("this is test") - against := "This is test" - if val := str.UcFirst(); val != against { - t.Errorf("Expected: to be %s but got: %s", against, val) + tests := []struct { + name string + arg string + want string + }{ + { + name: "leading lowercase", + arg: "test input", + want: "Test input", + }, + { + name: "empty string", + arg: "", + want: "", + }, + { + name: "multi-byte leading character", + arg: "δδδ", + want: "Δδδ", + }, } -} -func TestInput_EmptyUcFirst(t *testing.T) { - str := New("") - against := "" - if val := str.UcFirst(); 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).UcFirst(); got != tt.want { + t.Errorf("UcFirst(%v) = %v, want %v", tt.arg, got, tt.want) + } + }) } }