Skip to content

Instantly share code, notes, and snippets.

@al-the-x
Created May 21, 2011 16:32
Show Gist options
  • Save al-the-x/984661 to your computer and use it in GitHub Desktop.
Save al-the-x/984661 to your computer and use it in GitHub Desktop.
Coding Dojo -- May 21, 2011
var Server = function()
{
var clients = [ ];
this.sendMessage = function()
{
return true;
}
this.receiveMessage = function()
{
return true;
}
this.connect = function( client )
{
clients.push(client);
return true;
}
this.disconnect = function(c)
{
for (x = 0; x< clients.length; x++)
{
if (clients[x].id == c.id)
{
clients.splice(x, 1);
}
}
}
this.getClients = function()
{
return clients;
}
}
(function(){ // closure
load('javascript/main.js', 'javascript/jsunity.js');
jsUnity.log = print;
/**
* Attaches the following assertion methods to
* the global scope for convenience.
* - assertException
* - assertTrue
* - assertFalse
* - assertIdentical
* - assertNotIdentical
* - assertEqual
* - assertNotEqual
* - assertMatch
* - assertNotMatch
* - assertTypeOf
* - assertNotTypeOf
* - assertInstanceOf
* - assertNotInstanceOf
* - assertNull
* - assertNotNull
* - assertUndefined
* - assertNotUndefined
* - assertNaN
* - assertNotNaN
* - fail
*/
jsUnity.attachAssertions()
})();
jsUnity.run(function SomeTest ( )
{
function setUp ( )
{
print('setting up');
server = new Server();
}
function tearDown ( )
{
print('tearing down');
}
function testServerIsNotUndefined ( )
{
assertNotUndefined(server, 'Server does not exist!');
}
function testSendsAMessage ()
{
assertTrue(server.sendMessage());
}
function testReceiveMessage()
{
assertTrue(server.receiveMessage());
fail('This be broke, yo!');
}
function testCanGetClients()
{
assertEqual([], server.getClients());
}
function testClientCanConnect()
{
client = { };
assertEqual(0, server.getClients().length,
'Initially, the client list should be empty.');
assertTrue(server.connect(client));
assertEqual(1, server.getClients().length,
'After connecting, the server should have a client.');
assertIdentical(client, server.getClients()[0])
}
function testClientCanDisconnect()
{
var client = {id:1};
server.connect(client);
server.disconnect(client);
assertEqual(0, server.getClients().length);
}
}); // END jsUnity.run
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment