/
poll_tags_spec.rb
461 lines (308 loc) · 14.1 KB
/
poll_tags_spec.rb
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
require File.dirname(__FILE__) + '/../spec_helper'
describe 'Poll Tags' do
dataset :pages
before do
options = { Option.new(:title => 'One') => 0,
Option.new(:title => 'Two') => 4,
Option.new(:title => 'Three') => 6 }
poll = Poll.create(:title => 'Test Poll', :options => options.keys)
options.each do |option, response_count|
response_count.times do
poll.submit_response option
end
end
Poll.create(:title => 'Current Poll',
:start_date => Date.today,
:options => [ Option.new(:title => 'Foo'),
Option.new(:title => 'Bar'),
Option.new(:title => 'Baz') ])
end
describe '<r:poll> with no start dates' do
before do
Poll.update_all('start_date = NULL', 'start_date IS NOT NULL')
end
it 'should not accept an empty tag' do
tag = %{<r:poll/>}
pages(:home).should render(tag).with_error("'title' attribute required")
end
end
describe 'with no polls' do
before do
Poll.delete_all
end
describe '<r:poll>' do
it 'should accept a bare tag and generate an error message' do
tag = %{<r:poll/>}
expected = 'No polls found'
pages(:home).should render(tag).as(expected)
end
it 'should accept a title attribute and generate an error message' do
tag = %{<r:poll title="Test Poll"/>}
expected = 'No polls found'
pages(:home).should render(tag).as(expected)
end
end
describe '<r:poll:title>' do
it 'should generate an error message and no inner content' do
tag = %{<r:poll><h2><r:title/></h2></r:poll>}
expected = 'No polls found'
pages(:home).should render(tag).as(expected)
end
end
end
describe '<r:poll>' do
it 'should accept an empty tag and generate no output' do
tag = %{<r:poll/>}
expected = ''
pages(:home).should render(tag).as(expected)
end
it 'should accept a title attribute and generate no output' do
tag = %{<r:poll title="Test Poll"/>}
expected = ''
pages(:home).should render(tag).as(expected)
end
it 'should accept an id tag and generate no output' do
tag = %{<r:poll id="#{Poll.find(:first).id}"/>}
expected = ''
pages(:home).should render(tag).as(expected)
end
end
describe '<r:poll:title>' do
it 'should show the title of the specified poll' do
title = 'Test Poll'
tag = %{<r:poll title="#{title}"><r:title/></r:poll>}
pages(:home).should render(tag).as(title)
end
it 'should show the title of the current poll' do
title = 'Current Poll'
tag = %{<r:poll><r:title/></r:poll>}
pages(:home).should render(tag).as(title)
end
end
describe '<r:poll:form>' do
it 'should generate an empty form' do
tag = %{<r:poll title="Test Poll"><r:form/></r:poll>}
expected = %r{<form action="/pages/\d+/poll_response" method="post" id="poll_form">\s*<div>\s*<input type="hidden"[^>]*>\s*</div>\s*</form>}
pages(:home).should render(tag).matching(expected)
end
end
describe '<r:poll:form:submit>' do
it 'should generate an empty form with a default submit button' do
tag = %{<r:poll title="Test Poll"><r:form><r:submit/></r:form></r:poll>}
expected = %r{<form action="/pages/\d+/poll_response" method="post" id="poll_form">\s*<div>\s*<input type="submit".*?value="Submit"[^>]*>\s*<input type="hidden"[^>]*>\s*</div>\s*</form>}
pages(:home).should render(tag).matching(expected)
end
it 'should generate an empty form with a custom submit button' do
tag = %{<r:poll title="Test Poll"><r:form><r:submit value="Vote"/></r:form></r:poll>}
expected = %r{<form action="/pages/\d+/poll_response" method="post" id="poll_form">\s*<div>\s*<input type="submit".*?value="Vote"[^>]*>\s*<input type="hidden"[^>]*>\s*</div>\s*</form>}
pages(:home).should render(tag).matching(expected)
end
end
describe '<r:poll:form:options>' do
it 'should generate an empty form' do
tag = %{<r:poll title="Test Poll"><r:form><r:options/></r:form></r:poll>}
expected = %r{<form action="/pages/\d+/poll_response" method="post" id="poll_form">\s*<div>\s*<input type="hidden"[^>]*>\s*</div>\s*</form>}
pages(:home).should render(tag).matching(expected)
end
end
describe '<r:poll:form:options:each>' do
it 'should generate an empty form' do
tag = %{<r:poll title="Test Poll"><r:form><r:options:each/></r:form></r:poll>}
expected = %r{<form action="/pages/\d+/poll_response" method="post" id="poll_form">\s*<div>\s*<input type="hidden"[^>]*>\s*</div>\s*</form>}
pages(:home).should render(tag).matching(expected)
end
end
describe '<r:poll:form:options:each:input>' do
it 'should generate a form with input options' do
tag = %{<r:poll title="Test Poll"><r:form><r:options:each><r:input/></r:options:each></r:form></r:poll>}
expected = %r{<form action="/pages/\d+/poll_response" method="post" id="poll_form">\s*<div>\s*(<input type="radio"[^>]*>\s*){3}<input type="hidden"[^>]*>\s*</div>\s*</form>}
pages(:home).should render(tag).matching(expected)
end
end
describe '<r:poll:form:options:each:title>' do
it 'should show the option titles' do
tag = %{<r:poll title="Test Poll"><r:form><r:options:each><r:input/><r:title/></r:options:each></r:form></r:poll>}
expected = %r{<form action="/pages/\d+/poll_response" method="post" id="poll_form">\s*<div>\s*(<input type="radio"[^>]*>\s*(One|Two|Three)\s*){3}<input type="hidden"[^>]*>\s*</div>\s*</form>}
pages(:home).should render(tag).matching(expected)
end
end
describe '<r:poll:options:each:title>' do
it 'should show the option titles in random order' do
tag = %{<r:poll title="Test Poll"><r:options order="rand"><r:each><r:title/></r:each></r:options></r:poll>}
expected = /OneTwoThree|OneThreeTwo|TwoOneThree|TwoThreeOne|ThreeOneTwo|ThreeTwoOne/
pages(:home).should render(tag).matching(expected)
end
end
describe '<r:poll:options:each:number_responses>' do
it 'should show the response numbers in descending order' do
tag = %{<r:poll title="Test Poll"><r:options order="desc"><r:each><p><r:number_responses/></p></r:each></r:options></r:poll>}
expected = '<p>6</p><p>4</p><p>0</p>'
pages(:home).should render(tag).as(expected)
end
end
describe '<r:poll:options:each:percent_responses>' do
it 'should show the response percentages in ascending order' do
tag = %{<r:poll title="Test Poll"><r:options order="asc"><r:each><p><r:percent_responses/></p></r:each></r:options></r:poll>}
expected = '<p>0.0</p><p>40.0</p><p>60.0</p>'
pages(:home).should render(tag).as(expected)
end
end
describe '<r:poll:options:each:if_first>' do
it 'should render inner tags only if the current context is the first option' do
tag = %{<r:poll title="Test Poll"><ul><r:options order="desc"><r:each><li<r:if_first> class="first"</r:if_first>><r:percent_responses /></li></r:each></r:options></ul></r:poll>}
expected = '<ul><li class="first">60.0</li><li>40.0</li><li>0.0</li></ul>'
pages(:home).should render(tag).as(expected)
end
end
describe '<r:poll:options:each:if_last>' do
it 'should render inner tags only if the current context is the last option' do
tag = %{<r:poll title="Test Poll"><ul><r:options order="desc"><r:each><li<r:if_last> class="last"</r:if_last>><r:percent_responses /></li></r:each></r:options></ul></r:poll>}
expected = '<ul><li>60.0</li><li>40.0</li><li class="last">0.0</li></ul>'
pages(:home).should render(tag).as(expected)
end
end
describe '<r:poll:options:each:unless_first>' do
it 'should render inner tags only unless the current context is the first option' do
tag = %{<r:poll title="Test Poll"><r:options order="desc"><r:each><r:unless_first>,</r:unless_first><r:percent_responses /></r:each></r:options></r:poll>}
expected = '60.0,40.0,0.0'
pages(:home).should render(tag).as(expected)
end
end
describe '<r:poll:options:each:unless_last>' do
it 'should render inner tags only unless the current context is the last option' do
tag = %{<r:poll title="Test Poll"><r:options order="desc"><r:each><r:percent_responses /><r:unless_last>,</r:unless_last></r:each></r:options></r:poll>}
expected = '60.0,40.0,0.0'
pages(:home).should render(tag).as(expected)
end
end
##
## Test poll archives
##
describe '<r:polls>' do
it 'should accept an empty tag and generate no output' do
tag = %{<r:polls/>}
expected = ''
pages(:home).should render(tag).as(expected)
end
it 'should accept valid options and generate no output' do
tag = %{<r:polls per_page="10" by="title" order="asc" show_current="false" />}
expected = ''
pages(:home).should render(tag).as(expected)
end
describe 'with per_page option' do
it 'should not accept a non-positive integer and generate an error' do
tag = %{<r:polls per_page="-5" />}
pages(:home).should render(tag).with_error('the per_page attribute of the polls tag must be a positive integer')
end
it 'should not accept a non-integer value and generate an error' do
tag = %{<r:polls per_page="no" />}
pages(:home).should render(tag).with_error('the per_page attribute of the polls tag must be a positive integer')
end
end
describe 'with by option' do
it 'should not accept an invalid field name and generate an error' do
tag = %{<r:polls by="foobar"/>}
pages(:home).should render(tag).with_error('the by attribute of the polls tag must specify a valid field name')
end
end
describe 'with order option' do
it 'should accept "asc" and generate no output' do
tag = %{<r:polls order="asc"/>}
expected = ''
pages(:home).should render(tag).as(expected)
end
it 'should accept "desc" and generate no output' do
tag = %{<r:polls order="desc"/>}
expected = ''
pages(:home).should render(tag).as(expected)
end
it 'should not accept a value other than "asc" or "desc" and generate an error' do
tag = %{<r:polls order="xxx"/>}
pages(:home).should render(tag).with_error('the order attribute of the polls tag must be either "asc" or "desc"')
end
end
describe 'with no polls' do
before do
Poll.delete_all
end
it 'should accept a bare tag and generate an error message' do
tag = %{<r:polls/>}
expected = 'No polls found'
pages(:home).should render(tag).as(expected)
end
end
end
describe '<r:polls:each>' do
it 'should generate no output' do
tag = %{<r:polls><r:each></r:each></r:polls>}
expected = ''
pages(:home).should render(tag).as(expected)
end
end
describe '<r:polls:each:poll>' do
it 'should generate no output' do
tag = %{<r:polls><r:each><r:poll/></r:each></r:polls>}
expected = ''
pages(:home).should render(tag).as(expected)
end
end
describe '<r:polls:each:poll:title>' do
before do
# Create a future poll that should never appear in any results
Poll.create(:title => 'Next Poll', :start_date => Date.today + 1.week)
end
it 'should generate an ordered list of poll titles in descending order' do
tag = %{<r:polls order="desc" show_current="true"><r:each><r:poll><p><r:title/></p></r:poll></r:each></r:polls>}
expected = '<p>Test Poll</p><p>Current Poll</p>'
pages(:home).should render(tag).as(expected)
end
it 'should generate a list of the one poll that has a non-future start date' do
tag = %{<r:polls by="start_date" show_current="true"><r:each><r:poll><p><r:title/></p></r:poll></r:each></r:polls>}
expected = '<p>Current Poll</p>'
pages(:home).should render(tag).as(expected)
end
it 'should generate an ordered list of poll titles in descending order by start date' do
poll = Poll.find_by_title('Test Poll').update_attribute(:start_date, Date.today - 1.week)
tag = %{<r:polls by="start_date" order="desc" show_current="true"><r:each><r:poll><p><r:title/></p></r:poll></r:each></r:polls>}
expected = '<p>Current Poll</p><p>Test Poll</p>'
pages(:home).should render(tag).as(expected)
end
end
describe '<r:polls:each:poll:options:title>' do
it 'should generate a list containing the current poll with its options' do
tag = %{<r:polls by="start_date" show_current="true"><r:each><r:poll><p><r:title/></p><r:options order="desc"><r:each><p><r:title/></p></r:each></r:options></r:poll></r:each></r:polls>}
expected = '<p>Current Poll</p><p>Foo</p><p>Bar</p><p>Baz</p>'
pages(:home).should render(tag).as(expected)
end
it 'should generate no output when sorting by start date and excluding the current poll' do
tag = %{<r:polls by="start_date"><r:each><r:poll><p><r:title/></p><r:options order="desc"><r:each><p><r:title/></p></r:each></r:options></r:poll></r:each></r:polls>}
expected = ''
pages(:home).should render(tag).as(expected)
end
end
describe '<r:polls:pages>' do
describe 'with less than per_page polls' do
it 'should not generate pagination links' do
tag = %{<r:polls by="title"><r:pages/></r:polls>}
expected = ''
pages(:home).should render(tag).as(expected)
end
end
describe 'with more than per_page polls' do
before do
(1..20).each do |i|
Poll.create(:title => "Poll #{i}",
:start_date => Date.today - i.weeks,
:options => [ Option.new(:title => "Foo #{i}"),
Option.new(:title => "Bar #{i}"),
Option.new(:title => "Baz #{i}") ])
end
end
it 'should generate pagination links' do
tag = %{<r:polls per_page="5" by="start_date" order="desc"><r:pages/></r:polls>}
expected = %r{<span class="current">1</span>}
pages(:home).should render(tag).matching(expected)
end
end
end
end