Commit 0a873941 0a873941d3589b75976f5fd0b2d5bc4bc09d5a09 by Vincent Peybernes

Start project

0 parents
.idea
node_modules
\ No newline at end of file
{
"name": "relative-placement-js",
"version": "0.0.1",
"devDependencies": {
"gulp": "*",
"nodeunit": "*",
"mocha": "*",
"karma": "*",
"karma-mocha": "*"
}
}
/**
* Created by Techniv on 30/11/2016.
*/
(function (define) {
// TODO define exhaustive module platform
module.exports = define();
})(function(){
/**
*
* @constructor
* @property {Candidate[]} candidateList
* @property {Number} numberOfCandidates
*
*/
function RelativePlacement() {
Object.defineProperties(this, {
candidateList: {value: {}},
numberOfCandidates: {get: () => Object.keys(this.candidateList).length}
});
}
/**
* @name addCandidate
* @param {String} candidateName
* @methodOf RelativePlacement
* @this RelativePlacement
*/
RelativePlacement.prototype.addCandidate = function addCandidate(candidateName){
if(this.candidateList[candidateName]) throw new Error('"' +candidateName+ '" already exist in candidate list.');
this.candidateList[candidateName] = new Candidate(candidateName);
};
/**
* @name addCandidates
* @param {String[]} candidateNames
* @methodOf RelativePlacement
* @this RelativePlacement
*/
RelativePlacement.prototype.addCandidates = function (candidateNames) {
for(let i = 0; i < candidateNames.length; i++){
this.addCandidate(candidateNames[i]);
}
};
RelativePlacement.prototype.addVote = function () {};
RelativePlacement.prototype.addVotes = function () {};
RelativePlacement.prototype.getResult = function () {};
function Candidate(name) {
Object.defineProperties(this, {
name: {value: name, enumerable: true},
votes: {value:[], enumerable: true},
placement: {value:[], enumerable: false}
});
}
return RelativePlacement;
});
\ No newline at end of file
/**
* Created by Techniv on 30/11/2016.
*/
var assert = require('assert');
var RelativePlacement = require('../../relative-placement');
describe('RelativePlacement', () => {
it('should provide RelativePlacement constructor', () => {
assert.ok(RelativePlacement instanceof Function, 'provide constructor');
assert.ok(RelativePlacement.prototype.addCandidate instanceof Function, 'provide "addCandidate" function in prototype');
assert.ok(RelativePlacement.prototype.addCandidates instanceof Function, 'provide "addCandidates" function in prototype');
assert.ok(RelativePlacement.prototype.addVote instanceof Function, 'provide "addVote" function in prototype');
assert.ok(RelativePlacement.prototype.addVotes instanceof Function, 'provide "addVotes" function in prototype');
assert.ok(RelativePlacement.prototype.getResult instanceof Function, 'provide "getResult" function in prototype');
});
it('should correctly instantiate RelativePlacement election', () => {
var rp = new RelativePlacement();
assert.deepEqual({}, rp.candidateList);
});
describe('Add candidates', () => {
/** @var RelativePlacement */
var election;
beforeEach(()=> election = new RelativePlacement());
it('should add a candidate', () => {
assert.equal(0, election.numberOfCandidates);
election.addCandidate('foo');
assert.equal(1, election.numberOfCandidates);
assert.deepEqual({
name: 'foo',
votes:[]
}, election.candidateList['foo']);
assert.deepEqual([], election.candidateList['foo'].placement);
});
it('should add some candidates', () => {
assert.equal(0, election.numberOfCandidates);
election.addCandidates(['foo', 'bar']);
assert.equal(2, election.numberOfCandidates);
assert.deepEqual({
name: 'foo',
votes:[]
}, election.candidateList['foo']);
assert.deepEqual({
name: 'bar',
votes:[]
}, election.candidateList['bar']);
});
it('should not add a same candidate multiple time', () => {
});
});
});
\ No newline at end of file
[
{
"votes":{
"320": [1,1,1,1,1],
"284": [2,4,2,4,3],
"126": [6,3,6,2,2],
"330": [4,2,3,3,5],
"288": [3,5,4,6,4],
"324": [5,6,5,5,6]
},
"result":["320","284","126","330","288","324"]
}
]
\ No newline at end of file