summaryrefslogtreecommitdiffstats
path: root/test/rss/test_maker_itunes.rb
blob: 21a4dd1f29db836f7a5771b7377dc87bab3cc620 (plain)
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
462
463
464
465
466
467
468
469
470
471
require "rss-testcase"

require "rss/maker"

module RSS
  class TestMakerITunes < TestCase
    def test_author
      assert_maker_itunes_author(%w(channel))
      assert_maker_itunes_author(%w(items last))
    end

    def test_block
      assert_maker_itunes_block(%w(channel))
      assert_maker_itunes_block(%w(items last))
    end

    def test_category
      assert_maker_itunes_category(%w(channel))
    end

    def test_image
      assert_maker_itunes_image(%w(channel))
    end

    def test_duration
      assert_maker_itunes_duration(%w(items last))
    end

    def test_explicit
      assert_maker_itunes_explicit(%w(channel))
      assert_maker_itunes_explicit(%w(items last))
    end

    def test_keywords
      assert_maker_itunes_keywords(%w(channel))
      assert_maker_itunes_keywords(%w(items last))
    end

    def test_new_feed_url
      assert_maker_itunes_new_feed_url(%w(channel))
    end

    def test_owner
      assert_maker_itunes_owner(%w(channel))
    end

    def test_subtitle
      assert_maker_itunes_subtitle(%w(channel))
      assert_maker_itunes_subtitle(%w(items last))
    end

    def test_summary
      assert_maker_itunes_summary(%w(channel))
      assert_maker_itunes_summary(%w(items last))
    end

    private

    def assert_maker_itunes_author(maker_readers, feed_readers=nil)
      _wrap_assertion do
        feed_readers ||= maker_readers
        author = "John Doe"
        rss20 = ::RSS::Maker.make("rss2.0") do |maker|
          setup_dummy_channel(maker)
          setup_dummy_item(maker)

          target = chain_reader(maker, maker_readers)
          target.itunes_author = author
        end
        target = chain_reader(rss20, feed_readers)
        assert_equal(author, target.itunes_author)
      end
    end

    def _assert_maker_itunes_block(value, boolean_value, maker_readers,
                                   feed_readers)
      rss20 = ::RSS::Maker.make("rss2.0") do |maker|
        setup_dummy_channel(maker)
        setup_dummy_item(maker)

        target = chain_reader(maker, maker_readers)
        target.itunes_block = value
        assert_equal(value, target.itunes_block)
        assert_equal(boolean_value, target.itunes_block?)
      end
      target = chain_reader(rss20, feed_readers)
      if [true, false].include?(value)
        feed_expected_value = value = value ? "yes" : "no"
      else
        feed_expected_value = value
      end
      assert_equal(value, target.itunes_block)
      assert_equal(boolean_value, target.itunes_block?)
    end

    def assert_maker_itunes_block(maker_readers, feed_readers=nil)
      _wrap_assertion do
        feed_readers ||= maker_readers
        _assert_maker_itunes_block("yes", true, maker_readers, feed_readers)
        _assert_maker_itunes_block("Yes", true, maker_readers, feed_readers)
        _assert_maker_itunes_block("no", false, maker_readers, feed_readers)
        _assert_maker_itunes_block("", false, maker_readers, feed_readers)
        _assert_maker_itunes_block(true, true, maker_readers, feed_readers)
        _assert_maker_itunes_block(false, false, maker_readers, feed_readers)
        _assert_maker_itunes_block(nil, false, maker_readers, feed_readers)
      end
    end

    def _assert_maker_itunes_category(categories, maker_readers, feed_readers)
      rss20 = ::RSS::Maker.make("rss2.0") do |maker|
        setup_dummy_channel(maker)
        setup_dummy_item(maker)

        target = chain_reader(maker, maker_readers)
        categories.each do |category|
          sub_target = target.itunes_categories
          if category.is_a?(Array)
            category.each do |sub_category|
              sub_target = sub_target.new_category
              sub_target.text = sub_category
            end
          else
            sub_target.new_category.text = category
          end
        end
      end

      target = chain_reader(rss20, feed_readers)
      actual_categories = target.itunes_categories.collect do |category|
        cat = category.text
        if category.itunes_categories.empty?
          cat
        else
          [cat, *category.itunes_categories.collect {|c| c.text}]
        end
      end
      assert_equal(categories, actual_categories)
    end

    def assert_maker_itunes_category(maker_readers, feed_readers=nil)
      _wrap_assertion do
        feed_readers ||= maker_readers
        _assert_maker_itunes_category(["Audio Blogs"],
                                      maker_readers, feed_readers)
        _assert_maker_itunes_category([["Arts & Entertainment", "Games"]],
                                      maker_readers, feed_readers)
        _assert_maker_itunes_category([["Arts & Entertainment", "Games"],
                                       ["Technology", "Computers"],
                                       "Audio Blogs"],
                                      maker_readers, feed_readers)
      end
    end

    def assert_maker_itunes_image(maker_readers, feed_readers=nil)
      _wrap_assertion do
        feed_readers ||= maker_readers
        url = "http://example.com/podcasts/everything/AllAboutEverything.jpg"

        rss20 = ::RSS::Maker.make("rss2.0") do |maker|
          setup_dummy_channel(maker)
          setup_dummy_item(maker)

          target = chain_reader(maker, maker_readers)
          target.itunes_image = url
        end

        target = chain_reader(rss20, feed_readers)
        assert_not_nil(target.itunes_image)
        assert_equal(url, target.itunes_image.href)
      end
    end

    def _assert_maker_itunes_duration(hour, minute, second, value,
                                      maker_readers, feed_readers)
      _assert_maker_itunes_duration_by_value(hour, minute, second, value,
                                             maker_readers, feed_readers)
      _assert_maker_itunes_duration_by_hour_minute_second(hour, minute, second,
                                                          value,
                                                          maker_readers,
                                                          feed_readers)
    end

    def _assert_maker_itunes_duration_by(hour, minute, second, value,
                                         maker_readers, feed_readers)
      expected_value = nil
      rss20 = ::RSS::Maker.make("rss2.0") do |maker|
        setup_dummy_channel(maker)
        setup_dummy_item(maker)

        target = chain_reader(maker, maker_readers)
        expected_value = yield(target)
        assert_equal(expected_value, target.itunes_duration)
        target.itunes_duration do |duration|
          assert_equal([hour, minute, second, expected_value],
                       [duration.hour, duration.minute,
                        duration.second, duration.content])
        end
      end
      target = chain_reader(rss20, feed_readers)
      duration = target.itunes_duration
      assert_not_nil(duration)
      assert_equal([hour, minute, second, expected_value],
                   [duration.hour, duration.minute,
                    duration.second, duration.content])
    end

    def _assert_maker_itunes_duration_by_value(hour, minute, second, value,
                                               maker_readers, feed_readers)
      _assert_maker_itunes_duration_by(hour, minute, second, value,
                                       maker_readers, feed_readers) do |target|
        target.itunes_duration = value
        value
      end
    end

    def _assert_maker_itunes_duration_by_hour_minute_second(hour, minute, second,
                                                            value,
                                                            maker_readers,
                                                            feed_readers)
      _assert_maker_itunes_duration_by(hour, minute, second, value,
                                       maker_readers, feed_readers) do |target|
        target.itunes_duration do |duration|
          duration.hour = hour
          duration.minute = minute
          duration.second = second
        end
        value.split(":").collect {|v| "%02d" % v.to_i}.join(":")
      end
    end

    def _assert_maker_itunes_duration_invalid_value(value, maker_readers)
      assert_raise(ArgumentError) do
        ::RSS::Maker.make("rss2.0") do |maker|
          setup_dummy_channel(maker)
          setup_dummy_item(maker)

          target = chain_reader(maker, maker_readers)
          target.itunes_duration = value
        end
      end
    end

    def assert_maker_itunes_duration(maker_readers, feed_readers=nil)
      _wrap_assertion do
        feed_readers ||= maker_readers
        _assert_maker_itunes_duration(7, 14, 5, "07:14:05", maker_readers,
                                      feed_readers)
        _assert_maker_itunes_duration(7, 14, 5, "7:14:05", maker_readers,
                                      feed_readers)
        _assert_maker_itunes_duration(0, 4, 55, "04:55", maker_readers,
                                      feed_readers)
        _assert_maker_itunes_duration(0, 4, 5, "4:05", maker_readers,
                                      feed_readers)

        _assert_maker_itunes_duration_invalid_value("5", maker_readers)
        _assert_maker_itunes_duration_invalid_value("09:07:14:05", maker_readers)
        _assert_maker_itunes_duration_invalid_value("10:5", maker_readers)
        _assert_maker_itunes_duration_invalid_value("10:03:5", maker_readers)
        _assert_maker_itunes_duration_invalid_value("10:3:05", maker_readers)

        _assert_maker_itunes_duration_invalid_value("xx:xx:xx", maker_readers)
      end
    end

    def _assert_maker_itunes_explicit(explicit, value,
                                      maker_readers, feed_readers)
      rss20 = ::RSS::Maker.make("rss2.0") do |maker|
        setup_dummy_channel(maker)
        setup_dummy_item(maker)

        target = chain_reader(maker, maker_readers)
        target.itunes_explicit = value
        assert_equal(explicit, target.itunes_explicit?)
      end
      target = chain_reader(rss20, feed_readers)
      assert_equal(value, target.itunes_explicit)
      assert_equal(explicit, target.itunes_explicit?)
    end

    def assert_maker_itunes_explicit(maker_readers, feed_readers=nil)
      _wrap_assertion do
        feed_readers ||= maker_readers
        _assert_maker_itunes_explicit(true, "yes", maker_readers, feed_readers)
        _assert_maker_itunes_explicit(false, "clean",
                                      maker_readers, feed_readers)
        _assert_maker_itunes_explicit(nil, "no", maker_readers, feed_readers)
      end
    end

    def _assert_maker_itunes_keywords(keywords, value,
                                      maker_readers, feed_readers)
      _assert_maker_itunes_keywords_by_value(keywords, value,
                                             maker_readers, feed_readers)
      _assert_maker_itunes_keywords_by_keywords(keywords, maker_readers,
                                                feed_readers)
    end

    def _assert_maker_itunes_keywords_by(keywords, maker_readers, feed_readers)
      rss20 = ::RSS::Maker.make("rss2.0") do |maker|
        setup_dummy_channel(maker)
        setup_dummy_item(maker)

        target = chain_reader(maker, maker_readers)
        yield(target)
      end
      assert_nothing_raised do
        rss20 = ::RSS::Parser.parse(rss20.to_s)
      end
      target = chain_reader(rss20, feed_readers)
      assert_equal(keywords, target.itunes_keywords)
    end

    def _assert_maker_itunes_keywords_by_value(keywords, value,
                                               maker_readers, feed_readers)
      _assert_maker_itunes_keywords_by(keywords, maker_readers,
                                       feed_readers) do |target|
        target.itunes_keywords = value
      end
    end

    def _assert_maker_itunes_keywords_by_keywords(keywords,
                                                  maker_readers, feed_readers)
      _assert_maker_itunes_keywords_by(keywords, maker_readers,
                                       feed_readers) do |target|
        target.itunes_keywords = keywords
      end
    end

    def assert_maker_itunes_keywords(maker_readers, feed_readers=nil)
      _wrap_assertion do
        feed_readers ||= maker_readers
        _assert_maker_itunes_keywords(["salt"], "salt",
                                      maker_readers, feed_readers)
        _assert_maker_itunes_keywords(["salt"], " salt ",
                                      maker_readers, feed_readers)
        _assert_maker_itunes_keywords(["salt", "pepper", "shaker", "exciting"],
                                      "salt, pepper, shaker, exciting",
                                      maker_readers, feed_readers)
        _assert_maker_itunes_keywords(["metric", "socket", "wrenches",
                                       "toolsalt"],
                                      "metric, socket, wrenches, toolsalt",
                                      maker_readers, feed_readers)
        _assert_maker_itunes_keywords(["olitics", "red", "blue", "state"],
                                      "olitics, red, blue, state",
                                      maker_readers, feed_readers)
      end
    end

    def assert_maker_itunes_new_feed_url(maker_readers, feed_readers=nil)
      feed_readers ||= maker_readers
      url = "http://newlocation.com/example.rss"

      rss20 = ::RSS::Maker.make("rss2.0") do |maker|
        setup_dummy_channel(maker)
        setup_dummy_item(maker)

        target = chain_reader(maker, maker_readers)
        target.itunes_new_feed_url = url
      end
      target = chain_reader(rss20, feed_readers)
      assert_equal(url, target.itunes_new_feed_url)
    end

    def _assert_maker_itunes_owner(name, email, maker_readers, feed_readers)
      rss20 = ::RSS::Maker.make("rss2.0") do |maker|
        setup_dummy_channel(maker)
        setup_dummy_item(maker)

        target = chain_reader(maker, maker_readers)
        owner = target.itunes_owner
        owner.itunes_name = name
        owner.itunes_email = email
      end
      owner = chain_reader(rss20, feed_readers).itunes_owner
      if name.nil? and email.nil?
        assert_nil(owner)
      else
        assert_not_nil(owner)
        assert_equal(name, owner.itunes_name)
        assert_equal(email, owner.itunes_email)
      end
    end

    def assert_maker_itunes_owner(maker_readers, feed_readers=nil)
      _wrap_assertion do
        feed_readers ||= maker_readers
        _assert_maker_itunes_owner("John Doe", "john.doe@example.com",
                                   maker_readers, feed_readers)

        not_set_name = (["maker"] + maker_readers + ["itunes_owner"]).join(".")
        assert_not_set_error(not_set_name, ["itunes_name"]) do
          _assert_maker_itunes_owner(nil, "john.doe@example.com",
                                     maker_readers, feed_readers)
        end
        assert_not_set_error(not_set_name, ["itunes_email"]) do
          _assert_maker_itunes_owner("John Doe", nil,
                                     maker_readers, feed_readers)
        end

        _assert_maker_itunes_owner(nil, nil, maker_readers, feed_readers)
      end
    end

    def _assert_maker_itunes_subtitle(subtitle, maker_readers, feed_readers)
      rss20 = ::RSS::Maker.make("rss2.0") do |maker|
        setup_dummy_channel(maker)
        setup_dummy_item(maker)

        target = chain_reader(maker, maker_readers)
        target.itunes_subtitle = subtitle
      end

      target = chain_reader(rss20, feed_readers)
      assert_equal(subtitle, target.itunes_subtitle)
    end

    def assert_maker_itunes_subtitle(maker_readers, feed_readers=nil)
      _wrap_assertion do
        feed_readers ||= maker_readers
        _assert_maker_itunes_subtitle("A show about everything",
                                      maker_readers, feed_readers)
        _assert_maker_itunes_subtitle("A short primer on table spices",
                                      maker_readers, feed_readers)
        _assert_maker_itunes_subtitle("Comparing socket wrenches is fun!",
                                      maker_readers, feed_readers)
        _assert_maker_itunes_subtitle("Red + Blue != Purple",
                                      maker_readers, feed_readers)
      end
    end

    def _assert_maker_itunes_summary(summary, maker_readers, feed_readers)
      rss20 = ::RSS::Maker.make("rss2.0") do |maker|
        setup_dummy_channel(maker)
        setup_dummy_item(maker)

        target = chain_reader(maker, maker_readers)
        target.itunes_summary = summary
      end

      target = chain_reader(rss20, feed_readers)
      assert_equal(summary, target.itunes_summary)
    end

    def assert_maker_itunes_summary(maker_readers, feed_readers=nil)
      _wrap_assertion do
        feed_readers ||= maker_readers
        _assert_maker_itunes_summary("All About Everything is a show about " +
                                     "everything. Each week we dive into any " +
                                     "subject known to man and talk about it " +
                                     "as much as we can. Look for our Podcast " +
                                     "in the iTunes Music Store",
                                     maker_readers, feed_readers)
        _assert_maker_itunes_summary("This week we talk about salt and pepper " +
                                     "shakers, comparing and contrasting pour " +
                                     "rates, construction materials, and " +
                                     "overall aesthetics. Come and join the " +
                                     "party!",
                                     maker_readers, feed_readers)
        _assert_maker_itunes_summary("This week we talk about metric vs. old " +
                                     "english socket wrenches. Which one is " +
                                     "better? Do you really need both? Get " +
                                     "all of your answers here.",
                                     maker_readers, feed_readers)
        _assert_maker_itunes_summary("This week we talk about surviving in a " +
                                     "Red state if you're a Blue person. Or " +
                                     "vice versa.",
                                     maker_readers, feed_readers)
      end
    end
  end
end