* test/rss/*: Test::Unit::TestCase -> RSS::TestCase and

Test::Unit::Assertions -> RSS::Assertions.


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@5991 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
kou 2004-03-21 10:03:17 +00:00
Родитель cfbe158fae
Коммит abe876ed4e
12 изменённых файлов: 1036 добавлений и 1009 удалений

Просмотреть файл

@ -1,3 +1,8 @@
Sun Mar 21 18:57:37 2004 Kouhei Sutou <kou@cozmixng.org>
* test/rss/*: Test::Unit::TestCase -> RSS::TestCase and
Test::Unit::Assertions -> RSS::Assertions.
Sun Mar 21 18:48:20 2004 Kouhei Sutou <kou@cozmixng.org> Sun Mar 21 18:48:20 2004 Kouhei Sutou <kou@cozmixng.org>
* lib/rss/{rss,dublincore,syndication}.rb: handled W3CDTF correctly. * lib/rss/{rss,dublincore,syndication}.rb: handled W3CDTF correctly.

Просмотреть файл

@ -1,130 +0,0 @@
# -*- tab-width: 2 -*- vim: ts=2
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
def assert_xml_stylesheet_attrs(xsl, attrs)
_wrap_assertion do
normalized_attrs = {}
attrs.each do |name, value|
normalized_attrs[name.to_s] = value
end
::RSS::XMLStyleSheet::ATTRIBUTES.each do |name|
assert_equal(normalized_attrs[name], xsl.send(name))
end
end
end
def assert_xml_stylesheet(target, xsl, attrs)
_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)
else
assert_nil(target)
assert_equal("", xsl.to_s)
end
end
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)
end
pi_str = rdf.to_s.gsub(/<\?xml .*\n/, "").gsub(/\s*<rdf:RDF.*\z/m, "")
assert_equal(xss_strs.join("\n"), pi_str)
end
end
end
end

127
test/rss/rss-assertions.rb Normal file
Просмотреть файл

@ -0,0 +1,127 @@
# -*- tab-width: 2 -*- vim: ts=2
module RSS
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
def assert_xml_stylesheet_attrs(xsl, attrs)
_wrap_assertion do
normalized_attrs = {}
attrs.each do |name, value|
normalized_attrs[name.to_s] = value
end
::RSS::XMLStyleSheet::ATTRIBUTES.each do |name|
assert_equal(normalized_attrs[name], xsl.send(name))
end
end
end
def assert_xml_stylesheet(target, xsl, attrs)
_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)
else
assert_nil(target)
assert_equal("", xsl.to_s)
end
end
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)
end
pi_str = rdf.to_s.gsub(/<\?xml .*\n/, "").gsub(/\s*<rdf:RDF.*\z/m, "")
assert_equal(xss_strs.join("\n"), pi_str)
end
end
end

Просмотреть файл

@ -1,51 +1,58 @@
# -*- tab-width: 2 -*- vim: ts=2 # -*- tab-width: 2 -*- vim: ts=2
require 'my-assertions' require "test/unit"
require 'rss-assertions'
module TestRSSMixin module RSS
class TestCase < Test::Unit::TestCase
include RSS include RSS
include Assertions
XMLDECL_VERSION = "1.0" XMLDECL_VERSION = "1.0"
XMLDECL_ENCODING = "UTF-8" XMLDECL_ENCODING = "UTF-8"
XMLDECL_STANDALONE = "no" XMLDECL_STANDALONE = "no"
RDF_ABOUT = "http://www.xml.com/xml/news.rss" RDF_ABOUT = "http://www.xml.com/xml/news.rss"
RDF_RESOURCE = "http://xml.com/universal/images/xml_tiny.gif" RDF_RESOURCE = "http://xml.com/universal/images/xml_tiny.gif"
TITLE_VALUE = "XML.com" TITLE_VALUE = "XML.com"
LINK_VALUE = "http://xml.com/pub" LINK_VALUE = "http://xml.com/pub"
URL_VALUE = "http://xml.com/universal/images/xml_tiny.gif" URL_VALUE = "http://xml.com/universal/images/xml_tiny.gif"
NAME_VALUE = "hogehoge" NAME_VALUE = "hogehoge"
DESCRIPTION_VALUE = " DESCRIPTION_VALUE = "
XML.com features a rich mix of information and services XML.com features a rich mix of information and services
for the XML community. for the XML community.
" "
RESOURCES = [ RESOURCES = [
"http://xml.com/pub/2000/08/09/xslt/xslt.html", "http://xml.com/pub/2000/08/09/xslt/xslt.html",
"http://xml.com/pub/2000/08/09/rdfdb/index.html", "http://xml.com/pub/2000/08/09/rdfdb/index.html",
] ]
private def default_test
def make_xmldecl(v=XMLDECL_VERSION, e=XMLDECL_ENCODING, s=XMLDECL_STANDALONE) # This class isn't tested
rv = "<?xml version='#{v}'" end
rv << " encoding='#{e}'" if e
rv << " standalone='#{s}'" if s
rv << "?>"
rv
end
def make_RDF(content=nil, xmlns=[]) private
<<-EORSS 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} #{make_xmldecl}
<rdf:RDF xmlns="#{URI}" xmlns:rdf="#{RDF::URI}" <rdf:RDF xmlns="#{URI}" xmlns:rdf="#{RDF::URI}"
#{xmlns.collect {|pre, uri| "xmlns:#{pre}='#{uri}'"}.join(' ')}> #{xmlns.collect {|pre, uri| "xmlns:#{pre}='#{uri}'"}.join(' ')}>
#{block_given? ? yield : content} #{block_given? ? yield : content}
</rdf:RDF> </rdf:RDF>
EORSS EORSS
end end
def make_channel(content=nil) def make_channel(content=nil)
<<-EOC <<-EOC
<channel rdf:about="#{RDF_ABOUT}"> <channel rdf:about="#{RDF_ABOUT}">
<title>#{TITLE_VALUE}</title> <title>#{TITLE_VALUE}</title>
<link>#{LINK_VALUE}</link> <link>#{LINK_VALUE}</link>
@ -64,10 +71,10 @@ EORSS
#{block_given? ? yield : content} #{block_given? ? yield : content}
</channel> </channel>
EOC EOC
end end
def make_image(content=nil) def make_image(content=nil)
<<-EOI <<-EOI
<image rdf:about="#{RDF_ABOUT}"> <image rdf:about="#{RDF_ABOUT}">
<title>#{TITLE_VALUE}</title> <title>#{TITLE_VALUE}</title>
<url>#{URL_VALUE}</url> <url>#{URL_VALUE}</url>
@ -75,10 +82,10 @@ EOC
#{block_given? ? yield : content} #{block_given? ? yield : content}
</image> </image>
EOI EOI
end end
def make_item(content=nil) def make_item(content=nil)
<<-EOI <<-EOI
<item rdf:about="#{RDF_ABOUT}"> <item rdf:about="#{RDF_ABOUT}">
<title>#{TITLE_VALUE}</title> <title>#{TITLE_VALUE}</title>
<link>#{LINK_VALUE}</link> <link>#{LINK_VALUE}</link>
@ -86,10 +93,10 @@ EOI
#{block_given? ? yield : content} #{block_given? ? yield : content}
</item> </item>
EOI EOI
end end
def make_textinput(content=nil) def make_textinput(content=nil)
<<-EOT <<-EOT
<textinput rdf:about="#{RDF_ABOUT}"> <textinput rdf:about="#{RDF_ABOUT}">
<title>#{TITLE_VALUE}</title> <title>#{TITLE_VALUE}</title>
<description>#{DESCRIPTION_VALUE}</description> <description>#{DESCRIPTION_VALUE}</description>
@ -98,29 +105,29 @@ EOI
#{block_given? ? yield : content} #{block_given? ? yield : content}
</textinput> </textinput>
EOT EOT
end end
def make_sample_RDF def make_sample_RDF
make_RDF(<<-EOR) make_RDF(<<-EOR)
#{make_channel} #{make_channel}
#{make_image} #{make_image}
#{make_item} #{make_item}
#{make_textinput} #{make_textinput}
EOR EOR
end end
def make_Rss2(content=nil, xmlns=[]) def make_Rss2(content=nil, xmlns=[])
<<-EORSS <<-EORSS
#{make_xmldecl} #{make_xmldecl}
<rss version="2.0" <rss version="2.0"
#{xmlns.collect {|pre, uri| "xmlns:#{pre}='#{uri}'"}.join(' ')}> #{xmlns.collect {|pre, uri| "xmlns:#{pre}='#{uri}'"}.join(' ')}>
#{block_given? ? yield : content} #{block_given? ? yield : content}
</rss> </rss>
EORSS EORSS
end end
def make_channel2(content=nil) def make_channel2(content=nil)
<<-EOC <<-EOC
<channel> <channel>
<title>#{TITLE_VALUE}</title> <title>#{TITLE_VALUE}</title>
<link>#{LINK_VALUE}</link> <link>#{LINK_VALUE}</link>
@ -141,10 +148,10 @@ EORSS
#{block_given? ? yield : content} #{block_given? ? yield : content}
</channel> </channel>
EOC EOC
end end
def make_item2(content=nil) def make_item2(content=nil)
<<-EOI <<-EOI
<item> <item>
<title>#{TITLE_VALUE}</title> <title>#{TITLE_VALUE}</title>
<link>#{LINK_VALUE}</link> <link>#{LINK_VALUE}</link>
@ -152,5 +159,6 @@ EOC
#{block_given? ? yield : content} #{block_given? ? yield : content}
</item> </item>
EOI EOI
end
end end
end end

Просмотреть файл

@ -1,259 +1,259 @@
# -*- tab-width: 2 -*- vim: ts=2 # -*- tab-width: 2 -*- vim: ts=2
require "test/unit"
require "rexml/document" require "rexml/document"
require "rss-testcase"
require "rss/1.0" require "rss/1.0"
require "common"
class TestCore < Test::Unit::TestCase module RSS
class TestCore < TestCase
include TestRSSMixin def setup
def setup @rdf_prefix = "rdf"
@rdf_uri = "http://www.w3.org/1999/02/22-rdf-syntax-ns#"
@uri = "http://purl.org/rss/1.0/"
@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 end
assert_equal(@rdf_uri, doc.root.namespace) def test_RDF
end version = "1.0"
encoding = "UTF-8"
standalone = "no"
def test_not_displayed_xml_stylesheets rdf = RDF.new(version, encoding, standalone)
rdf = RDF.new()
plain_rdf = rdf.to_s
3.times do
rdf.xml_stylesheets.push(XMLStyleSheet.new)
assert_equal(plain_rdf, rdf.to_s)
end
end
def test_xml_stylesheets doc = REXML::Document.new(rdf.to_s(false))
[
[{:href => "a.xsl", :type => "text/xsl"}],
[
{:href => "a.xsl", :type => "text/xsl"},
{:href => "a.css", :type => "text/css"},
],
].each do |attrs_ary|
assert_xml_stylesheet_pis(attrs_ary)
end
end
def test_channel xmldecl = doc.xml_decl
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(version encoding standalone).each do |x|
%w(title link description image items textinput).each do |x| assert_equal(instance_eval(x), xmldecl.send(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 end
assert_equal(excepted, value)
end
assert_equal(@uri, c.elements["items"].namespace)
assert_equal("items", c.elements["items"].name)
end assert_equal(@rdf_uri, doc.root.namespace)
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 end
doc = REXML::Document.new(make_RDF(image.to_s)) def test_not_displayed_xml_stylesheets
i = doc.root.elements[1] rdf = RDF.new()
plain_rdf = rdf.to_s
3.times do
rdf.xml_stylesheets.push(XMLStyleSheet.new)
assert_equal(plain_rdf, rdf.to_s)
end
end
def test_xml_stylesheets
[
[{:href => "a.xsl", :type => "text/xsl"}],
[
{:href => "a.xsl", :type => "text/xsl"},
{:href => "a.css", :type => "text/css"},
],
].each do |attrs_ary|
assert_xml_stylesheet_pis(attrs_ary)
end
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
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
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 end

Просмотреть файл

@ -1,25 +1,26 @@
# -*- tab-width: 2 -*- vim: ts=2 # -*- tab-width: 2 -*- vim: ts=2
require "test/unit" require "rss-testcase"
require "rss/1.0" require "rss/1.0"
require "rss/2.0" require "rss/2.0"
require "common"
class TestAccessor < Test::Unit::TestCase module RSS
include TestRSSMixin class TestAccessor < TestCase
def test_date def test_date
channel = Rss::Channel.new channel = Rss::Channel.new
channel.do_validate = false channel.do_validate = false
channel.pubDate = nil channel.pubDate = nil
assert_nil(channel.pubDate) assert_nil(channel.pubDate)
time = Time.now time = Time.now
channel.pubDate = time channel.pubDate = time
assert_equal(time, channel.pubDate) assert_equal(time, channel.pubDate)
channel.pubDate = nil
assert_nil(channel.pubDate)
end
channel.pubDate = nil
assert_nil(channel.pubDate)
end end
end end

Просмотреть файл

@ -1,96 +1,97 @@
# -*- tab-width: 2 -*- vim: ts=2 # -*- tab-width: 2 -*- vim: ts=2
require "test/unit"
require "cgi" require "cgi"
require "rexml/document" require "rexml/document"
require "rss-testcase"
require "rss/1.0" require "rss/1.0"
require "rss/content" require "rss/content"
require "common"
class TestContent < Test::Unit::TestCase module RSS
include TestRSSMixin class TestContent < TestCase
def setup def setup
@prefix = "content" @prefix = "content"
@uri = "http://purl.org/rss/1.0/modules/content/" @uri = "http://purl.org/rss/1.0/modules/content/"
@parents = %w(item) @parents = %w(item)
@elems = { @elems = {
:encoded => "<em>ATTENTION</em>", :encoded => "<em>ATTENTION</em>",
} }
@content_nodes = @elems.collect do |name, value| @content_nodes = @elems.collect do |name, value|
"<#{@prefix}:#{name}>#{CGI.escapeHTML(value.to_s)}</#{@prefix}:#{name}>" "<#{@prefix}:#{name}>#{CGI.escapeHTML(value.to_s)}</#{@prefix}:#{name}>"
end.join("\n") end.join("\n")
@rss_source = make_RDF(<<-EOR, {@prefix => @uri}) @rss_source = make_RDF(<<-EOR, {@prefix => @uri})
#{make_channel()} #{make_channel()}
#{make_image()} #{make_image()}
#{make_item(@content_nodes)} #{make_item(@content_nodes)}
#{make_textinput()} #{make_textinput()}
EOR EOR
@rss = Parser.parse(@rss_source) @rss = Parser.parse(@rss_source)
end
def test_parser
assert_nothing_raised do
Parser.parse(@rss_source)
end end
@elems.each do |tag, value| def test_parser
assert_too_much_tag(tag.to_s, "item") do
Parser.parse(make_RDF(<<-EOR, {@prefix => @uri})) 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_channel()}
#{make_item(("<" + @prefix + ":" + tag.to_s + ">" + #{make_item(("<" + @prefix + ":" + tag.to_s + ">" +
CGI.escapeHTML(value.to_s) + CGI.escapeHTML(value.to_s) +
"</" + @prefix + ":" + tag.to_s + ">") * 2)} "</" + @prefix + ":" + tag.to_s + ">") * 2)}
EOR EOR
end
end end
end end
end def test_accessor
def test_accessor new_value = {
:encoded => "<![CDATA[<it>hoge</it>]]>",
}
new_value = { @elems.each do |name, value|
:encoded => "<![CDATA[<it>hoge</it>]]>", @parents.each do |parent|
} meth = "#{RSS::CONTENT_PREFIX}_#{name}"
assert_equal(value, @rss.send(parent).send(meth))
@elems.each do |name, value| @rss.send(parent).send("#{meth}=", new_value[name].to_s)
@parents.each do |parent| assert_equal(new_value[name], @rss.send(parent).send(meth))
meth = "#{RSS::CONTENT_PREFIX}_#{name}" end
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 end
end def test_to_s
def test_to_s @elems.each do |name, value|
excepted = "<#{@prefix}:#{name}>#{CGI.escapeHTML(value)}</#{@prefix}:#{name}>"
@elems.each do |name, value| @parents.each do |parent|
excepted = "<#{@prefix}:#{name}>#{CGI.escapeHTML(value)}</#{@prefix}:#{name}>" meth = "#{RSS::CONTENT_PREFIX}_#{name}_element"
@parents.each do |parent| assert_equal(excepted, @rss.send(parent).send(meth))
meth = "#{RSS::CONTENT_PREFIX}_#{name}_element" end
assert_equal(excepted, @rss.send(parent).send(meth))
end end
end
REXML::Document.new(@rss_source).root.each_element do |parent| REXML::Document.new(@rss_source).root.each_element do |parent|
if @parents.include?(parent.name) if @parents.include?(parent.name)
parent.each_element do |elem| parent.each_element do |elem|
if elem.namespace == @uri if elem.namespace == @uri
assert_equal(elem.text, @elems[elem.name.intern].to_s) assert_equal(elem.text, @elems[elem.name.intern].to_s)
end
end end
end end
end end
end end
end end
end end

Просмотреть файл

@ -1,125 +1,126 @@
# -*- tab-width: 2 -*- vim: ts=2 # -*- tab-width: 2 -*- vim: ts=2
require "test/unit"
require "cgi" require "cgi"
require "rexml/document" require "rexml/document"
require "rss-testcase"
require "rss/1.0" require "rss/1.0"
require "rss/dublincore" require "rss/dublincore"
require "common"
class TestDublinCore < Test::Unit::TestCase module RSS
include TestRSSMixin class TestDublinCore < TestCase
def setup def setup
@prefix = "dc" @prefix = "dc"
@uri = "http://purl.org/dc/elements/1.1/" @uri = "http://purl.org/dc/elements/1.1/"
@parents = %w(channel image item textinput) @parents = %w(channel image item textinput)
t = Time.iso8601("2000-01-01T12:00:05+00:00") t = Time.iso8601("2000-01-01T12:00:05+00:00")
class << t class << t
alias_method(:to_s, :iso8601) alias_method(:to_s, :iso8601)
end end
@elems = { @elems = {
:title => "hoge", :title => "hoge",
:description => :description =>
" XML is placing increasingly heavy loads on the existing technical " XML is placing increasingly heavy loads on
infrastructure of the Internet.", the existing technical infrastructure of the Internet.",
:creator => "Rael Dornfest (mailto:rael@oreilly.com)", :creator => "Rael Dornfest (mailto:rael@oreilly.com)",
:subject => "XML", :subject => "XML",
:publisher => "The O'Reilly Network", :publisher => "The O'Reilly Network",
:contributor => "hogehoge", :contributor => "hogehoge",
:type => "fugafuga", :type => "fugafuga",
:format => "hohoho", :format => "hohoho",
:identifier => "fufufu", :identifier => "fufufu",
:source => "barbar", :source => "barbar",
:language => "ja", :language => "ja",
:relation => "cococo", :relation => "cococo",
:rights => "Copyright (c) 2000 O'Reilly &amp; Associates, Inc.", :rights => "Copyright (c) 2000 O'Reilly &amp; Associates, Inc.",
:date => t, :date => t,
} }
@dc_nodes = @elems.collect do |name, value| @dc_nodes = @elems.collect do |name, value|
"<#{@prefix}:#{name}>#{value}</#{@prefix}:#{name}>" "<#{@prefix}:#{name}>#{value}</#{@prefix}:#{name}>"
end.join("\n") end.join("\n")
@rss_source = make_RDF(<<-EOR, {@prefix => @uri}) @rss_source = make_RDF(<<-EOR, {@prefix => @uri})
#{make_channel(@dc_nodes)} #{make_channel(@dc_nodes)}
#{make_image(@dc_nodes)} #{make_image(@dc_nodes)}
#{make_item(@dc_nodes)} #{make_item(@dc_nodes)}
#{make_textinput(@dc_nodes)} #{make_textinput(@dc_nodes)}
EOR EOR
@rss = Parser.parse(@rss_source) @rss = Parser.parse(@rss_source)
end
def test_parser
assert_nothing_raised do
Parser.parse(@rss_source)
end end
@elems.each do |tag, value| def test_parser
assert_too_much_tag(tag.to_s, "channel") do
Parser.parse(make_RDF(<<-EOR, {@prefix => @uri})) 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 + ">" + #{make_channel(("<" + @prefix + ":" + tag.to_s + ">" +
value.to_s + value.to_s +
"</" + @prefix + ":" + tag.to_s + ">") * 2)} "</" + @prefix + ":" + tag.to_s + ">") * 2)}
#{make_item} #{make_item}
EOR EOR
end
end end
end end
end def test_accessor
def test_accessor new_value = "hoge"
new_value = "hoge" @elems.each do |name, value|
@parents.each do |parent|
@elems.each do |name, value| parsed_value = @rss.send(parent).send("dc_#{name}")
@parents.each do |parent| if parsed_value.kind_of?(String)
parsed_value = @rss.send(parent).send("dc_#{name}") parsed_value = CGI.escapeHTML(parsed_value)
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 end
@rss.send(parent).send("dc_#{name}=", t.iso8601) assert_equal(value, parsed_value)
assert_equal(t, @rss.send(parent).send("dc_#{name}")) if name == :date
else t = Time.iso8601("2003-01-01T02:30:23+09:00")
@rss.send(parent).send("dc_#{name}=", new_value) class << t
assert_equal(new_value, @rss.send(parent).send("dc_#{name}")) alias_method(:to_s, :iso8601)
end end
end @rss.send(parent).send("dc_#{name}=", t.iso8601)
end assert_equal(t, @rss.send(parent).send("dc_#{name}"))
else
end @rss.send(parent).send("dc_#{name}=", new_value)
assert_equal(new_value, @rss.send(parent).send("dc_#{name}"))
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
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
end end
end end

Просмотреть файл

@ -1,130 +1,138 @@
# -*- tab-width: 2 -*- vim: ts=2 # -*- tab-width: 2 -*- vim: ts=2
require "test/unit" require "rss-testcase"
require "rss/1.0" require "rss/1.0"
require "common"
class TestParser < Test::Unit::TestCase module RSS
include TestRSSMixin class TestParser < TestCase
def test_RDF def setup
assert_ns("", RDF::URI) do @_default_parser = Parser.default_parser
Parser.parse(<<-EOR) end
def teardown
Parser.default_parser = @_default_parser
end
def test_RDF
assert_ns("", RDF::URI) do
Parser.parse(<<-EOR)
#{make_xmldecl} #{make_xmldecl}
<RDF/> <RDF/>
EOR EOR
end end
assert_ns("", RDF::URI) do assert_ns("", RDF::URI) do
Parser.parse(<<-EOR) Parser.parse(<<-EOR)
#{make_xmldecl} #{make_xmldecl}
<RDF xmlns="hoge"/> <RDF xmlns="hoge"/>
EOR EOR
end end
assert_ns("rdf", RDF::URI) do assert_ns("rdf", RDF::URI) do
Parser.parse(<<-EOR) Parser.parse(<<-EOR)
#{make_xmldecl} #{make_xmldecl}
<rdf:RDF xmlns:rdf="hoge"/> <rdf:RDF xmlns:rdf="hoge"/>
EOR EOR
end end
assert_parse(<<-EOR, :missing_tag, "channel", "RDF") assert_parse(<<-EOR, :missing_tag, "channel", "RDF")
#{make_xmldecl} #{make_xmldecl}
<rdf:RDF xmlns:rdf="#{RSS::RDF::URI}"/> <rdf:RDF xmlns:rdf="#{RSS::RDF::URI}"/>
EOR EOR
assert_parse(<<-EOR, :missing_tag, "channel", "RDF") assert_parse(<<-EOR, :missing_tag, "channel", "RDF")
#{make_xmldecl} #{make_xmldecl}
<RDF xmlns="#{RSS::RDF::URI}"/> <RDF xmlns="#{RSS::RDF::URI}"/>
EOR EOR
assert_parse(<<-EOR, :missing_tag, "channel", "RDF") assert_parse(<<-EOR, :missing_tag, "channel", "RDF")
#{make_xmldecl} #{make_xmldecl}
<RDF xmlns="#{RSS::RDF::URI}"/> <RDF xmlns="#{RSS::RDF::URI}"/>
EOR EOR
assert_parse(make_RDF(<<-EOR), :missing_tag, "item", "RDF") assert_parse(make_RDF(<<-EOR), :missing_tag, "item", "RDF")
#{make_channel} #{make_channel}
EOR EOR
assert_parse(make_RDF(<<-EOR), :missing_tag, "item", "RDF") assert_parse(make_RDF(<<-EOR), :missing_tag, "item", "RDF")
#{make_channel} #{make_channel}
#{make_image} #{make_image}
EOR EOR
assert_parse(make_RDF(<<-EOR), :missing_tag, "item", "RDF") assert_parse(make_RDF(<<-EOR), :missing_tag, "item", "RDF")
#{make_channel} #{make_channel}
#{make_textinput} #{make_textinput}
EOR EOR
assert_too_much_tag("image", "RDF") do assert_too_much_tag("image", "RDF") do
Parser.parse(make_RDF(<<-EOR)) Parser.parse(make_RDF(<<-EOR))
#{make_channel} #{make_channel}
#{make_image} #{make_image}
#{make_image} #{make_image}
#{make_item} #{make_item}
#{make_textinput} #{make_textinput}
EOR 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 end
assert_parse(rss, :nothing_raised)
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 end
end def test_channel
def test_channel assert_parse(make_RDF(<<-EOR), :missing_attribute, "channel", "about")
assert_parse(make_RDF(<<-EOR), :missing_attribute, "channel", "about")
<channel /> <channel />
EOR EOR
assert_parse(make_RDF(<<-EOR), :missing_tag, "title", "channel") assert_parse(make_RDF(<<-EOR), :missing_tag, "title", "channel")
<channel rdf:about="http://example.com/"/> <channel rdf:about="http://example.com/"/>
EOR EOR
assert_parse(make_RDF(<<-EOR), :missing_tag, "link", "channel") assert_parse(make_RDF(<<-EOR), :missing_tag, "link", "channel")
<channel rdf:about="http://example.com/"> <channel rdf:about="http://example.com/">
<title>hoge</title> <title>hoge</title>
</channel> </channel>
EOR EOR
assert_parse(make_RDF(<<EOR), :missing_tag, "description", "channel") assert_parse(make_RDF(<<EOR), :missing_tag, "description", "channel")
<channel rdf:about="http://example.com/"> <channel rdf:about="http://example.com/">
<title>hoge</title> <title>hoge</title>
<link>http://example.com/</link> <link>http://example.com/</link>
</channel> </channel>
EOR EOR
assert_parse(make_RDF(<<-EOR), :missing_tag, "items", "channel") assert_parse(make_RDF(<<-EOR), :missing_tag, "items", "channel")
<channel rdf:about="http://example.com/"> <channel rdf:about="http://example.com/">
<title>hoge</title> <title>hoge</title>
<link>http://example.com/</link> <link>http://example.com/</link>
@ -132,7 +140,7 @@ EOR
</channel> </channel>
EOR EOR
assert_parse(make_RDF(<<-EOR), :missing_attribute, "image", "resource") assert_parse(make_RDF(<<-EOR), :missing_attribute, "image", "resource")
<channel rdf:about="http://example.com/"> <channel rdf:about="http://example.com/">
<title>hoge</title> <title>hoge</title>
<link>http://example.com/</link> <link>http://example.com/</link>
@ -141,7 +149,7 @@ EOR
</channel> </channel>
EOR EOR
assert_parse(make_RDF(<<-EOR), :missing_tag, "items", "channel") assert_parse(make_RDF(<<-EOR), :missing_tag, "items", "channel")
<channel rdf:about="http://example.com/"> <channel rdf:about="http://example.com/">
<title>hoge</title> <title>hoge</title>
<link>http://example.com/</link> <link>http://example.com/</link>
@ -150,7 +158,7 @@ EOR
</channel> </channel>
EOR EOR
rss = make_RDF(<<-EOR) rss = make_RDF(<<-EOR)
<channel rdf:about="http://example.com/"> <channel rdf:about="http://example.com/">
<title>hoge</title> <title>hoge</title>
<link>http://example.com/</link> <link>http://example.com/</link>
@ -160,15 +168,15 @@ EOR
</channel> </channel>
EOR EOR
assert_missing_tag("Seq", "items") do assert_missing_tag("Seq", "items") do
Parser.parse(rss) Parser.parse(rss)
end end
assert_missing_tag("item", "RDF") do assert_missing_tag("item", "RDF") do
Parser.parse(rss, false).validate Parser.parse(rss, false).validate
end end
assert_parse(make_RDF(<<-EOR), :missing_tag, "item", "RDF") assert_parse(make_RDF(<<-EOR), :missing_tag, "item", "RDF")
<channel rdf:about="http://example.com/"> <channel rdf:about="http://example.com/">
<title>hoge</title> <title>hoge</title>
<link>http://example.com/</link> <link>http://example.com/</link>
@ -181,7 +189,7 @@ EOR
</channel> </channel>
EOR EOR
assert_parse(make_RDF(<<-EOR), :missing_attribute, "textinput", "resource") assert_parse(make_RDF(<<-EOR), :missing_attribute, "textinput", "resource")
<channel rdf:about="http://example.com/"> <channel rdf:about="http://example.com/">
<title>hoge</title> <title>hoge</title>
<link>http://example.com/</link> <link>http://example.com/</link>
@ -195,7 +203,7 @@ EOR
</channel> </channel>
EOR EOR
assert_parse(make_RDF(<<-EOR), :missing_tag, "item", "RDF") assert_parse(make_RDF(<<-EOR), :missing_tag, "item", "RDF")
<channel rdf:about="http://example.com/"> <channel rdf:about="http://example.com/">
<title>hoge</title> <title>hoge</title>
<link>http://example.com/</link> <link>http://example.com/</link>
@ -209,30 +217,30 @@ EOR
</channel> </channel>
EOR EOR
end end
def test_image def test_image
assert_parse(make_RDF(<<-EOR), :missing_attribute, "image", "about") assert_parse(make_RDF(<<-EOR), :missing_attribute, "image", "about")
#{make_channel} #{make_channel}
<image> <image>
</image> </image>
EOR EOR
assert_parse(make_RDF(<<-EOR), :missing_tag, "title", "image") assert_parse(make_RDF(<<-EOR), :missing_tag, "title", "image")
#{make_channel} #{make_channel}
<image rdf:about="http://example.com/hoge.png"> <image rdf:about="http://example.com/hoge.png">
</image> </image>
EOR EOR
assert_parse(make_RDF(<<-EOR), :missing_tag, "url", "image") assert_parse(make_RDF(<<-EOR), :missing_tag, "url", "image")
#{make_channel} #{make_channel}
<image rdf:about="http://example.com/hoge.png"> <image rdf:about="http://example.com/hoge.png">
<title>hoge</title> <title>hoge</title>
</image> </image>
EOR EOR
assert_parse(make_RDF(<<-EOR), :missing_tag, "link", "image") assert_parse(make_RDF(<<-EOR), :missing_tag, "link", "image")
#{make_channel} #{make_channel}
<image rdf:about="http://example.com/hoge.png"> <image rdf:about="http://example.com/hoge.png">
<title>hoge</title> <title>hoge</title>
@ -240,7 +248,7 @@ EOR
</image> </image>
EOR EOR
rss = make_RDF(<<-EOR) rss = make_RDF(<<-EOR)
#{make_channel} #{make_channel}
<image rdf:about="http://example.com/hoge.png"> <image rdf:about="http://example.com/hoge.png">
<title>hoge</title> <title>hoge</title>
@ -249,15 +257,15 @@ EOR
</image> </image>
EOR EOR
assert_missing_tag("url", "image") do assert_missing_tag("url", "image") do
Parser.parse(rss) Parser.parse(rss)
end end
assert_missing_tag("item", "RDF") do assert_missing_tag("item", "RDF") do
Parser.parse(rss, false).validate Parser.parse(rss, false).validate
end end
assert_parse(make_RDF(<<-EOR), :missing_tag, "item", "RDF") assert_parse(make_RDF(<<-EOR), :missing_tag, "item", "RDF")
#{make_channel} #{make_channel}
<image rdf:about="http://example.com/hoge.png"> <image rdf:about="http://example.com/hoge.png">
<title>hoge</title> <title>hoge</title>
@ -266,25 +274,25 @@ EOR
</image> </image>
EOR EOR
end end
def test_item def test_item
assert_parse(make_RDF(<<-EOR), :missing_attribute, "item", "about") assert_parse(make_RDF(<<-EOR), :missing_attribute, "item", "about")
#{make_channel} #{make_channel}
#{make_image} #{make_image}
<item> <item>
</item> </item>
EOR EOR
assert_parse(make_RDF(<<-EOR), :missing_tag, "title", "item") assert_parse(make_RDF(<<-EOR), :missing_tag, "title", "item")
#{make_channel} #{make_channel}
#{make_image} #{make_image}
<item rdf:about="http://example.com/hoge.html"> <item rdf:about="http://example.com/hoge.html">
</item> </item>
EOR EOR
assert_parse(make_RDF(<<-EOR), :missing_tag, "link", "item") assert_parse(make_RDF(<<-EOR), :missing_tag, "link", "item")
#{make_channel} #{make_channel}
#{make_image} #{make_image}
<item rdf:about="http://example.com/hoge.html"> <item rdf:about="http://example.com/hoge.html">
@ -292,8 +300,8 @@ EOR
</item> </item>
EOR EOR
assert_too_much_tag("title", "item") do assert_too_much_tag("title", "item") do
Parser.parse(make_RDF(<<-EOR)) Parser.parse(make_RDF(<<-EOR))
#{make_channel} #{make_channel}
#{make_image} #{make_image}
<item rdf:about="http://example.com/hoge.html"> <item rdf:about="http://example.com/hoge.html">
@ -302,9 +310,9 @@ EOR
<link>http://example.com/hoge.html</link> <link>http://example.com/hoge.html</link>
</item> </item>
EOR EOR
end end
assert_parse(make_RDF(<<-EOR), :nothing_raised) assert_parse(make_RDF(<<-EOR), :nothing_raised)
#{make_channel} #{make_channel}
#{make_image} #{make_image}
<item rdf:about="http://example.com/hoge.html"> <item rdf:about="http://example.com/hoge.html">
@ -313,7 +321,7 @@ EOR
</item> </item>
EOR EOR
assert_parse(make_RDF(<<-EOR), :nothing_raised) assert_parse(make_RDF(<<-EOR), :nothing_raised)
#{make_channel} #{make_channel}
#{make_image} #{make_image}
<item rdf:about="http://example.com/hoge.html"> <item rdf:about="http://example.com/hoge.html">
@ -323,11 +331,11 @@ EOR
</item> </item>
EOR EOR
end end
def test_textinput def test_textinput
assert_parse(make_RDF(<<-EOR), :missing_attribute, "textinput", "about") assert_parse(make_RDF(<<-EOR), :missing_attribute, "textinput", "about")
#{make_channel} #{make_channel}
#{make_image} #{make_image}
#{make_item} #{make_item}
@ -335,7 +343,7 @@ EOR
</textinput> </textinput>
EOR EOR
assert_parse(make_RDF(<<-EOR), :missing_tag, "title", "textinput") assert_parse(make_RDF(<<-EOR), :missing_tag, "title", "textinput")
#{make_channel} #{make_channel}
#{make_image} #{make_image}
#{make_item} #{make_item}
@ -343,7 +351,7 @@ EOR
</textinput> </textinput>
EOR EOR
assert_parse(make_RDF(<<-EOR), :missing_tag, "description", "textinput") assert_parse(make_RDF(<<-EOR), :missing_tag, "description", "textinput")
#{make_channel} #{make_channel}
#{make_image} #{make_image}
#{make_item} #{make_item}
@ -352,8 +360,8 @@ EOR
</textinput> </textinput>
EOR EOR
assert_too_much_tag("title", "textinput") do assert_too_much_tag("title", "textinput") do
Parser.parse(make_RDF(<<-EOR)) Parser.parse(make_RDF(<<-EOR))
#{make_channel} #{make_channel}
#{make_image} #{make_image}
#{make_item} #{make_item}
@ -363,9 +371,9 @@ EOR
<description>hogehoge</description> <description>hogehoge</description>
</textinput> </textinput>
EOR EOR
end end
assert_parse(make_RDF(<<-EOR), :missing_tag, "name", "textinput") assert_parse(make_RDF(<<-EOR), :missing_tag, "name", "textinput")
#{make_channel} #{make_channel}
#{make_image} #{make_image}
#{make_item} #{make_item}
@ -375,7 +383,7 @@ EOR
</textinput> </textinput>
EOR EOR
assert_parse(make_RDF(<<-EOR), :missing_tag, "link", "textinput") assert_parse(make_RDF(<<-EOR), :missing_tag, "link", "textinput")
#{make_channel} #{make_channel}
#{make_image} #{make_image}
#{make_item} #{make_item}
@ -386,7 +394,7 @@ EOR
</textinput> </textinput>
EOR EOR
assert_parse(make_RDF(<<-EOR), :nothing_raised) assert_parse(make_RDF(<<-EOR), :nothing_raised)
#{make_channel} #{make_channel}
#{make_image} #{make_image}
#{make_item} #{make_item}
@ -398,32 +406,34 @@ EOR
</textinput> </textinput>
EOR EOR
end end
def test_ignore def test_ignore
rss = make_RDF(<<-EOR) rss = make_RDF(<<-EOR)
#{make_channel} #{make_channel}
#{make_item} #{make_item}
<a/> <a/>
EOR EOR
assert_parse(rss, :nothing_raised) assert_parse(rss, :nothing_raised)
assert_not_excepted_tag("a", "RDF") do assert_not_excepted_tag("a", "RDF") do
Parser.parse(rss, true, false) Parser.parse(rss, true, false)
end
end
def test_default_parser
assert_nothing_raised() do
Parser.default_parser = RSS::AVAILABLE_PARSERS.first
end
assert_raise(RSS::NotValidXMLParser) do
Parser.default_parser = RSS::Parser
end
end end
end end
def test_default_parser
assert_nothing_raised() do
Parser.default_parser = RSS::AVAILABLE_PARSERS.first
end
assert_raise(RSS::NotValidXMLParser) do
Parser.default_parser = RSS::Parser
end
end
end end

Просмотреть файл

@ -1,124 +1,125 @@
# -*- tab-width: 2 -*- vim: ts=2 # -*- tab-width: 2 -*- vim: ts=2
require "test/unit"
require "cgi" require "cgi"
require "rexml/document" require "rexml/document"
require "rss-testcase"
require "rss/1.0" require "rss/1.0"
require "rss/syndication" require "rss/syndication"
require "common"
class TestSyndication < Test::Unit::TestCase module RSS
include TestRSSMixin class TestSyndication < TestCase
def setup def setup
@prefix = "sy" @prefix = "sy"
@uri = "http://purl.org/rss/1.0/modules/syndication/" @uri = "http://purl.org/rss/1.0/modules/syndication/"
@parents = %w(channel) @parents = %w(channel)
t = Time.iso8601("2000-01-01T12:00:05+00:00") t = Time.iso8601("2000-01-01T12:00:05+00:00")
class << t class << t
alias_method(:to_s, :iso8601) alias_method(:to_s, :iso8601)
end end
@elems = { @elems = {
:updatePeriod => "hourly", :updatePeriod => "hourly",
:updateFrequency => 2, :updateFrequency => 2,
:updateBase => t, :updateBase => t,
} }
@sy_nodes = @elems.collect do |name, value| @sy_nodes = @elems.collect do |name, value|
"<#{@prefix}:#{name}>#{CGI.escapeHTML(value.to_s)}</#{@prefix}:#{name}>" "<#{@prefix}:#{name}>#{CGI.escapeHTML(value.to_s)}</#{@prefix}:#{name}>"
end.join("\n") end.join("\n")
@rss_source = make_RDF(<<-EOR, {@prefix => @uri}) @rss_source = make_RDF(<<-EOR, {@prefix => @uri})
#{make_channel(@sy_nodes)} #{make_channel(@sy_nodes)}
#{make_image()} #{make_image()}
#{make_item()} #{make_item()}
#{make_textinput()} #{make_textinput()}
EOR EOR
@rss = Parser.parse(@rss_source) @rss = Parser.parse(@rss_source)
end
def test_parser
assert_nothing_raised do
Parser.parse(@rss_source)
end end
@elems.each do |tag, value| def test_parser
assert_too_much_tag(tag.to_s, "channel") do
Parser.parse(make_RDF(<<-EOR, {@prefix => @uri})) 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 + ">" + #{make_channel(("<" + @prefix + ":" + tag.to_s + ">" +
CGI.escapeHTML(value.to_s) + CGI.escapeHTML(value.to_s) +
"</" + @prefix + ":" + tag.to_s + ">") * 2)} "</" + @prefix + ":" + tag.to_s + ">") * 2)}
#{make_item} #{make_item}
EOR 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 end
end end
%w(-2 0.3 -0.4).each do |x| def test_accessor
@parents.each do |parent|
assert_not_available_value("updateBase", x) do t = Time.iso8601("2003-01-01T12:00:23+09:00")
@rss.send(parent).sy_updateBase = x 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
end end
end
end %w(hourly daily weekly monthly yearly).each do |x|
@parents.each do |parent|
def test_to_s assert_nothing_raised do
@rss.send(parent).sy_updatePeriod = x
@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
%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
end end
end end

Просмотреть файл

@ -1,135 +1,137 @@
# -*- tab-width: 2 -*- vim: ts=2 # -*- tab-width: 2 -*- vim: ts=2
require "test/unit"
require "cgi" require "cgi"
require "rexml/document" require "rexml/document"
require "rss-testcase"
require "rss/1.0" require "rss/1.0"
require "rss/2.0" require "rss/2.0"
require "rss/trackback" require "rss/trackback"
require "common"
class TestTrackBack < Test::Unit::TestCase module RSS
include TestRSSMixin class TestTrackBack < TestCase
def setup def setup
@prefix = "trackback" @prefix = "trackback"
@uri = "http://madskills.com/public/xml/rss/module/trackback/" @uri = "http://madskills.com/public/xml/rss/module/trackback/"
@parents = %w(item) @parents = %w(item)
@elems = { @elems = {
:ping => "http://bar.com/tb.cgi?tb_id=rssplustrackback", :ping => "http://bar.com/tb.cgi?tb_id=rssplustrackback",
:about => "http://foo.com/trackback/tb.cgi?tb_id=20020923", :about => "http://foo.com/trackback/tb.cgi?tb_id=20020923",
} }
@content_nodes = @elems.collect do |name, value| @content_nodes = @elems.collect do |name, value|
"<#{@prefix}:#{name} rdf:resource=\"#{CGI.escapeHTML(value.to_s)}\"/>" "<#{@prefix}:#{name} rdf:resource=\"#{CGI.escapeHTML(value.to_s)}\"/>"
end.join("\n") end.join("\n")
@content_nodes2 = @elems.collect do |name, value| @content_nodes2 = @elems.collect do |name, value|
"<#{@prefix}:#{name}>#{CGI.escapeHTML(value.to_s)}</#{@prefix}:#{name}>" "<#{@prefix}:#{name}>#{CGI.escapeHTML(value.to_s)}</#{@prefix}:#{name}>"
end.join("\n") end.join("\n")
@rss_source = make_RDF(<<-EOR, {@prefix => @uri}) @rss_source = make_RDF(<<-EOR, {@prefix => @uri})
#{make_channel()} #{make_channel()}
#{make_image()} #{make_image()}
#{make_item(@content_nodes)} #{make_item(@content_nodes)}
#{make_textinput()} #{make_textinput()}
EOR EOR
@rss = Parser.parse(@rss_source) @rss = Parser.parse(@rss_source)
@rss2_source = make_Rss2(nil, {@prefix => @uri}) do @rss2_source = make_Rss2(nil, {@prefix => @uri}) do
make_channel2(nil) do make_channel2(nil) do
make_item2(@content_nodes2) make_item2(@content_nodes2)
end
end
@rss2 = Parser.parse(@rss2_source, false)
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|
accessor = "#{RSS::TRACKBACK_PREFIX}_#{name}"
target_accessor = "resource"
target = @rss.send(parent).send(accessor)
target2 = @rss2.channel.send(parent, -1)
assert_equal(value, target.send(target_accessor))
assert_equal(value, target2.send(accessor))
target.send("#{target_accessor}=", new_value[name].to_s)
if name == :about
# abount is zero or more
target2.send("#{accessor}=", 0, new_value[name].to_s)
else
target2.send("#{accessor}=", new_value[name].to_s)
end end
assert_equal(new_value[name], target.send(target_accessor))
assert_equal(new_value[name], target2.send(accessor))
end end
@rss2 = Parser.parse(@rss2_source, false)
end end
end def test_parser
def test_to_s assert_nothing_raised do
Parser.parse(@rss_source)
@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
@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 end
REXML::Document.new(@rss_source).root.each_element do |parent| def test_accessor
if @parents.include?(parent.name)
parent.each_element do |elem| new_value = {
if elem.namespace == @uri :ping => "http://baz.com/trackback/tb.cgi?tb_id=20030808",
assert_equal(elem.attributes["resource"], @elems[elem.name.intern]) :about => "http://hoge.com/trackback/tb.cgi?tb_id=90030808",
}
@elems.each do |name, value|
@parents.each do |parent|
accessor = "#{RSS::TRACKBACK_PREFIX}_#{name}"
target_accessor = "resource"
target = @rss.send(parent).send(accessor)
target2 = @rss2.channel.send(parent, -1)
assert_equal(value, target.send(target_accessor))
assert_equal(value, target2.send(accessor))
target.send("#{target_accessor}=", new_value[name].to_s)
if name == :about
# abount is zero or more
target2.send("#{accessor}=", 0, new_value[name].to_s)
else
target2.send("#{accessor}=", new_value[name].to_s)
end
assert_equal(new_value[name], target.send(target_accessor))
assert_equal(new_value[name], target2.send(accessor))
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 end
end end
end end
end end

Просмотреть файл

@ -1,109 +1,110 @@
# -*- tab-width: 2 -*- vim: ts=2 # -*- tab-width: 2 -*- vim: ts=2
require "test/unit"
require "rexml/document" require "rexml/document"
require "rss-testcase"
require "rss/1.0" require "rss/1.0"
require "rss/xml-stylesheet" require "rss/xml-stylesheet"
require "common"
class TestXMLStyleSheet < Test::Unit::TestCase module RSS
include TestRSSMixin class TestXMLStyleSheet < TestCase
def test_accessor def test_accessor
[ [
{:href => "a.xsl", :type => "text/xsl"}, {:href => "a.xsl", :type => "text/xsl"},
{:media => "print", :title => "FOO"}, {:media => "print", :title => "FOO"},
{:charset => "UTF-8", :alternate => "yes"}, {:charset => "UTF-8", :alternate => "yes"},
].each do |attrs| ].each do |attrs|
assert_xml_stylesheet_attrs(XMLStyleSheet.new(*attrs), attrs) assert_xml_stylesheet_attrs(XMLStyleSheet.new(*attrs), attrs)
end
end
def test_to_s
[
{:href => "a.xsl", :type => "text/xsl"},
{:type => "text/xsl"},
{:href => "a.xsl", :guess_type => "text/xsl"},
{:href => "a.css", :type => "text/css"},
{:href => "a.css", :type => "text/xsl",
:guess_type => "text/css"},
{:href => "a.xsl", :type => "text/xsl",
:title => "sample", :media => "printer",
:charset => "UTF-8", :alternate => "yes"},
{:href => "a.css", :guess_type => "text/css",
:alternate => "no"},
{:type => "text/xsl", :title => "sample",
:media => "printer", :charset => "UTF-8",
:alternate => "yes"},
].each do |attrs|
target, contents = parse_pi(XMLStyleSheet.new(*attrs).to_s)
assert_xml_stylesheet(target, XMLStyleSheet.new(*contents), attrs)
end
end
def test_bad_alternate
%w(a ___ ??? BAD_ALTERNATE).each do |value|
xss = XMLStyleSheet.new
assert_raise(NotAvailableValueError) do
xss.alternate = value
end
xss.do_validate = false
assert_nothing_raised do
xss.alternate = value
end
assert_nil(xss.alternate)
end
end
def test_parse
[
[{:href => "a.xsl", :type => "text/xsl"},],
[{:media => "print", :title => "FOO"},],
[{:charset => "UTF-8", :alternate => "yes"},],
[{:href => "a.xsl", :type => "text/xsl"},
{:type => "text/xsl"},
{:href => "a.xsl", :guess_type => "text/xsl"},
{:href => "a.css", :type => "text/css"},
{:href => "a.css", :type => "text/xsl",
:guess_type => "text/css"},
{:href => "a.xsl", :type => "text/xsl",
:title => "sample", :media => "printer",
:charset => "UTF-8", :alternate => "yes"},
{:href => "a.css", :guess_type => "text/css",
:alternate => "no"},
{:type => "text/xsl", :title => "sample",
:media => "printer", :charset => "UTF-8",
:alternate => "yes"},],
].each do |xsss|
doc = REXML::Document.new(make_sample_RDF)
root = doc.root
xsss.each do |xss|
content = xss.collect do |key, name|
%Q[#{key}="#{name}"]
end.join(" ")
pi = REXML::Instruction.new("xml-stylesheet", content)
root.previous_sibling = pi
end
rss = Parser.parse(doc.to_s)
have_href_xsss = xsss.find_all {|xss| xss.has_key?(:href)}
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])
end end
end end
end
def parse_pi(pi) def test_to_s
/\A\s*<\?(\S+)([^(?:\?>)]+)\?>\s*\z/ =~ pi [
target = $1 {:href => "a.xsl", :type => "text/xsl"},
dummy = REXML::Document.new("<dummy #{$2}/>").root {:type => "text/xsl"},
contents = {} {:href => "a.xsl", :guess_type => "text/xsl"},
dummy.attributes.each do |name, value| {:href => "a.css", :type => "text/css"},
contents[name] = value {:href => "a.css", :type => "text/xsl",
:guess_type => "text/css"},
{:href => "a.xsl", :type => "text/xsl",
:title => "sample", :media => "printer",
:charset => "UTF-8", :alternate => "yes"},
{:href => "a.css", :guess_type => "text/css",
:alternate => "no"},
{:type => "text/xsl", :title => "sample",
:media => "printer", :charset => "UTF-8",
:alternate => "yes"},
].each do |attrs|
target, contents = parse_pi(XMLStyleSheet.new(*attrs).to_s)
assert_xml_stylesheet(target, XMLStyleSheet.new(*contents), attrs)
end
end end
[target, contents]
end
def test_bad_alternate
%w(a ___ ??? BAD_ALTERNATE).each do |value|
xss = XMLStyleSheet.new
assert_raise(NotAvailableValueError) do
xss.alternate = value
end
xss.do_validate = false
assert_nothing_raised do
xss.alternate = value
end
assert_nil(xss.alternate)
end
end
def test_parse
[
[{:href => "a.xsl", :type => "text/xsl"},],
[{:media => "print", :title => "FOO"},],
[{:charset => "UTF-8", :alternate => "yes"},],
[{:href => "a.xsl", :type => "text/xsl"},
{:type => "text/xsl"},
{:href => "a.xsl", :guess_type => "text/xsl"},
{:href => "a.css", :type => "text/css"},
{:href => "a.css", :type => "text/xsl",
:guess_type => "text/css"},
{:href => "a.xsl", :type => "text/xsl",
:title => "sample", :media => "printer",
:charset => "UTF-8", :alternate => "yes"},
{:href => "a.css", :guess_type => "text/css",
:alternate => "no"},
{:type => "text/xsl", :title => "sample",
:media => "printer", :charset => "UTF-8",
:alternate => "yes"},],
].each do |xsss|
doc = REXML::Document.new(make_sample_RDF)
root = doc.root
xsss.each do |xss|
content = xss.collect do |key, name|
%Q[#{key}="#{name}"]
end.join(" ")
pi = REXML::Instruction.new("xml-stylesheet", content)
root.previous_sibling = pi
end
rss = Parser.parse(doc.to_s)
have_href_xsss = xsss.find_all {|xss| xss.has_key?(:href)}
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])
end
end
end
def parse_pi(pi)
/\A\s*<\?(\S+)([^(?:\?>)]+)\?>\s*\z/ =~ pi
target = $1
dummy = REXML::Document.new("<dummy #{$2}/>").root
contents = {}
dummy.attributes.each do |name, value|
contents[name] = value
end
[target, contents]
end
end
end end