~ubuntu-branches/ubuntu/intrepid/ruby1.8/intrepid-updates

« back to all changes in this revision

Viewing changes to test/rss/test_to_s.rb

  • Committer: Bazaar Package Importer
  • Author(s): akira yamada
  • Date: 2007-03-13 22:11:58 UTC
  • mfrom: (1.1.5 upstream)
  • Revision ID: james.westby@ubuntu.com-20070313221158-h3oql37brlaf2go2
Tags: 1.8.6-1
* new upstream version, 1.8.6.
* libruby1.8 conflicts with libopenssl-ruby1.8 (< 1.8.6) (closes: #410018)
* changed packaging style to cdbs from dbs.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
require "rexml/document"
 
2
 
 
3
require "rss-testcase"
 
4
 
 
5
require "rss/maker"
 
6
require "rss/1.0"
 
7
require "rss/2.0"
 
8
require "rss/content"
 
9
require "rss/dublincore"
 
10
require "rss/syndication"
 
11
require "rss/trackback"
 
12
 
 
13
module RSS
 
14
  class TestToS < TestCase
 
15
    
 
16
    def setup
 
17
      @image_url = "http://example.com/foo.png"
 
18
      @textinput_link = "http://example.com/search.cgi"
 
19
      @item_links = [
 
20
        "http://example.com/1",
 
21
        "http://example.com/2",
 
22
      ]
 
23
      
 
24
      setup_xml_declaration_info
 
25
      setup_xml_stylesheet_infos
 
26
      setup_channel_info
 
27
      setup_item_infos
 
28
      setup_image_info
 
29
      setup_textinput_info
 
30
 
 
31
      setup_dublin_core_info
 
32
      setup_syndication_info
 
33
      setup_content_info
 
34
      setup_trackback_info
 
35
    end
 
36
 
 
37
    def test_to_s_10
 
38
      rss = RSS::Maker.make("1.0") do |maker|
 
39
        setup_full(maker)
 
40
      end
 
41
 
 
42
      assert_xml_declaration(@version, @encoding, @standalone, rss)
 
43
      assert_xml_stylesheets(@xs_infos, rss.xml_stylesheets)
 
44
      assert_channel10(@channel_info, rss.channel)
 
45
      assert_items10(@item_infos, rss.items)
 
46
      rss.items.each do |item|
 
47
        assert_trackback(@trackback_info, item)
 
48
      end
 
49
      assert_image10(@image_info, rss.image)
 
50
      assert_textinput10(@textinput_info, rss.textinput)
 
51
 
 
52
      rss = RSS::Parser.parse(rss.to_s)
 
53
 
 
54
      assert_xml_declaration(@version, @encoding, @standalone, rss)
 
55
      assert_xml_stylesheets(@xs_infos, rss.xml_stylesheets)
 
56
      assert_channel10(@channel_info, rss.channel)
 
57
      assert_items10(@item_infos, rss.items)
 
58
      assert_image10(@image_info, rss.image)
 
59
      assert_textinput10(@textinput_info, rss.textinput)
 
60
    end
 
61
    
 
62
    def test_to_s_09
 
63
      rss = RSS::Maker.make("0.91") do |maker|
 
64
        setup_full(maker)
 
65
      end
 
66
 
 
67
      assert_xml_declaration(@version, @encoding, @standalone, rss)
 
68
      assert_xml_stylesheets(@xs_infos, rss.xml_stylesheets)
 
69
      assert_channel09(@channel_info, rss.channel)
 
70
      assert_items09(@item_infos, rss.items)
 
71
      assert_image09(@image_info, rss.image)
 
72
      assert_textinput09(@textinput_info, rss.textinput)
 
73
 
 
74
      rss = RSS::Parser.parse(rss.to_s)
 
75
 
 
76
      assert_xml_declaration(@version, @encoding, @standalone, rss)
 
77
      assert_xml_stylesheets(@xs_infos, rss.xml_stylesheets)
 
78
      assert_channel09(@channel_info, rss.channel)
 
79
      assert_items09(@item_infos, rss.items)
 
80
      assert_image09(@image_info, rss.image)
 
81
      assert_textinput09(@textinput_info, rss.textinput)
 
82
    end
 
83
    
 
84
    def test_to_s_20
 
85
      rss = RSS::Maker.make("2.0") do |maker|
 
86
        setup_full(maker)
 
87
      end
 
88
 
 
89
      assert_xml_declaration(@version, @encoding, @standalone, rss)
 
90
      assert_xml_stylesheets(@xs_infos, rss.xml_stylesheets)
 
91
      assert_channel20(@channel_info, rss.channel)
 
92
      assert_items20(@item_infos, rss.items)
 
93
      assert_image20(@image_info, rss.image)
 
94
      assert_textinput20(@textinput_info, rss.textinput)
 
95
 
 
96
      rss = RSS::Parser.parse(rss.to_s)
 
97
 
 
98
      assert_xml_declaration(@version, @encoding, @standalone, rss)
 
99
      assert_xml_stylesheets(@xs_infos, rss.xml_stylesheets)
 
100
      assert_channel20(@channel_info, rss.channel)
 
101
      assert_items20(@item_infos, rss.items)
 
102
      assert_image20(@image_info, rss.image)
 
103
      assert_textinput20(@textinput_info, rss.textinput)
 
104
    end
 
105
    
 
106
    private
 
107
    def setup_xml_declaration_info
 
108
      @version = "1.0"
 
109
      @encoding = "UTF-8"
 
110
      @standalone = false
 
111
    end
 
112
 
 
113
    def setup_xml_stylesheet_infos
 
114
      @xs_infos = [
 
115
        {
 
116
          "href" => "XXX.xsl",
 
117
          "type" => "text/xsl",
 
118
          "title" => "XXX",
 
119
          "media" => "print",
 
120
          "alternate" => "no",
 
121
        },
 
122
        {
 
123
          "href" => "YYY.css",
 
124
          "type" => "text/css",
 
125
          "title" => "YYY",
 
126
          "media" => "all",
 
127
          "alternate" => "no",
 
128
        },
 
129
      ]
 
130
    end
 
131
 
 
132
    def setup_channel_info
 
133
      @channel_info = {
 
134
        "about" => "http://example.com/index.rdf",
 
135
        "title" => "Sample RSS",
 
136
        "link" => "http://example.com/",
 
137
        "description" => "Sample\n\n\n\n\nSite",
 
138
        "language" => "en",
 
139
        "copyright" => "FDL",
 
140
        "managingEditor" => "foo@example.com",
 
141
        "webMaster" => "webmaster@example.com",
 
142
        "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))',
 
143
        "docs" => "http://backend.userland.com/rss091",
 
144
        "skipDays" => [
 
145
          "Monday",
 
146
          "Friday",
 
147
        ],
 
148
        "skipHours" => [
 
149
          "12",
 
150
          "23",
 
151
        ],
 
152
        "date" => Time.now,
 
153
        "lastBuildDate" => Time.now - 3600,
 
154
        "generator" => "RSS Maker",
 
155
        "ttl" => "60",
 
156
        "cloud" => {
 
157
          "domain" => "rpc.sys.com",
 
158
          "port" => "80",
 
159
          "path" => "/RPC2",
 
160
          "registerProcedure" => "myCloud.rssPleaseNotify",
 
161
          "protocol" => "xml-rpc",
 
162
        },
 
163
        "category" => {
 
164
          "domain" => "http://example.com/misc/",
 
165
          "content" => "misc",
 
166
        },
 
167
 
 
168
        "image" => {
 
169
          "resource" => @image_url,
 
170
        },
 
171
 
 
172
        "textinput" => {
 
173
          "resource" => @textinput_link,
 
174
        },
 
175
 
 
176
        "items" => @item_links.collect{|link| {"resource" => link}},
 
177
      }
 
178
    end
 
179
 
 
180
    def setup_item_infos
 
181
      @item_infos = [
 
182
        {
 
183
          "title" => "Sample item1",
 
184
          "link" => @item_links[0],
 
185
          "description" => "Sample description1",
 
186
          "date" => Time.now - 3600,
 
187
          "author" => "foo@example.com",
 
188
          "comments" => "http://example.com/1/comments",
 
189
          "guid" => {
 
190
            "isPermaLink" => "true",
 
191
            "content" => "http://example.com/1",
 
192
          },
 
193
          "enclosure" => {
 
194
            "url" => "http://example.com/1.mp3",
 
195
            "length" => "100",
 
196
            "type" => "audio/mpeg",
 
197
          },
 
198
          "source" => {
 
199
            "url" => "http:/example.com/",
 
200
            "content" => "Sample site",
 
201
          },
 
202
          "category" => {
 
203
            "domain" => "http://example.com/misc/",
 
204
            "content" => "misc",
 
205
          },
 
206
        },
 
207
 
 
208
        {
 
209
          "title" => "Sample item2",
 
210
          "link" => @item_links[1],
 
211
          "description" => "Sample description2",
 
212
          "date" => Time.now - 7200,
 
213
          "author" => "foo@example.com",
 
214
          "comments" => "http://example.com/2/comments",
 
215
          "guid" => {
 
216
            "isPermaLink" => "false",
 
217
            "content" => "http://example.com/2",
 
218
          },
 
219
          "enclosure" => {
 
220
            "url" => "http://example.com/2.mp3",
 
221
            "length" => "200",
 
222
            "type" => "audio/mpeg",
 
223
          },
 
224
          "source" => {
 
225
            "url" => "http:/example.com/",
 
226
            "content" => "Sample site",
 
227
          },
 
228
          "category" => {
 
229
            "domain" => "http://example.com/misc/",
 
230
            "content" => "misc",
 
231
          },
 
232
        },
 
233
      ]
 
234
    end
 
235
 
 
236
    def setup_image_info
 
237
      @image_info = {
 
238
        "title" => "Sample image",
 
239
        "url" => @image_url,
 
240
        "width" => "88",
 
241
        "height" => "31",
 
242
        "description" => "Sample",
 
243
      }
 
244
    end
 
245
 
 
246
    def setup_textinput_info
 
247
      @textinput_info = {
 
248
        "title" => "Sample textinput",
 
249
        "description" => "Search",
 
250
        "name" => "key",
 
251
        "link" => @textinput_link,
 
252
      }
 
253
    end
 
254
 
 
255
    def setup_dublin_core_info
 
256
      @dc_info = {
 
257
        "title" => "DC title",
 
258
        "description" => "DC desc",
 
259
        "creator" => "DC creator",
 
260
        "subject" => "DC subject",
 
261
        "publisher" => "DC publisher",
 
262
        "contributor" => "DC contributor",
 
263
        "type" => "DC type",
 
264
        "format" => "DC format",
 
265
        "identifier" => "DC identifier",
 
266
        "source" => "DC source",
 
267
        "language" => "ja",
 
268
        "relation" => "DC relation",
 
269
        "coverage" => "DC coverage",
 
270
        "rights" => "DC rights",
 
271
        "date" => Time.now - 60,
 
272
      }
 
273
    end
 
274
 
 
275
    def setup_syndication_info
 
276
      @sy_info = {
 
277
        "updatePeriod" => "hourly",
 
278
        "updateFrequency" => "2",
 
279
        "updateBase" => Time.now - 3600,
 
280
      }
 
281
    end
 
282
    
 
283
    def setup_content_info
 
284
      @content_info = {
 
285
        "encoded" => "<p>p</p>",
 
286
      }
 
287
    end
 
288
    
 
289
    def setup_trackback_info
 
290
      @trackback_info = {
 
291
        "ping" => "http://example.com/tb.cgi?tb_id=XXX",
 
292
        "abouts" => [
 
293
          "http://example.net/tb.cgi?tb_id=YYY",
 
294
          "http://example.org/tb.cgi?tb_id=ZZZ",
 
295
        ]
 
296
      }
 
297
    end
 
298
 
 
299
 
 
300
    def setup_full(maker)
 
301
      setup_xml_declaration(maker)
 
302
      setup_xml_stylesheets(maker)
 
303
      setup_channel(maker)
 
304
      setup_image(maker)
 
305
      setup_items(maker)
 
306
      setup_textinput(maker)
 
307
    end
 
308
 
 
309
    def setup_xml_declaration(maker)
 
310
      %w(version encoding standalone).each do |name|
 
311
        maker.__send__("#{name}=", instance_eval("@#{name}"))
 
312
      end
 
313
    end
 
314
 
 
315
    def setup_xml_stylesheets(maker)
 
316
      @xs_infos.each do |info|
 
317
        xs = maker.xml_stylesheets.new_xml_stylesheet
 
318
        info.each do |name, value|
 
319
          xs.__send__("#{name}=", value)
 
320
        end
 
321
      end
 
322
    end
 
323
 
 
324
    def setup_channel(maker)
 
325
      channel = maker.channel
 
326
      info = @channel_info
 
327
      
 
328
      %w(about title link description language copyright
 
329
         managingEditor webMaster rating docs date
 
330
         lastBuildDate generator ttl).each do |name|
 
331
        channel.__send__("#{name}=", info[name])
 
332
      end
 
333
 
 
334
      skipDays = channel.skipDays
 
335
      info["skipDays"].each do |day|
 
336
        new_day = skipDays.new_day
 
337
        new_day.content = day
 
338
      end
 
339
 
 
340
      skipHours = channel.skipHours
 
341
      info["skipHours"].each do |hour|
 
342
        new_hour = skipHours.new_hour
 
343
        new_hour.content = hour
 
344
      end
 
345
      
 
346
      cloud = channel.cloud
 
347
      %w(domain port path registerProcedure protocol).each do |name|
 
348
        cloud.__send__("#{name}=", info["cloud"][name])
 
349
      end
 
350
 
 
351
      category = channel.categories.new_category
 
352
      %w(domain content).each do |name|
 
353
        category.__send__("#{name}=", info["category"][name])
 
354
      end
 
355
    end
 
356
    
 
357
    def setup_image(maker)
 
358
      image = maker.image
 
359
      info = @image_info
 
360
 
 
361
      %w(title url width height description).each do |name|
 
362
        image.__send__("#{name}=", info[name])
 
363
      end
 
364
    end
 
365
    
 
366
    def setup_items(maker)
 
367
      items = maker.items
 
368
 
 
369
      @item_infos.each do |info|
 
370
        item = items.new_item
 
371
        %w(title link description date author comments).each do |name|
 
372
          item.__send__("#{name}=", info[name])
 
373
        end
 
374
        
 
375
        guid = item.guid
 
376
        %w(isPermaLink content).each do |name|
 
377
          guid.__send__("#{name}=", info["guid"][name])
 
378
        end
 
379
 
 
380
        enclosure = item.enclosure
 
381
        %w(url length type).each do |name|
 
382
          enclosure.__send__("#{name}=", info["enclosure"][name])
 
383
        end
 
384
 
 
385
        source = item.source
 
386
        %w(url content).each do |name|
 
387
          source.__send__("#{name}=", info["source"][name])
 
388
        end
 
389
 
 
390
        category = item.categories.new_category
 
391
        %w(domain content).each do |name|
 
392
          category.__send__("#{name}=", info["category"][name])
 
393
        end
 
394
 
 
395
        setup_trackback(item)
 
396
      end
 
397
    end
 
398
 
 
399
    def setup_textinput(maker)
 
400
      textinput = maker.textinput
 
401
      info = @textinput_info
 
402
 
 
403
      %w(title description name link).each do |name|
 
404
        textinput.__send__("#{name}=", info[name])
 
405
      end
 
406
    end
 
407
 
 
408
    def setup_content(target)
 
409
      prefix = "content"
 
410
      %w(encoded).each do |name|
 
411
        target.__send__("#{prefix}_#{name}=", @content_info[name])
 
412
      end
 
413
    end
 
414
    
 
415
    def setup_dublin_core(target)
 
416
      prefix = "dc"
 
417
      %w(title description creator subject publisher
 
418
        contributor type format identifier source language
 
419
        relation coverage rights).each do |name|
 
420
        target.__send__("#{prefix}_#{name}=", @dc_info[name])
 
421
      end
 
422
    end
 
423
    
 
424
    def setup_syndicate(target)
 
425
      prefix = "sy"
 
426
      %w(updatePeriod updateFrequency updateBase).each do |name|
 
427
        target.__send__("#{prefix}_#{name}=", @sy_info[name])
 
428
      end
 
429
    end
 
430
    
 
431
    def setup_trackback(target)
 
432
      target.trackback_ping = @trackback_info["ping"]
 
433
      @trackback_info["abouts"].each do |about|
 
434
        new_about = target.trackback_abouts.new_about
 
435
        new_about.value = about
 
436
      end
 
437
    end
 
438
    
 
439
  end
 
440
end