util.js 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. // FF no like
  44. it('log', function(done) {
  45. var 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. expect(arg.join(' ')).to.be.equal('PeerJS: hi');
  52. done();
  53. }
  54. util.log('hi');
  55. // reset
  56. console.log = consolelog;
  57. util.debug = false;
  58. })
  59. it('setZeroTimeout')
  60. it('blobToArrayBuffer', function(done) {
  61. var blob = new Blob(['hi']);
  62. util.blobToArrayBuffer(blob, function(result) {
  63. expect(result.byteLength).to.be.equal(2);
  64. expect(result.slice).to.be.a('function');
  65. expect(result instanceof ArrayBuffer).to.be.equal(true);
  66. done();
  67. });
  68. })
  69. it('blobToBinaryString', function(done) {
  70. var blob = new Blob(['hi']);
  71. util.blobToBinaryString(blob, function(result) {
  72. expect(result).to.equal('hi');
  73. done();
  74. });
  75. })
  76. it('binaryStringToArrayBuffer', function() {
  77. var ba = util.binaryStringToArrayBuffer('\0\0');
  78. expect(ba.byteLength).to.be.equal(2);
  79. expect(ba.slice).to.be.a('function');
  80. expect(ba instanceof ArrayBuffer).to.be.equal(true);
  81. })
  82. it('randomToken', function() {
  83. testRandom(util.randomToken);
  84. })
  85. });