2 var assert, cases, vows, util;
4 vows = require('vows');
6 assert = require('assert');
8 util = require('../../lib/util');
10 cases = require('./cases');
12 vows.describe('Module Inflector methods').addBatch({
13 'Test inflector method': {
14 topic: require('../../lib/methods'),
16 'word': function(topic) {
17 var i, words, _i, _len, _ref, _results;
18 words = cases.CamelToUnderscore;
19 _ref = Object.keys(words);
21 for (_i = 0, _len = _ref.length; _i < _len; _i++) {
23 _results.push(assert.equal(topic.camelize(words[i]), i));
27 'word with first letter lower': function(topic) {
28 var i, words, _i, _len, _ref, _results;
29 words = cases.UnderscoreToLowerCamel;
30 _ref = Object.keys(words);
32 for (_i = 0, _len = _ref.length; _i < _len; _i++) {
34 _results.push(assert.equal(topic.camelize(i, false), words[i]));
38 'path': function(topic) {
39 var i, words, _i, _len, _ref, _results;
40 words = cases.CamelWithModuleToUnderscoreWithSlash;
41 _ref = Object.keys(words);
43 for (_i = 0, _len = _ref.length; _i < _len; _i++) {
45 _results.push(assert.equal(topic.camelize(words[i]), i));
49 'path with first letter lower': function(topic) {
50 return assert.equal(topic.camelize('bullet_record/errors', false), 'bulletRecord.Errors');
54 'word': function(topic) {
55 var i, words, _i, _j, _len, _len2, _ref, _ref2, _results;
56 words = cases.CamelToUnderscore;
57 _ref = Object.keys(words);
58 for (_i = 0, _len = _ref.length; _i < _len; _i++) {
60 assert.equal(topic.underscore(i), words[i]);
62 words = cases.CamelToUnderscoreWithoutReverse;
63 _ref2 = Object.keys(words);
65 for (_j = 0, _len2 = _ref2.length; _j < _len2; _j++) {
67 _results.push(assert.equal(topic.underscore(i), words[i]));
71 'path': function(topic) {
72 var i, words, _i, _len, _ref, _results;
73 words = cases.CamelWithModuleToUnderscoreWithSlash;
74 _ref = Object.keys(words);
76 for (_i = 0, _len = _ref.length; _i < _len; _i++) {
78 _results.push(assert.equal(topic.underscore(i), words[i]));
82 'from dasherize': function(topic) {
83 var i, words, _i, _len, _ref, _results;
84 words = cases.UnderscoresToDashes;
85 _ref = Object.keys(words);
87 for (_i = 0, _len = _ref.length; _i < _len; _i++) {
89 _results.push(assert.equal(topic.underscore(topic.dasherize(i)), i));
95 'underscored_word': function(topic) {
96 var i, words, _i, _len, _ref, _results;
97 words = cases.UnderscoresToDashes;
98 _ref = Object.keys(words);
100 for (_i = 0, _len = _ref.length; _i < _len; _i++) {
102 _results.push(assert.equal(topic.dasherize(i), words[i]));
108 'module name': function(topic) {
109 return assert.equal(topic.demodulize('BulletRecord.CoreExtensions.Inflections'), 'Inflections');
111 'isolated module name': function(topic) {
112 return assert.equal(topic.demodulize('Inflections'), 'Inflections');
116 'normal': function(topic) {
117 var i, words, _i, _len, _ref, _results;
118 words = cases.ClassNameToForeignKeyWithoutUnderscore;
119 _ref = Object.keys(words);
121 for (_i = 0, _len = _ref.length; _i < _len; _i++) {
123 _results.push(assert.equal(topic.foreign_key(i, false), words[i]));
127 'with_underscore': function(topic) {
128 var i, words, _i, _len, _ref, _results;
129 words = cases.ClassNameToForeignKeyWithUnderscore;
130 _ref = Object.keys(words);
132 for (_i = 0, _len = _ref.length; _i < _len; _i++) {
134 _results.push(assert.equal(topic.foreign_key(i), words[i]));
139 'ordinalize': function(topic) {
140 var i, words, _i, _len, _ref, _results;
141 words = cases.OrdinalNumbers;
142 _ref = Object.keys(words);
144 for (_i = 0, _len = _ref.length; _i < _len; _i++) {
146 _results.push(assert.equal(topic.ordinalize(i), words[i]));
152 'Test inflector inflection methods': {
155 Inflector = require('../../lib/methods');
156 Inflector.inflections["default"]();
160 'empty': function(topic) {
161 return assert.equal(topic.pluralize(''), '');
163 'uncountable': function(topic) {
164 return assert.equal(topic.pluralize('money'), 'money');
166 'normal': function(topic) {
167 topic.inflections.irregular('octopus', 'octopi');
168 return assert.equal(topic.pluralize('octopus'), 'octopi');
170 'cases': function(topic) {
171 var i, words, _i, _j, _len, _len2, _ref, _ref2, _results;
172 words = cases.SingularToPlural;
173 _ref = Object.keys(words);
174 for (_i = 0, _len = _ref.length; _i < _len; _i++) {
176 assert.equal(topic.pluralize(i), words[i]);
178 _ref2 = Object.keys(words);
180 for (_j = 0, _len2 = _ref2.length; _j < _len2; _j++) {
182 _results.push(assert.equal(topic.pluralize(util.string.capitalize(i)), util.string.capitalize(words[i])));
186 'cases plural': function(topic) {
187 var i, words, _i, _j, _len, _len2, _ref, _ref2, _results;
188 words = cases.SingularToPlural;
189 _ref = Object.keys(words);
190 for (_i = 0, _len = _ref.length; _i < _len; _i++) {
192 assert.equal(topic.pluralize(words[i]), words[i]);
194 _ref2 = Object.keys(words);
196 for (_j = 0, _len2 = _ref2.length; _j < _len2; _j++) {
198 _results.push(assert.equal(topic.pluralize(util.string.capitalize(words[i])), util.string.capitalize(words[i])));
204 'empty': function(topic) {
205 return assert.equal(topic.singularize(''), '');
207 'uncountable': function(topic) {
208 return assert.equal(topic.singularize('money'), 'money');
210 'normal': function(topic) {
211 topic.inflections.irregular('octopus', 'octopi');
212 return assert.equal(topic.singularize('octopi'), 'octopus');
214 'cases': function(topic) {
215 var i, words, _i, _j, _len, _len2, _ref, _ref2, _results;
216 words = cases.SingularToPlural;
217 _ref = Object.keys(words);
218 for (_i = 0, _len = _ref.length; _i < _len; _i++) {
220 assert.equal(topic.singularize(words[i]), i);
222 _ref2 = Object.keys(words);
224 for (_j = 0, _len2 = _ref2.length; _j < _len2; _j++) {
226 _results.push(assert.equal(topic.singularize(util.string.capitalize(words[i])), util.string.capitalize(i)));
232 'normal': function(topic) {
233 var i, words, _i, _j, _k, _len, _len2, _len3, _results;
234 words = topic.inflections.uncountables;
235 for (_i = 0, _len = words.length; _i < _len; _i++) {
237 assert.equal(topic.singularize(i), i);
239 for (_j = 0, _len2 = words.length; _j < _len2; _j++) {
241 assert.equal(topic.pluralize(i), i);
244 for (_k = 0, _len3 = words.length; _k < _len3; _k++) {
246 _results.push(assert.equal(topic.singularize(i), topic.pluralize(i)));
250 'greedy': function(topic) {
251 var countable_word, uncountable_word;
252 uncountable_word = "ors";
253 countable_word = "sponsor";
254 topic.inflections.uncountable(uncountable_word);
255 assert.equal(topic.singularize(uncountable_word), uncountable_word);
256 assert.equal(topic.pluralize(uncountable_word), uncountable_word);
257 assert.equal(topic.pluralize(uncountable_word), topic.singularize(uncountable_word));
258 assert.equal(topic.singularize(countable_word), 'sponsor');
259 assert.equal(topic.pluralize(countable_word), 'sponsors');
260 return assert.equal(topic.singularize(topic.pluralize(countable_word)), 'sponsor');
264 'normal': function(topic) {
265 var i, words, _i, _len, _ref, _results;
266 words = cases.UnderscoreToHuman;
267 _ref = Object.keys(words);
269 for (_i = 0, _len = _ref.length; _i < _len; _i++) {
271 _results.push(assert.equal(topic.humanize(i), words[i]));
275 'with rule': function(topic) {
276 topic.inflections.human(/^(.*)_cnt$/i, '$1_count');
277 topic.inflections.human(/^prefix_(.*)$/i, '$1');
278 assert.equal(topic.humanize('jargon_cnt'), 'Jargon count');
279 return assert.equal(topic.humanize('prefix_request'), 'Request');
281 'with string': function(topic) {
282 topic.inflections.human('col_rpted_bugs', 'Reported bugs');
283 assert.equal(topic.humanize('col_rpted_bugs'), 'Reported bugs');
284 return assert.equal(topic.humanize('COL_rpted_bugs'), 'Col rpted bugs');
286 'with _id': function(topic) {
287 return assert.equal(topic.humanize('author_id'), 'Author');
291 'normal': function(topic) {
292 var i, words, _i, _len, _ref, _results;
293 words = cases.MixtureToTitleCase;
294 _ref = Object.keys(words);
296 for (_i = 0, _len = _ref.length; _i < _len; _i++) {
298 _results.push(assert.equal(topic.titleize(i), words[i]));
302 'with hyphens': function(topic) {
303 return assert.equal(topic.titleize('x-men: the last stand'), 'X Men: The Last Stand');
305 'with ampersands': function(topic) {
306 return assert.equal(topic.titleize('garfunkel & oates'), 'Garfunkel & Oates');
309 'tableize': function(topic) {
310 var i, words, _i, _len, _ref, _results;
311 words = cases.ClassNameToTableName;
312 _ref = Object.keys(words);
314 for (_i = 0, _len = _ref.length; _i < _len; _i++) {
316 _results.push(assert.equal(topic.tableize(i), words[i]));
321 'underscore': function(topic) {
322 var i, words, _i, _j, _len, _len2, _ref, _ref2, _results;
323 words = cases.ClassNameToTableName;
324 _ref = Object.keys(words);
325 for (_i = 0, _len = _ref.length; _i < _len; _i++) {
327 assert.equal(topic.classify(words[i]), i);
329 _ref2 = Object.keys(words);
331 for (_j = 0, _len2 = _ref2.length; _j < _len2; _j++) {
333 _results.push(assert.equal(topic.classify('table_prefix.' + words[i]), i));
337 'normal': function(topic) {
338 topic.inflections.irregular('octopus', 'octopi');
339 return assert.equal(topic.classify('octopi'), 'Octopus');
343 })["export"](module);