Skip to content

Commit ffdca60

Browse files
eivindrkyleconroy
andauthored
fix for index out of range [0] in codegen.LowerTitle when input is empty (#1069)
Co-authored-by: Kyle Conroy <[email protected]>
1 parent 1fb4aab commit ffdca60

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

internal/codegen/utils.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ import (
66
)
77

88
func LowerTitle(s string) string {
9+
if s == "" {
10+
return s
11+
}
12+
913
a := []rune(s)
1014
a[0] = unicode.ToLower(a[0])
1115
return string(a)

internal/codegen/utils_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package codegen
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestLowerTitle(t *testing.T) {
8+
9+
// empty
10+
if LowerTitle("") != "" {
11+
t.Fatal("expected empty title to remain empty")
12+
}
13+
14+
// all lowercase
15+
if LowerTitle("lowercase") != "lowercase" {
16+
t.Fatal("expected no changes when input is all lowercase")
17+
}
18+
19+
// all uppercase
20+
if LowerTitle("UPPERCASE") != "uPPERCASE" {
21+
t.Fatal("expected first rune to be lower when input is all uppercase")
22+
}
23+
24+
// Title Case
25+
if LowerTitle("Title Case") != "title Case" {
26+
t.Fatal("expected first rune to be lower when input is Title Case")
27+
}
28+
}

0 commit comments

Comments
 (0)