Explorar o código

Merge pull request #15 from lovebear/tests

Tests thanks @lovebear
Eric Zhang %!s(int64=12) %!d(string=hai) anos
pai
achega
238c0ec356
Modificáronse 7 ficheiros con 271 adicións e 1 borrados
  1. 6 1
      package.json
  2. 19 0
      test/adapter.js
  3. 65 0
      test/connection.js
  4. 25 0
      test/peer.js
  5. 23 0
      test/socket.js
  6. 35 0
      test/test.html
  7. 98 0
      test/util.js

+ 6 - 1
package.json

@@ -1,5 +1,10 @@
 {
   "name": "peerjs",
   "version": "0.1.0",
-  "description": "PeerJS client library"
+  "description": "PeerJS client library",
+  "devDependencies": {
+    "uglify-js": "*",
+    "mocha": "*",
+    "expect.js": "*"
+  }
 }

+ 19 - 0
test/adapter.js

@@ -0,0 +1,19 @@
+describe('adapter', function() {
+
+  it('sets browerisms', function() {
+    expect(exports.util.browserisms).to.match(/^Firefox||Webkit$/);
+  })
+
+  it('sets RTCPeerConnection', function() {
+    expect(RTCPeerConnection).to.be.a('function');
+  })
+
+  it('sets RTCSessionDescription', function() {
+    expect(RTCSessionDescription).to.be.a('function');
+  })
+
+  it('sets getUserMedia', function() {
+    expect(getUserMedia).to.be.a('function');
+  })
+
+});

+ 65 - 0
test/connection.js

@@ -0,0 +1,65 @@
+describe('DataConnection', function() {
+
+  it('constructor');
+
+  it('inherits from EventEmitter');
+
+  it('initialize');
+
+  it('_setupOffer', function() {
+
+  });
+
+  it('_setupDataChannel', function() {
+
+  });
+
+  it('_startPeerConnection', function() {
+
+  });
+
+  it('_setupIce', function() {
+
+  });
+
+  it('_configureDataChannel', function() {
+
+  });
+
+  it('_makeOffer', function() {
+
+  });
+
+  it('_makeAnswer', function() {
+
+  });
+
+  it('_cleanup', function() {
+
+  });
+
+  it('_handleDataMessage', function() {
+
+  });
+
+  it('close', function() {
+
+  });
+
+  it('send', function() {
+
+  });
+
+  it('handleSDP', function() {
+
+  });
+
+  it('handleCandidate', function() {
+
+  });
+
+  it('handleLeave"', function() {
+
+  });
+
+});

+ 25 - 0
test/peer.js

@@ -0,0 +1,25 @@
+describe('Peer', function() {
+
+  it('constructor')
+
+  it('inherits from EventEmitter')
+
+  it('_getId')
+
+  it('_init')
+
+  it('_handleServerJSONMessage')
+
+  it('_processQueue')
+
+  it('_abort')
+
+  it('_cleanup')
+
+  it('_attachConnectionListeners')
+
+  it('connect')
+  
+  it('destroy')
+
+});

+ 23 - 0
test/socket.js

@@ -0,0 +1,23 @@
+describe('Socket', function() {
+
+  it('constructor')
+
+  it('inherits from EventEmitter')
+
+  it('start')
+
+  it('_startWebSocket')
+
+  it('_startXhrStream')
+
+  it('_handleStream')
+
+  it('_setHTTPTimeout')
+
+  it('send')
+
+  it('close')
+  
+  it('_wsOpen')
+
+});

+ 35 - 0
test/test.html

