diff --git a/stringy.go b/stringy.go index bfcad0d..1fa20dc 100644 --- a/stringy.go +++ b/stringy.go @@ -230,7 +230,7 @@ func (i *input) LcFirst() string { // Lines returns slice of strings by removing white space characters func (i *input) Lines() []string { - input := getInput(*i) + input := getInput(*i) result := matchWordRegexp.ReplaceAllString(input, " ") return strings.Fields(strings.TrimSpace(result)) } @@ -363,7 +363,7 @@ func (i *input) ToLower() (result string) { // it can be chained on function which return StringManipulation interface func (i *input) Title() (result string) { input := getInput(*i) - wordArray := strings.Split(input, " ") + wordArray := strings.Fields(input) for i, word := range wordArray { wordArray[i] = strings.ToUpper(string(word[0])) + strings.ToLower(word[1:]) } diff --git a/stringy_test.go b/stringy_test.go index 2aaa6df..b633b24 100644 --- a/stringy_test.go +++ b/stringy_test.go @@ -313,10 +313,69 @@ func TestInput_TeaseEmpty(t *testing.T) { } func TestInput_Title(t *testing.T) { - str := New("this is just AN eXample") - against := "This Is Just An Example" - if val := str.Title(); val != against { - t.Errorf("Expected: to be %s but got: %s", against, val) + tests := []struct { + name string + input string + want string + }{ + { + name: "basic title case", + input: "this is just AN eXample", + want: "This Is Just An Example", + }, + { + name: "multiple spaces", + input: "hello world", + want: "Hello World", + }, + { + name: "tabs and spaces", + input: "hello\t\t\tworld ", + want: "Hello World", + }, + { + name: "newlines", + input: "hello\nworld", + want: "Hello World", + }, + { + name: "mixed whitespace", + input: "hello \t\n world", + want: "Hello World", + }, + { + name: "leading and trailing whitespace", + input: " hello world ", + want: "Hello World", + }, + { + name: "single word", + input: "hello", + want: "Hello", + }, + { + name: "empty string", + input: "", + want: "", + }, + { + name: "only whitespace", + input: " \t\n ", + want: "", + }, + { + name: "special characters", + input: "hello-world", + want: "Hello-world", + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := New(tt.input).Title(); got != tt.want { + t.Errorf("Title() = %q, want %q", got, tt.want) + } + }) } }