tests.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. 'use strict';
  2. var inspect = require('object-inspect');
  3. var forEach = require('for-each');
  4. var v = require('es-value-fixtures');
  5. module.exports = function (groupBy, t) {
  6. t.test('callback function', function (st) {
  7. forEach(v.nonFunctions, function (nonFunction) {
  8. st['throws'](
  9. function () { groupBy([], nonFunction); },
  10. TypeError,
  11. inspect(nonFunction) + ' is not a function'
  12. );
  13. });
  14. st.end();
  15. });
  16. t.test('grouping', function (st) {
  17. st.deepEqual(
  18. groupBy([], function () { return 'a'; }),
  19. { __proto__: null },
  20. 'an empty array produces an empty object'
  21. );
  22. var arr = [0, -0, 1, 2, 3, 4, 5, NaN, Infinity, -Infinity];
  23. var parity = function (x) {
  24. if (x !== x) {
  25. return void undefined;
  26. }
  27. if (!isFinite(x)) {
  28. return '∞';
  29. }
  30. return x % 2 === 0 ? 'even' : 'odd';
  31. };
  32. var grouped = {
  33. __proto__: null,
  34. even: [0, -0, 2, 4],
  35. odd: [1, 3, 5],
  36. undefined: [NaN],
  37. '∞': [Infinity, -Infinity]
  38. };
  39. st.deepEqual(
  40. groupBy(arr, parity),
  41. grouped,
  42. inspect(arr) + ' group by parity groups to ' + inspect(grouped)
  43. );
  44. st.deepEqual(
  45. groupBy(arr, function (x, i) {
  46. st.equal(this, undefined, 'receiver is as expected'); // eslint-disable-line no-invalid-this
  47. st.equal(x, arr[i], 'second argument ' + i + ' is ' + inspect(arr[i]));
  48. return 42;
  49. }),
  50. { __proto__: null, 42: arr },
  51. 'thisArg and callback arguments are as expected'
  52. );
  53. st.end();
  54. });
  55. };