github.com/yoheimuta/protolint@v0.49.8-0.20240515023657-4ecaebb7575d/linter/strs/pluralize_test.go (about) 1 package strs_test 2 3 import ( 4 "testing" 5 6 "github.com/yoheimuta/protolint/linter/strs" 7 ) 8 9 func TestPluralizeClient_ToPlural(t *testing.T) { 10 tests := []struct { 11 name string 12 word string 13 pluralizedWord string 14 }{ 15 {"PluralizeNormalSingularWord", "car", "cars"}, 16 {"PluralizeSingularWord", "person", "people"}, 17 {"PluralizePluralWord", "people", "people"}, 18 {"PluralizeNonstandardPluralWord", "persons", "people"}, 19 {"PluralizeNoPluralFormWord", "moose", "moose"}, 20 {"PluralizePluralLatinWord", "cacti", "cacti"}, 21 {"PluralizeNonstandardPluralLatinWord", "cactuses", "cacti"}, 22 {"PluralizePluralCamelCaseWord", "office_chairs", "office_chairs"}, 23 {"PluralizeSingularCamelCaseWord", "office_chair", "office_chairs"}, 24 {"special case #312: appends s to uri", "uri", "uris"}, 25 {"special case #312: appends s to module_uri", "module_uri", "module_uris"}, 26 {"special case #312: not change uris", "uris", "uris"}, 27 {"special case #312: not change module_uris", "module_uris", "module_uris"}, 28 } 29 for _, test := range tests { 30 test := test 31 t.Run(test.name, func(t *testing.T) { 32 c := strs.NewPluralizeClient() 33 if got := c.ToPlural(test.word); got != test.pluralizedWord { 34 t.Errorf("Plural(%s) got %s, but want %s", test.word, got, test.pluralizedWord) 35 } 36 }) 37 } 38 } 39 40 func TestPluralizeClient_AddPluralRule(t *testing.T) { 41 tests := []struct { 42 name string 43 word string 44 pluralizedWord string 45 rule string 46 replacement string 47 }{ 48 { 49 name: "normal conversion", 50 word: "regex", 51 pluralizedWord: "regexes", 52 }, 53 { 54 name: "special conversion after adding manually", 55 word: "regex", 56 pluralizedWord: "regexii", 57 rule: "(?i)gex$", 58 replacement: "gexii", 59 }, 60 } 61 for _, test := range tests { 62 test := test 63 t.Run(test.name, func(t *testing.T) { 64 c := strs.NewPluralizeClient() 65 if 0 < len(test.rule) { 66 c.AddPluralRule(test.rule, test.replacement) 67 } 68 if got := c.ToPlural(test.word); got != test.pluralizedWord { 69 t.Errorf("Plural(%s) got %s, but want %s", test.word, got, test.pluralizedWord) 70 } 71 }) 72 } 73 } 74 75 func TestPluralizeClient_AddSingularRule(t *testing.T) { 76 tests := []struct { 77 name string 78 word string 79 pluralizedWord string 80 rule string 81 replacement string 82 }{ 83 { 84 name: "normal conversion", 85 word: "singles", 86 pluralizedWord: "singles", 87 }, 88 { 89 name: "special conversion after adding manually", 90 word: "singles", 91 pluralizedWord: "singulars", 92 rule: "(?i)singles$", 93 replacement: "singular", 94 }, 95 } 96 for _, test := range tests { 97 test := test 98 t.Run(test.name, func(t *testing.T) { 99 c := strs.NewPluralizeClient() 100 if 0 < len(test.rule) { 101 c.AddSingularRule(test.rule, test.replacement) 102 } 103 if got := c.ToPlural(test.word); got != test.pluralizedWord { 104 t.Errorf("Singular(%s) got %s, but want %s", test.word, got, test.pluralizedWord) 105 } 106 }) 107 } 108 }