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