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

« back to all changes in this revision

Viewing changes to test/rss/test_atom.rb

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Klose
  • Date: 2007-09-04 16:01:17 UTC
  • mfrom: (1.1.8 upstream)
  • Revision ID: james.westby@ubuntu.com-20070904160117-i15zckg2nhxe9fyw
Tags: 1.9.0+20070830-2ubuntu1
* Sync from Debian; remaining changes:
  - Add -g to CFLAGS.
* Fixes build failure on ia64.
* Fixes build failure with gcc-4.2 on lpia.
* Robustify check for target_os, fixing build failure on lpia.
* Set Ubuntu maintainer address.

Show diffs side-by-side

added added

removed removed

Lines of Context:
217
217
    end
218
218
 
219
219
 
220
 
    def test_to_xml
 
220
    def test_to_xml(with_convenience_way=true)
221
221
      atom = RSS::Parser.parse(make_feed)
222
222
      assert_equal(atom.to_s, atom.to_xml)
223
223
      assert_equal(atom.to_s, atom.to_xml("atom"))
228
228
      rss09_xml = atom.to_xml("0.91") do |maker|
229
229
        maker.channel.language = "en-us"
230
230
        maker.channel.link = "http://example.com/"
231
 
        maker.channel.description.content = atom.title.content
 
231
        if with_convenience_way
 
232
          maker.channel.description = atom.title.content
 
233
        else
 
234
          maker.channel.description {|d| d.content = atom.title.content}
 
235
        end
232
236
 
233
237
        maker.image.url = "http://example.com/logo.png"
234
238
        maker.image.title = "Logo"
238
242
 
239
243
      rss20_xml = atom.to_xml("2.0") do |maker|
240
244
        maker.channel.link = "http://example.com/"
241
 
        maker.channel.description.content = atom.title.content
 
245
        if with_convenience_way
 
246
          maker.channel.description = atom.title.content
 
247
        else
 
248
          maker.channel.description {|d| d.content = atom.title.content}
 
249
        end
242
250
      end
243
251
      rss20 = RSS::Parser.parse(rss20_xml)
244
252
      assert_equal("2.0", rss20.rss_version)
245
253
      assert_equal(["rss", "2.0", nil], rss20.feed_info)
246
254
    end
247
255
 
 
256
    def test_to_xml_with_new_api_since_018
 
257
      test_to_xml(false)
 
258
    end
 
259
 
248
260
    private
249
261
    def setup_entry(entry)
250
262
      _wrap_assertion do
277
289
        assert_not_equal("", entry.to_s)
278
290
      end
279
291
    end
 
292
 
 
293
 
 
294
    def assert_atom_person_to_s(target_class)
 
295
      _wrap_assertion do
 
296
        name = "A person"
 
297
        uri = "http://example.com/person/"
 
298
        email = "person@example.com"
 
299
 
 
300
        target = target_class.new
 
301
        assert_equal("", target.to_s)
 
302
 
 
303
        target = target_class.new
 
304
        person_name = target_class::Name.new
 
305
        person_name.content = name
 
306
        target.name = person_name
 
307
        xml_target = REXML::Document.new(target.to_s).root
 
308
        assert_equal(["name"], xml_target.elements.collect {|e| e.name})
 
309
        assert_equal([name], xml_target.elements.collect {|e| e.text})
 
310
 
 
311
        person_uri = target_class::Uri.new
 
312
        person_uri.content = uri
 
313
        target.uri = person_uri
 
314
        xml_target = REXML::Document.new(target.to_s).root
 
315
        assert_equal(["name", "uri"], xml_target.elements.collect {|e| e.name})
 
316
        assert_equal([name, uri], xml_target.elements.collect {|e| e.text})
 
317
 
 
318
        person_email = target_class::Email.new
 
319
        person_email.content = email
 
320
        target.email = person_email
 
321
        xml_target = REXML::Document.new(target.to_s).root
 
322
        assert_equal(["name", "uri", "email"],
 
323
                     xml_target.elements.collect {|e| e.name})
 
324
        assert_equal([name, uri, email],
 
325
                     xml_target.elements.collect {|e| e.text})
 
326
      end
 
327
    end
 
328
 
 
329
    def assert_atom_category_to_s(target_class)
 
330
      _wrap_assertion do
 
331
        term = "music"
 
332
        scheme = "http://example.com/music"
 
333
        label = "Music"
 
334
 
 
335
        category = target_class.new
 
336
        assert_equal("", category.to_s)
 
337
 
 
338
        category = target_class.new
 
339
        category.scheme = scheme
 
340
        assert_equal("", category.to_s)
 
341
 
 
342
        category = target_class.new
 