@@ -0,0 +1,35 @@
+<html>
+<head>
+  <meta charset="utf-8">
+  <title>Mocha Tests</title>
+  <link rel="stylesheet" href="../node_modules/mocha/mocha.css" />
+</head>
+<body>
+  <div id="mocha"></div>
+  <script>
+    var exports = this;
+  </script>
+  <script src="../deps/js-binarypack/lib/bufferbuilder.js"></script>
+  <script src="../deps/js-binarypack/lib/binarypack.js"></script>
+  <script src="../deps/EventEmitter/EventEmitter.js"></script>
+  <script src="../lib/util.js"></script>
+  <script src="../lib/adapter.js"></script>
+  <script src="../lib/connection.js"></script>
+  <script src="../lib/peer.js"></script>
+  <script src="../lib/socket.js"></script>
+
+  <script src="../node_modules/expect.js/expect.js"></script>
+  <script src="../node_modules/mocha/mocha.js"></script>
+  <script>mocha.setup('bdd')</script>
+
+  <script src="adapter.js"></script>
+  <script src="util.js"></script>
+  <script src="connection.js"></script>
+  <script src="peer.js"></script>
+  <script src="socket.js"></script>
+
+  <script>
+    mocha.run();
+  </script>
+</body>
+</html>

+ 98 - 0
test/util.js

@@ -0,0 +1,98 @@
+describe('util', function() {
+
+  var testRandom = function(fn) {
+    var i = 0
+      , generated = {};
+    while(i < 25) {
+      var p = fn();
+      if (generated[p]) throw new Error('not so random')
+      generated[p] = 1;
+      i++;
+    }
+  }
+
+  it('inherits', function() {
+    function ctor() {}
+    function superCtor() {}
+    superCtor.prototype.test = function() { return 5; }
+    util.inherits(ctor, superCtor);
+    expect(new ctor()).to.be.a(superCtor);
+    expect(new ctor().test()).to.be.equal(5);
+  })
+
+  /*
+   *  extend overwrites keys if already exists
+   *  leaves existing keys alone otherwise
+   */
+  it('extend', function() {
+    var a = {a: 1, b: 2, c: 3, d: 4}
+      , b = {d: 2};
+    util.extend(b, a);
+    expect(b).to.eql(a);
+    expect(b.d).to.be.equal(4);
+    b = {z: 2};
+    util.extend(b, a);
+    expect(b.z).to.be.equal(2);
+  })
+
+  it('pack', function() {
+    expect(util.pack).to.be.equal(BinaryPack.pack);
+  })
+
+  it('unpack', function() {
+    expect(util.unpack).to.be.equal(BinaryPack.unpack);
+  })
+
+  it('randomPort', function() {
+    testRandom(util.randomPort);
+  })
+
+  // FF no like
+  it('log', function(done) {
+    var consolelog = console.log;
+    // default is false
+    expect(util.debug).to.be.equal(false);
+    util.debug = true;
+    console.log = function() {
+      var arg = Array.prototype.slice.call(arguments);
+      expect(arg.join(' ')).to.be.equal('PeerJS:  hi');
+      done();
+    }
+    util.log('hi');
+    // reset
+    console.log = consolelog;
+    util.debug = false;
+  })
+
+  it('setZeroTimeout')
+
+  it('blobToArrayBuffer', function(done) {
+    var blob = new Blob(['hi']);
+    util.blobToArrayBuffer(blob, function(result) {
+      expect(result.byteLength).to.be.equal(2);
+      expect(result.slice).to.be.a('function');
+      expect(result instanceof ArrayBuffer).to.be.equal(true);
+      done();
+    });
+  })
+
+  it('blobToBinaryString', function(done) {
+    var blob = new Blob(['hi']);
+    util.blobToBinaryString(blob, function(result) {
+      expect(result).to.equal('hi');
+      done();
+    });
+  })
+
+  it('binaryStringToArrayBuffer', function() {
+    var ba = util.binaryStringToArrayBuffer('\0\0');
+    expect(ba.byteLength).to.be.equal(2);
+    expect(ba.slice).to.be.a('function');
+    expect(ba instanceof ArrayBuffer).to.be.equal(true);
+  })
+
+  it('randomToken', function() {
+    testRandom(util.randomToken);
+  })
+
+});