GlobalSpec.js
6.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/**
* Created by Techniv on 30/11/2016.
*/
var assert = require('assert');
var RelativePlacement = require('../../lib/relative-placement');
describe('RelativePlacement global', () => {
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:[],
placements: [],
cumulativePlacement: []
}, election.candidateList['foo']);
});
it('should add some candidates', () => {
assert.equal(0, election.numberOfCandidates);
election.addCandidates(['foo', 'bar']);
assert.equal(2, election.numberOfCandidates);
assert.deepEqual({
name: 'foo',
votes:[],
placements: [],
cumulativePlacement: []
}, election.candidateList['foo']);
assert.deepEqual({
name: 'bar',
votes:[],
placements: [],
cumulativePlacement: []
}, election.candidateList['bar']);
});
it('should not add a same candidate multiple time', () => {
assert.equal(0, election.numberOfCandidates);
election.addCandidate('foo');
assert.equal(1, election.numberOfCandidates);
assert.throws(()=>{
election.addCandidate('foo');
}, (err) => {
return err instanceof Error && /already exist/.test(err.message);
},
'unexpected error'
);
assert.equal(1, election.numberOfCandidates);
});
});
describe('Add votes', () => {
var election;
var candidates = ['A','B','C'];
beforeEach(()=> {
election = new RelativePlacement();
election.addCandidates(candidates);
});
it('should add a vote to each candidates', () => {
var vote = candidates;
election.addVote(vote);
for(let i = 0; i < vote.length; i++){
let candidate = election.candidateList[vote[i]];
assert.equal(candidate.votes.length, 1, 'number of candidates vote');
assert.equal(candidate.votes[0], i, 'value of candidates vote');
}
});
it('should has same sizes between candidate list and vote', () => {
assert.throws(() => {
election.addVote(['A','B']);
},
err => err instanceof Error && /same size/.test(err.message),
'smaller vote'
);
assert.equal(election.votes.length, 0, 'no vote registered');
assert.equal(election.candidateList['A'].votes.length, 0, 'no vote registered in candidate');
assert.throws(() => {
election.addVote(['A','B','C','D']);
},
err => err instanceof Error && /same size/.test(err.message),
'bigger vote'
);
assert.equal(election.votes.length, 0, 'no vote registered');
assert.equal(election.candidateList['A'].votes.length, 0, 'no vote registered in candidate');
});
it('should not has unknown candidate in vote', () => {
assert.throws(() => {
election.addVote(['A','B','D']);
},
err => err instanceof Error && /not present/.test(err.message));
assert.equal(election.votes.length, 0, 'no vote registered');
assert.equal(election.candidateList['A'].votes.length, 0, 'no vote registered in candidate');
});
it('should has an unique vote by candidate', () => {
assert.throws(() => {
election.addVote(['A','B','A']);
},
err => err instanceof Error && /unique/.test(err.message));
assert.equal(election.votes.length, 0, 'no vote registered');
assert.equal(election.candidateList['A'].votes.length, 0, 'no vote registered in candidate');
});
it('should lock candidate after add the first vote', () => {
election.addVote(candidates);
assert.throws(()=>election.addCandidate('D'));
});
});
describe('Get result', () => {
/** @var RelativePlacement */
var election;
var candidates = ['A','B','C'];
beforeEach(()=> {
election = new RelativePlacement();
election.addCandidates(candidates);
election.addVote(candidates);
});
it('should return an array', () => {
assert.ok(election.getResult() instanceof Array);
});
it('should return only all candidates', () => {
var result = election.getResult();
assert.equal(result.length, candidates.length);
assert.ok(candidates.every(name => result.indexOf(name) != -1));
assert.ok(result.every(name => candidates.indexOf(name) != -1));
});
it('should have placements sum in candidates', () => {
election.addVote(['A', 'B', 'C']);
election.addVote(['B', 'A', 'C']);
election.addVote(['A', 'C', 'B']);
election.getResult();
var candidate = election.candidateList['A'];
assert.equal(candidate.placements.length, 3);
assert.equal(candidate.cumulativePlacement.length, 3);
assert.equal(candidate.votes.length, 4);
});
});
});