summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author(no author) <(no author)@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2004-11-29 06:22:42 +0000
committer(no author) <(no author)@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2004-11-29 06:22:42 +0000
commit9a851dc15725a8a5fdd91648814bf1bf6ce10fb6 (patch)
treed402587b31c0ad2d7243a9a3c2f41f6132c1d542
parentf6348ca0eac1eb3fd89f1bce233204b096de3e98 (diff)
This commit was manufactured by cvs2svn to create branch 'ruby_1_8'.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_1_8@7415 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rwxr-xr-xsample/rss/blend.rb76
-rw-r--r--test/io/nonblock/test_flush.rb26
-rw-r--r--test/rss/test_setup_maker_0.9.rb221
-rw-r--r--test/rss/test_setup_maker_1.0.rb276
-rw-r--r--test/rss/test_setup_maker_2.0.rb295
-rw-r--r--test/rss/test_to_s.rb440
6 files changed, 1334 insertions, 0 deletions
diff --git a/sample/rss/blend.rb b/sample/rss/blend.rb
new file mode 100755
index 0000000000..2aa30d7fc9
--- /dev/null
+++ b/sample/rss/blend.rb
@@ -0,0 +1,76 @@
+#!/usr/bin/env ruby
+
+require "rss/1.0"
+require "rss/2.0"
+require "rss/dublincore"
+require "rss/maker"
+
+feeds = []
+verbose = false
+encoding = "UTF-8"
+
+def error(exception)
+ mark = "=" * 20
+ mark = "#{mark} error #{mark}"
+ STDERR.puts mark
+ STDERR.puts exception.class
+ STDERR.puts exception.message
+ STDERR.puts exception.backtrace
+ STDERR.puts mark
+end
+
+before_time = Time.now
+ARGV.each do |fname|
+ if fname == '-v'
+ verbose = true
+ next
+ end
+ rss = nil
+ f = File.new(fname).read
+ begin
+ ## do validate parse
+ rss = RSS::Parser.parse(f)
+ rescue RSS::InvalidRSSError
+ error($!) if verbose
+ ## do non validate parse for invalid RSS 1.0
+ begin
+ rss = RSS::Parser.parse(f, false)
+ rescue RSS::Error
+ ## invalid RSS.
+ error($!) if verbose
+ end
+ rescue RSS::Error
+ error($!) if verbose
+ end
+ if rss.nil?
+ STDERR.puts "#{fname} does not include RSS 1.0 or 0.9x/2.0"
+ else
+ begin
+ rss.output_encoding = encoding
+ rescue RSS::UnknownConversionMethodError
+ error($!) if verbose
+ end
+ feeds << rss
+ end
+end
+processing_time = Time.now - before_time
+
+rss = RSS::Maker.make("1.0") do |maker|
+ maker.encoding = encoding
+ maker.channel.about = "http://example.com/blend.rdf"
+ maker.channel.title = "blended feeds"
+ maker.channel.link = "http://example.com/"
+ maker.channel.description = "blended feeds generated by RSS Parser"
+
+ feeds.each do |feed|
+ feed.items.each do |item|
+ item.setup_maker(maker)
+ end
+ end
+ maker.items.do_sort = true
+ maker.items.max_size = 15
+end
+puts rss
+
+STDERR.puts "Used XML parser: #{RSS::Parser.default_parser}"
+STDERR.puts "Processing time: #{processing_time}s"
diff --git a/test/io/nonblock/test_flush.rb b/test/io/nonblock/test_flush.rb
new file mode 100644
index 0000000000..dbe20722cf
--- /dev/null
+++ b/test/io/nonblock/test_flush.rb
@@ -0,0 +1,26 @@
+require 'test/unit'
+require 'io/nonblock'
+
+class TestIONonblock < Test::Unit::TestCase
+ def test_flush # [ruby-dev:24985]
+ r,w = IO.pipe
+ w.nonblock = true
+ w.sync = false
+ w << "b"
+ w.flush
+ w << "a" * 4096
+ Thread.new {
+ Thread.pass
+ w.close
+ }
+ result = ""
+ t = Thread.new {
+ while (Thread.pass; s = r.read(4096))
+ result << s
+ end
+ }
+ assert_raise(IOError) {w.flush}
+ t.join
+ assert_equal("b", result)
+ end
+end
diff --git a/test/rss/test_setup_maker_0.9.rb b/test/rss/test_setup_maker_0.9.rb
new file mode 100644
index 0000000000..e0f26d5593
--- /dev/null
+++ b/test/rss/test_setup_maker_0.9.rb
@@ -0,0 +1,221 @@
+require "rss-testcase"
+
+require "rss/maker"
+
+module RSS
+ class TestSetupMaker09 < TestCase
+
+ def test_setup_maker_channel
+ title = "fugafuga"
+ link = "http://hoge.com"
+ description = "fugafugafugafuga"
+ language = "ja"
+ copyright = "foo"
+ managingEditor = "bar"
+ webMaster = "web master"
+ rating = "6"
+ docs = "http://foo.com/doc"
+ skipDays = [
+ "Sunday",
+ "Monday",
+ ]
+ skipHours = [
+ 0,
+ 13,
+ ]
+ pubDate = Time.now
+ lastBuildDate = Time.now
+
+ rss = RSS::Maker.make("0.91") do |maker|
+ maker.channel.title = title
+ maker.channel.link = link
+ maker.channel.description = description
+ maker.channel.language = language
+ maker.channel.copyright = copyright
+ maker.channel.managingEditor = managingEditor
+ maker.channel.webMaster = webMaster
+ maker.channel.rating = rating
+ maker.channel.docs = docs
+ maker.channel.pubDate = pubDate
+ maker.channel.lastBuildDate = lastBuildDate
+
+ skipDays.each do |day|
+ new_day = maker.channel.skipDays.new_day
+ new_day.content = day
+ end
+ skipHours.each do |hour|
+ new_hour = maker.channel.skipHours.new_hour
+ new_hour.content = hour
+ end
+ end
+
+ new_rss = RSS::Maker.make("0.91") do |maker|
+ rss.channel.setup_maker(maker)
+ end
+ channel = new_rss.channel
+
+ assert_equal(title, channel.title)
+ assert_equal(link, channel.link)
+ assert_equal(description, channel.description)
+ assert_equal(language, channel.language)
+ assert_equal(copyright, channel.copyright)
+ assert_equal(managingEditor, channel.managingEditor)
+ assert_equal(webMaster, channel.webMaster)
+ assert_equal(rating, channel.rating)
+ assert_equal(docs, channel.docs)
+ assert_equal(pubDate, channel.pubDate)
+ assert_equal(lastBuildDate, channel.lastBuildDate)
+
+ skipDays.each_with_index do |day, i|
+ assert_equal(day, channel.skipDays.days[i].content)
+ end
+ skipHours.each_with_index do |hour, i|
+ assert_equal(hour, channel.skipHours.hours[i].content)
+ end
+
+ assert(channel.items.empty?)
+ assert_nil(channel.image)
+ assert_nil(channel.textInput)
+ end
+
+ def test_setup_maker_image
+ title = "fugafuga"
+ link = "http://hoge.com"
+ url = "http://hoge.com/hoge.png"
+ width = 144
+ height = 400
+ description = "an image"
+
+ rss = RSS::Maker.make("0.91") do |maker|
+ setup_dummy_channel(maker)
+ maker.channel.link = link
+
+ maker.image.title = title
+ maker.image.url = url
+ maker.image.width = width
+ maker.image.height = height
+ maker.image.description = description
+ end
+
+ new_rss = RSS::Maker.make("0.91") do |maker|
+ rss.channel.setup_maker(maker)
+ rss.image.setup_maker(maker)
+ end
+
+ image = new_rss.image
+ assert_equal(title, image.title)
+ assert_equal(link, image.link)
+ assert_equal(url, image.url)
+ assert_equal(width, image.width)
+ assert_equal(height, image.height)
+ assert_equal(description, image.description)
+ end
+
+ def test_setup_maker_textinput
+ title = "fugafuga"
+ description = "text hoge fuga"
+ name = "hoge"
+ link = "http://hoge.com"
+
+ rss = RSS::Maker.make("0.91") do |maker|
+ setup_dummy_channel(maker)
+
+ maker.textinput.title = title
+ maker.textinput.description = description
+ maker.textinput.name = name
+ maker.textinput.link = link
+ end
+
+ new_rss = RSS::Maker.make("0.91") do |maker|
+ rss.channel.setup_maker(maker)
+ rss.textinput.setup_maker(maker)
+ end
+
+ textInput = new_rss.channel.textInput
+ assert_equal(title, textInput.title)
+ assert_equal(description, textInput.description)
+ assert_equal(name, textInput.name)
+ assert_equal(link, textInput.link)
+ end
+
+ def test_setup_maker_items
+ title = "TITLE"
+ link = "http://hoge.com/"
+ description = "text hoge fuga"
+
+ item_size = 5
+
+ rss = RSS::Maker.make("0.91") do |maker|
+ setup_dummy_channel(maker)
+
+ item_size.times do |i|
+ item = maker.items.new_item
+ item.title = "#{title}#{i}"
+ item.link = "#{link}#{i}"
+ item.description = "#{description}#{i}"
+ end
+ end
+
+ new_rss = RSS::Maker.make("0.91") do |maker|
+ rss.channel.setup_maker(maker)
+
+ rss.items.each do |item|
+ item.setup_maker(maker)
+ end
+ end
+
+ assert_equal(item_size, new_rss.items.size)
+ new_rss.items.each_with_index do |item, i|
+ assert_equal("#{title}#{i}", item.title)
+ assert_equal("#{link}#{i}", item.link)
+ assert_equal("#{description}#{i}", item.description)
+ end
+
+ end
+
+ def test_setup_maker
+ encoding = "EUC-JP"
+ standalone = true
+
+ href = 'a.xsl'
+ type = 'text/xsl'
+ title = 'sample'
+ media = 'printer'
+ charset = 'UTF-8'
+ alternate = 'yes'
+
+ rss = RSS::Maker.make("0.91") do |maker|
+ maker.encoding = encoding
+ maker.standalone = standalone
+
+ xss = maker.xml_stylesheets.new_xml_stylesheet
+ xss.href = href
+ xss.type = type
+ xss.title = title
+ xss.media = media
+ xss.charset = charset
+ xss.alternate = alternate
+
+ setup_dummy_channel(maker)
+ end
+
+ new_rss = RSS::Maker.make("0.91") do |maker|
+ rss.setup_maker(maker)
+ end
+
+ assert_equal("0.91", new_rss.rss_version)
+ assert_equal(encoding, new_rss.encoding)
+ assert_equal(standalone, new_rss.standalone)
+
+ xss = rss.xml_stylesheets.first
+ assert_equal(1, rss.xml_stylesheets.size)
+ assert_equal(href, xss.href)
+ assert_equal(type, xss.type)
+ assert_equal(title, xss.title)
+ assert_equal(media, xss.media)
+ assert_equal(charset, xss.charset)
+ assert_equal(alternate, xss.alternate)
+ end
+
+ end
+end
diff --git a/test/rss/test_setup_maker_1.0.rb b/test/rss/test_setup_maker_1.0.rb
new file mode 100644
index 0000000000..52557486f4
--- /dev/null
+++ b/test/rss/test_setup_maker_1.0.rb
@@ -0,0 +1,276 @@
+require "rss-testcase"
+
+require "rss/maker"
+
+module RSS
+ class TestSetupMaker10 < TestCase
+
+ def setup
+ t = Time.iso8601("2000-01-01T12:00:05+00:00")
+ class << t
+ alias_method(:to_s, :iso8601)
+ end
+
+ @dc_elems = {
+ :title => "hoge",
+ :description =>
+ " XML is placing increasingly heavy loads on
+ the existing technical infrastructure of the Internet.",
+ :creator => "Rael Dornfest (mailto:rael@oreilly.com)",
+ :subject => "XML",
+ :publisher => "The O'Reilly Network",
+ :contributor => "hogehoge",
+ :type => "fugafuga",
+ :format => "hohoho",
+ :identifier => "fufufu",
+ :source => "barbar",
+ :language => "ja",
+ :relation => "cococo",
+ :rights => "Copyright (c) 2000 O'Reilly &amp; Associates, Inc.",
+ :date => t,
+ }
+
+ @sy_elems = {
+ :updatePeriod => "hourly",
+ :updateFrequency => 2,
+ :updateBase => t,
+ }
+
+ @content_elems = {
+ :encoded => "<em>ATTENTION</em>",
+ }
+
+ @trackback_elems = {
+ :ping => "http://bar.com/tb.cgi?tb_id=rssplustrackback",
+ :about => [
+ "http://foo.com/trackback/tb.cgi?tb_id=20020923",
+ "http://foo.com/trackback/tb.cgi?tb_id=20021010",
+ ],
+ }
+ end
+
+ def test_setup_maker_channel
+ about = "http://hoge.com"
+ title = "fugafuga"
+ link = "http://hoge.com"
+ description = "fugafugafugafuga"
+
+ rss = RSS::Maker.make("1.0") do |maker|
+ maker.channel.about = about
+ maker.channel.title = title
+ maker.channel.link = link
+ maker.channel.description = description
+
+ @dc_elems.each do |var, value|
+ maker.channel.__send__("dc_#{var}=", value)
+ end
+
+ @sy_elems.each do |var, value|
+ maker.channel.__send__("sy_#{var}=", value)
+ end
+ end
+
+ new_rss = RSS::Maker.make("1.0") do |maker|
+ rss.channel.setup_maker(maker)
+ end
+ channel = new_rss.channel
+
+ assert_equal(about, channel.about)
+ assert_equal(title, channel.title)
+ assert_equal(link, channel.link)
+ assert_equal(description, channel.description)
+ assert_equal(true, channel.items.Seq.lis.empty?)
+ assert_nil(channel.image)
+ assert_nil(channel.textinput)
+
+ @dc_elems.each do |var, value|
+ assert_equal(channel.__send__("dc_#{var}"), value)
+ end
+
+ @sy_elems.each do |var, value|
+ assert_equal(channel.__send__("sy_#{var}"), value)
+ end
+
+ end
+
+ def test_setup_maker_image
+ title = "fugafuga"
+ link = "http://hoge.com"
+ url = "http://hoge.com/hoge.png"
+
+ rss = RSS::Maker.make("1.0") do |maker|
+ setup_dummy_channel(maker)
+ maker.channel.link = link
+
+ maker.image.title = title
+ maker.image.url = url
+
+ @dc_elems.each do |var, value|
+ maker.image.__send__("dc_#{var}=", value)
+ end
+ end
+
+ new_rss = RSS::Maker.make("1.0") do |maker|
+ rss.channel.setup_maker(maker)
+ rss.image.setup_maker(maker)
+ end
+
+ image = new_rss.image
+ assert_equal(url, image.about)
+ assert_equal(url, new_rss.channel.image.resource)
+ assert_equal(title, image.title)
+ assert_equal(link, image.link)
+ assert_equal(url, image.url)
+
+ @dc_elems.each do |var, value|
+ assert_equal(image.__send__("dc_#{var}"), value)
+ end
+ end
+
+ def test_setup_maker_textinput
+ title = "fugafuga"
+ description = "text hoge fuga"
+ name = "hoge"
+ link = "http://hoge.com"
+
+ rss = RSS::Maker.make("1.0") do |maker|
+ setup_dummy_channel(maker)
+
+ maker.textinput.link = link
+ maker.textinput.title = title
+ maker.textinput.description = description
+ maker.textinput.name = name
+
+ @dc_elems.each do |var, value|
+ maker.textinput.__send__("dc_#{var}=", value)
+ end
+ end
+
+ new_rss = RSS::Maker.make("1.0") do |maker|
+ rss.channel.setup_maker(maker)
+ rss.textinput.setup_maker(maker)
+ end
+
+ textinput = new_rss.textinput
+ assert_equal(link, textinput.about)
+ assert_equal(link, new_rss.channel.textinput.resource)
+ assert_equal(title, textinput.title)
+ assert_equal(name, textinput.name)
+ assert_equal(description, textinput.description)
+ assert_equal(link, textinput.link)
+
+ @dc_elems.each do |var, value|
+ assert_equal(textinput.__send__("dc_#{var}"), value)
+ end
+ end
+
+ def test_setup_maker_items
+ title = "TITLE"
+ link = "http://hoge.com/"
+ description = "text hoge fuga"
+
+ item_size = 5
+
+ rss = RSS::Maker.make("1.0") do |maker|
+ setup_dummy_channel(maker)
+
+ item_size.times do |i|
+ item = maker.items.new_item
+ item.title = "#{title}#{i}"
+ item.link = "#{link}#{i}"
+ item.description = "#{description}#{i}"
+
+ @dc_elems.each do |var, value|
+ item.__send__("dc_#{var}=", value)
+ end
+
+ @content_elems.each do |var, value|
+ item.__send__("content_#{var}=", value)
+ end
+
+ item.trackback_ping = @trackback_elems[:ping]
+ @trackback_elems[:about].each do |value|
+ new_about = item.trackback_abouts.new_about
+ new_about.value = value
+ end
+ end
+ end
+
+ new_rss = RSS::Maker.make("1.0") do |maker|
+ rss.channel.setup_maker(maker)
+
+ rss.items.each do |item|
+ item.setup_maker(maker)
+ end
+ end
+
+ assert_equal(item_size, new_rss.items.size)
+ new_rss.items.each_with_index do |item, i|
+ assert_equal("#{link}#{i}", item.about)
+ assert_equal("#{title}#{i}", item.title)
+ assert_equal("#{link}#{i}", item.link)
+ assert_equal("#{description}#{i}", item.description)
+
+ @dc_elems.each do |var, value|
+ assert_equal(item.__send__("dc_#{var}"), value)
+ end
+
+ @content_elems.each do |var, value|
+ assert_equal(item.__send__("content_#{var}"), value)
+ end
+
+ assert_equal(@trackback_elems[:ping], item.trackback_ping)
+ assert_equal(@trackback_elems[:about].size, item.trackback_abouts.size)
+ item.trackback_abouts.each_with_index do |about, i|
+ assert_equal(@trackback_elems[:about][i], about.value)
+ end
+ end
+
+ end
+
+ def test_setup_maker
+ encoding = "EUC-JP"
+ standalone = true
+
+ href = 'a.xsl'
+ type = 'text/xsl'
+ title = 'sample'
+ media = 'printer'
+ charset = 'UTF-8'
+ alternate = 'yes'
+
+ rss = RSS::Maker.make("1.0") do |maker|
+ maker.encoding = encoding
+ maker.standalone = standalone
+
+ xss = maker.xml_stylesheets.new_xml_stylesheet
+ xss.href = href
+ xss.type = type
+ xss.title = title
+ xss.media = media
+ xss.charset = charset
+ xss.alternate = alternate
+
+ setup_dummy_channel(maker)
+ end
+
+ new_rss = RSS::Maker.make("1.0") do |maker|
+ rss.setup_maker(maker)
+ end
+
+ assert_equal("1.0", new_rss.rss_version)
+ assert_equal(encoding, new_rss.encoding)
+ assert_equal(standalone, new_rss.standalone)
+
+ xss = rss.xml_stylesheets.first
+ assert_equal(1, rss.xml_stylesheets.size)
+ assert_equal(href, xss.href)
+ assert_equal(type, xss.type)
+ assert_equal(title, xss.title)
+ assert_equal(media, xss.media)
+ assert_equal(charset, xss.charset)
+ assert_equal(alternate, xss.alternate)
+ end
+
+ end
+end
diff --git a/test/rss/test_setup_maker_2.0.rb b/test/rss/test_setup_maker_2.0.rb
new file mode 100644
index 0000000000..f28f837e43
--- /dev/null
+++ b/test/rss/test_setup_maker_2.0.rb
@@ -0,0 +1,295 @@
+require "rss-testcase"
+
+require "rss/maker"
+
+module RSS
+ class TestSetupMaker20 < TestCase
+
+ def test_setup_maker_channel
+ title = "fugafuga"
+ link = "http://hoge.com"
+ description = "fugafugafugafuga"
+ language = "ja"
+ copyright = "foo"
+ managingEditor = "bar"
+ webMaster = "web master"
+ rating = "6"
+ docs = "http://foo.com/doc"
+ skipDays = [
+ "Sunday",
+ "Monday",
+ ]
+ skipHours = [
+ 0,
+ 13,
+ ]
+ pubDate = Time.now
+ lastBuildDate = Time.now
+ categories = [
+ "Nespapers",
+ "misc",
+ ]
+ generator = "RSS Maker"
+ ttl = 60
+
+ rss = RSS::Maker.make("2.0") do |maker|
+ maker.channel.title = title
+ maker.channel.link = link
+ maker.channel.description = description
+ maker.channel.language = language
+ maker.channel.copyright = copyright
+ maker.channel.managingEditor = managingEditor
+ maker.channel.webMaster = webMaster
+ maker.channel.rating = rating
+ maker.channel.docs = docs
+ maker.channel.pubDate = pubDate
+ maker.channel.lastBuildDate = lastBuildDate
+
+ skipDays.each do |day|
+ new_day = maker.channel.skipDays.new_day
+ new_day.content = day
+ end
+ skipHours.each do |hour|
+ new_hour = maker.channel.skipHours.new_hour
+ new_hour.content = hour
+ end
+
+
+ categories.each do |category|
+ new_category = maker.channel.categories.new_category
+ new_category.content = category
+ end
+
+ maker.channel.generator = generator
+ maker.channel.ttl = ttl
+ end
+
+ new_rss = RSS::Maker.make("2.0") do |maker|
+ rss.channel.setup_maker(maker)
+ end
+ channel = new_rss.channel
+
+ assert_equal(title, channel.title)
+ assert_equal(link, channel.link)
+ assert_equal(description, channel.description)
+ assert_equal(language, channel.language)
+ assert_equal(copyright, channel.copyright)
+ assert_equal(managingEditor, channel.managingEditor)
+ assert_equal(webMaster, channel.webMaster)
+ assert_equal(rating, channel.rating)
+ assert_equal(docs, channel.docs)
+ assert_equal(pubDate, channel.pubDate)
+ assert_equal(lastBuildDate, channel.lastBuildDate)
+
+ skipDays.each_with_index do |day, i|
+ assert_equal(day, channel.skipDays.days[i].content)
+ end
+ skipHours.each_with_index do |hour, i|
+ assert_equal(hour, channel.skipHours.hours[i].content)
+ end
+
+
+ channel.categories.each_with_index do |category, i|
+ assert_equal(categories[i], category.content)
+ end
+
+ assert_equal(generator, channel.generator)
+ assert_equal(ttl, channel.ttl)
+
+
+ assert(channel.items.empty?)
+ assert_nil(channel.image)
+ assert_nil(channel.textInput)
+ end
+
+ def test_setup_maker_image
+ title = "fugafuga"
+ link = "http://hoge.com"
+ url = "http://hoge.com/hoge.png"
+ width = 144
+ height = 400
+ description = "an image"
+
+ rss = RSS::Maker.make("2.0") do |maker|
+ setup_dummy_channel(maker)
+ maker.channel.link = link
+
+ maker.image.title = title
+ maker.image.url = url
+ maker.image.width = width
+ maker.image.height = height
+ maker.image.description = description
+ end
+
+ new_rss = RSS::Maker.make("2.0") do |maker|
+ rss.channel.setup_maker(maker)
+ rss.image.setup_maker(maker)
+ end
+
+ image = new_rss.image
+ assert_equal(title, image.title)
+ assert_equal(link, image.link)
+ assert_equal(url, image.url)
+ assert_equal(width, image.width)
+ assert_equal(height, image.height)
+ assert_equal(description, image.description)
+ end
+
+ def test_setup_maker_textinput
+ title = "fugafuga"
+ description = "text hoge fuga"
+ name = "hoge"
+ link = "http://hoge.com"
+
+ rss = RSS::Maker.make("2.0") do |maker|
+ setup_dummy_channel(maker)
+
+ maker.textinput.title = title
+ maker.textinput.description = description
+ maker.textinput.name = name
+ maker.textinput.link = link
+ end
+
+ new_rss = RSS::Maker.make("2.0") do |maker|
+ rss.channel.setup_maker(maker)
+ rss.textinput.setup_maker(maker)
+ end
+
+ textInput = new_rss.channel.textInput
+ assert_equal(title, textInput.title)
+ assert_equal(description, textInput.description)
+ assert_equal(name, textInput.name)
+ assert_equal(link, textInput.link)
+ end
+
+ def test_setup_maker_items
+ title = "TITLE"
+ link = "http://hoge.com/"
+ description = "text hoge fuga"
+ author = "oprah@oxygen.net"
+ comments = "http://www.myblog.org/cgi-local/mt/mt-comments.cgi?entry_id=290"
+ pubDate = Time.now
+
+ guid_isPermaLink = true
+ guid_content = "http://inessential.com/2002/09/01.php#a2"
+
+ enclosure_url = "http://www.scripting.com/mp3s/weatherReportSuite.mp3"
+ enclosure_length = "12216320"
+ enclosure_type = "audio/mpeg"
+
+ source_url = "http://static.userland.com/tomalak/links2.xml"
+ source_content = "Tomalak's Realm"
+
+ category_domain = "http://www.fool.com/cusips"
+ category_content = "MSFT"
+
+ item_size = 5
+
+ rss = RSS::Maker.make("2.0") do |maker|
+ setup_dummy_channel(maker)
+
+ item_size.times do |i|
+ item = maker.items.new_item
+ item.title = "#{title}#{i}"
+ item.link = "#{link}#{i}"
+ item.description = "#{description}#{i}"
+ item.author = "#{author}#{i}"
+ item.comments = "#{comments}#{i}"
+ item.date = pubDate
+
+ item.guid.isPermaLink = guid_isPermaLink
+ item.guid.content = guid_content
+
+ item.enclosure.url = enclosure_url
+ item.enclosure.length = enclosure_length
+ item.enclosure.type = enclosure_type
+
+ item.source.url = source_url
+ item.source.content = source_content
+
+ category = item.categories.new_category
+ category.domain = category_domain
+ category.content = category_content
+ end
+ end
+
+ new_rss = RSS::Maker.make("2.0") do |maker|
+ rss.channel.setup_maker(maker)
+
+ rss.items.each do |item|
+ item.setup_maker(maker)
+ end
+ end
+
+ assert_equal(item_size, new_rss.items.size)
+ new_rss.items.each_with_index do |item, i|
+ assert_equal("#{title}#{i}", item.title)
+ assert_equal("#{link}#{i}", item.link)
+ assert_equal("#{description}#{i}", item.description)
+ assert_equal("#{author}#{i}", item.author)
+ assert_equal("#{comments}#{i}", item.comments)
+ assert_equal(pubDate, item.pubDate)
+
+ assert_equal(guid_isPermaLink, item.guid.isPermaLink)
+ assert_equal(guid_content, item.guid.content)
+
+ assert_equal(enclosure_url, item.enclosure.url)
+ assert_equal(enclosure_length, item.enclosure.length)
+ assert_equal(enclosure_type, item.enclosure.type)
+
+ assert_equal(source_url, item.source.url)
+ assert_equal(source_content, item.source.content)
+
+ assert_equal(1, item.categories.size)
+ assert_equal(category_domain, item.category.domain)
+ assert_equal(category_content, item.category.content)
+ end
+
+ end
+
+ def test_setup_maker
+ encoding = "EUC-JP"
+ standalone = true
+
+ href = 'a.xsl'
+ type = 'text/xsl'
+ title = 'sample'
+ media = 'printer'
+ charset = 'UTF-8'
+ alternate = 'yes'
+
+ rss = RSS::Maker.make("2.0") do |maker|
+ maker.encoding = encoding
+ maker.standalone = standalone
+
+ xss = maker.xml_stylesheets.new_xml_stylesheet
+ xss.href = href
+ xss.type = type
+ xss.title = title
+ xss.media = media
+ xss.charset = charset
+ xss.alternate = alternate
+
+ setup_dummy_channel(maker)
+ end
+
+ new_rss = RSS::Maker.make("2.0") do |maker|
+ rss.setup_maker(maker)
+ end
+
+ assert_equal("2.0", new_rss.rss_version)
+ assert_equal(encoding, new_rss.encoding)
+ assert_equal(standalone, new_rss.standalone)
+
+ xss = rss.xml_stylesheets.first
+ assert_equal(1, rss.xml_stylesheets.size)
+ assert_equal(href, xss.href)
+ assert_equal(type, xss.type)
+ assert_equal(title, xss.title)
+ assert_equal(media, xss.media)
+ assert_equal(charset, xss.charset)
+ assert_equal(alternate, xss.alternate)
+ end
+
+ end
+end
diff --git a/test/rss/test_to_s.rb b/test/rss/test_to_s.rb
new file mode 100644
index 0000000000..d6c761e7ef
--- /dev/null
+++ b/test/rss/test_to_s.rb
@@ -0,0 +1,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" => "ture",
+ "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