diff options
Diffstat (limited to 'test')
-rw-r--r-- | test/rss/common.rb | 100 | ||||
-rw-r--r-- | test/rss/each_parser.rb | 17 | ||||
-rw-r--r-- | test/rss/my-assertions.rb | 89 | ||||
-rw-r--r-- | test/rss/test.rb | 16 | ||||
-rw-r--r-- | test/rss/test_1.0.rb | 237 | ||||
-rw-r--r-- | test/rss/test_accessor.rb | 24 | ||||
-rw-r--r-- | test/rss/test_content.rb | 94 | ||||
-rw-r--r-- | test/rss/test_dublincore.rb | 123 | ||||
-rw-r--r-- | test/rss/test_parser.rb | 418 | ||||
-rw-r--r-- | test/rss/test_syndication.rb | 122 | ||||
-rw-r--r-- | test/rss/test_trackback.rb | 110 |
11 files changed, 1350 insertions, 0 deletions
diff --git a/test/rss/common.rb b/test/rss/common.rb new file mode 100644 index 0000000000..d92057f621 --- /dev/null +++ b/test/rss/common.rb @@ -0,0 +1,100 @@ +require 'test/my-assertions' + +module TestRSSMixin + + include RSS + + XMLDECL_VERSION = "1.0" + XMLDECL_ENCODING = "UTF-8" + XMLDECL_STANDALONE = "no" + + RDF_ABOUT = "http://www.xml.com/xml/news.rss" + RDF_RESOURCE = "http://xml.com/universal/images/xml_tiny.gif" + TITLE_VALUE = "XML.com" + LINK_VALUE = "http://xml.com/pub" + URL_VALUE = "http://xml.com/universal/images/xml_tiny.gif" + NAME_VALUE = "hogehoge" + DESCRIPTION_VALUE = " + XML.com features a rich mix of information and services + for the XML community. + " + RESOURCES = [ + "http://xml.com/pub/2000/08/09/xslt/xslt.html", + "http://xml.com/pub/2000/08/09/rdfdb/index.html", + ] + + private + def make_xmldecl(v=XMLDECL_VERSION, e=XMLDECL_ENCODING, s=XMLDECL_STANDALONE) + rv = "<?xml version='#{v}'" + rv << " encoding='#{e}'" if e + rv << " standalone='#{s}'" if s + rv << "?>" + rv + end + + def make_RDF(content=nil, xmlns=[]) + <<-EORSS +#{make_xmldecl} +<rdf:RDF xmlns="#{URI}" xmlns:rdf="#{RDF::URI}" +#{xmlns.collect {|pre, uri| "xmlns:#{pre}='#{uri}'"}.join(' ')}> +#{block_given? ? yield : content} +</rdf:RDF> +EORSS + end + + def make_channel(content=nil) + <<-EOC +<channel rdf:about="#{RDF_ABOUT}"> + <title>#{TITLE_VALUE}</title> + <link>#{LINK_VALUE}</link> + <description>#{DESCRIPTION_VALUE}</description> + + <image rdf:resource="#{RDF_RESOURCE}" /> + + <items> + <rdf:Seq> +#{RESOURCES.collect do |res| '<rdf:li resource="' + res + '" />' end.join("\n")} + </rdf:Seq> + </items> + + <textinput rdf:resource="#{RDF_RESOURCE}" /> + +#{block_given? ? yield : content} +</channel> +EOC + end + + def make_image(content=nil) + <<-EOI +<image rdf:about="#{RDF_ABOUT}"> + <title>#{TITLE_VALUE}</title> + <url>#{URL_VALUE}</url> + <link>#{LINK_VALUE}</link> +#{block_given? ? yield : content} +</image> +EOI + end + + def make_item(content=nil) + <<-EOI +<item rdf:about="#{RDF_ABOUT}"> + <title>#{TITLE_VALUE}</title> + <link>#{LINK_VALUE}</link> + <description>#{DESCRIPTION_VALUE}</description> +#{block_given? ? yield : content} +</item> +EOI + end + + def make_textinput(content=nil) + <<-EOT +<textinput rdf:about="#{RDF_ABOUT}"> + <title>#{TITLE_VALUE}</title> + <description>#{DESCRIPTION_VALUE}</description> + <name>#{NAME_VALUE}</name> + <link>#{LINK_VALUE}</link> +#{block_given? ? yield : content} +</textinput> +EOT + end +end diff --git a/test/rss/each_parser.rb b/test/rss/each_parser.rb new file mode 100644 index 0000000000..b1ea9c5e11 --- /dev/null +++ b/test/rss/each_parser.rb @@ -0,0 +1,17 @@ +#!/usr/bin/env ruby + +require "rbconfig" + +c = Config::CONFIG +ruby = File.join(c['bindir'], c['ruby_install_name']) + +module RSS + AVAILABLE_PARSERS = [ARGV.shift] +end + +def load_test_file(name) + puts "Loading #{name} ..." + require name +end + +load_test_file(ARGV.shift) diff --git a/test/rss/my-assertions.rb b/test/rss/my-assertions.rb new file mode 100644 index 0000000000..6e88839987 --- /dev/null +++ b/test/rss/my-assertions.rb @@ -0,0 +1,89 @@ +module Test + module Unit + module Assertions + + def assert_parse(rss, assert_method, *args) + send("assert_#{assert_method}", *args) do + ::RSS::Parser.parse(rss) + end + send("assert_#{assert_method}", *args) do + ::RSS::Parser.parse(rss, false).validate + end + end + + def assert_ns(prefix, uri) + _wrap_assertion do + begin + yield + flunk("Not raise NSError") + rescue ::RSS::NSError => e + assert_equal(prefix, e.prefix) + assert_equal(uri, e.uri) + end + end + end + + def assert_missing_tag(tag, parent) + _wrap_assertion do + begin + yield + flunk("Not raise MissingTagError") + rescue ::RSS::MissingTagError => e + assert_equal(tag, e.tag) + assert_equal(parent, e.parent) + end + end + end + + def assert_too_much_tag(tag, parent) + _wrap_assertion do + begin + yield + flunk("Not raise TooMuchTagError") + rescue ::RSS::TooMuchTagError => e + assert_equal(tag, e.tag) + assert_equal(parent, e.parent) + end + end + end + + def assert_missing_attribute(tag, attrname) + _wrap_assertion do + begin + yield + flunk("Not raise MissingAttributeError") + rescue ::RSS::MissingAttributeError => e + assert_equal(tag, e.tag) + assert_equal(attrname, e.attribute) + end + end + end + + def assert_not_excepted_tag(tag, parent) + _wrap_assertion do + begin + yield + flunk("Not raise NotExceptedTagError") + rescue ::RSS::NotExceptedTagError => e + assert_equal(tag, e.tag) + assert_equal(parent, e.parent) + end + end + end + + def assert_not_available_value(tag, value) + _wrap_assertion do + begin + yield + flunk("Not raise NotAvailableValueError") + rescue ::RSS::NotAvailableValueError => e + assert_equal(tag, e.tag) + assert_equal(value, e.value) + end + end + end + + end + end +end + diff --git a/test/rss/test.rb b/test/rss/test.rb new file mode 100644 index 0000000000..3b462cc4a3 --- /dev/null +++ b/test/rss/test.rb @@ -0,0 +1,16 @@ +#!/usr/bin/env ruby + +require "rbconfig" +require "rss/parser" + +c = Config::CONFIG +ruby = File.join(c['bindir'], c['ruby_install_name']) + +RSS::AVAILABLE_PARSERS.each do |parser| + puts "------------------------------------" + puts "Using #{parser}" + puts "------------------------------------" + Dir.glob(ARGV.shift || "test/test_*") do |file| + puts(`#{ruby} #{if $DEBUG then '-d' end} -I. -I./lib test/each_parser.rb #{parser} #{file} #{ARGV.join(' ')}`) + end +end diff --git a/test/rss/test_1.0.rb b/test/rss/test_1.0.rb new file mode 100644 index 0000000000..485a8c4e0e --- /dev/null +++ b/test/rss/test_1.0.rb @@ -0,0 +1,237 @@ +require "test/unit" +require "rexml/document" + +require "rss/1.0" +require "test/common" + +class TestCore < Test::Unit::TestCase + + include TestRSSMixin + + def setup + + @rdf_prefix = "rdf" + @rdf_uri = "http://www.w3.org/1999/02/22-rdf-syntax-ns#" + @uri = "http://purl.org/rss/1.0/" + + end + + def test_RDF + + version = "1.0" + encoding = "UTF-8" + standalone = "no" + + rdf = RDF.new(version, encoding, standalone) + + doc = REXML::Document.new(rdf.to_s(false)) + + xmldecl = doc.xml_decl + + %w(version encoding standalone).each do |x| + assert_equal(instance_eval(x), xmldecl.send(x)) + end + + assert_equal(@rdf_uri, doc.root.namespace) + + end + + def test_channel + + about = "http://hoge.com" + title = "fugafuga" + link = "http://hoge.com" + description = "fugafugafugafuga" + resource = "http://hoge.com/hoge.png" + image = RDF::Channel::Image.new(resource) + items = RDF::Channel::Items.new + textinput = RDF::Channel::Textinput.new(resource) + + channel = RDF::Channel.new(about) + %w(title link description image items textinput).each do |x| + channel.send("#{x}=", instance_eval(x)) + end + + doc = REXML::Document.new(make_RDF(channel.to_s)) + c = doc.root.elements[1] + + assert_equal(about, c.attributes["about"]) + %w(title link description image textinput).each do |x| + elem = c.elements[x] + assert_equal(x, elem.name) + assert_equal(@uri, elem.namespace) + if x == "image" or x == "textinput" + excepted = resource + res = elem.attributes.get_attribute("resource") + assert_equal(@rdf_uri, res.namespace) + value = res.value + else + excepted = instance_eval(x) + value = elem.text + end + assert_equal(excepted, value) + end + assert_equal(@uri, c.elements["items"].namespace) + assert_equal("items", c.elements["items"].name) + + end + + def test_channel_image + + resource = "http://hoge.com/hoge.png" + image = RDF::Channel::Image.new(resource) + + doc = REXML::Document.new(make_RDF(image.to_s)) + i = doc.root.elements[1] + + assert_equal("image", i.name) + assert_equal(@uri, i.namespace) + + res = i.attributes.get_attribute("resource") + + assert_equal(@rdf_uri, res.namespace) + assert_equal(resource, res.value) + + end + + def test_channel_textinput + + resource = "http://hoge.com/hoge.png" + textinput = RDF::Channel::Textinput.new(resource) + + doc = REXML::Document.new(make_RDF(textinput.to_s)) + t = doc.root.elements[1] + + assert_equal("textinput", t.name) + assert_equal(@uri, t.namespace) + + res = t.attributes.get_attribute("resource") + + assert_equal(@rdf_uri, res.namespace) + assert_equal(resource, res.value) + + end + + def test_items + + items = RDF::Channel::Items.new + + doc = REXML::Document.new(make_RDF(items.to_s)) + i = doc.root.elements[1] + + assert_equal("items", i.name) + assert_equal(@uri, i.namespace) + + assert_equal(1, i.elements.size) + assert_equal("Seq", i.elements[1].name) + assert_equal(@rdf_uri, i.elements[1].namespace) + + end + + def test_seq + + seq = RDF::Seq.new + + doc = REXML::Document.new(make_RDF(seq.to_s)) + s = doc.root.elements[1] + + assert_equal("Seq", s.name) + assert_equal(@rdf_uri, s.namespace) + + end + + def test_li + + resource = "http://hoge.com/" + li = RDF::Li.new(resource) + + doc = REXML::Document.new(make_RDF(li.to_s)) + l = doc.root.elements[1] + + assert_equal("li", l.name) + assert_equal(@rdf_uri, l.namespace(l.prefix)) + + res = l.attributes.get_attribute("resource") + + assert_equal('', res.instance_eval("@prefix")) + assert_equal(resource, res.value) + + end + + def test_image + + about = "http://hoge.com" + title = "fugafuga" + url = "http://hoge.com/hoge" + link = "http://hoge.com/fuga" + + image = RDF::Image.new(about) + %w(title url link).each do |x| + image.send("#{x}=", instance_eval(x)) + end + + doc = REXML::Document.new(make_RDF(image.to_s)) + i = doc.root.elements[1] + + assert_equal(about, i.attributes["about"]) + %w(title url link).each do |x| + elem = i.elements[x] + assert_equal(x, elem.name) + assert_equal(@uri, elem.namespace) + assert_equal(instance_eval(x), elem.text) + end + + end + + def test_item + + about = "http://hoge.com" + title = "fugafuga" + link = "http://hoge.com/fuga" + description = "hogehogehoge" + + item = RDF::Item.new(about) + %w(title link description).each do |x| + item.send("#{x}=", instance_eval(x)) + end + + doc = REXML::Document.new(make_RDF(item.to_s)) + i = doc.root.elements[1] + + assert_equal(about, i.attributes["about"]) + %w(title link description).each do |x| + elem = i.elements[x] + assert_equal(x, elem.name) + assert_equal(@uri, elem.namespace) + assert_equal(instance_eval(x), elem.text) + end + + end + + def test_textinput + + about = "http://hoge.com" + title = "fugafuga" + link = "http://hoge.com/fuga" + name = "foo" + description = "hogehogehoge" + + textinput = RDF::Textinput.new(about) + %w(title link name description).each do |x| + textinput.send("#{x}=", instance_eval(x)) + end + + doc = REXML::Document.new(make_RDF(textinput.to_s)) + t = doc.root.elements[1] + + assert_equal(about, t.attributes["about"]) + %w(title link name description).each do |x| + elem = t.elements[x] + assert_equal(x, elem.name) + assert_equal(@uri, elem.namespace) + assert_equal(instance_eval(x), elem.text) + end + + end + +end diff --git a/test/rss/test_accessor.rb b/test/rss/test_accessor.rb new file mode 100644 index 0000000000..563e5eca9c --- /dev/null +++ b/test/rss/test_accessor.rb @@ -0,0 +1,24 @@ +require "test/unit" +require "rss/parser" +require "rss/1.0" +require "rss/2.0" +require "test/common" + +class TestAccessor < Test::Unit::TestCase + include TestRSSMixin + + def test_date + channel = Rss::Channel.new + channel.do_validate = false + channel.pubDate = nil + assert_nil(channel.pubDate) + + time = Time.now + channel.pubDate = time + assert_equal(time, channel.pubDate) + + channel.pubDate = nil + assert_nil(channel.pubDate) + end + +end diff --git a/test/rss/test_content.rb b/test/rss/test_content.rb new file mode 100644 index 0000000000..46a76b1ae4 --- /dev/null +++ b/test/rss/test_content.rb @@ -0,0 +1,94 @@ +require "test/unit" +require "cgi-lib" +require "rexml/document" + +require "rss/parser" +require "rss/content" +require "test/common" + +class TestContent < Test::Unit::TestCase + include TestRSSMixin + + def setup + @prefix = "content" + @uri = "http://purl.org/rss/1.0/modules/content/" + + @parents = %w(item) + + @elems = { + :encoded => "<em>ATTENTION</em>", + } + + @content_nodes = @elems.collect do |name, value| + "<#{@prefix}:#{name}>#{CGI.escapeHTML(value.to_s)}</#{@prefix}:#{name}>" + end.join("\n") + + @rss_source = make_RDF(<<-EOR, {@prefix => @uri}) +#{make_channel()} +#{make_image()} +#{make_item(@content_nodes)} +#{make_textinput()} +EOR + + @rss = Parser.parse(@rss_source) + end + + def test_parser + + assert_nothing_raised do + Parser.parse(@rss_source) + end + + @elems.each do |tag, value| + assert_too_much_tag(tag.to_s, "item") do + Parser.parse(make_RDF(<<-EOR, {@prefix => @uri})) +#{make_channel()} +#{make_item(("<" + @prefix + ":" + tag.to_s + ">" + + CGI.escapeHTML(value.to_s) + + "</" + @prefix + ":" + tag.to_s + ">") * 2)} +EOR + end + end + + end + + def test_accessor + + new_value = { + :encoded => "<![CDATA[<it>hoge</it>]]>", + } + + @elems.each do |name, value| + @parents.each do |parent| + meth = "#{RSS::CONTENT_PREFIX}_#{name}" + assert_equal(value, @rss.send(parent).send(meth)) + @rss.send(parent).send("#{meth}=", new_value[name].to_s) + assert_equal(new_value[name], @rss.send(parent).send(meth)) + end + end + + end + + def test_to_s + + @elems.each do |name, value| + excepted = "<#{@prefix}:#{name}>#{CGI.escapeHTML(value)}</#{@prefix}:#{name}>" + @parents.each do |parent| + meth = "#{RSS::CONTENT_PREFIX}_#{name}_element" + assert_equal(excepted, @rss.send(parent).send(meth)) + end + end + + REXML::Document.new(@rss_source).root.each_element do |parent| + if @parents.include?(parent.name) + parent.each_element do |elem| + if elem.namespace == @uri + assert_equal(elem.text, @elems[elem.name.intern].to_s) + end + end + end + end + + end + +end diff --git a/test/rss/test_dublincore.rb b/test/rss/test_dublincore.rb new file mode 100644 index 0000000000..e33c3cda34 --- /dev/null +++ b/test/rss/test_dublincore.rb @@ -0,0 +1,123 @@ +require "test/unit" +require "cgi-lib" +require "rexml/document" + +require "rss/parser" +require "rss/dublincore" +require "test/common" + +class TestDublinCore < Test::Unit::TestCase + include TestRSSMixin + + def setup + @prefix = "dc" + @uri = "http://purl.org/dc/elements/1.1/" + + @parents = %w(channel image item textinput) + + t = Time.iso8601("2000-01-01T12:00:05+00:00") + class << t + alias_method(:to_s, :iso8601) + end + + @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 & Associates, Inc.", + :date => t, + } + + @dc_nodes = @elems.collect do |name, value| + "<#{@prefix}:#{name}>#{value}</#{@prefix}:#{name}>" + end.join("\n") + + @rss_source = make_RDF(<<-EOR, {@prefix => @uri}) +#{make_channel(@dc_nodes)} +#{make_image(@dc_nodes)} +#{make_item(@dc_nodes)} +#{make_textinput(@dc_nodes)} +EOR + + @rss = Parser.parse(@rss_source) + end + + def test_parser + + assert_nothing_raised do + Parser.parse(@rss_source) + end + + @elems.each do |tag, value| + assert_too_much_tag(tag.to_s, "channel") do + Parser.parse(make_RDF(<<-EOR, {@prefix => @uri})) +#{make_channel(("<" + @prefix + ":" + tag.to_s + ">" + + value.to_s + + "</" + @prefix + ":" + tag.to_s + ">") * 2)} +#{make_item} +EOR + end + end + + end + + def test_accessor + + new_value = "hoge" + + @elems.each do |name, value| + @parents.each do |parent| + parsed_value = @rss.send(parent).send("dc_#{name}") + if parsed_value.kind_of?(String) + parsed_value = CGI.escapeHTML(parsed_value) + end + assert_equal(value, parsed_value) + if name == :date + t = Time.iso8601("2003-01-01T02:30:23+09:00") + class << t + alias_method(:to_s, :iso8601) + end + @rss.send(parent).send("dc_#{name}=", t.iso8601) + assert_equal(t, @rss.send(parent).send("dc_#{name}")) + else + @rss.send(parent).send("dc_#{name}=", new_value) + assert_equal(new_value, @rss.send(parent).send("dc_#{name}")) + end + end + end + + end + + def test_to_s + + @elems.each do |name, value| + excepted = "<#{@prefix}:#{name}>#{value}</#{@prefix}:#{name}>" + @parents.each do |parent| + assert_equal(excepted, @rss.send(parent).send("dc_#{name}_element")) + end + end + + REXML::Document.new(@rss_source).root.each_element do |parent| + if @parents.include?(parent.name) + parent.each_element do |elem| + if elem.namespace == @uri + assert_equal(CGI.escapeHTML(elem.text), @elems[elem.name.intern].to_s) + end + end + end + end + + end + +end diff --git a/test/rss/test_parser.rb b/test/rss/test_parser.rb new file mode 100644 index 0000000000..ed6f4d6622 --- /dev/null +++ b/test/rss/test_parser.rb @@ -0,0 +1,418 @@ +require "test/unit" +require "rss/parser" +require "rss/1.0" +require "test/common" + +class TestParser < Test::Unit::TestCase + include TestRSSMixin + + def test_RDF + assert_ns("", RDF::URI) do + Parser.parse(<<-EOR) +#{make_xmldecl} +<RDF/> +EOR + end + + assert_ns("", RDF::URI) do + Parser.parse(<<-EOR) +#{make_xmldecl} +<RDF xmlns="hoge"/> +EOR + end + + assert_ns("rdf", RDF::URI) do + Parser.parse(<<-EOR) +#{make_xmldecl} +<rdf:RDF xmlns:rdf="hoge"/> +EOR + end + + assert_parse(<<-EOR, :missing_tag, "channel", "RDF") +#{make_xmldecl} +<rdf:RDF xmlns:rdf="#{RSS::RDF::URI}"/> +EOR + + assert_parse(<<-EOR, :missing_tag, "channel", "RDF") +#{make_xmldecl} +<RDF xmlns="#{RSS::RDF::URI}"/> +EOR + + assert_parse(<<-EOR, :missing_tag, "channel", "RDF") +#{make_xmldecl} +<RDF xmlns="#{RSS::RDF::URI}"/> +EOR + + assert_parse(make_RDF(<<-EOR), :missing_tag, "item", "RDF") +#{make_channel} +EOR + + assert_parse(make_RDF(<<-EOR), :missing_tag, "item", "RDF") +#{make_channel} +#{make_image} +EOR + + assert_parse(make_RDF(<<-EOR), :missing_tag, "item", "RDF") +#{make_channel} +#{make_textinput} +EOR + + assert_too_much_tag("image", "RDF") do + Parser.parse(make_RDF(<<-EOR)) +#{make_channel} +#{make_image} +#{make_image} +#{make_item} +#{make_textinput} +EOR + end + + assert_not_excepted_tag("image", "RDF") do + Parser.parse(make_RDF(<<-EOR)) +#{make_channel} +#{make_item} +#{make_image} +#{make_textinput} +EOR + end + + assert_parse(make_RDF(<<-EOR), :nothing_raised) +#{make_channel} +#{make_image} +#{make_item} +EOR + + assert_parse(make_RDF(<<-EOR), :nothing_raised) +#{make_channel} +#{make_image} +#{make_item} +#{make_textinput} +EOR + + 1.step(15, 3) do |i| + rss = make_RDF() do + res = make_channel + i.times { res << make_item } + res + end + assert_parse(rss, :nothing_raised) + end + + end + + def test_channel + + assert_parse(make_RDF(<<-EOR), :missing_attribute, "channel", "about") +<channel /> +EOR + + assert_parse(make_RDF(<<-EOR), :missing_tag, "title", "channel") +<channel rdf:about="http://example.com/"/> +EOR + + assert_parse(make_RDF(<<-EOR), :missing_tag, "link", "channel") +<channel rdf:about="http://example.com/"> + <title>hoge</title> +</channel> +EOR + + assert_parse(make_RDF(<<EOR), :missing_tag, "description", "channel") +<channel rdf:about="http://example.com/"> + <title>hoge</title> + <link>http://example.com/</link> +</channel> +EOR + + assert_parse(make_RDF(<<-EOR), :missing_tag, "items", "channel") +<channel rdf:about="http://example.com/"> + <title>hoge</title> + <link>http://example.com/</link> + <description>hogehoge</description> +</channel> +EOR + + assert_parse(make_RDF(<<-EOR), :missing_attribute, "image", "resource") +<channel rdf:about="http://example.com/"> + <title>hoge</title> + <link>http://example.com/</link> + <description>hogehoge</description> + <image/> +</channel> +EOR + + assert_parse(make_RDF(<<-EOR), :missing_tag, "items", "channel") +<channel rdf:about="http://example.com/"> + <title>hoge</title> + <link>http://example.com/</link> + <description>hogehoge</description> + <image rdf:resource="http://example.com/hoge.png" /> +</channel> +EOR + + rss = make_RDF(<<-EOR) +<channel rdf:about="http://example.com/"> + <title>hoge</title> + <link>http://example.com/</link> + <description>hogehoge</description> + <image rdf:resource="http://example.com/hoge.png" /> + <items/> +</channel> +EOR + + assert_missing_tag("Seq", "items") do + Parser.parse(rss) + end + + assert_missing_tag("item", "RDF") do + Parser.parse(rss, false).validate + end + + assert_parse(make_RDF(<<-EOR), :missing_tag, "item", "RDF") +<channel rdf:about="http://example.com/"> + <title>hoge</title> + <link>http://example.com/</link> + <description>hogehoge</description> + <image rdf:resource="http://example.com/hoge.png" /> + <items> + <rdf:Seq> + </rdf:Seq> + </items> +</channel> +EOR + + assert_parse(make_RDF(<<-EOR), :missing_attribute, "textinput", "resource") +<channel rdf:about="http://example.com/"> + <title>hoge</title> + <link>http://example.com/</link> + <description>hogehoge</description> + <image rdf:resource="http://example.com/hoge.png" /> + <items> + <rdf:Seq> + </rdf:Seq> + </items> + <textinput/> +</channel> +EOR + + assert_parse(make_RDF(<<-EOR), :missing_tag, "item", "RDF") +<channel rdf:about="http://example.com/"> + <title>hoge</title> + <link>http://example.com/</link> + <description>hogehoge</description> + <image rdf:resource="http://example.com/hoge.png" /> + <items> + <rdf:Seq> + </rdf:Seq> + </items> + <textinput rdf:resource="http://example.com/search" /> +</channel> +EOR + + end + + def test_image + + assert_parse(make_RDF(<<-EOR), :missing_attribute, "image", "about") +#{make_channel} +<image> +</image> +EOR + + assert_parse(make_RDF(<<-EOR), :missing_tag, "title", "image") +#{make_channel} +<image rdf:about="http://example.com/hoge.png"> +</image> +EOR + + assert_parse(make_RDF(<<-EOR), :missing_tag, "url", "image") +#{make_channel} +<image rdf:about="http://example.com/hoge.png"> + <title>hoge</title> +</image> +EOR + + assert_parse(make_RDF(<<-EOR), :missing_tag, "link", "image") +#{make_channel} +<image rdf:about="http://example.com/hoge.png"> + <title>hoge</title> + <url>http://example.com/hoge.png</url> +</image> +EOR + + rss = make_RDF(<<-EOR) +#{make_channel} +<image rdf:about="http://example.com/hoge.png"> + <title>hoge</title> + <link>http://example.com/</link> + <url>http://example.com/hoge.png</url> +</image> +EOR + + assert_missing_tag("url", "image") do + Parser.parse(rss) + end + + assert_missing_tag("item", "RDF") do + Parser.parse(rss, false).validate + end + + assert_parse(make_RDF(<<-EOR), :missing_tag, "item", "RDF") +#{make_channel} +<image rdf:about="http://example.com/hoge.png"> + <title>hoge</title> + <url>http://example.com/hoge.png</url> + <link>http://example.com/</link> +</image> +EOR + + end + + def test_item + + assert_parse(make_RDF(<<-EOR), :missing_attribute, "item", "about") +#{make_channel} +#{make_image} +<item> +</item> +EOR + + assert_parse(make_RDF(<<-EOR), :missing_tag, "title", "item") +#{make_channel} +#{make_image} +<item rdf:about="http://example.com/hoge.html"> +</item> +EOR + + assert_parse(make_RDF(<<-EOR), :missing_tag, "link", "item") +#{make_channel} +#{make_image} +<item rdf:about="http://example.com/hoge.html"> + <title>hoge</title> +</item> +EOR + + assert_too_much_tag("title", "item") do + Parser.parse(make_RDF(<<-EOR)) +#{make_channel} +#{make_image} +<item rdf:about="http://example.com/hoge.html"> + <title>hoge</title> + <title>hoge</title> + <link>http://example.com/hoge.html</link> +</item> +EOR + end + + assert_parse(make_RDF(<<-EOR), :nothing_raised) +#{make_channel} +#{make_image} +<item rdf:about="http://example.com/hoge.html"> + <title>hoge</title> + <link>http://example.com/hoge.html</link> +</item> +EOR + + assert_parse(make_RDF(<<-EOR), :nothing_raised) +#{make_channel} +#{make_image} +<item rdf:about="http://example.com/hoge.html"> + <title>hoge</title> + <link>http://example.com/hoge.html</link> + <description>hogehoge</description> +</item> +EOR + + end + + def test_textinput + + assert_parse(make_RDF(<<-EOR), :missing_attribute, "textinput", "about") +#{make_channel} +#{make_image} +#{make_item} +<textinput> +</textinput> +EOR + + assert_parse(make_RDF(<<-EOR), :missing_tag, "title", "textinput") +#{make_channel} +#{make_image} +#{make_item} +<textinput rdf:about="http://example.com/search.html"> +</textinput> +EOR + + assert_parse(make_RDF(<<-EOR), :missing_tag, "description", "textinput") +#{make_channel} +#{make_image} +#{make_item} +<textinput rdf:about="http://example.com/search.html"> + <title>hoge</title> +</textinput> +EOR + + assert_too_much_tag("title", "textinput") do + Parser.parse(make_RDF(<<-EOR)) +#{make_channel} +#{make_image} +#{make_item} +<textinput rdf:about="http://example.com/search.html"> + <title>hoge</title> + <title>hoge</title> + <description>hogehoge</description> +</textinput> +EOR + end + + assert_parse(make_RDF(<<-EOR), :missing_tag, "name", "textinput") +#{make_channel} +#{make_image} +#{make_item} +<textinput rdf:about="http://example.com/search.html"> + <title>hoge</title> + <description>hogehoge</description> +</textinput> +EOR + + assert_parse(make_RDF(<<-EOR), :missing_tag, "link", "textinput") +#{make_channel} +#{make_image} +#{make_item} +<textinput rdf:about="http://example.com/search.html"> + <title>hoge</title> + <description>hogehoge</description> + <name>key</name> +</textinput> +EOR + + assert_parse(make_RDF(<<-EOR), :nothing_raised) +#{make_channel} +#{make_image} +#{make_item} +<textinput rdf:about="http://example.com/search.html"> + <title>hoge</title> + <description>hogehoge</description> + <name>key</name> + <link>http://example.com/search.html</link> +</textinput> +EOR + + end + + def test_ignore + + rss = make_RDF(<<-EOR) +#{make_channel} +#{make_item} +<a/> +EOR + + assert_parse(rss, :nothing_raised) + + assert_not_excepted_tag("a", "RDF") do + Parser.parse(rss, true, false) + end + + end + +end diff --git a/test/rss/test_syndication.rb b/test/rss/test_syndication.rb new file mode 100644 index 0000000000..45e55c6fc6 --- /dev/null +++ b/test/rss/test_syndication.rb @@ -0,0 +1,122 @@ +require "test/unit" +require "cgi-lib" +require "rexml/document" + +require "rss/parser" +require "rss/syndication" +require "test/common" + +class TestSyndication < Test::Unit::TestCase + include TestRSSMixin + + def setup + @prefix = "sy" + @uri = "http://purl.org/rss/1.0/modules/syndication/" + + @parents = %w(channel) + + t = Time.iso8601("2000-01-01T12:00:05+00:00") + class << t + alias_method(:to_s, :iso8601) + end + + @elems = { + :updatePeriod => "hourly", + :updateFrequency => 2, + :updateBase => t, + } + + @sy_nodes = @elems.collect do |name, value| + "<#{@prefix}:#{name}>#{CGI.escapeHTML(value.to_s)}</#{@prefix}:#{name}>" + end.join("\n") + + @rss_source = make_RDF(<<-EOR, {@prefix => @uri}) +#{make_channel(@sy_nodes)} +#{make_image()} +#{make_item()} +#{make_textinput()} +EOR + + @rss = Parser.parse(@rss_source) + end + + def test_parser + + assert_nothing_raised do + Parser.parse(@rss_source) + end + + @elems.each do |tag, value| + assert_too_much_tag(tag.to_s, "channel") do + Parser.parse(make_RDF(<<-EOR, {@prefix => @uri})) +#{make_channel(("<" + @prefix + ":" + tag.to_s + ">" + + CGI.escapeHTML(value.to_s) + + "</" + @prefix + ":" + tag.to_s + ">") * 2)} +#{make_item} +EOR + end + end + + end + + def test_accessor + + t = Time.iso8601("2003-01-01T12:00:23+09:00") + class << t + alias_method(:to_s, :iso8601) + end + + new_value = { + :updatePeriod => "daily", + :updateFrequency => +11, + :updateBase => t, + } + + @elems.each do |name, value| + @parents.each do |parent| + assert_equal(value, @rss.send(parent).send("sy_#{name}")) + @rss.send(parent).send("sy_#{name}=", new_value[name].to_s) + assert_equal(new_value[name], @rss.send(parent).send("sy_#{name}")) + end + end + + %w(hourly daily weekly monthly yearly).each do |x| + @parents.each do |parent| + assert_nothing_raised do + @rss.send(parent).sy_updatePeriod = x + end + end + end + + %w(-2 0.3 -0.4).each do |x| + @parents.each do |parent| + assert_not_available_value("updateBase", x) do + @rss.send(parent).sy_updateBase = x + end + end + end + + end + + def test_to_s + + @elems.each do |name, value| + excepted = "<#{@prefix}:#{name}>#{value}</#{@prefix}:#{name}>" + @parents.each do |parent| + assert_equal(excepted, @rss.send(parent).send("sy_#{name}_element")) + end + end + + REXML::Document.new(@rss_source).root.each_element do |parent| + if @parents.include?(parent.name) + parent.each_element do |elem| + if elem.namespace == @uri + assert_equal(elem.text, @elems[elem.name.intern].to_s) + end + end + end + end + + end + +end diff --git a/test/rss/test_trackback.rb b/test/rss/test_trackback.rb new file mode 100644 index 0000000000..c49bda5e33 --- /dev/null +++ b/test/rss/test_trackback.rb @@ -0,0 +1,110 @@ +require "test/unit" +require "cgi-lib" +require "rexml/document" + +require "rss/parser" +require "rss/trackback" +require "test/common" + +class TestTrackBack < Test::Unit::TestCase + include TestRSSMixin + + def setup + @prefix = "trackback" + @uri = "http://madskills.com/public/xml/rss/module/trackback/" + + @parents = %w(item) + + @elems = { + :ping => "http://bar.com/tb.cgi?tb_id=rssplustrackback", + :about => "http://foo.com/trackback/tb.cgi?tb_id=20020923", + } + + @content_nodes = @elems.collect do |name, value| + "<#{@prefix}:#{name} rdf:resource=\"#{CGI.escapeHTML(value.to_s)}\"/>" + end.join("\n") + + @rss_source = make_RDF(<<-EOR, {@prefix => @uri}) +#{make_channel()} +#{make_image()} +#{make_item(@content_nodes)} +#{make_textinput()} +EOR + + @rss = Parser.parse(@rss_source) + end + + def test_parser + + assert_nothing_raised do + Parser.parse(@rss_source) + end + + @elems.find_all{|k, v| k == :ping}.each do |tag, value| + assert_too_much_tag(tag.to_s, "item") do + Parser.parse(make_RDF(<<-EOR, {@prefix => @uri})) +#{make_channel()} +#{make_item(("<" + @prefix + ":" + tag.to_s + " rdf:resource=\"" + + CGI.escapeHTML(value.to_s) + + "\"/>") * 2)} +EOR + end + end + + @elems.find_all{|k, v| k == :about}.each do |tag, value| + assert_missing_tag("trackback:ping", "item") do + Parser.parse(make_RDF(<<-EOR, {@prefix => @uri})) +#{make_channel()} +#{make_item(("<" + @prefix + ":" + tag.to_s + " rdf:resource=\"" + + CGI.escapeHTML(value.to_s) + + "\"/>") * 2)} +EOR + end + + end + + end + + def test_accessor + + new_value = { + :ping => "http://baz.com/trackback/tb.cgi?tb_id=20030808", + :about => "http://hoge.com/trackback/tb.cgi?tb_id=90030808", + } + + @elems.each do |name, value| + @parents.each do |parent| + elem = @rss.send(parent).send("#{RSS::TRACKBACK_PREFIX}_#{name}") + meth = "resource" + assert_equal(value, elem.send(meth)) + elem.send("#{meth}=", new_value[name].to_s) + assert_equal(new_value[name], elem.send(meth)) + end + end + + end + + def test_to_s + + @elems.each do |name, value| + excepted = %Q!<#{@prefix}:#{name} rdf:resource="#{CGI.escapeHTML(value)}"/>! + @parents.each do |parent| + meth = "#{RSS::TRACKBACK_PREFIX}_#{name}_element" + meth << "s" if name == :about + assert_equal(excepted, @rss.send(parent).send(meth)) + end + end + + REXML::Document.new(@rss_source).root.each_element do |parent| + if @parents.include?(parent.name) + parent.each_element do |elem| + if elem.namespace == @uri + assert_equal(elem.attributes["resource"], @elems[elem.name.intern]) + end + end + end + end + + end + +end |