UNPKG

6.25 kBJavaScriptView Raw
1'use strict';
2
3
4var Assert = require('assert');
5
6
7// pluralization function
8var pluralize = require('../lib/babelfish/pluralizer');
9
10
11function testPluralizarionRules(definition) {
12 var tests = {};
13
14 definition.langs.forEach(function (lang) {
15 // en: function () {}
16 tests[lang] = function () {
17 // test each form
18 definition.forms.forEach(function (form) {
19 // for each sample data
20 definition.count[form].forEach(function (n) {
21 var r = pluralize(lang, n, definition.forms);
22 Assert.equal(r, form, n + ' expected to be ' + form + ', got ' + r);
23 });
24 });
25 };
26 });
27
28 return tests;
29}
30
31require('vows').describe('BabelFish.Pluralizer').addBatch({
32
33 'Azerbaijani': testPluralizarionRules({
34 langs: ['az'],
35 forms: ['other'],
36 count: {
37 other: [0, 1.5, 2, 2.5, 10]
38 }
39 }),
40
41 'Manx': testPluralizarionRules({
42 langs: ['gv'],
43 forms: ['one', 'other'],
44 count: {
45 one: [0, 1, 2, 11, 12, 20, 21, 22],
46 other: [0.1, 1.4, 3, 4, 5.6, 10, 13, 19, 23, 24, 30]
47 }
48 }),
49
50 'Central Morocco Tamazight': testPluralizarionRules({
51 langs: ['tzm'],
52 forms: ['one', 'other'],
53 count: {
54 one: [0, 1, 11, 12, 99],
55 other: [0.1, 1.4, 2, 3.5, 10, 100, 120.4]
56 }
57 }),
58
59 'Macedonian': testPluralizarionRules({
60 langs: ['mk'],
61 forms: ['one', 'other'],
62 count: {
63 one: [1, 21, 31, 41, 51, 61],
64 other: [0, 0.1, 2, 3.5, 10, 11, 12, 20, 22]
65 }
66 }),
67
68 'Filipino': testPluralizarionRules({
69 langs: ['fil'],
70 forms: ['one', 'other'],
71 count: {
72 one: [0, 1],
73 other: [0.1, 1.3, 2, 3, 3.5, 5, 7]
74 }
75 }),
76
77 'English': testPluralizarionRules({
78 langs: ['en'],
79 forms: ['one', 'other'],
80 count: {
81 one: [1],
82 other: [0, 1.5, 2, 10]
83 }
84 }),
85
86 'Latvian': testPluralizarionRules({
87 langs: ['lv'],
88 forms: ['zero', 'one', 'other'],
89 count: {
90 zero: [0],
91 one: [1, 21, 31, 41, 101],
92 other: [0.1, 1.1, 2, 2.5, 3, 11, 100, 111, 120]
93 }
94 }),
95
96 'Colognian': testPluralizarionRules({
97 langs: ['ksh'],
98 forms: ['zero', 'one', 'other'],
99 count: {
100 zero: [0],
101 one: [1],
102 other: [0.1, 1.1, 2, 2.5, 3]
103 }
104 }),
105
106 'Irish': testPluralizarionRules({
107 langs: ['ga'],
108 forms: ['one', 'two', 'other'],
109 count: {
110 one: [1],
111 two: [2],
112 other: [0, 0.1, 1.1, 2.5, 3]
113 }
114 }),
115
116 'Russian': testPluralizarionRules({
117 langs: ['ru'],
118 forms: ['one', 'few', 'many', 'other'],
119 count: {
120 one: [1, 21, 31],
121 few: [2, 22, 32],
122 many: [0, 5, 20],
123 other: [1.05, 1.1, 1.2]
124 }
125 }),
126
127 'Polish': testPluralizarionRules({
128 langs: ['pl'],
129 forms: ['one', 'few', 'many', 'other'],
130 count: {
131 one: [1],
132 few: [2, 3, 4, 22, 23, 24, 32, 33, 34],
133 many: [0, 5, 6, 10, 11, 12, 14, 20, 21, 25, 30, 31, 112],
134 other: [1.05, 1.1, 1.2]
135 }
136 }),
137
138 'Lithuanian': testPluralizarionRules({
139 langs: ['lt'],
140 forms: ['one', 'few', 'other'],
141 count: {
142 one: [1, 21, 31, 41, 51, 61],
143 few: [2, 3, 4, 9, 22, 23, 29, 32, 39],
144 other: [1.2, 2.07, 10.94, 0, 10, 11, 20, 30, 40, 50]
145 }
146 }),
147
148 'Tachelhit': testPluralizarionRules({
149 langs: ['shi'],
150 forms: ['one', 'few', 'other'],
151 count: {
152 one: [0, 0.5, 1],
153 few : [2, 3, 10],
154 other: [1.5, 2.5, 11, 11.5, 12]
155 }
156 }),
157
158 'Moldavian': testPluralizarionRules({
159 langs: ['mo'],
160 forms: ['one', 'few', 'other'],
161 count: {
162 one: [1],
163 few: [0, 2, 3, 10, 19, 101, 111, 119, 201, 211, 219],
164 other: [20, 21, 99, 100, 120, 121, 200, 1.2, 2.07, 20.94]
165 }
166 }),
167
168 'Czech': testPluralizarionRules({
169 langs: ['cs'],
170 forms: ['one', 'few', 'other'],
171 count: {
172 one: [1],
173 few: [2, 3, 4],
174 other: [0, 5, 999, 1.2, 2.07, 5.94]
175 }
176 }),
177
178 'Slovenian': testPluralizarionRules({
179 langs: ['sl'],
180 forms: ['one', 'two', 'few', 'other'],
181 count: {
182 one: [1, 101, 201, 301, 401, 501],
183 two: [2, 102, 202, 302, 402, 502],
184 few: [3, 4, 103, 104, 203, 204],
185 other: [0, 5, 100, 105, 200, 205, 300, 1.2, 2.07, 3.94, 5.81]
186 }
187 }),
188
189 'Maltese': testPluralizarionRules({
190 langs: ['mt'],
191 forms: ['one', 'few', 'many', 'other'],
192 count: {
193 one: [1],
194 few: [0, 2, 3, 4, 10, 102, 110, 202, 210],
195 many: [11, 12, 13, 19, 111, 119, 211, 219],
196 other: [20, 21, 30, 100, 101, 120, 201, 220, 301, 1.2, 2.07, 11.94, 20.81]
197 }
198 }),
199
200 'Arabic': testPluralizarionRules({
201 langs: ['ar'],
202 forms: ['zero', 'one', 'two', 'few', 'many', 'other'],
203 count: {
204 zero: [0],
205 one: [1],
206 two: [2],
207 few: [3, 4, 10, 103, 104, 110, 203, 204, 210],
208 many: [11, 12, 13, 20, 99, 111, 112, 120, 199, 211, 299],
209 other: [100, 101, 102, 200, 201, 202, 300, 301, 302, 0.2, 1.07, 2.94, 3.81, 11.68, 100.55]
210 }
211 }),
212
213 'Breton': testPluralizarionRules({
214 langs: ['br'],
215 forms: ['zero', 'one', 'two', 'few', 'many', 'other'],
216 count: {
217 zero: [0],
218 one: [1],
219 two: [2],
220 few: [3],
221 many: [6],
222 other: [4, 5, 7, 10, 990, 0.2, 1.07, 2.94, 3.81, 6.68, 4.55]
223 }
224 }),
225
226 //
227 // Fractional mutations
228 //
229
230 'French': testPluralizarionRules({
231 langs: ['fr'],
232 forms: ['one', 'other'],
233 count: {
234 one: [0, 0.5, 1, 1.5],
235 other: [2, 2.5, 3, 10]
236 }
237 }),
238
239 'Langi': testPluralizarionRules({
240 langs: ['lag'],
241 forms: ['zero', 'one', 'other'],
242 count: {
243 zero: [0],
244 one: [0.5, 1, 1.5],
245 other: [2, 2.5, 3, 10]
246 }
247 }),
248
249}).addBatch({
250 'With unknown language': {
251 topic: function () {
252 return pluralize('unknown', 1, ['one']);
253 },
254 'returns error message': function (result) {
255 Assert.equal(result, '[pluralizer for (unknown) not exists]');
256 }
257 },
258
259 'With insufficient plural form': {
260 topic: function () {
261 return pluralize('ru', 1.1, ['one', 'few', 'many']);
262 },
263 'returns error message': function (result) {
264 Assert.equal(result, '[plural form N3 not found in translation]');
265 }
266 }
267}).export(module);