Skip to content
This repository has been archived by the owner on Sep 23, 2022. It is now read-only.

Commit

Permalink
Finished simple Client/Server mapper
Browse files Browse the repository at this point in the history
  • Loading branch information
Vincent Jousse committed Feb 22, 2011
1 parent 05650f0 commit 7070751
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 12 deletions.
19 changes: 13 additions & 6 deletions lib/client-server-mapper.js
Expand Up @@ -61,6 +61,17 @@ ClientServerMapper.prototype.map = function(client, server) {
throw new Error("Your server should have an uuid");
}

if(self.clients[server.uuid] != null) {
throw new Error("A client already exists for this server");
}

if(self.servers[client.uuid] != null) {
throw new Error("A server already exists for this client");
}

self.clients[server.uuid] = client;
self.servers[client.uuid] = server;

return this;

}
Expand All @@ -74,9 +85,7 @@ ClientServerMapper.prototype.map = function(client, server) {
ClientServerMapper.prototype.getClientForServer = function(server) {
var self = this;


return this;

return self.clients[server.uuid];
}


Expand All @@ -88,9 +97,7 @@ ClientServerMapper.prototype.getClientForServer = function(server) {
ClientServerMapper.prototype.getServerForClient = function(client) {
var self = this;


return this;

return self.servers[client.uuid];
}


Expand Down
75 changes: 69 additions & 6 deletions test/client-server-mapper_test.js
Expand Up @@ -8,20 +8,50 @@ var ClientServerMapper = require( 'client-server-mapper' );

/* ------------------------------ Fixtures ------------------------------ */

var client = {
, name: 'some client name'
var clientId = 1;
var serverId = 1;

function getClientWithoutUuid() {

var client = {
name: 'some client name'
, description: 'some client description'
};
};

return client;

}


var server = {
, name: 'some server name'
function getClient() {
var client = getClientWithoutUuid();
client.uuid = clientId++;

return client;
}

function getServerWithoutUuid() {
var server = {
name: 'some server name'
, description: 'some server description'
};
};

return server;
}


function getServer() {
var server = getServerWithoutUuid();
server.uuid = serverId++;

return server;
}
exports['uuid exception'] = function(test) {
var mapper = new ClientServerMapper();

client = getClientWithoutUuid();
server = getServerWithoutUuid();

test.throws(
function() {
mapper.map(client, server);
Expand All @@ -32,6 +62,39 @@ exports['uuid exception'] = function(test) {
test.finish();
};


exports['map'] = function(test) {
var mapper = new ClientServerMapper();

var client = getClient();
var server = getServer();

test.doesNotThrow(
function() {
mapper.map(client, server);
},
'/uuid/'
);

clientMapped = mapper.getClientForServer(server);
test.equal(clientMapped.uuid,client.uuid);

serverMapped = mapper.getServerForClient(client);
test.equal(serverMapped.uuid,server.uuid);

var newClient = getClient();

test.throws(
function() {
mapper.map(newClient, server);
},
'/exists/'
);


test.finish();
};

/* ------------------------------ Run ------------------------------ */
if ( module == require.main )
require( 'async_testing' ).run( __filename, process.ARGV )

0 comments on commit 7070751

Please sign in to comment.