File: inflector_test.go

package info (click to toggle)
golang-github-gedex-inflector 0.0~git20170307.0.16278e9-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 88 kB
  • ctags: 33
  • sloc: makefile: 2
file content (205 lines) | stat: -rw-r--r-- 5,529 bytes parent folder | download | duplicates (2)
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
// Copyright 2013 Akeda Bagus <admin@gedex.web.id>. All rights reserved.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package inflector

import (
	"testing"
)

type inflector func(string) string

type inflectorTest struct {
	in    string
	out   string
	match bool
}

var pluralTests = []inflectorTest{
	{"categoria", "categorias", true},
	{"house", "houses", true},
	{"powerhouse", "powerhouses", true},
	{"Bus", "Buses", true},
	{"bus", "buses", true},
	{"menu", "menus", true},
	{"news", "news", true},
	{"food_menu", "food_menus", true},
	{"Menu", "Menus", true},
	{"FoodMenu", "FoodMenus", true},
	{"quiz", "quizzes", true},
	{"matrix_row", "matrix_rows", true},
	{"matrix", "matrices", true},
	{"vertex", "vertices", true},
	{"index", "indices", true},
	{"Alias", "Aliases", true},
	{"Aliases", "Aliases", false},
	{"Media", "Media", true},
	{"NodeMedia", "NodeMedia", true},
	{"alumnus", "alumni", true},
	{"bacillus", "bacilli", true},
	{"cactus", "cacti", true},
	{"focus", "foci", true},
	{"fungus", "fungi", true},
	{"nucleus", "nuclei", true},
	{"octopus", "octopuses", true},
	{"radius", "radii", true},
	{"stimulus", "stimuli", true},
	{"syllabus", "syllabi", true},
	{"terminus", "termini", true},
	{"virus", "viri", true},
	{"person", "people", true},
	{"people", "people", false},
	{"glove", "gloves", true},
	{"crisis", "crises", true},
	{"tax", "taxes", true},
	{"wave", "waves", true},
	{"bureau", "bureaus", true},
	{"cafe", "cafes", true},
	{"roof", "roofs", true},
	{"foe", "foes", true},
	{"cookie", "cookies", true},
	{"wolf", "wolves", true},
	{"thief", "thieves", true},
	{"potato", "potatoes", true},
	{"hero", "heroes", true},
	{"buffalo", "buffalo", true},
	{"tooth", "teeth", true},
	{"goose", "geese", true},
	{"foot", "feet", true},
	{"objective", "objectives", true},
	{"specie", "species", false},
	{"species", "species", true},
	{"", "", true},
}

var singularTests = []inflectorTest{
	{"categorias", "categoria", true},
	{"menus", "menu", true},
	{"news", "news", true},
	{"food_menus", "food_menu", true},
	{"Menus", "Menu", true},
	{"FoodMenus", "FoodMenu", true},
	{"houses", "house", true},
	{"powerhouses", "powerhouse", true},
	{"quizzes", "quiz", true},
	{"Buses", "Bus", true},
	{"buses", "bus", true},
	{"matrix_rows", "matrix_row", true},
	{"matrices", "matrix", true},
	{"vertices", "vertex", true},
	{"indices", "index", true},
	{"Aliases", "Alias", true},
	{"Alias", "Alias", false},
	{"Media", "Media", true},
	{"NodeMedia", "NodeMedia", true},
	{"alumni", "alumnus", true},
	{"bacilli", "bacillus", true},
	{"cacti", "cactus", true},
	{"foci", "focus", true},
	{"fungi", "fungus", true},
	{"nuclei", "nucleus", true},
	{"octopuses", "octopus", true},
	{"radii", "radius", true},
	{"stimuli", "stimulus", true},
	{"syllabi", "syllabus", true},
	{"termini", "terminus", true},
	{"viri", "virus", true},
	{"people", "person", true},
	{"gloves", "glove", true},
	{"doves", "dove", true},
	{"lives", "life", true},
	{"knives", "knife", true},
	{"wolves", "wolf", true},
	{"slaves", "slave", true},
	{"shelves", "shelf", true},
	{"taxis", "taxi", true},
	{"taxes", "tax", true},
	{"Taxes", "Tax", true},
	{"AwesomeTaxes", "AwesomeTax", true},
	{"faxes", "fax", true},
	{"waxes", "wax", true},
	{"niches", "niche", true},
	{"waves", "wave", true},
	{"bureaus", "bureau", true},
	{"genetic_analyses", "genetic_analysis", true},
	{"doctor_diagnoses", "doctor_diagnosis", true},
	{"parantheses", "paranthesis", true},
	{"Causes", "Cause", true},
	{"colossuses", "colossus", true},
	{"diagnoses", "diagnosis", true},
	{"bases", "basis", true},
	{"analyses", "analysis", true},
	{"curves", "curve", true},
	{"cafes", "cafe", true},
	{"roofs", "roof", true},
	{"foes", "foe", true},
	{"databases", "database", true},
	{"cookies", "cookie", true},
	{"thieves", "thief", true},
	{"potatoes", "potato", true},
	{"heroes", "hero", true},
	{"buffalos", "buffalo", false},
	{"babies", "baby", true},
	{"teeth", "tooth", true},
	{"geese", "goose", true},
	{"feet", "foot", true},
	{"objectives", "objective", true},
	{"species", "species", true},
	{"", "", true},
}

func TestPluralize(t *testing.T) {
	for i, test := range pluralTests {
		s := Pluralize(test.in)
		if s != test.out {
			t.Errorf("test %d Pluralize(%s) = %s, expected: %s", i, test.in, s, test.out)
		}

		// Second retrieval should returns the same result.
		// This is also tests the cache
		s = Pluralize(test.in)
		if s != test.out {
			t.Errorf("test %d (2) Pluralize(%s) = %s, expected: %s", i, test.in, s, test.out)
		}
	}
}

func TestSingularize(t *testing.T) {
	for i, test := range singularTests {
		s := Singularize(test.in)
		if s != test.out {
			t.Errorf("test %d Singularize(%s) = %s, expected: %s", i, test.in, s, test.out)
		}

		// Second retrieval should returns the same result.
		// This is also tests the cache
		s = Singularize(test.in)
		if s != test.out {
			t.Errorf("test %d (2) Singularize(%s) = %s, expected: %s", i, test.in, s, test.out)
		}
	}
}

func TestReverse(t *testing.T) {
	for i, test := range pluralTests {
		if !test.match {
			continue
		}
		s := Singularize(Pluralize(test.in))
		if s != test.in {
			t.Errorf("test %d Singularize(Pluralize(%s)) != %s, got: %s", i, test.in, test.in, s)
		}
	}

	for i, test := range singularTests {
		if !test.match {
			continue
		}
		s := Pluralize(Singularize(test.in))
		if s != test.in {
			t.Errorf("test %d Pluralize(Singularize(%s)) != %s, got: %s", i, test.in, test.in, s)
		}
	}
}