diff --git a/generator.go b/generator.go index cf7d612..dec6b37 100644 --- a/generator.go +++ b/generator.go @@ -139,8 +139,6 @@ func (g *Generator) Generate() (string, error) { return strings.Join(g.applyCasing(words), g.delimiter), nil } -var titleCaser = cases.Title(language.English) - func (g *Generator) applyCasing(words []string) []string { for i, word := range words { switch g.casing { @@ -149,7 +147,7 @@ func (g *Generator) applyCasing(words []string) []string { case Upper: words[i] = strings.ToUpper(word) case Title: - words[i] = titleCaser.String(word) + words[i] = cases.Title(language.English).String(word) } } return words diff --git a/generator_test.go b/generator_test.go index 165e0bf..78736e6 100644 --- a/generator_test.go +++ b/generator_test.go @@ -235,6 +235,51 @@ func TestWithDictionary(t *testing.T) { } } +func TestGenerateTitleConcurrent(t *testing.T) { + testCases := []struct { + name string + adjective string + noun string + want string + }{ + { + name: "mixed case and hyphen", + adjective: "hAPPY", + noun: "t-shirt", + want: "Happy-T-Shirt", + }, + { + name: "apostrophe and underscore", + adjective: "don't", + noun: "foo_bar", + want: "Don't-Foo_bar", + }, + { + name: "unicode", + adjective: "ßeta", + noun: "ΟΣ", + want: "Sseta-Ος", + }, + } + for _, tc := range testCases { + tc := tc + t.Run(tc.name, func(t *testing.T) { + t.Parallel() + dict := NewCustomDictionary([]string{tc.adjective}, nil, []string{tc.noun}, nil) + g := NewGenerator(WithDictionary(dict), WithCasing(Title), WithSeed(1)) + for i := 0; i < 100; i++ { + got, err := g.Generate() + if err != nil { + t.Fatal(err) + } + if got != tc.want { + t.Fatalf("Generate() = %q, want %q", got, tc.want) + } + } + }) + } +} + func TestCasingFromString(t *testing.T) { t.Log("Given the need to parse casing strings") {