1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
|
package inflection
import (
"strings"
"testing"
)
var inflections = map[string]string{
"star": "stars",
"STAR": "STARS",
"Star": "Stars",
"bus": "buses",
"fish": "fish",
"mouse": "mice",
"query": "queries",
"ability": "abilities",
"agency": "agencies",
"movie": "movies",
"archive": "archives",
"index": "indices",
"wife": "wives",
"safe": "saves",
"half": "halves",
"move": "moves",
"salesperson": "salespeople",
"person": "people",
"spokesman": "spokesmen",
"man": "men",
"woman": "women",
"basis": "bases",
"diagnosis": "diagnoses",
"diagnosis_a": "diagnosis_as",
"datum": "data",
"medium": "media",
"stadium": "stadia",
"analysis": "analyses",
"node_child": "node_children",
"child": "children",
"experience": "experiences",
"day": "days",
"comment": "comments",
"foobar": "foobars",
"newsletter": "newsletters",
"old_news": "old_news",
"news": "news",
"series": "series",
"species": "species",
"quiz": "quizzes",
"perspective": "perspectives",
"ox": "oxen",
"photo": "photos",
"buffalo": "buffaloes",
"tomato": "tomatoes",
"dwarf": "dwarves",
"elf": "elves",
"information": "information",
"equipment": "equipment",
"criterion": "criteria",
}
// storage is used to restore the state of the global variables
// on each test execution, to ensure no global state pollution
type storage struct {
singulars RegularSlice
plurals RegularSlice
irregulars IrregularSlice
uncountables []string
}
var backup = storage{}
func init() {
AddIrregular("criterion", "criteria")
copy(backup.singulars, singularInflections)
copy(backup.plurals, pluralInflections)
copy(backup.irregulars, irregularInflections)
copy(backup.uncountables, uncountableInflections)
}
func restore() {
copy(singularInflections, backup.singulars)
copy(pluralInflections, backup.plurals)
copy(irregularInflections, backup.irregulars)
copy(uncountableInflections, backup.uncountables)
}
func TestPlural(t *testing.T) {
for key, value := range inflections {
if v := Plural(strings.ToUpper(key)); v != strings.ToUpper(value) {
t.Errorf("%v's plural should be %v, but got %v", strings.ToUpper(key), strings.ToUpper(value), v)
}
if v := Plural(strings.Title(key)); v != strings.Title(value) {
t.Errorf("%v's plural should be %v, but got %v", strings.Title(key), strings.Title(value), v)
}
if v := Plural(key); v != value {
t.Errorf("%v's plural should be %v, but got %v", key, value, v)
}
}
}
func TestSingular(t *testing.T) {
for key, value := range inflections {
if v := Singular(strings.ToUpper(value)); v != strings.ToUpper(key) {
t.Errorf("%v's singular should be %v, but got %v", strings.ToUpper(value), strings.ToUpper(key), v)
}
if v := Singular(strings.Title(value)); v != strings.Title(key) {
t.Errorf("%v's singular should be %v, but got %v", strings.Title(value), strings.Title(key), v)
}
if v := Singular(value); v != key {
t.Errorf("%v's singular should be %v, but got %v", value, key, v)
}
}
}
func TestAddPlural(t *testing.T) {
defer restore()
ln := len(pluralInflections)
AddPlural("", "")
if ln+1 != len(pluralInflections) {
t.Errorf("Expected len %d, got %d", ln+1, len(pluralInflections))
}
}
func TestAddSingular(t *testing.T) {
defer restore()
ln := len(singularInflections)
AddSingular("", "")
if ln+1 != len(singularInflections) {
t.Errorf("Expected len %d, got %d", ln+1, len(singularInflections))
}
}
func TestAddIrregular(t *testing.T) {
defer restore()
ln := len(irregularInflections)
AddIrregular("", "")
if ln+1 != len(irregularInflections) {
t.Errorf("Expected len %d, got %d", ln+1, len(irregularInflections))
}
}
func TestAddUncountable(t *testing.T) {
defer restore()
ln := len(uncountableInflections)
AddUncountable("", "")
if ln+2 != len(uncountableInflections) {
t.Errorf("Expected len %d, got %d", ln+2, len(uncountableInflections))
}
}
func TestGetPlural(t *testing.T) {
plurals := GetPlural()
if len(plurals) != len(pluralInflections) {
t.Errorf("Expected len %d, got %d", len(plurals), len(pluralInflections))
}
}
func TestGetSingular(t *testing.T) {
singular := GetSingular()
if len(singular) != len(singularInflections) {
t.Errorf("Expected len %d, got %d", len(singular), len(singularInflections))
}
}
func TestGetIrregular(t *testing.T) {
irregular := GetIrregular()
if len(irregular) != len(irregularInflections) {
t.Errorf("Expected len %d, got %d", len(irregular), len(irregularInflections))
}
}
func TestGetUncountable(t *testing.T) {
uncountables := GetUncountable()
if len(uncountables) != len(uncountableInflections) {
t.Errorf("Expected len %d, got %d", len(uncountables), len(uncountableInflections))
}
}
func TestSetPlural(t *testing.T) {
defer restore()
SetPlural(RegularSlice{{}, {}})
if len(pluralInflections) != 2 {
t.Errorf("Expected len 2, got %d", len(pluralInflections))
}
}
func TestSetSingular(t *testing.T) {
defer restore()
SetSingular(RegularSlice{{}, {}})
if len(singularInflections) != 2 {
t.Errorf("Expected len 2, got %d", len(singularInflections))
}
}
func TestSetIrregular(t *testing.T) {
defer restore()
SetIrregular(IrregularSlice{{}, {}})
if len(irregularInflections) != 2 {
t.Errorf("Expected len 2, got %d", len(irregularInflections))
}
}
func TestSetUncountable(t *testing.T) {
defer restore()
SetUncountable([]string{"", ""})
if len(uncountableInflections) != 2 {
t.Errorf("Expected len 2, got %d", len(uncountableInflections))
}
}
|