343
        category.label = label
 
344
        assert_equal("", category.to_s)
 
345
 
 
346
        category = target_class.new
 
347
        category.scheme = scheme
 
348
        category.label = label
 
349
        assert_equal("", category.to_s)
 
350
 
 
351
        category = target_class.new
 
352
        category.term = term
 
353
        xml = REXML::Document.new(category.to_s).root
 
354
        assert_rexml_element([], {"term" => term}, nil, xml)
 
355
 
 
356
        category = target_class.new
 
357
        category.term = term
 
358
        category.scheme = scheme
 
359
        xml = REXML::Document.new(category.to_s).root
 
360
        assert_rexml_element([], {"term" => term, "scheme" => scheme}, nil, xml)
 
361
 
 
362
        category = target_class.new
 
363
        category.term = term
 
364
        category.label = label
 
365
        xml = REXML::Document.new(category.to_s).root
 
366
        assert_rexml_element([], {"term" => term, "label" => label}, nil, xml)
 
367
 
 
368
        category = target_class.new
 
369
        category.term = term
 
370
        category.scheme = scheme
 
371
        category.label = label
 
372
        xml = REXML::Document.new(category.to_s).root
 
373
        attrs = {"term" => term, "scheme" => scheme, "label" => label}
 
374
        assert_rexml_element([], attrs, nil, xml)
 
375
      end
 
376
    end
 
377
 
 
378
    def assert_atom_generator_to_s(target_class)
 
379
      _wrap_assertion do
 
380
        content = "Feed generator"
 
381
        uri = "http://example.com/generator"
 
382
        version = "0.0.1"
 
383
 
 
384
        generator = target_class.new
 
385
        assert_equal("", generator.to_s)
 
386
 
 
387
        generator = target_class.new
 
388
        generator.uri = uri
 
389
        assert_equal("", generator.to_s)
 
390
 
 
391
        generator = target_class.new
 
392
        generator.version = version
 
393
        assert_equal("", generator.to_s)
 
394
 
 
395
        generator = target_class.new
 
396
        generator.uri = uri
 
397
        generator.version = version
 
398
        assert_equal("", generator.to_s)
 
399
 
 
400
        generator = target_class.new
 
401
        generator.content = content
 
402
        xml = REXML::Document.new(generator.to_s).root
 
403
        assert_rexml_element([], {}, content, xml)
 
404
 
 
405
        generator = target_class.new
 
406
        generator.content = content
 
407
        generator.uri = uri
 
408
        xml = REXML::Document.new(generator.to_s).root
 
409
        assert_rexml_element([], {"uri" => uri}, content, xml)
 
410
 
 
411
        generator = target_class.new
 
412
        generator.content = content
 
413
        generator.version = version
 
414
        xml = REXML::Document.new(generator.to_s).root
 
415
        assert_rexml_element([], {"version" => version}, content, xml)
 
416
 
 
417
        generator = target_class.new
 
418
        generator.content = content
 
419
        generator.uri = uri
 
420
        generator.version = version
 
421
        xml = REXML::Document.new(generator.to_s).root
 
422
        assert_rexml_element([], {"uri" => uri, "version" => version},
 
423
                             content, xml)
 
424
      end
 
425
    end
 
426
 
 
427
    def assert_atom_icon_to_s(target_class)
 
428
      _wrap_assertion do
 
429
        content = "http://example.com/icon.png"
 
430
 
 
431
        icon = target_class.new
 
432
        assert_equal("", icon.to_s)
 
433
 
 
434
        icon = target_class.new
 
435
        icon.content = content
 
436
        xml = REXML::Document.new(icon.to_s).root
 
437
        assert_rexml_element([], {}, content, xml)
 
438
      end
 
439
    end
 
440
 
 
441
    def assert_atom_id_to_s(target_class)
 
442
      _wrap_assertion do
 
443
        content = "http://example.com/1"
 
444
 
 
445
        id = target_class.new
 
446
        assert_equal("", id.to_s)
 
447
 
 
448
        id = target_class.new
 
449
        id.content = content
 
450
        xml = REXML::Document.new(id.to_s).root
 
451
        assert_rexml_element([], {}, content, xml)
 
452
      end
 
453
    end
 
454
 
 
455
    def assert_atom_link_to_s(target_class)
 
456
      _wrap_assertion do
 
457
        href = "http://example.com/atom.xml"
 
458
        rel = "self"
 
459
        type = "application/atom+xml"
 
460
        hreflang = "ja"
 
461
        title = "Atom Feed"
 
462
        length = "801"
 
463
 
 
464
        link = target_class.new
 
465
        assert_equal("", link.to_s)
 
466
 
 
467
        link = target_class.new
 
