Commit 0a873941 0a873941d3589b75976f5fd0b2d5bc4bc09d5a09 by Vincent Peybernes

Start project

0 parents
1 .idea
2 node_modules
...\ No newline at end of file ...\ No newline at end of file
1 {
2 "name": "relative-placement-js",
3 "version": "0.0.1",
4 "devDependencies": {
5 "gulp": "*",
6 "nodeunit": "*",
7 "mocha": "*",
8 "karma": "*",
9 "karma-mocha": "*"
10 }
11 }
1 /**
2 * Created by Techniv on 30/11/2016.
3 */
4 (function (define) {
5 // TODO define exhaustive module platform
6 module.exports = define();
7 })(function(){
8
9 /**
10 *
11 * @constructor
12 * @property {Candidate[]} candidateList
13 * @property {Number} numberOfCandidates
14 *
15 */
16 function RelativePlacement() {
17 Object.defineProperties(this, {
18 candidateList: {value: {}},
19 numberOfCandidates: {get: () => Object.keys(this.candidateList).length}
20 });
21 }
22
23 /**
24 * @name addCandidate
25 * @param {String} candidateName
26 * @methodOf RelativePlacement
27 * @this RelativePlacement
28 */
29 RelativePlacement.prototype.addCandidate = function addCandidate(candidateName){
30 if(this.candidateList[candidateName]) throw new Error('"' +candidateName+ '" already exist in candidate list.');
31 this.candidateList[candidateName] = new Candidate(candidateName);
32 };
33
34 /**
35 * @name addCandidates
36 * @param {String[]} candidateNames
37 * @methodOf RelativePlacement
38 * @this RelativePlacement
39 */
40 RelativePlacement.prototype.addCandidates = function (candidateNames) {
41 for(let i = 0; i < candidateNames.length; i++){
42 this.addCandidate(candidateNames[i]);
43 }
44 };
45
46 RelativePlacement.prototype.addVote = function () {};
47 RelativePlacement.prototype.addVotes = function () {};
48 RelativePlacement.prototype.getResult = function () {};
49
50 function Candidate(name) {
51 Object.defineProperties(this, {
52 name: {value: name, enumerable: true},
53 votes: {value:[], enumerable: true},
54 placement: {value:[], enumerable: false}
55 });
56 }
57
58 return RelativePlacement;
59 });
...\ No newline at end of file ...\ No newline at end of file
1 /**
2 * Created by Techniv on 30/11/2016.
3 */
4 var assert = require('assert');
5 var RelativePlacement = require('../../relative-placement');
6
7 describe('RelativePlacement', () => {
8 it('should provide RelativePlacement constructor', () => {
9 assert.ok(RelativePlacement instanceof Function, 'provide constructor');
10 assert.ok(RelativePlacement.prototype.addCandidate instanceof Function, 'provide "addCandidate" function in prototype');
11 assert.ok(RelativePlacement.prototype.addCandidates instanceof Function, 'provide "addCandidates" function in prototype');
12 assert.ok(RelativePlacement.prototype.addVote instanceof Function, 'provide "addVote" function in prototype');
13 assert.ok(RelativePlacement.prototype.addVotes instanceof Function, 'provide "addVotes" function in prototype');
14 assert.ok(RelativePlacement.prototype.getResult instanceof Function, 'provide "getResult" function in prototype');
15 });
16
17 it('should correctly instantiate RelativePlacement election', () => {
18 var rp = new RelativePlacement();
19
20 assert.deepEqual({}, rp.candidateList);
21 });
22
23 describe('Add candidates', () => {
24 /** @var RelativePlacement */
25 var election;
26
27 beforeEach(()=> election = new RelativePlacement());
28
29 it('should add a candidate', () => {
30 assert.equal(0, election.numberOfCandidates);
31 election.addCandidate('foo');
32 assert.equal(1, election.numberOfCandidates);
33 assert.deepEqual({
34 name: 'foo',
35 votes:[]
36 }, election.candidateList['foo']);
37 assert.deepEqual([], election.candidateList['foo'].placement);
38 });
39
40 it('should add some candidates', () => {
41 assert.equal(0, election.numberOfCandidates);
42 election.addCandidates(['foo', 'bar']);
43 assert.equal(2, election.numberOfCandidates);
44
45 assert.deepEqual({
46 name: 'foo',
47 votes:[]
48 }, election.candidateList['foo']);
49 assert.deepEqual({
50 name: 'bar',
51 votes:[]
52 }, election.candidateList['bar']);
53 });
54
55 it('should not add a same candidate multiple time', () => {
56
57 });
58 });
59 });
...\ No newline at end of file ...\ No newline at end of file
1 [
2 {
3 "votes":{
4 "320": [1,1,1,1,1],
5 "284": [2,4,2,4,3],
6 "126": [6,3,6,2,2],
7 "330": [4,2,3,3,5],
8 "288": [3,5,4,6,4],
9 "324": [5,6,5,5,6]
10 },
11 "result":["320","284","126","330","288","324"]
12 }
13 ]
...\ No newline at end of file ...\ No newline at end of file