util.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. describe('util', function() {
  2. var testRandom = function(fn) {
  3. var i = 0
  4. , generated = {};
  5. while(i < 25) {
  6. var p = fn();
  7. if (generated[p]) throw new Error('not so random')
  8. generated[p] = 1;
  9. i++;
  10. }
  11. }
  12. it('inherits', function() {
  13. function ctor() {}
  14. function superCtor() {}
  15. superCtor.prototype.test = function() { return 5; }
  16. util.inherits(ctor, superCtor);
  17. expect(new ctor()).to.be.a(superCtor);
  18. expect(new ctor().test()).to.be.equal(5);
  19. })
  20. /*
  21. * extend overwrites keys if already exists
  22. * leaves existing keys alone otherwise
  23. */
  24. it('extend', function() {
  25. var a = {a: 1, b: 2, c: 3, d: 4}
  26. , b = {d: 2};
  27. util.extend(b, a);
  28. expect(b).to.eql(a);
  29. expect(b.d).to.be.equal(4);
  30. b = {z: 2};
  31. util.extend(b, a);
  32. expect(b.z).to.be.equal(2);
  33. })
  34. it('pack', function() {
  35. expect(util.pack).to.be.equal(BinaryPack.pack);
  36. })
  37. it('unpack', function() {
  38. expect(util.unpack).to.be.equal(BinaryPack.unpack);
  39. })
  40. it('randomPort', function() {
  41. testRandom(util.randomPort);
  42. })
  43. it('log', function() {
  44. var called = false
  45. , consolelog = console.log;
  46. // default is false
  47. expect(util.debug).to.be.equal(false);
  48. util.debug = true;
  49. console.log = function() {
  50. var arg = Array.prototype.slice.call(arguments);
  51. called = true;
  52. expect(arg.join(' ')).to.be.equal('PeerJS: hi');
  53. }
  54. util.log('hi');
  55. expect(called).to.be.equal(true);
  56. // reset
  57. console.log = consolelog;
  58. util.debug = false;
  59. })
  60. it('setZeroTimeout')
  61. it('blobToArrayBuffer')
  62. it('blobToBinaryString')
  63. it('binaryStringToArrayBuffer')
  64. it('randomToken', function() {
  65. testRandom(util.randomToken);
  66. })
  67. });