methods-test.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. (function() {
  2. var assert, cases, vows, util;
  3. vows = require('vows');
  4. assert = require('assert');
  5. util = require('../../lib/util');
  6. cases = require('./cases');
  7. vows.describe('Module Inflector methods').addBatch({
  8. 'Test inflector method': {
  9. topic: require('../../lib/methods'),
  10. 'camelize': {
  11. 'word': function(topic) {
  12. var i, words, _i, _len, _ref, _results;
  13. words = cases.CamelToUnderscore;
  14. _ref = Object.keys(words);
  15. _results = [];
  16. for (_i = 0, _len = _ref.length; _i < _len; _i++) {
  17. i = _ref[_i];
  18. _results.push(assert.equal(topic.camelize(words[i]), i));
  19. }
  20. return _results;
  21. },
  22. 'word with first letter lower': function(topic) {
  23. var i, words, _i, _len, _ref, _results;
  24. words = cases.UnderscoreToLowerCamel;
  25. _ref = Object.keys(words);
  26. _results = [];
  27. for (_i = 0, _len = _ref.length; _i < _len; _i++) {
  28. i = _ref[_i];
  29. _results.push(assert.equal(topic.camelize(i, false), words[i]));
  30. }
  31. return _results;
  32. },
  33. 'path': function(topic) {
  34. var i, words, _i, _len, _ref, _results;
  35. words = cases.CamelWithModuleToUnderscoreWithSlash;
  36. _ref = Object.keys(words);
  37. _results = [];
  38. for (_i = 0, _len = _ref.length; _i < _len; _i++) {
  39. i = _ref[_i];
  40. _results.push(assert.equal(topic.camelize(words[i]), i));
  41. }
  42. return _results;
  43. },
  44. 'path with first letter lower': function(topic) {
  45. return assert.equal(topic.camelize('bullet_record/errors', false), 'bulletRecord.Errors');
  46. }
  47. },
  48. 'underscore': {
  49. 'word': function(topic) {
  50. var i, words, _i, _j, _len, _len2, _ref, _ref2, _results;
  51. words = cases.CamelToUnderscore;
  52. _ref = Object.keys(words);
  53. for (_i = 0, _len = _ref.length; _i < _len; _i++) {
  54. i = _ref[_i];
  55. assert.equal(topic.underscore(i), words[i]);
  56. }
  57. words = cases.CamelToUnderscoreWithoutReverse;
  58. _ref2 = Object.keys(words);
  59. _results = [];
  60. for (_j = 0, _len2 = _ref2.length; _j < _len2; _j++) {
  61. i = _ref2[_j];
  62. _results.push(assert.equal(topic.underscore(i), words[i]));
  63. }
  64. return _results;
  65. },
  66. 'path': function(topic) {
  67. var i, words, _i, _len, _ref, _results;
  68. words = cases.CamelWithModuleToUnderscoreWithSlash;
  69. _ref = Object.keys(words);
  70. _results = [];
  71. for (_i = 0, _len = _ref.length; _i < _len; _i++) {
  72. i = _ref[_i];
  73. _results.push(assert.equal(topic.underscore(i), words[i]));
  74. }
  75. return _results;
  76. },
  77. 'from dasherize': function(topic) {
  78. var i, words, _i, _len, _ref, _results;
  79. words = cases.UnderscoresToDashes;
  80. _ref = Object.keys(words);
  81. _results = [];
  82. for (_i = 0, _len = _ref.length; _i < _len; _i++) {
  83. i = _ref[_i];
  84. _results.push(assert.equal(topic.underscore(topic.dasherize(i)), i));
  85. }
  86. return _results;
  87. }
  88. },
  89. 'dasherize': {
  90. 'underscored_word': function(topic) {
  91. var i, words, _i, _len, _ref, _results;
  92. words = cases.UnderscoresToDashes;
  93. _ref = Object.keys(words);
  94. _results = [];
  95. for (_i = 0, _len = _ref.length; _i < _len; _i++) {
  96. i = _ref[_i];
  97. _results.push(assert.equal(topic.dasherize(i), words[i]));
  98. }
  99. return _results;
  100. }
  101. },
  102. 'demodulize': {
  103. 'module name': function(topic) {
  104. return assert.equal(topic.demodulize('BulletRecord.CoreExtensions.Inflections'), 'Inflections');
  105. },
  106. 'isolated module name': function(topic) {
  107. return assert.equal(topic.demodulize('Inflections'), 'Inflections');
  108. }
  109. },
  110. 'foreign_key': {
  111. 'normal': function(topic) {
  112. var i, words, _i, _len, _ref, _results;
  113. words = cases.ClassNameToForeignKeyWithoutUnderscore;
  114. _ref = Object.keys(words);
  115. _results = [];
  116. for (_i = 0, _len = _ref.length; _i < _len; _i++) {
  117. i = _ref[_i];
  118. _results.push(assert.equal(topic.foreign_key(i, false), words[i]));
  119. }
  120. return _results;
  121. },
  122. 'with_underscore': function(topic) {
  123. var i, words, _i, _len, _ref, _results;
  124. words = cases.ClassNameToForeignKeyWithUnderscore;
  125. _ref = Object.keys(words);
  126. _results = [];
  127. for (_i = 0, _len = _ref.length; _i < _len; _i++) {
  128. i = _ref[_i];
  129. _results.push(assert.equal(topic.foreign_key(i), words[i]));
  130. }
  131. return _results;
  132. }
  133. },
  134. 'ordinalize': function(topic) {
  135. var i, words, _i, _len, _ref, _results;
  136. words = cases.OrdinalNumbers;
  137. _ref = Object.keys(words);
  138. _results = [];
  139. for (_i = 0, _len = _ref.length; _i < _len; _i++) {
  140. i = _ref[_i];
  141. _results.push(assert.equal(topic.ordinalize(i), words[i]));
  142. }
  143. return _results;
  144. }
  145. }
  146. }).addBatch({
  147. 'Test inflector inflection methods': {
  148. topic: function() {
  149. var Inflector;
  150. Inflector = require('../../lib/methods');
  151. Inflector.inflections["default"]();
  152. return Inflector;
  153. },
  154. 'pluralize': {
  155. 'empty': function(topic) {
  156. return assert.equal(topic.pluralize(''), '');
  157. },
  158. 'uncountable': function(topic) {
  159. return assert.equal(topic.pluralize('money'), 'money');
  160. },
  161. 'normal': function(topic) {
  162. topic.inflections.irregular('octopus', 'octopi');
  163. return assert.equal(topic.pluralize('octopus'), 'octopi');
  164. },
  165. 'cases': function(topic) {
  166. var i, words, _i, _j, _len, _len2, _ref, _ref2, _results;
  167. words = cases.SingularToPlural;
  168. _ref = Object.keys(words);
  169. for (_i = 0, _len = _ref.length; _i < _len; _i++) {
  170. i = _ref[_i];
  171. assert.equal(topic.pluralize(i), words[i]);
  172. }
  173. _ref2 = Object.keys(words);
  174. _results = [];
  175. for (_j = 0, _len2 = _ref2.length; _j < _len2; _j++) {
  176. i = _ref2[_j];
  177. _results.push(assert.equal(topic.pluralize(util.string.capitalize(i)), util.string.capitalize(words[i])));
  178. }
  179. return _results;
  180. },
  181. 'cases plural': function(topic) {
  182. var i, words, _i, _j, _len, _len2, _ref, _ref2, _results;
  183. words = cases.SingularToPlural;
  184. _ref = Object.keys(words);
  185. for (_i = 0, _len = _ref.length; _i < _len; _i++) {
  186. i = _ref[_i];
  187. assert.equal(topic.pluralize(words[i]), words[i]);
  188. }
  189. _ref2 = Object.keys(words);
  190. _results = [];
  191. for (_j = 0, _len2 = _ref2.length; _j < _len2; _j++) {
  192. i = _ref2[_j];
  193. _results.push(assert.equal(topic.pluralize(util.string.capitalize(words[i])), util.string.capitalize(words[i])));
  194. }
  195. return _results;
  196. }
  197. },
  198. 'singuralize': {
  199. 'empty': function(topic) {
  200. return assert.equal(topic.singularize(''), '');
  201. },
  202. 'uncountable': function(topic) {
  203. return assert.equal(topic.singularize('money'), 'money');
  204. },
  205. 'normal': function(topic) {
  206. topic.inflections.irregular('octopus', 'octopi');
  207. return assert.equal(topic.singularize('octopi'), 'octopus');
  208. },
  209. 'cases': function(topic) {
  210. var i, words, _i, _j, _len, _len2, _ref, _ref2, _results;
  211. words = cases.SingularToPlural;
  212. _ref = Object.keys(words);
  213. for (_i = 0, _len = _ref.length; _i < _len; _i++) {
  214. i = _ref[_i];
  215. assert.equal(topic.singularize(words[i]), i);
  216. }
  217. _ref2 = Object.keys(words);
  218. _results = [];
  219. for (_j = 0, _len2 = _ref2.length; _j < _len2; _j++) {
  220. i = _ref2[_j];
  221. _results.push(assert.equal(topic.singularize(util.string.capitalize(words[i])), util.string.capitalize(i)));
  222. }
  223. return _results;
  224. }
  225. },
  226. 'uncountablility': {
  227. 'normal': function(topic) {
  228. var i, words, _i, _j, _k, _len, _len2, _len3, _results;
  229. words = topic.inflections.uncountables;
  230. for (_i = 0, _len = words.length; _i < _len; _i++) {
  231. i = words[_i];
  232. assert.equal(topic.singularize(i), i);
  233. }
  234. for (_j = 0, _len2 = words.length; _j < _len2; _j++) {
  235. i = words[_j];
  236. assert.equal(topic.pluralize(i), i);
  237. }
  238. _results = [];
  239. for (_k = 0, _len3 = words.length; _k < _len3; _k++) {
  240. i = words[_k];
  241. _results.push(assert.equal(topic.singularize(i), topic.pluralize(i)));
  242. }
  243. return _results;
  244. },
  245. 'greedy': function(topic) {
  246. var countable_word, uncountable_word;
  247. uncountable_word = "ors";
  248. countable_word = "sponsor";
  249. topic.inflections.uncountable(uncountable_word);
  250. assert.equal(topic.singularize(uncountable_word), uncountable_word);
  251. assert.equal(topic.pluralize(uncountable_word), uncountable_word);
  252. assert.equal(topic.pluralize(uncountable_word), topic.singularize(uncountable_word));
  253. assert.equal(topic.singularize(countable_word), 'sponsor');
  254. assert.equal(topic.pluralize(countable_word), 'sponsors');
  255. return assert.equal(topic.singularize(topic.pluralize(countable_word)), 'sponsor');
  256. }
  257. },
  258. 'humanize': {
  259. 'normal': function(topic) {
  260. var i, words, _i, _len, _ref, _results;
  261. words = cases.UnderscoreToHuman;
  262. _ref = Object.keys(words);
  263. _results = [];
  264. for (_i = 0, _len = _ref.length; _i < _len; _i++) {
  265. i = _ref[_i];
  266. _results.push(assert.equal(topic.humanize(i), words[i]));
  267. }
  268. return _results;
  269. },
  270. 'with rule': function(topic) {
  271. topic.inflections.human(/^(.*)_cnt$/i, '$1_count');
  272. topic.inflections.human(/^prefix_(.*)$/i, '$1');
  273. assert.equal(topic.humanize('jargon_cnt'), 'Jargon count');
  274. return assert.equal(topic.humanize('prefix_request'), 'Request');
  275. },
  276. 'with string': function(topic) {
  277. topic.inflections.human('col_rpted_bugs', 'Reported bugs');
  278. assert.equal(topic.humanize('col_rpted_bugs'), 'Reported bugs');
  279. return assert.equal(topic.humanize('COL_rpted_bugs'), 'Col rpted bugs');
  280. },
  281. 'with _id': function(topic) {
  282. return assert.equal(topic.humanize('author_id'), 'Author');
  283. },
  284. 'with just _id': function(topic) {
  285. return assert.equal(topic.humanize('_id'), '');
  286. }
  287. },
  288. 'titleize': {
  289. 'normal': function(topic) {
  290. var i, words, _i, _len, _ref, _results;
  291. words = cases.MixtureToTitleCase;
  292. _ref = Object.keys(words);
  293. _results = [];
  294. for (_i = 0, _len = _ref.length; _i < _len; _i++) {
  295. i = _ref[_i];
  296. _results.push(assert.equal(topic.titleize(i), words[i]));
  297. }
  298. return _results;
  299. },
  300. 'with hyphens': function(topic) {
  301. return assert.equal(topic.titleize('x-men: the last stand'), 'X Men: The Last Stand');
  302. },
  303. 'with ampersands': function(topic) {
  304. return assert.equal(topic.titleize('garfunkel & oates'), 'Garfunkel & Oates');
  305. }
  306. },
  307. 'tableize': function(topic) {
  308. var i, words, _i, _len, _ref, _results;
  309. words = cases.ClassNameToTableName;
  310. _ref = Object.keys(words);
  311. _results = [];
  312. for (_i = 0, _len = _ref.length; _i < _len; _i++) {
  313. i = _ref[_i];
  314. _results.push(assert.equal(topic.tableize(i), words[i]));
  315. }
  316. return _results;
  317. },
  318. 'classify': {
  319. 'underscore': function(topic) {
  320. var i, words, _i, _j, _len, _len2, _ref, _ref2, _results;
  321. words = cases.ClassNameToTableName;
  322. _ref = Object.keys(words);
  323. for (_i = 0, _len = _ref.length; _i < _len; _i++) {
  324. i = _ref[_i];
  325. assert.equal(topic.classify(words[i]), i);
  326. }
  327. _ref2 = Object.keys(words);
  328. _results = [];
  329. for (_j = 0, _len2 = _ref2.length; _j < _len2; _j++) {
  330. i = _ref2[_j];
  331. _results.push(assert.equal(topic.classify('table_prefix.' + words[i]), i));
  332. }
  333. return _results;
  334. },
  335. 'normal': function(topic) {
  336. topic.inflections.irregular('octopus', 'octopi');
  337. return assert.equal(topic.classify('octopi'), 'Octopus');
  338. }
  339. }
  340. }
  341. })["export"](module);
  342. }).call(this);