From b0538cb6d79956cc3dc799910644e23729346383 Mon Sep 17 00:00:00 2001 From: kou Date: Fri, 19 Nov 2004 08:25:25 +0000 Subject: * lib/rss/rss.rb (RSS::VERSION): 0.1.0 -> 0.1.1. * lib/rss: #to_s used #tag. * test/rss/test_to_s.rb: added. * lib/rss/maker.rb (RSS::Maker.make): changed API. It's not received modules which is used as the second argument. * lib/rss/xml-stylesheet.rb (RSS::XMLStyleSheet#alternate): changed return value type which is not String but Boolean. * lib/rss/2.0.rb (RSS::Rss::Channel#ttl): changed return value type which is not String but Integer. * lib/rss/0.9.rb (RSS::Rss::Channel): has s and has s. * lib/rss/maker/0.9.rb (RSS::Maker::RSS09::Channel): ditto. * lib/rss/0.9.rb (RSS::Rss::Channel::Item): has s. * lib/rss/maker/2.0.rb (RSS::Maker::Rss20::Channel::Item): ditto. * lib/rss/2.0.rb (RSS::Rss::Channel): has s. * lib/rss/maker/2.0.rb (RSS::Maker::RSS20::Channel): ditto. * lib/rss/trackback.rb: parent element has s. * lib/rss/maker/trackback.rb: ditto. git-svn-id: http://svn.ruby-lang.org/repos/ruby/trunk@7319 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- test/rss/rss-assertions.rb | 329 ++++++++++++++++++++++++++--- test/rss/test_1.0.rb | 5 +- test/rss/test_maker_0.9.rb | 30 ++- test/rss/test_maker_2.0.rb | 56 +++-- test/rss/test_maker_content.rb | 2 +- test/rss/test_maker_dc.rb | 2 +- test/rss/test_maker_sy.rb | 2 +- test/rss/test_maker_trackback.rb | 13 +- test/rss/test_parser.rb | 12 +- test/rss/test_to_s.rb | 440 +++++++++++++++++++++++++++++++++++++++ test/rss/test_trackback.rb | 10 +- test/rss/test_xml-stylesheet.rb | 6 +- 12 files changed, 840 insertions(+), 67 deletions(-) create mode 100644 test/rss/test_to_s.rb (limited to 'test/rss') diff --git a/test/rss/rss-assertions.rb b/test/rss/rss-assertions.rb index 6b83cbe09..a51fc1fd6 100644 --- a/test/rss/rss-assertions.rb +++ b/test/rss/rss-assertions.rb @@ -94,27 +94,32 @@ module RSS end end end + + def assert_xml_declaration(version, encoding, standalone, rss) + _wrap_assertion do + assert_equal(version, rss.version) + assert_equal(encoding, rss.encoding) + assert_equal(standalone, rss.standalone) + end + end - def assert_xml_stylesheet_attrs(xsl, attrs) + def assert_xml_stylesheet_attrs(attrs, xsl) _wrap_assertion do - normalized_attrs = {} - attrs.each do |name, value| - normalized_attrs[name.to_s] = value - end + n_attrs = normalized_attrs(attrs) ::RSS::XMLStyleSheet::ATTRIBUTES.each do |name| - assert_equal(normalized_attrs[name], xsl.send(name)) + assert_equal(n_attrs[name], xsl.send(name)) end end end - def assert_xml_stylesheet(target, xsl, attrs) + def assert_xml_stylesheet(target, attrs, xsl) _wrap_assertion do if attrs.has_key?(:href) if !attrs.has_key?(:type) and attrs.has_key?(:guess_type) attrs[:type] = attrs[:guess_type] end assert_equal("xml-stylesheet", target) - assert_xml_stylesheet_attrs(xsl, attrs) + assert_xml_stylesheet_attrs(attrs, xsl) else assert_nil(target) assert_equal("", xsl.to_s) @@ -123,39 +128,313 @@ module RSS end def assert_xml_stylesheet_pis(attrs_ary) - rdf = ::RSS::RDF.new() - xss_strs = [] - attrs_ary.each do |attrs| - xss = ::RSS::XMLStyleSheet.new(*attrs) - xss_strs.push(xss.to_s) - rdf.xml_stylesheets.push(xss) + _wrap_assertion do + rdf = ::RSS::RDF.new() + xss_strs = [] + attrs_ary.each do |attrs| + xss = ::RSS::XMLStyleSheet.new(*attrs) + xss_strs.push(xss.to_s) + rdf.xml_stylesheets.push(xss) + end + pi_str = rdf.to_s.gsub(/<\?xml .*\n/, "").gsub(/\s* "http://bar.com/tb.cgi?tb_id=rssplustrackback", - :about => "http://foo.com/trackback/tb.cgi?tb_id=20020923", + :abouts => [ + "http://foo.com/trackback/tb.cgi?tb_id=20020923", + "http://bar.com/trackback/tb.cgi?tb_id=20041114", + ], } end def test_rss10 - rss = RSS::Maker.make("1.0", ["trackback"]) do |maker| + rss = RSS::Maker.make("1.0") do |maker| setup_dummy_channel(maker) setup_dummy_item(maker) item = maker.items.last - @elements.each do |name, value| - item.__send__("#{accessor_name(name)}=", value) + item.trackback_ping = @elements[:ping] + @elements[:abouts].each do |about| + new_about = item.trackback_abouts.new_about + new_about.value = about end end assert_trackback(@elements, rss.items.last) diff --git a/test/rss/test_parser.rb b/test/rss/test_parser.rb index de4894997..019030bcc 100644 --- a/test/rss/test_parser.rb +++ b/test/rss/test_parser.rb @@ -116,7 +116,7 @@ EOR def test_channel - assert_parse(make_RDF(<<-EOR), :missing_attribute, "channel", "about") + assert_parse(make_RDF(<<-EOR), :missing_attribute, "channel", "rdf:about") EOR @@ -145,7 +145,7 @@ EOR EOR - assert_parse(make_RDF(<<-EOR), :missing_attribute, "image", "resource") + assert_parse(make_RDF(<<-EOR), :missing_attribute, "image", "rdf:resource") hoge http://example.com/ @@ -194,7 +194,7 @@ EOR EOR - assert_parse(make_RDF(<<-EOR), :missing_attribute, "textinput", "resource") + assert_parse(make_RDF(<<-EOR), :missing_attribute, "textinput", "rdf:resource") hoge http://example.com/ @@ -257,7 +257,7 @@ EOR def test_image - assert_parse(make_RDF(<<-EOR), :missing_attribute, "image", "about") + assert_parse(make_RDF(<<-EOR), :missing_attribute, "image", "rdf:about") #{make_channel} @@ -314,7 +314,7 @@ EOR def test_item - assert_parse(make_RDF(<<-EOR), :missing_attribute, "item", "about") + assert_parse(make_RDF(<<-EOR), :missing_attribute, "item", "rdf:about") #{make_channel} #{make_image} @@ -371,7 +371,7 @@ EOR def test_textinput - assert_parse(make_RDF(<<-EOR), :missing_attribute, "textinput", "about") + assert_parse(make_RDF(<<-EOR), :missing_attribute, "textinput", "rdf:about") #{make_channel} #{make_image} #{make_item} diff --git a/test/rss/test_to_s.rb b/test/rss/test_to_s.rb new file mode 100644 index 000000000..e2263ca17 --- /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.9") 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

