test.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. var arity0 = require('./0'),
  2. arity1 = require('./1'),
  3. arity2 = require('./2'),
  4. arity3 = require('./3'),
  5. arity4 = require('./4'),
  6. arity5 = require('./5'),
  7. arityN = require('./N'),
  8. spy = require('sinon').spy(),
  9. expect = require('expect.js');
  10. function createArray(l) {
  11. var arr = [];
  12. for (var i = 0; i < l; i++) {
  13. arr.push(i);
  14. }
  15. return arr;
  16. }
  17. function hasArity(wrapped, fn, l) {
  18. var arr = createArray(l);
  19. expect(wrapped).to.be.a('function');
  20. expect(wrapped.length).to.be.eql(l);
  21. wrapped.call(null, arr);
  22. expect(fn.calledWith.call(fn, arr)).to.be.ok();
  23. }
  24. describe('arity-function', () => {
  25. describe('#arity0', () => {
  26. it('should return a function with length 0', () => {
  27. var spy0 = arity0(spy);
  28. hasArity(spy0, spy, 0);
  29. });
  30. });
  31. describe('#arity1', () => {
  32. it('should return a function with length 1', () => {
  33. var spy1 = arity1(spy);
  34. hasArity(spy1, spy, 1);
  35. });
  36. });
  37. describe('#arity2', () => {
  38. it('should return a function with length 2', () => {
  39. var spy2 = arity2(spy);
  40. hasArity(spy2, spy, 2);
  41. });
  42. });
  43. describe('#arity3', () => {
  44. it('should return a function with length 3', () => {
  45. var spy3 = arity3(spy);
  46. hasArity(spy3, spy, 3);
  47. });
  48. });
  49. describe('#arity4', () => {
  50. it('should return a function with length 4', () => {
  51. var spy4 = arity4(spy);
  52. hasArity(spy4, spy, 4);
  53. });
  54. });
  55. describe('#arity5', () => {
  56. it('should return a function with length 5', () => {
  57. var spy5 = arity5(spy);
  58. hasArity(spy5, spy, 5);
  59. });
  60. });
  61. describe('#arityN', () => {
  62. it('should return a function with length N', () => {
  63. var spy0 = arityN(spy, 0);
  64. hasArity(spy0, spy, 0);
  65. var spy1 = arityN(spy, 1);
  66. hasArity(spy1, spy, 1);
  67. var spy2 = arityN(spy, 2);
  68. hasArity(spy2, spy, 2);
  69. var spy3 = arityN(spy, 3);
  70. hasArity(spy3, spy, 3);
  71. var spy4 = arityN(spy, 4);
  72. hasArity(spy4, spy, 4);
  73. var spy5 = arityN(spy, 5);
  74. hasArity(spy5, spy, 5);
  75. var spyX = arityN(spy, undefined);
  76. hasArity(spyX, spy, 0);
  77. spyX = arityN(spy, null);
  78. hasArity(spyX, spy, 0);
  79. var newFn = arityN(function(a,b,c,d,e,f,g,h,i,j,k){ return true; }, 9);
  80. expect(newFn.length).to.be.eql(11);
  81. expect(newFn()).to.be.eql(true);
  82. });
  83. });
  84. });