PageRenderTime 44ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/test/unit/tests/enumerable.test.js

http://github.com/sstephenson/prototype
JavaScript | 456 lines | 373 code | 78 blank | 5 comment | 13 complexity | c3281ef445e78b6fa2d3456e20e9e33c MD5 | raw file
  1. Fixtures.Enumerable = {
  2. People: [
  3. {name: 'Sam Stephenson', nickname: 'sam-'},
  4. {name: 'Marcel Molina Jr.', nickname: 'noradio'},
  5. {name: 'Scott Barron', nickname: 'htonl'},
  6. {name: 'Nicholas Seckar', nickname: 'Ulysses'}
  7. ],
  8. Nicknames: $w('sam- noradio htonl Ulysses'),
  9. Basic: [1, 2, 3],
  10. Primes: [
  11. 1, 2, 3, 5, 7, 11, 13, 17, 19, 23,
  12. 29, 31, 37, 41, 43, 47, 53, 59, 61, 67,
  13. 71, 73, 79, 83, 89, 97
  14. ],
  15. Z: []
  16. };
  17. for (var i = 1; i <= 100; i++)
  18. Fixtures.Enumerable.Z.push(i);
  19. ///////
  20. function prime(value) {
  21. for (var i = 2; i < value; i++) {
  22. if (value % i == 0) return false;
  23. }
  24. return true;
  25. }
  26. suite('Enumerable', function () {
  27. this.name = 'enumerable';
  28. test('#each ($break)', function () {
  29. var result = 0;
  30. Fixtures.Enumerable.Basic.each(function(value) {
  31. if ((result = value) == 2) throw $break;
  32. });
  33. assert.equal(2, result);
  34. });
  35. test('#each (return acts as continue)', function () {
  36. var results = [];
  37. Fixtures.Enumerable.Basic.each(function(value) {
  38. if (value == 2) return;
  39. results.push(value);
  40. });
  41. assert.equal('1, 3', results.join(', '));
  42. });
  43. test('#each (passes value, index, collection to the iterator)', function () {
  44. var i = 0;
  45. Fixtures.Enumerable.Basic.each(function(value, index, collection) {
  46. assert.strictEqual(Fixtures.Enumerable.Basic[i], value);
  47. assert.strictEqual(i, index);
  48. assert.strictEqual(Fixtures.Enumerable.Basic, collection);
  49. i++;
  50. });
  51. });
  52. test('#each (chaining)', function () {
  53. assert.equal(Fixtures.Enumerable.Primes, Fixtures.Enumerable.Primes.each(Prototype.emptyFunction));
  54. assert.equal(3, Fixtures.Enumerable.Basic.each(Prototype.emptyFunction).length);
  55. });
  56. test('context', function () {
  57. var results = [];
  58. Fixtures.Enumerable.Basic.each(function(value) {
  59. results.push(value * this.i);
  60. }, { i: 2 });
  61. assert.equal('2 4 6', results.join(' '));
  62. assert(Fixtures.Enumerable.Basic.all(function(value){
  63. return value >= this.min && value <= this.max;
  64. }, { min: 1, max: 3 }));
  65. assert(!Fixtures.Enumerable.Basic.all(function(value){
  66. return value >= this.min && value <= this.max;
  67. }));
  68. assert(Fixtures.Enumerable.Basic.any(function(value){
  69. return value == this.target_value;
  70. }, { target_value: 2 }));
  71. });
  72. test('#any', function () {
  73. assert(!([].any()));
  74. assert([true, true, true].any());
  75. assert([true, false, false].any());
  76. assert(![false, false, false].any());
  77. assert(Fixtures.Enumerable.Basic.any(function(value) {
  78. return value > 2;
  79. }));
  80. assert(!Fixtures.Enumerable.Basic.any(function(value) {
  81. return value > 5;
  82. }));
  83. });
  84. test('#any (passes value, index, collection to the iterator)', function () {
  85. var i = 0;
  86. Fixtures.Enumerable.Basic.any(function(value, index, collection) {
  87. assert.strictEqual(Fixtures.Enumerable.Basic[i], value);
  88. assert.strictEqual(i, index);
  89. assert.strictEqual(Fixtures.Enumerable.Basic, collection);
  90. i++;
  91. // Iterate over all values
  92. return value > 5;
  93. }, this);
  94. });
  95. test('#all', function () {
  96. assert([].all());
  97. assert([true, true, true].all());
  98. assert(![true, false, false].all());
  99. assert(![false, false, false].all());
  100. assert(Fixtures.Enumerable.Basic.all(function(value) {
  101. return value > 0;
  102. }));
  103. assert(!Fixtures.Enumerable.Basic.all(function(value) {
  104. return value > 1;
  105. }));
  106. });
  107. test('#all (passes value, context, collection to the iterator)', function () {
  108. var i = 0;
  109. Fixtures.Enumerable.Basic.all(function(value, index, collection) {
  110. assert.strictEqual(Fixtures.Enumerable.Basic[i], value);
  111. assert.strictEqual(i, index);
  112. assert.strictEqual(Fixtures.Enumerable.Basic, collection);
  113. i++;
  114. // Iterate over all values
  115. return value > 0;
  116. });
  117. });
  118. test('#collect', function () {
  119. assert.equal(Fixtures.Enumerable.Nicknames.join(', '),
  120. Fixtures.Enumerable.People.collect(function(person) {
  121. return person.nickname;
  122. }).join(", "));
  123. assert.equal(26, Fixtures.Enumerable.Primes.map().length);
  124. });
  125. test('#collect (passes value, index, collection to the iterator)', function () {
  126. var i = 0;
  127. Fixtures.Enumerable.Basic.collect(function(value, index, collection) {
  128. assert.strictEqual(Fixtures.Enumerable.Basic[i], value);
  129. assert.strictEqual(i, index);
  130. assert.strictEqual(Fixtures.Enumerable.Basic, collection);
  131. i++;
  132. return value;
  133. });
  134. });
  135. test('#detect', function () {
  136. assert.equal('Marcel Molina Jr.',
  137. Fixtures.Enumerable.People.detect(function(person) {
  138. return person.nickname.match(/no/);
  139. }).name);
  140. });
  141. test('#detect (passes value, index, collection to the iterator)', function () {
  142. var i = 0;
  143. Fixtures.Enumerable.Basic.detect(function(value, index, collection) {
  144. assert.strictEqual(Fixtures.Enumerable.Basic[i], value);
  145. assert.strictEqual(i, index);
  146. assert.strictEqual(Fixtures.Enumerable.Basic, collection);
  147. i++;
  148. // Iterate over all values
  149. return value > 5;
  150. });
  151. });
  152. test('#eachSlice', function () {
  153. assert.enumEqual([], [].eachSlice(2));
  154. assert.equal(1, [1].eachSlice(1).length);
  155. assert.enumEqual([1], [1].eachSlice(1)[0]);
  156. assert.equal(2, Fixtures.Enumerable.Basic.eachSlice(2).length);
  157. assert.enumEqual(
  158. [3, 2, 1, 11, 7, 5, 19, 17, 13, 31, 29, 23, 43, 41, 37, 59, 53, 47, 71, 67, 61, 83, 79, 73, 97, 89],
  159. Fixtures.Enumerable.Primes.eachSlice( 3, function(slice){ return slice.reverse(); }).flatten()
  160. );
  161. assert.enumEqual(Fixtures.Enumerable.Basic, Fixtures.Enumerable.Basic.eachSlice(-10));
  162. assert.enumEqual(Fixtures.Enumerable.Basic, Fixtures.Enumerable.Basic.eachSlice(0));
  163. assert.notStrictEqual(Fixtures.Enumerable.Basic, Fixtures.Enumerable.Basic.eachSlice(0));
  164. });
  165. test('#each (with index)', function () {
  166. var nicknames = [], indexes = [];
  167. Fixtures.Enumerable.People.each(function(person, index) {
  168. nicknames.push(person.nickname);
  169. indexes.push(index);
  170. });
  171. assert.equal(Fixtures.Enumerable.Nicknames.join(', '),
  172. nicknames.join(', '));
  173. assert.equal('0, 1, 2, 3', indexes.join(', '));
  174. });
  175. test('#findAll', function () {
  176. assert.equal(Fixtures.Enumerable.Primes.join(', '),
  177. Fixtures.Enumerable.Z.findAll(prime).join(', '));
  178. });
  179. test('#findAll (passes value, index, collection to the iterator)', function () {
  180. var i = 0;
  181. Fixtures.Enumerable.Basic.findAll(function(value, index, collection) {
  182. assert.strictEqual(Fixtures.Enumerable.Basic[i], value);
  183. assert.strictEqual(i, index);
  184. assert.strictEqual(Fixtures.Enumerable.Basic, collection);
  185. i++;
  186. return value > 5;
  187. });
  188. });
  189. test('#grep', function () {
  190. assert.equal('noradio, htonl',
  191. Fixtures.Enumerable.Nicknames.grep(/o/).join(", "));
  192. assert.equal('NORADIO, HTONL',
  193. Fixtures.Enumerable.Nicknames.grep(/o/, function(nickname) {
  194. return nickname.toUpperCase();
  195. }).join(", "));
  196. assert.enumEqual(
  197. $('grepHeader', 'grepCell'),
  198. $('grepTable', 'grepTBody', 'grepRow', 'grepHeader', 'grepCell').grep(new Selector('.cell'))
  199. );
  200. // troublesome characters
  201. assert.enumEqual(['?a', 'c?'], ['?a','b','c?'].grep('?'));
  202. assert.enumEqual(['*a', 'c*'], ['*a','b','c*'].grep('*'));
  203. assert.enumEqual(['+a', 'c+'], ['+a','b','c+'].grep('+'));
  204. assert.enumEqual(['{1}a', 'c{1}'], ['{1}a','b','c{1}'].grep('{1}'));
  205. assert.enumEqual(['(a', 'c('], ['(a','b','c('].grep('('));
  206. assert.enumEqual(['|a', 'c|'], ['|a','b','c|'].grep('|'));
  207. });
  208. test('#grep (passes value, index, collection to the iterator)', function () {
  209. var i = 0;
  210. Fixtures.Enumerable.Basic.grep(/\d/, function(value, index, collection) {
  211. assert.strictEqual(Fixtures.Enumerable.Basic[i], value);
  212. assert.strictEqual(i, index);
  213. assert.strictEqual(Fixtures.Enumerable.Basic, collection);
  214. i++;
  215. return value;
  216. });
  217. });
  218. test('#include', function () {
  219. assert(Fixtures.Enumerable.Nicknames.include('sam-'));
  220. assert(Fixtures.Enumerable.Nicknames.include('noradio'));
  221. assert(!Fixtures.Enumerable.Nicknames.include('gmosx'));
  222. assert(Fixtures.Enumerable.Basic.include(2));
  223. assert(Fixtures.Enumerable.Basic.include('2'));
  224. assert(!Fixtures.Enumerable.Basic.include('4'));
  225. });
  226. test('#inGroupsOf', function () {
  227. assert.enumEqual([], [].inGroupsOf(3));
  228. var arr = [1, 2, 3, 4, 5, 6].inGroupsOf(3);
  229. assert.equal(2, arr.length);
  230. assert.enumEqual([1, 2, 3], arr[0]);
  231. assert.enumEqual([4, 5, 6], arr[1]);
  232. arr = [1, 2, 3, 4, 5, 6].inGroupsOf(4);
  233. assert.equal(2, arr.length);
  234. assert.enumEqual([1, 2, 3, 4], arr[0]);
  235. assert.enumEqual([5, 6, null, null], arr[1]);
  236. var basic = Fixtures.Enumerable.Basic;
  237. arr = basic.inGroupsOf(4,'x');
  238. assert.equal(1, arr.length);
  239. assert.enumEqual([1, 2, 3, 'x'], arr[0]);
  240. assert.enumEqual([1,2,3,'a'], basic.inGroupsOf(2, 'a').flatten());
  241. arr = basic.inGroupsOf(5, '');
  242. assert.equal(1, arr.length);
  243. assert.enumEqual([1, 2, 3, '', ''], arr[0]);
  244. assert.enumEqual([1,2,3,0], basic.inGroupsOf(2, 0).flatten());
  245. assert.enumEqual([1,2,3,false], basic.inGroupsOf(2, false).flatten());
  246. });
  247. test('#inject', function () {
  248. assert.equal(1061,
  249. Fixtures.Enumerable.Primes.inject(0, function(sum, value) {
  250. return sum + value;
  251. })
  252. );
  253. });
  254. test('#inject (passes memo, value, index, collection to the iterator)', function () {
  255. var i = 0;
  256. Fixtures.Enumerable.Basic.inject(0, function(memo, value, index, collection) {
  257. assert.strictEqual(Fixtures.Enumerable.Basic[i], value);
  258. assert.strictEqual(i, index);
  259. assert.strictEqual(Fixtures.Enumerable.Basic, collection);
  260. i++;
  261. return memo + value;
  262. });
  263. });
  264. test('#invoke', function () {
  265. var result = [[2, 1, 3], [6, 5, 4]].invoke('sort');
  266. assert.equal(2, result.length);
  267. assert.equal('1, 2, 3', result[0].join(', '));
  268. assert.equal('4, 5, 6', result[1].join(', '));
  269. result = result.invoke('invoke', 'toString', 2);
  270. assert.equal('1, 10, 11', result[0].join(', '));
  271. assert.equal('100, 101, 110', result[1].join(', '));
  272. });
  273. test('#max', function () {
  274. assert.equal(100, Fixtures.Enumerable.Z.max());
  275. assert.equal(97, Fixtures.Enumerable.Primes.max());
  276. assert.equal(2, [ -9, -8, -7, -6, -4, -3, -2, 0, -1, 2 ].max());
  277. assert.equal('sam-', Fixtures.Enumerable.Nicknames.max()); // ?s > ?U
  278. });
  279. test('#max (passes value, index, collection to the iterator)', function () {
  280. var i = 0;
  281. Fixtures.Enumerable.Basic.max(function(value, index, collection) {
  282. assert.strictEqual(Fixtures.Enumerable.Basic[i], value);
  283. assert.strictEqual(i, index);
  284. assert.strictEqual(Fixtures.Enumerable.Basic, collection);
  285. i++;
  286. return value;
  287. });
  288. });
  289. test('#min', function () {
  290. assert.equal(1, Fixtures.Enumerable.Z.min());
  291. assert.equal(0, [ 1, 2, 3, 4, 5, 6, 7, 8, 0, 9 ].min());
  292. assert.equal('Ulysses', Fixtures.Enumerable.Nicknames.min()); // ?U < ?h
  293. });
  294. test('#min (passes value, index, collection to the iterator)', function () {
  295. var i = 0;
  296. Fixtures.Enumerable.Basic.min(function(value, index, collection) {
  297. assert.strictEqual(Fixtures.Enumerable.Basic[i], value);
  298. assert.strictEqual(i, index);
  299. assert.strictEqual(Fixtures.Enumerable.Basic, collection);
  300. i++;
  301. return value;
  302. }, this);
  303. });
  304. test('#partition', function () {
  305. var result = Fixtures.Enumerable.People.partition(function(person) {
  306. return person.name.length < 15;
  307. }).invoke('pluck', 'nickname');
  308. assert.equal(2, result.length);
  309. assert.equal('sam-, htonl', result[0].join(', '));
  310. assert.equal('noradio, Ulysses', result[1].join(', '));
  311. });
  312. test('#partition (passes value, index, collection to the iterator)', function () {
  313. var i = 0;
  314. Fixtures.Enumerable.Basic.partition(function(value, index, collection) {
  315. assert.strictEqual(Fixtures.Enumerable.Basic[i], value);
  316. assert.strictEqual(i, index);
  317. assert.strictEqual(Fixtures.Enumerable.Basic, collection);
  318. i++;
  319. return value < 2;
  320. }, this);
  321. });
  322. test('#pluck', function () {
  323. assert.equal(Fixtures.Enumerable.Nicknames.join(', '),
  324. Fixtures.Enumerable.People.pluck('nickname').join(', '));
  325. });
  326. test('#reject', function () {
  327. assert.equal(0,
  328. Fixtures.Enumerable.Nicknames.reject(Prototype.K).length);
  329. assert.equal('sam-, noradio, htonl',
  330. Fixtures.Enumerable.Nicknames.reject(function(nickname) {
  331. return nickname != nickname.toLowerCase();
  332. }).join(', '));
  333. });
  334. test('#reject (passes value, index, collection to the iterator)', function () {
  335. var i = 0;
  336. Fixtures.Enumerable.Basic.reject(function(value, index, collection) {
  337. assert.strictEqual(Fixtures.Enumerable.Basic[i], value);
  338. assert.strictEqual(i, index);
  339. assert.strictEqual(Fixtures.Enumerable.Basic, collection);
  340. i++;
  341. return value < 2;
  342. });
  343. });
  344. test('#sortBy', function () {
  345. assert.equal('htonl, noradio, sam-, Ulysses',
  346. Fixtures.Enumerable.People.sortBy(function(value) {
  347. return value.nickname.toLowerCase();
  348. }).pluck('nickname').join(', '));
  349. });
  350. test('#sortBy (passes value, index, collection to the iterator)', function () {
  351. var i = 0;
  352. Fixtures.Enumerable.Basic.sortBy(function(value, index, collection) {
  353. assert.strictEqual(Fixtures.Enumerable.Basic[i], value);
  354. assert.strictEqual(i, index);
  355. assert.strictEqual(Fixtures.Enumerable.Basic, collection);
  356. i++;
  357. return value;
  358. });
  359. });
  360. test('#toArray', function () {
  361. var result = Fixtures.Enumerable.People.toArray();
  362. assert(result != Fixtures.Enumerable.People, '#toArray should create a new object');
  363. assert.equal(Fixtures.Enumerable.Nicknames.join(', '),
  364. result.pluck('nickname').join(', ')); // but the values are the same
  365. });
  366. test('#zip', function () {
  367. var result = [1, 2, 3].zip([4, 5, 6], [7, 8, 9]);
  368. assert.equal('[[1, 4, 7], [2, 5, 8], [3, 6, 9]]', result.inspect());
  369. result = [1, 2, 3].zip([4, 5, 6], [7, 8, 9], function(array) { return array.reverse(); });
  370. assert.equal('[[7, 4, 1], [8, 5, 2], [9, 6, 3]]', result.inspect());
  371. });
  372. test('#size', function () {
  373. assert.equal(4, Fixtures.Enumerable.People.size());
  374. assert.equal(4, Fixtures.Enumerable.Nicknames.size());
  375. assert.equal(26, Fixtures.Enumerable.Primes.size());
  376. assert.equal(0, [].size());
  377. });
  378. });