", + } + 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 diff --git a/test/rss/test_trackback.rb b/test/rss/test_trackback.rb index bfe39d005..a3f023e88 100644 --- a/test/rss/test_trackback.rb +++ b/test/rss/test_trackback.rb @@ -88,19 +88,19 @@ EOR @elems.each do |name, value| @parents.each do |parent| accessor = "#{RSS::TRACKBACK_PREFIX}_#{name}" - target_accessor = "resource" - target = @rss.send(parent).send(accessor) + target = @rss.send(parent) target20 = @rss20.channel.send(parent, -1) - assert_equal(value, target.send(target_accessor)) + assert_equal(value, target.send(accessor)) assert_equal(value, target20.send(accessor)) - target.send("#{target_accessor}=", new_value[name].to_s) if name == :about # abount is zero or more + target.send("#{accessor}=", 0, new_value[name].to_s) target20.send("#{accessor}=", 0, new_value[name].to_s) else + target.send("#{accessor}=", new_value[name].to_s) target20.send("#{accessor}=", new_value[name].to_s) end - assert_equal(new_value[name], target.send(target_accessor)) + assert_equal(new_value[name], target.send(accessor)) assert_equal(new_value[name], target20.send(accessor)) end end diff --git a/test/rss/test_xml-stylesheet.rb b/test/rss/test_xml-stylesheet.rb index ab16d6e2f..c88a858f5 100644 --- a/test/rss/test_xml-stylesheet.rb +++ b/test/rss/test_xml-stylesheet.rb @@ -14,7 +14,7 @@ module RSS {:media => "print", :title => "FOO"}, {:charset => "UTF-8", :alternate => "yes"}, ].each do |attrs| - assert_xml_stylesheet_attrs(XMLStyleSheet.new(*attrs), attrs) + assert_xml_stylesheet_attrs(attrs, XMLStyleSheet.new(*attrs)) end end @@ -36,7 +36,7 @@ module RSS :alternate => "yes"}, ].each do |attrs| target, contents = parse_pi(XMLStyleSheet.new(*attrs).to_s) - assert_xml_stylesheet(target, XMLStyleSheet.new(*contents), attrs) + assert_xml_stylesheet(target, attrs, XMLStyleSheet.new(*contents)) end end @@ -88,7 +88,7 @@ module RSS assert_equal(have_href_xsss.size, rss.xml_stylesheets.size) rss.xml_stylesheets.each_with_index do |stylesheet, i| target, = parse_pi(stylesheet.to_s) - assert_xml_stylesheet(target, stylesheet, have_href_xsss[i]) + assert_xml_stylesheet(target, have_href_xsss[i], stylesheet) end end end -- cgit