468
        link.href = href
 
469
        xml = REXML::Document.new(link.to_s).root
 
470
        assert_rexml_element([], {"href" => href}, nil, xml)
 
471
 
 
472
        optional_arguments = %w(rel type hreflang title length)
 
473
        optional_arguments.each do |name|
 
474
          rest = optional_arguments.reject {|x| x == name}
 
475
 
 
476
          link = target_class.new
 
477
          link.__send__("#{name}=", eval(name))
 
478
          assert_equal("", link.to_s)
 
479
 
 
480
          rest.each do |n|
 
481
            link.__send__("#{n}=", eval(n))
 
482
            assert_equal("", link.to_s)
 
483
          end
 
484
 
 
485
          link = target_class.new
 
486
          link.href = href
 
487
          link.__send__("#{name}=", eval(name))
 
488
          attrs = [["href", href], [name, eval(name)]]
 
489
          xml = REXML::Document.new(link.to_s).root
 
490
          assert_rexml_element([], attrs, nil, xml)
 
491
 
 
492
          rest.each do |n|
 
493
            link.__send__("#{n}=", eval(n))
 
494
            attrs << [n, eval(n)]
 
495
            xml = REXML::Document.new(link.to_s).root
 
496
            assert_rexml_element([], attrs, nil, xml)
 
497
          end
 
498
        end
 
499
      end
 
500
    end
 
501
 
 
502
    def assert_atom_logo_to_s(target_class)
 
503
      _wrap_assertion do
 
504
        content = "http://example.com/logo.png"
 
505
 
 
506
        logo = target_class.new
 
507
        assert_equal("", logo.to_s)
 
508
 
 
509
        logo = target_class.new
 
510
        logo.content = content
 
511
        xml = REXML::Document.new(logo.to_s).root
 
512
        assert_rexml_element([], {}, content, xml)
 
513
      end
 
514
    end
 
515
 
 
516
    def assert_atom_text_construct_to_s(target_class)
 
517
      _wrap_assertion do
 
518
        text_content = "plain text"
 
519
        html_content = "<em>#{text_content}</em>"
 
520
        xhtml_uri = "http://www.w3.org/1999/xhtml"
 
521
        xhtml_em = RSS::XML::Element.new("em", nil, xhtml_uri, {}, text_content)
 
522
        xhtml_content = RSS::XML::Element.new("div", nil, xhtml_uri,
 
523
                                              {"xmlns" => xhtml_uri},
 
524
                                              [xhtml_em])
 
525
 
 
526
        text = target_class.new
 
527
        assert_equal("", text.to_s)
 
528
 
 
529
        text = target_class.new
 
530
        text.type = "text"
 
531
        assert_equal("", text.to_s)
 
532
 
 
533
        text = target_class.new
 
534
        text.content = text_content
 
535
        xml = REXML::Document.new(text.to_s).root
 
536
        assert_rexml_element([], {}, text_content, xml)
 
537
 
 
538
        text = target_class.new
 
539
        text.type = "text"
 
540
        text.content = text_content
 
541
        xml = REXML::Document.new(text.to_s).root
 
542
        assert_rexml_element([], {"type" => "text"}, text_content, xml)
 
543
 
 
544
        text = target_class.new
 
545
        text.type = "html"
 
546
        text.content = html_content
 
547
        xml = REXML::Document.new(text.to_s).root
 
548
        assert_rexml_element([], {"type" => "html"}, html_content, xml)
 
549
 
 
550
        text = target_class.new
 
551
        text.type = "xhtml"
 
552
        text.content = xhtml_content
 
553
        assert_equal("", text.to_s)
 
554
 
 
555
        text = target_class.new
 
556
        text.type = "xhtml"
 
557
        text.__send__(target_class.xml_setter, xhtml_content)
 
558
        xml = REXML::Document.new(text.to_s).root
 
559
        assert_rexml_element([[xhtml_uri, "div"]], {"type" => "xhtml"},
 
560
                             nil, xml)
 
561
        assert_rexml_element([[xhtml_uri, "em"]], nil, nil, xml.elements[1])
 
562
        assert_rexml_element([], {}, text_content, xml.elements[1].elements[1])
 
563
 
 
564
        text = target_class.new
 
565
        text.type = "xhtml"
 
566
        text.__send__(target_class.xml_setter, xhtml_em)
 
567
        xml = REXML::Document.new(text.to_s).root
 
568
        assert_rexml_element([[xhtml_uri, "div"]], {"type" => "xhtml"},
 
569
                             nil, xml)
 
570
        assert_rexml_element([[xhtml_uri, "em"]], nil, nil, xml.elements[1])
 
