summaryrefslogtreecommitdiff
path: root/test/rss/test_to_s.rb
blob: 1538534034d25d704b1a33f78b6f89d4b19d0587 (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
require "rexml/document"

require "rss-testcase"

require "rss/maker"
require "rss/1.0"
require "rss/2.0"
require "rss/content"
require "rss/dublincore"
require "rss/syndication"
require "rss/trackback"

module RSS
  class TestToS < TestCase
    
    def setup
      @image_url = "http://example.com/foo.png"
      @textinput_link = "http://example.com/search.cgi"
      @item_links = [
        "http://example.com/1",
        "http://example.com/2",
      ]
      
      setup_xml_declaration_info
      setup_xml_stylesheet_infos
      setup_channel_info
      setup_item_infos
      setup_image_info
      setup_textinput_info

      setup_dublin_core_info
      setup_syndication_info
      setup_content_info
      setup_trackback_info
    end

    def test_to_s_10
      rss = RSS::Maker.make("1.0") do |maker|
        setup_full(maker)
      end

      assert_xml_declaration(@version, @encoding, @standalone, rss)
      assert_xml_stylesheets(@xs_infos, rss.xml_stylesheets)
      assert_channel10(@channel_info, rss.channel)
      assert_items10(@item_infos, rss.items)
      rss.items.each do |item|
        assert_trackback(@trackback_info, item)
      end
      assert_image10(@image_info, rss.image)
      assert_textinput10(@textinput_info, rss.textinput)

      rss = RSS::Parser.parse(rss.to_s)

      assert_xml_declaration(@version, @encoding, @standalone, rss)
      assert_xml_stylesheets(@xs_infos, rss.xml_stylesheets)
      assert_channel10(@channel_info, rss.channel)
      assert_items10(@item_infos, rss.items)
      assert_image10(@image_info, rss.image)
      assert_textinput10(@textinput_info, rss.textinput)
    end
    
    def test_to_s_09
      rss = RSS::Maker.make("0.91") do |maker|
        setup_full(maker)
      end

      assert_xml_declaration(@version, @encoding, @standalone, rss)
      assert_xml_stylesheets(@xs_infos, rss.xml_stylesheets)
      assert_channel09(@channel_info, rss.channel)
      assert_items09(@item_infos, rss.items)
      assert_image09(@image_info, rss.image)
      assert_textinput09(@textinput_info, rss.textinput)

      rss = RSS::Parser.parse(rss.to_s)

      assert_xml_declaration(@version, @encoding, @standalone, rss)
      assert_xml_stylesheets(@xs_infos, rss.xml_stylesheets)
      assert_channel09(@channel_info, rss.channel)
      assert_items09(@item_infos, rss.items)
      assert_image09(@image_info, rss.image)
      assert_textinput09(@textinput_info, rss.textinput)
    end
    
    def test_to_s_20
      rss = RSS::Maker.make("2.0") do |maker|
        setup_full(maker)
      end

      assert_xml_declaration(@version, @encoding, @standalone, rss)
      assert_xml_stylesheets(@xs_infos, rss.xml_stylesheets)
      assert_channel20(@channel_info, rss.channel)
      assert_items20(@item_infos, rss.items)
      assert_image20(@image_info, rss.image)
      assert_textinput20(@textinput_info, rss.textinput)

      rss = RSS::Parser.parse(rss.to_s)

      assert_xml_declaration(@version, @encoding, @standalone, rss)
      assert_xml_stylesheets(@xs_infos, rss.xml_stylesheets)
      assert_channel20(@channel_info, rss.channel)
      assert_items20(@item_infos, rss.items)
      assert_image20(@image_info, rss.image)
      assert_textinput20(@textinput_info, rss.textinput)
    end
    
    private
    def setup_xml_declaration_info
      @version = "1.0"
      @encoding = "UTF-8"
      @standalone = false
    end

    def setup_xml_stylesheet_infos
      @xs_infos = [
        {
          "href" => "XXX.xsl",
          "type" => "text/xsl",
          "title" => "XXX",
          "media" => "print",
          "alternate" => "no",
        },
        {
          "href" => "YYY.css",
          "type" => "text/css",
          "title" => "YYY",
          "media" => "all",
          "alternate" => "no",
        },
      ]
    end

    def setup_channel_info
      @channel_info = {
        "about" => "http://example.com/index.rdf",
        "title" => "Sample RSS",
        "link" => "http://example.com/",
        "description" => "Sample\n\n\n\n\nSite",
        "language" => "en",
        "copyright" => "FDL",
        "managingEditor" => "foo@example.com",
        "webMaster" => "webmaster@example.com",
        "rating" => '(PICS-1.1 "http://www.rsac.org/ratingsv01.html" l gen true comment "RSACi North America Server" for "http://www.rsac.org" on "1996.04.16T08:15-0500" r (n 0 s 0 v 0 l 0))',
        "docs" => "http://backend.userland.com/rss091",
        "skipDays" => [
          "Monday",
          "Friday",
        ],
        "skipHours" => [
          "12",
          "23",
        ],
        "date" => Time.now,
        "lastBuildDate" => Time.now - 3600,
        "generator" => "RSS Maker",
        "ttl" => "60",
        "cloud" => {
          "domain" => "rpc.sys.com",
          "port" => "80",
          "path" => "/RPC2",
          "registerProcedure" => "myCloud.rssPleaseNotify",
          "protocol" => "xml-rpc",
        },
        "category" => {
          "domain" => "http://example.com/misc/",
          "content" => "misc",
        },

        "image" => {
          "resource" => @image_url,
        },

        "textinput" => {
          "resource" => @textinput_link,
        },

        "items" => @item_links.collect{|link| {"resource" => link}},
      }
    end

    def setup_item_infos
      @item_infos = [
        {
          "title" => "Sample item1",
          "link" => @item_links[0],
          "description" => "Sample description1",
          "date" => Time.now - 3600,
          "author" => "foo@example.com",
          "comments" => "http://example.com/1/comments",
          "guid" => {
            "isPermaLink" => "true",
            "content" => "http://example.com/1",
          },
          "enclosure" => {
            "url" => "http://example.com/1.mp3",
            "length" => "100",
            "type" => "audio/mpeg",
          },
          "source" => {
            "url" => "http:/example.com/",
            "content" => "Sample site",
          },
          "category" => {
            "domain" => "http://example.com/misc/",
            "content" => "misc",
          },
        },

        {
          "title" => "Sample item2",
          "link" => @item_links[1],
          "description" => "Sample description2",
          "date" => Time.now - 7200,
          "author" => "foo@example.com",
          "comments" => "http://example.com/2/comments",
          "guid" => {
            "isPermaLink" => "false",
            "content" => "http://example.com/2",
          },
          "enclosure" => {
            "url" => "http://example.com/2.mp3",
            "length" => "200",
            "type" => "audio/mpeg",
          },
          "source" => {
            "url" => "http:/example.com/",
            "content" => "Sample site",
          },
          "category" => {
            "domain" => "http://example.com/misc/",
            "content" => "misc",
          },
        },
      ]
    end

    def setup_image_info
      @image_info = {
        "title" => "Sample image",
        "url" => @image_url,
        "width" => "88",
        "height" => "31",
        "description" => "Sample",
      }
    end

    def setup_textinput_info
      @textinput_info = {
        "title" => "Sample textinput",
        "description" => "Search",
        "name" => "key",
        "link" => @textinput_link,
      }
    end

    def setup_dublin_core_info
      @dc_info = {
        "title" => "DC title",
        "description" => "DC desc",
        "creator" => "DC creator",
        "subject" => "DC subject",
        "publisher" => "DC publisher",
        "contributor" => "DC contributor",
        "type" => "DC type",
        "format" => "DC format",
        "identifier" => "DC identifier",
        "source" => "DC source",
        "language" => "ja",
        "relation" => "DC relation",
        "coverage" => "DC coverage",
        "rights" => "DC rights",
        "date" => Time.now - 60,
      }
    end

    def setup_syndication_info
      @sy_info = {
        "updatePeriod" => "hourly",
        "updateFrequency" => "2",
        "updateBase" => Time.now - 3600,
      }
    end
    
    def setup_content_info
      @content_info = {
        "encoded" => "<p>p</p>",
      }
    end
    
    def setup_trackback_info
      @trackback_info = {
        "ping" => "http://example.com/tb.cgi?tb_id=XXX",
        "abouts" => [
          "http://example.net/tb.cgi?tb_id=YYY",
          "http://example.org/tb.cgi?tb_id=ZZZ",
        ]
      }
    end


    def setup_full(maker)
      setup_xml_declaration(maker)
      setup_xml_stylesheets(maker)
      setup_channel(maker)
      setup_image(maker)
      setup_items(maker)
      setup_textinput(maker)
    end

    def setup_xml_declaration(maker)
      %w(version encoding standalone).each do |name|
        maker.__send__("#{name}=", instance_eval("@#{name}"))
      end
    end

    def setup_xml_stylesheets(maker)
      @xs_infos.each do |info|
        xs = maker.xml_stylesheets.new_xml_stylesheet
        info.each do |name, value|
          xs.__send__("#{name}=", value)
        end
      end
    end

    def setup_channel(maker)
      channel = maker.channel
      info = @channel_info
      
      %w(about title link description language copyright
         managingEditor webMaster rating docs date
         lastBuildDate generator ttl).each do |name|
        channel.__send__("#{name}=", info[name])
      end

      skipDays = channel.skipDays
      info["skipDays"].each do |day|
        new_day = skipDays.new_day
        new_day.content = day
      end

      skipHours = channel.skipHours
      info["skipHours"].each do |hour|
        new_hour = skipHours.new_hour
        new_hour.content = hour
      end
      
      cloud = channel.cloud
      %w(domain port path registerProcedure protocol).each do |name|
        cloud.__send__("#{name}=", info["cloud"][name])
      end

      category = channel.categories.new_category
      %w(domain content).each do |name|
        category.__send__("#{name}=", info["category"][name])
      end
    end
    
    def setup_image(maker)
      image = maker.image
      info = @image_info

      %w(title url width height description).each do |name|
        image.__send__("#{name}=", info[name])
      end
    end
    
    def setup_items(maker)
      items = maker.items

      @item_infos.each do |info|
        item = items.new_item
        %w(title link description date author comments).each do |name|
          item.__send__("#{name}=", info[name])
        end
        
        guid = item.guid
        %w(isPermaLink content).each do |name|
          guid.__send__("#{name}=", info["guid"][name])
        end

        enclosure = item.enclosure
        %w(url length type).each do |name|
          enclosure.__send__("#{name}=", info["enclosure"][name])
        end

        source = item.source
        %w(url content).each do |name|
          source.__send__("#{name}=", info["source"][name])
        end

        category = item.categories.new_category
        %w(domain content).each do |name|
          category.__send__("#{name}=", info["category"][name])
        end

        setup_trackback(item)
      end
    end

    def setup_textinput(maker)
      textinput = maker.textinput
      info = @textinput_info

      %w(title description name link).each do |name|
        textinput.__send__("#{name}=", info[name])
      end
    end

    def setup_content(target)
      prefix = "content"
      %w(encoded).each do |name|
        target.__send__("#{prefix}_#{name}=", @content_info[name])
      end
    end
    
    def setup_dublin_core(target)
      prefix = "dc"
      %w(title description creator subject publisher
        contributor type format identifier source language
        relation coverage rights).each do |name|
        target.__send__("#{prefix}_#{name}=", @dc_info[name])
      end
    end
    
    def setup_syndicate(target)
      prefix = "sy"
      %w(updatePeriod updateFrequency updateBase).each do |name|
        target.__send__("#{prefix}_#{name}=", @sy_info[name])
      end
    end
    
    def setup_trackback(target)
      target.trackback_ping = @trackback_info["ping"]
      @trackback_info["abouts"].each do |about|
        new_about = target.trackback_abouts.new_about
        new_about.value = about
      end
    end
    
  end
end