@@ -9,31 +9,31 @@ import (
9
9
)
10
10
11
11
const (
12
- selectStringTest = "select:"
13
- rankStringTest = "rank:"
14
- textStringTest = "text:"
15
- unmarshalingRankString = "could not unmarshal rank answers: "
16
- unmarshalingTextStringTest = "could not unmarshal text answers: "
12
+ selectIDTest = "select:"
13
+ rankIDTest = "rank:"
14
+ textIDTest = "text:"
15
+ unmarshalingRankID = "could not unmarshal rank answers: "
16
+ unmarshalingTextID = "could not unmarshal text answers: "
17
17
)
18
18
19
19
// Creating a ballot for the first question, which is a select question.
20
- var ballot1 = string (selectStringTest + encodedQuestionID (1 ) + ":1,0,1\n " +
21
- rankStringTest + encodedQuestionID (2 ) + ":1,2,0,,\n " +
22
- selectStringTest + encodedQuestionID (3 ) + ":1,0,1,1\n " +
23
- textStringTest + encodedQuestionID (4 ) + ":YmxhYmxhYmxhZg==,Y2VzdG1vaUVtaQ==\n \n " )
20
+ var ballot1 = string (selectIDTest + encodedQuestionID (1 ) + ":1,0,1\n " +
21
+ rankIDTest + encodedQuestionID (2 ) + ":1,2,0,,\n " +
22
+ selectIDTest + encodedQuestionID (3 ) + ":1,0,1,1\n " +
23
+ textIDTest + encodedQuestionID (4 ) + ":YmxhYmxhYmxhZg==,Y2VzdG1vaUVtaQ==\n \n " )
24
24
25
25
// Creating a ballot with the following questions:
26
26
// 1. Select one of three options
27
27
// 2. Rank four options
28
28
// 3. Select one of five options
29
29
// 4. Write two text answers
30
30
// 5. Write one text answer
31
- var ballot2 = string (selectStringTest + encodedQuestionID (1 ) + ":0,0,0\n " +
32
- rankStringTest + encodedQuestionID (2 ) + ":128,128,128,128\n " +
33
- selectStringTest + encodedQuestionID (3 ) + ":0,0,0,0,0\n " +
34
- textStringTest + encodedQuestionID (4 ) + ":xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx," +
31
+ var ballot2 = string (selectIDTest + encodedQuestionID (1 ) + ":0,0,0\n " +
32
+ rankIDTest + encodedQuestionID (2 ) + ":128,128,128,128\n " +
33
+ selectIDTest + encodedQuestionID (3 ) + ":0,0,0,0,0\n " +
34
+ textIDTest + encodedQuestionID (4 ) + ":xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx," +
35
35
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\n " +
36
- textStringTest + encodedQuestionID (5 ) + ":xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx,,\n \n " )
36
+ textIDTest + encodedQuestionID (5 ) + ":xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx,,\n \n " )
37
37
38
38
func encodedQuestionID (i int ) ID {
39
39
return ID (base64 .StdEncoding .EncodeToString ([]byte ("Q" + strconv .Itoa (i ))))
@@ -120,28 +120,28 @@ func TestBallot_Unmarshal(t *testing.T) {
120
120
require .EqualError (t , err , "a line in the ballot has length != 3: x" )
121
121
122
122
// with ID not encoded in base64
123
- ballotWrongID := string (selectStringTest + "aaa" + ":1,0,1\n " +
124
- rankStringTest + encodedQuestionID (2 ) + ":1,2,0,,\n " +
125
- selectStringTest + encodedQuestionID (3 ) + ":1,0,1,1\n " +
126
- textStringTest + encodedQuestionID (4 ) + ":YmxhYmxhYmxhZg==,Y2VzdG1vaUVtaQ==\n \n " )
123
+ ballotWrongID := string (selectIDTest + "aaa" + ":1,0,1\n " +
124
+ rankIDTest + encodedQuestionID (2 ) + ":1,2,0,,\n " +
125
+ selectIDTest + encodedQuestionID (3 ) + ":1,0,1,1\n " +
126
+ textIDTest + encodedQuestionID (4 ) + ":YmxhYmxhYmxhZg==,Y2VzdG1vaUVtaQ==\n \n " )
127
127
128
128
err = b .Unmarshal (ballotWrongID , election )
129
129
require .EqualError (t , err , "could not decode question ID: illegal base64 data at input byte 0" )
130
130
131
131
// with question ID not from the election
132
- ballotUnknownID := string (selectStringTest + encodedQuestionID (0 ) + ":1,0,1\n " +
133
- rankStringTest + encodedQuestionID (2 ) + ":1,2,0,,\n " +
134
- selectStringTest + encodedQuestionID (3 ) + ":1,0,1,1\n " +
135
- textStringTest + encodedQuestionID (4 ) + ":YmxhYmxhYmxhZg==,Y2VzdG1vaUVtaQ==\n \n " )
132
+ ballotUnknownID := string (selectIDTest + encodedQuestionID (0 ) + ":1,0,1\n " +
133
+ rankIDTest + encodedQuestionID (2 ) + ":1,2,0,,\n " +
134
+ selectIDTest + encodedQuestionID (3 ) + ":1,0,1,1\n " +
135
+ textIDTest + encodedQuestionID (4 ) + ":YmxhYmxhYmxhZg==,Y2VzdG1vaUVtaQ==\n \n " )
136
136
137
137
err = b .Unmarshal (ballotUnknownID , election )
138
138
require .EqualError (t , err , "wrong question ID: the question doesn't exist" )
139
139
140
140
// with too many answers in select question
141
- ballotWrongSelect := string (selectStringTest + encodedQuestionID (1 ) + ":1,0,1,0,0\n " +
142
- rankStringTest + encodedQuestionID (2 ) + ":1,2,0,,\n " +
143
- selectStringTest + encodedQuestionID (3 ) + ":1,0,1,1\n " +
144
- textStringTest + encodedQuestionID (4 ) + ":YmxhYmxhYmxhZg==,Y2VzdG1vaUVtaQ==\n \n " )
141
+ ballotWrongSelect := string (selectIDTest + encodedQuestionID (1 ) + ":1,0,1,0,0\n " +
142
+ rankIDTest + encodedQuestionID (2 ) + ":1,2,0,,\n " +
143
+ selectIDTest + encodedQuestionID (3 ) + ":1,0,1,1\n " +
144
+ textIDTest + encodedQuestionID (4 ) + ":YmxhYmxhYmxhZg==,Y2VzdG1vaUVtaQ==\n \n " )
145
145
146
146
election .BallotSize = len (ballotWrongSelect )
147
147
@@ -151,10 +151,10 @@ func TestBallot_Unmarshal(t *testing.T) {
151
151
" of answers: expected 3 got 5" )
152
152
153
153
// with wrong format answers in select question
154
- ballotWrongSelect = string (selectStringTest + encodedQuestionID (1 ) + ":1,0,wrong\n " +
155
- rankStringTest + encodedQuestionID (2 ) + ":1,2,0,,\n " +
156
- selectStringTest + encodedQuestionID (3 ) + ":1,0,1,1\n " +
157
- textStringTest + encodedQuestionID (4 ) + ":YmxhYmxhYmxhZg==,Y2VzdG1vaUVtaQ==\n \n " )
154
+ ballotWrongSelect = string (selectIDTest + encodedQuestionID (1 ) + ":1,0,wrong\n " +
155
+ rankIDTest + encodedQuestionID (2 ) + ":1,2,0,,\n " +
156
+ selectIDTest + encodedQuestionID (3 ) + ":1,0,1,1\n " +
157
+ textIDTest + encodedQuestionID (4 ) + ":YmxhYmxhYmxhZg==,Y2VzdG1vaUVtaQ==\n \n " )
158
158
159
159
election .BallotSize = len (ballotWrongSelect )
160
160
@@ -164,10 +164,10 @@ func TestBallot_Unmarshal(t *testing.T) {
164
164
"ParseBool: parsing \" wrong\" : invalid syntax" )
165
165
166
166
// with too many selected answers in select question
167
- ballotWrongSelect = string (selectStringTest + encodedQuestionID (1 ) + ":1,1,1\n " +
168
- rankStringTest + encodedQuestionID (2 ) + ":1,2,0,,\n " +
169
- selectStringTest + encodedQuestionID (3 ) + ":1,0,1,1\n " +
170
- textStringTest + encodedQuestionID (4 ) + ":YmxhYmxhYmxhZg==,Y2VzdG1vaUVtaQ==\n \n " )
167
+ ballotWrongSelect = string (selectIDTest + encodedQuestionID (1 ) + ":1,1,1\n " +
168
+ rankIDTest + encodedQuestionID (2 ) + ":1,2,0,,\n " +
169
+ selectIDTest + encodedQuestionID (3 ) + ":1,0,1,1\n " +
170
+ textIDTest + encodedQuestionID (4 ) + ":YmxhYmxhYmxhZg==,Y2VzdG1vaUVtaQ==\n \n " )
171
171
172
172
election .BallotSize = len (ballotWrongSelect )
173
173
@@ -176,10 +176,10 @@ func TestBallot_Unmarshal(t *testing.T) {
176
176
"failed to check number of answers: question Q1 has too many selected answers" )
177
177
178
178
// with not enough selected answers in select question
179
- ballotWrongSelect = string (selectStringTest + encodedQuestionID (1 ) + ":1,0,0\n " +
180
- rankStringTest + encodedQuestionID (2 ) + ":1,2,0,,\n " +
181
- selectStringTest + encodedQuestionID (3 ) + ":1,0,1,1\n " +
182
- textStringTest + encodedQuestionID (4 ) + ":YmxhYmxhYmxhZg==,Y2VzdG1vaUVtaQ==\n \n " )
179
+ ballotWrongSelect = string (selectIDTest + encodedQuestionID (1 ) + ":1,0,0\n " +
180
+ rankIDTest + encodedQuestionID (2 ) + ":1,2,0,,\n " +
181
+ selectIDTest + encodedQuestionID (3 ) + ":1,0,1,1\n " +
182
+ textIDTest + encodedQuestionID (4 ) + ":YmxhYmxhYmxhZg==,Y2VzdG1vaUVtaQ==\n \n " )
183
183
184
184
election .BallotSize = len (ballotWrongSelect )
185
185
@@ -188,114 +188,114 @@ func TestBallot_Unmarshal(t *testing.T) {
188
188
"failed to check number of answers: question Q1 has not enough selected answers" )
189
189
190
190
// with not enough answers in rank question
191
- ballotWrongRank := string (selectStringTest + encodedQuestionID (1 ) + ":1,0,1\n " +
192
- rankStringTest + encodedQuestionID (2 ) + ":1,2,0\n " +
193
- selectStringTest + encodedQuestionID (3 ) + ":1,0,1,1\n " +
194
- textStringTest + encodedQuestionID (4 ) + ":YmxhYmxhYmxhZg==,Y2VzdG1vaUVtaQ==\n \n " )
191
+ ballotWrongRank := string (selectIDTest + encodedQuestionID (1 ) + ":1,0,1\n " +
192
+ rankIDTest + encodedQuestionID (2 ) + ":1,2,0\n " +
193
+ selectIDTest + encodedQuestionID (3 ) + ":1,0,1,1\n " +
194
+ textIDTest + encodedQuestionID (4 ) + ":YmxhYmxhYmxhZg==,Y2VzdG1vaUVtaQ==\n \n " )
195
195
196
196
err = b .Unmarshal (ballotWrongRank , election )
197
197
require .EqualError (t , err , "could not unmarshal rank answers: question" +
198
198
" Q2 has a wrong number of answers: expected 5 got 3" )
199
199
200
200
// with wrong format answers in rank question
201
- ballotWrongRank = string (selectStringTest + encodedQuestionID (1 ) + ":1,0,1\n " +
202
- rankStringTest + encodedQuestionID (2 ) + ":1,x,0,,\n " +
203
- selectStringTest + encodedQuestionID (3 ) + ":1,0,1,1\n " +
204
- textStringTest + encodedQuestionID (4 ) + ":YmxhYmxhYmxhZg==,Y2VzdG1vaUVtaQ==\n \n " )
201
+ ballotWrongRank = string (selectIDTest + encodedQuestionID (1 ) + ":1,0,1\n " +
202
+ rankIDTest + encodedQuestionID (2 ) + ":1,x,0,,\n " +
203
+ selectIDTest + encodedQuestionID (3 ) + ":1,0,1,1\n " +
204
+ textIDTest + encodedQuestionID (4 ) + ":YmxhYmxhYmxhZg==,Y2VzdG1vaUVtaQ==\n \n " )
205
205
206
206
election .BallotSize = len (ballotWrongRank )
207
207
208
208
err = b .Unmarshal (ballotWrongRank , election )
209
- require .EqualError (t , err , unmarshalingRankString +
209
+ require .EqualError (t , err , unmarshalingRankID +
210
210
"could not parse rank value for Q.Q2: strconv.ParseInt: parsing \" x\" : invalid syntax" )
211
211
212
212
// with too many selected answers in rank question
213
- ballotWrongRank = string (selectStringTest + encodedQuestionID (1 ) + ":1,0,1\n " +
214
- rankStringTest + encodedQuestionID (2 ) + ":1,2,0,3,4\n " +
215
- selectStringTest + encodedQuestionID (3 ) + ":1,0,1,1\n " +
216
- textStringTest + encodedQuestionID (4 ) + ":YmxhYmxhYmxhZg==,Y2VzdG1vaUVtaQ==\n \n " )
213
+ ballotWrongRank = string (selectIDTest + encodedQuestionID (1 ) + ":1,0,1\n " +
214
+ rankIDTest + encodedQuestionID (2 ) + ":1,2,0,3,4\n " +
215
+ selectIDTest + encodedQuestionID (3 ) + ":1,0,1,1\n " +
216
+ textIDTest + encodedQuestionID (4 ) + ":YmxhYmxhYmxhZg==,Y2VzdG1vaUVtaQ==\n \n " )
217
217
218
218
election .BallotSize = len (ballotWrongRank )
219
219
220
220
err = b .Unmarshal (ballotWrongRank , election )
221
- require .EqualError (t , err , unmarshalingRankString +
221
+ require .EqualError (t , err , unmarshalingRankID +
222
222
"invalid rank not in range [0, MaxN[: 3" )
223
223
224
224
// with valid ranks but one is selected twice
225
- ballotWrongRank = string (selectStringTest + encodedQuestionID (1 ) + ":1,0,1\n " +
226
- rankStringTest + encodedQuestionID (2 ) + ":1,2,0,2,2\n " +
227
- selectStringTest + encodedQuestionID (3 ) + ":1,0,1,1\n " +
228
- textStringTest + encodedQuestionID (4 ) + ":YmxhYmxhYmxhZg==,Y2VzdG1vaUVtaQ==\n \n " )
225
+ ballotWrongRank = string (selectIDTest + encodedQuestionID (1 ) + ":1,0,1\n " +
226
+ rankIDTest + encodedQuestionID (2 ) + ":1,2,0,2,2\n " +
227
+ selectIDTest + encodedQuestionID (3 ) + ":1,0,1,1\n " +
228
+ textIDTest + encodedQuestionID (4 ) + ":YmxhYmxhYmxhZg==,Y2VzdG1vaUVtaQ==\n \n " )
229
229
230
230
election .BallotSize = len (ballotWrongRank )
231
231
232
232
err = b .Unmarshal (ballotWrongRank , election )
233
- require .EqualError (t , err , unmarshalingRankString +
233
+ require .EqualError (t , err , unmarshalingRankID +
234
234
"failed to check number of answers: question Q2 has too many selected answers" )
235
235
236
236
// with not enough selected answers in rank question
237
- ballotWrongRank = string (selectStringTest + encodedQuestionID (1 ) + ":1,0,1\n " +
238
- rankStringTest + encodedQuestionID (2 ) + ":1,,0,,\n " +
239
- selectStringTest + encodedQuestionID (3 ) + ":1,0,1,1\n " +
240
- textStringTest + encodedQuestionID (4 ) + ":YmxhYmxhYmxhZg==,Y2VzdG1vaUVtaQ==\n \n " )
237
+ ballotWrongRank = string (selectIDTest + encodedQuestionID (1 ) + ":1,0,1\n " +
238
+ rankIDTest + encodedQuestionID (2 ) + ":1,,0,,\n " +
239
+ selectIDTest + encodedQuestionID (3 ) + ":1,0,1,1\n " +
240
+ textIDTest + encodedQuestionID (4 ) + ":YmxhYmxhYmxhZg==,Y2VzdG1vaUVtaQ==\n \n " )
241
241
242
242
election .BallotSize = len (ballotWrongRank )
243
243
244
244
err = b .Unmarshal (ballotWrongRank , election )
245
- require .EqualError (t , err , unmarshalingRankString +
245
+ require .EqualError (t , err , unmarshalingRankID +
246
246
"failed to check number of answers: question" +
247
247
" Q2 has not enough selected answers" )
248
248
249
249
// with not enough answers in text question
250
- ballotWrongText := string (selectStringTest + encodedQuestionID (1 ) + ":1,0,1\n " +
251
- rankStringTest + encodedQuestionID (2 ) + ":1,2,0,,\n " +
252
- selectStringTest + encodedQuestionID (3 ) + ":1,0,1,1\n " +
253
- textStringTest + encodedQuestionID (4 ) + ":Y2VzdG1vaUVtaQ==\n \n " )
250
+ ballotWrongText := string (selectIDTest + encodedQuestionID (1 ) + ":1,0,1\n " +
251
+ rankIDTest + encodedQuestionID (2 ) + ":1,2,0,,\n " +
252
+ selectIDTest + encodedQuestionID (3 ) + ":1,0,1,1\n " +
253
+ textIDTest + encodedQuestionID (4 ) + ":Y2VzdG1vaUVtaQ==\n \n " )
254
254
255
255
election .BallotSize = len (ballotWrongText )
256
256
257
257
err = b .Unmarshal (ballotWrongText , election )
258
- require .EqualError (t , err , unmarshalingTextStringTest +
258
+ require .EqualError (t , err , unmarshalingTextID +
259
259
"question Q4 has a wrong number of answers: expected 2 got 1" )
260
260
261
261
// with wrong encoding in text question
262
- ballotWrongText = string (selectStringTest + encodedQuestionID (1 ) + ":1,0,1\n " +
263
- rankStringTest + encodedQuestionID (2 ) + ":1,2,0,,\n " +
264
- selectStringTest + encodedQuestionID (3 ) + ":1,0,1,1\n " +
265
- textStringTest + encodedQuestionID (4 ) + ":wrongEncoding,Y2VzdG1vaUVtaQ==\n \n " )
262
+ ballotWrongText = string (selectIDTest + encodedQuestionID (1 ) + ":1,0,1\n " +
263
+ rankIDTest + encodedQuestionID (2 ) + ":1,2,0,,\n " +
264
+ selectIDTest + encodedQuestionID (3 ) + ":1,0,1,1\n " +
265
+ textIDTest + encodedQuestionID (4 ) + ":wrongEncoding,Y2VzdG1vaUVtaQ==\n \n " )
266
266
267
267
election .BallotSize = len (ballotWrongText )
268
268
269
269
err = b .Unmarshal (ballotWrongText , election )
270
- require .EqualError (t , err , unmarshalingTextStringTest +
270
+ require .EqualError (t , err , unmarshalingTextID +
271
271
"could not decode text for Q.Q4: illegal base64 data at input byte 12" )
272
272
273
273
// with too many selected answers in text question
274
274
election .Configuration .Scaffold [0 ].Texts [0 ].MaxN = 1
275
275
276
- ballotWrongText = string (selectStringTest + encodedQuestionID (1 ) + ":1,0,1\n " +
277
- rankStringTest + encodedQuestionID (2 ) + ":1,2,0,,\n " +
278
- selectStringTest + encodedQuestionID (3 ) + ":1,0,1,1\n " +
279
- textStringTest + encodedQuestionID (4 ) + ":YmxhYmxhYmxhZg==,Y2VzdG1vaUVtaQ==\n \n " )
276
+ ballotWrongText = string (selectIDTest + encodedQuestionID (1 ) + ":1,0,1\n " +
277
+ rankIDTest + encodedQuestionID (2 ) + ":1,2,0,,\n " +
278
+ selectIDTest + encodedQuestionID (3 ) + ":1,0,1,1\n " +
279
+ textIDTest + encodedQuestionID (4 ) + ":YmxhYmxhYmxhZg==,Y2VzdG1vaUVtaQ==\n \n " )
280
280
281
281
election .BallotSize = len (ballotWrongText )
282
282
283
283
err = b .Unmarshal (ballotWrongText , election )
284
- require .EqualError (t , err , unmarshalingTextStringTest +
284
+ require .EqualError (t , err , unmarshalingTextID +
285
285
"failed to check number of answers: question Q4 has too many selected answers" )
286
286
287
287
election .Configuration .Scaffold [0 ].Texts [0 ].MaxN = 2
288
288
289
289
// with not enough elected answers in text question
290
- ballotWrongText = string (selectStringTest + encodedQuestionID (1 ) + ":1,0,1\n " +
291
- rankStringTest + encodedQuestionID (2 ) + ":1,2,0,,\n " +
292
- selectStringTest + encodedQuestionID (3 ) + ":1,0,1,1\n " +
293
- textStringTest + encodedQuestionID (4 ) + ":,Y2VzdG1vaUVtaQ==\n \n " )
290
+ ballotWrongText = string (selectIDTest + encodedQuestionID (1 ) + ":1,0,1\n " +
291
+ rankIDTest + encodedQuestionID (2 ) + ":1,2,0,,\n " +
292
+ selectIDTest + encodedQuestionID (3 ) + ":1,0,1,1\n " +
293
+ textIDTest + encodedQuestionID (4 ) + ":,Y2VzdG1vaUVtaQ==\n \n " )
294
294
295
295
election .BallotSize = len (ballotWrongText )
296
296
297
297
err = b .Unmarshal (ballotWrongText , election )
298
- require .EqualError (t , err , unmarshalingTextStringTest +
298
+ require .EqualError (t , err , unmarshalingTextID +
299
299
"failed to check number of answers: question Q4 has not enough selected answers" )
300
300
301
301
// with unknown question type
0 commit comments