571
        assert_rexml_element([], {}, text_content, xml.elements[1].elements[1])
 
572
      end
 
573
    end
 
574
 
 
575
    def assert_atom_date_construct_to_s(target_class)
 
576
      _wrap_assertion do
 
577
        date = target_class.new
 
578
        assert_equal("", date.to_s)
 
579
 
 
580
        [
 
581
         "2003-12-13T18:30:02Z",
 
582
         "2003-12-13T18:30:02.25Z",
 
583
         "2003-12-13T18:30:02+01:00",
 
584
         "2003-12-13T18:30:02.25+01:00",
 
585
        ].each do |content|
 
586
          date = target_class.new
 
587
          date.content = content
 
588
          xml = REXML::Document.new(date.to_s).root
 
589
          assert_rexml_element([], {}, content, xml, :time)
 
590
 
 
591
          date = target_class.new
 
592
          date.content = Time.parse(content)
 
593
          xml = REXML::Document.new(date.to_s).root
 
594
          assert_rexml_element([], {}, content, xml, :time)
 
595
        end
 
596
      end
 
597
    end
 
598
 
 
599
    def assert_atom_content_to_s(target_class)
 
600
      _wrap_assertion do
 
601
        assert_atom_text_construct_to_s(target_class)
 
602
        assert_atom_content_inline_other_xml_to_s(target_class)
 
603
        assert_atom_content_inline_other_text_to_s(target_class)
 
604
        assert_atom_content_inline_other_base64_to_s(target_class)
 
605
        assert_atom_content_out_of_line_to_s(target_class)
 
606
      end
 
607
    end
 
608
 
 
609
    def assert_atom_content_inline_other_xml_to_s(target_class)
 
610
      _wrap_assertion do
 
611
        content = target_class.new
 
612
        content.type = "text/xml"
 
613
        assert_equal("", content.to_s)
 
614
 
 
615
        content = target_class.new
 
616
        content.type = "text/xml"
 
617
        content.xml = RSS::XML::Element.new("em")
 
618
        xml = REXML::Document.new(content.to_s).root
 
619
        assert_rexml_element([["", "em"]], {"type" => "text/xml"}, nil, xml)
 
620
      end
 
621
    end
 
622
 
 
623
    def assert_atom_content_inline_other_text_to_s(target_class)
 
624
      _wrap_assertion do
 
625
        content = target_class.new
 
626
        content.type = "text/plain"
 
627
        assert_equal("", content.to_s)
 
628
 
 
629
        content = target_class.new
 
630
        content.type = "text/plain"
 
631
        content.xml = RSS::XML::Element.new("em")
 
632
        assert_equal("", content.to_s)
 
633
 
 
634
        content = target_class.new
 
635
        content.type = "text/plain"
 
636
        content.content = "content"
 
637
        xml = REXML::Document.new(content.to_s).root
 
638
        assert_rexml_element([], {"type" => "text/plain"}, "content", xml)
 
639
      end
 
640
    end
 
641
 
 
642
    def assert_atom_content_inline_other_base64_to_s(target_class)
 
643
      _wrap_assertion do
 
644
        require "zlib"
 
645
 
 
646
        text = ""
 
647
        char = "a"
 
648
        100.times do |i|
 
649
          text << char
 
650
          char.succ!
 
651
        end
 
652
 
 
653
        type = "application/zip"
 
654
        original_content = Zlib::Deflate.deflate(text)
 
655
 
 
656
        content = target_class.new
 
657
        content.type = type
 
658
        content.content = original_content
 
659
        xml = REXML::Document.new(content.to_s).root
 
660
        assert_rexml_element([], {"type" => type},
 
661
                             Base64.encode64(original_content), xml)
 
662
      end
 
663
    end
 
664
 
 
665
    def assert_atom_content_out_of_line_to_s(target_class)
 
666
      _wrap_assertion do
 
667
        type = "application/zip"
 
668
        src = "http://example.com/xxx.zip"
 
669
 
 
670
        content = target_class.new
 
671
        assert(!content.out_of_line?)
 
672
        content.src = src
 
673
        assert(content.out_of_line?)
 
674
        xml = REXML::Document.new(content.to_s).root
 
675
        assert_rexml_element([], {"src" => src}, nil, xml)
 
676
 
 
677
        content = target_class.new
 
678
        assert(!content.out_of_line?)
 
679
        content.type = type
 
680
        assert(!content.out_of_line?)
 
681
        content.src = src
 
682
        assert(content.out_of_line?)
 
683
        xml = REXML::Document.new(content.to_s).root
 
684
        assert_rexml_element([], {"type" => type, "src" => src}, nil, xml)
 
685
      end
 
686
    end
280
687
  end
281
688
end