test.js 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. 'use strict';
  2. var _ = require('lodash');
  3. var test = require('tape');
  4. var sum = require('./');
  5. test('creates unique hashes', function (t) {
  6. var results = [];
  7. sub([0,1,2,3]);
  8. sub({url:12});
  9. sub({headers:12});
  10. sub({headers:122});
  11. sub({headers:'122'});
  12. sub({headers:{accept:'text/plain'}});
  13. sub({payload:[0,1,2,3],headers:[{a:'b'}]});
  14. sub({a:function () {}});
  15. sub({b:function () {}});
  16. sub({b:function (a) {}});
  17. sub(function () {});
  18. sub(function (a) {});
  19. sub(function (b) {});
  20. sub(function (a) { return a;});
  21. sub(function (a) {return a;});
  22. sub('');
  23. sub('null');
  24. sub('false');
  25. sub('true');
  26. sub('0');
  27. sub('1');
  28. sub('void 0');
  29. sub('undefined');
  30. sub(null);
  31. sub(false);
  32. sub(true);
  33. sub(0);
  34. sub(1);
  35. sub(void 0);
  36. sub({});
  37. sub({a:{},b:{}});
  38. sub([]);
  39. sub(new Date());
  40. sub(global, 'global');
  41. t.equal(results.length, _.uniq(results).length);
  42. t.end();
  43. function sub (value, name) {
  44. var hash = sum(value);
  45. results.push(hash);
  46. console.log('%s from:', hash, name || value);
  47. }
  48. });
  49. test('hashes clash if same properties', function (t) {
  50. equals({a:'1'},{a:'1'});
  51. equals({a:'1',b:1},{b:1,a:'1'});
  52. t.end();
  53. function equals (a, b) {
  54. t.equal(sum(a), sum(b));
  55. }
  56. });