mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
* lib/rss/{rss,parser,0.9,1.0,2.0}.rb: supported RSS 0.9x/2.0
validation and validation which disregard order of elements. * test/rss/test_parser.rb: added tests for RSS 0.9x/2.0 validation. * test/rss/{test_trackback,rss-testcase}.rb: fixed no good method name. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@6590 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
9800838ff0
commit
e289fcf81a
9 changed files with 446 additions and 113 deletions
|
@ -19,6 +19,7 @@ module RSS
|
|||
LINK_VALUE = "http://xml.com/pub"
|
||||
URL_VALUE = "http://xml.com/universal/images/xml_tiny.gif"
|
||||
NAME_VALUE = "hogehoge"
|
||||
LANGUAGE_VALUE = "ja"
|
||||
DESCRIPTION_VALUE = "
|
||||
XML.com features a rich mix of information and services
|
||||
for the XML community.
|
||||
|
@ -28,6 +29,18 @@ module RSS
|
|||
"http://xml.com/pub/2000/08/09/rdfdb/index.html",
|
||||
]
|
||||
|
||||
CLOUD_DOMAIN = "data.ourfavoritesongs.com"
|
||||
CLOUD_PORT = "80"
|
||||
CLOUD_PATH = "/RPC2"
|
||||
CLOUD_REGISTER_PROCEDURE = "ourFavoriteSongs.rssPleaseNotify"
|
||||
CLOUD_PROTOCOL = "xml-rpc"
|
||||
|
||||
ENCLOSURE_URL = "http://www.scripting.com/mp3s/weatherReportSuite.mp3"
|
||||
ENCLOSURE_LENGTH = "12216320"
|
||||
ENCLOSURE_TYPE = "audio/mpeg"
|
||||
|
||||
CATEGORY_DOMAIN = "http://www.superopendirectory.com/"
|
||||
|
||||
def default_test
|
||||
# This class isn't tested
|
||||
end
|
||||
|
@ -116,7 +129,7 @@ EOT
|
|||
EOR
|
||||
end
|
||||
|
||||
def make_Rss2(content=nil, xmlns=[])
|
||||
def make_rss20(content=nil, xmlns=[])
|
||||
<<-EORSS
|
||||
#{make_xmldecl}
|
||||
<rss version="2.0"
|
||||
|
@ -126,12 +139,13 @@ EOR
|
|||
EORSS
|
||||
end
|
||||
|
||||
def make_channel2(content=nil)
|
||||
def make_channel20(content=nil)
|
||||
<<-EOC
|
||||
<channel>
|
||||
<title>#{TITLE_VALUE}</title>
|
||||
<link>#{LINK_VALUE}</link>
|
||||
<description>#{DESCRIPTION_VALUE}</description>
|
||||
<language>#{LANGUAGE_VALUE}</language>
|
||||
|
||||
<image>
|
||||
<url>#{RDF_RESOURCE}</url>
|
||||
|
@ -142,6 +156,9 @@ EORSS
|
|||
#{RESOURCES.collect do |res| '<item><link>' + res + '</link></item>' end.join("\n")}
|
||||
|
||||
<textInput>
|
||||
<title>#{TITLE_VALUE}</title>
|
||||
<description>#{DESCRIPTION_VALUE}</description>
|
||||
<name>#{NAME_VALUE}</name>
|
||||
<link>#{RDF_RESOURCE}</link>
|
||||
</textInput>
|
||||
|
||||
|
@ -150,7 +167,7 @@ EORSS
|
|||
EOC
|
||||
end
|
||||
|
||||
def make_item2(content=nil)
|
||||
def make_item20(content=nil)
|
||||
<<-EOI
|
||||
<item>
|
||||
<title>#{TITLE_VALUE}</title>
|
||||
|
@ -160,5 +177,17 @@ EOC
|
|||
</item>
|
||||
EOI
|
||||
end
|
||||
|
||||
def make_cloud20
|
||||
<<-EOC
|
||||
<cloud
|
||||
domain="#{CLOUD_DOMAIN}"
|
||||
port="#{CLOUD_PORT}"
|
||||
path="#{CLOUD_PATH}"
|
||||
registerProcedure="#{CLOUD_REGISTER_PROCEDURE}"
|
||||
protocol="#{CLOUD_PROTOCOL}" />
|
||||
EOC
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
|
|
|
@ -3,6 +3,8 @@
|
|||
require "rss-testcase"
|
||||
|
||||
require "rss/1.0"
|
||||
require "rss/2.0"
|
||||
require "rss/dublincore"
|
||||
|
||||
module RSS
|
||||
class TestParser < TestCase
|
||||
|
@ -76,14 +78,19 @@ EOR
|
|||
EOR
|
||||
end
|
||||
|
||||
assert_not_excepted_tag("image", "RDF") do
|
||||
Parser.parse(make_RDF(<<-EOR))
|
||||
assert_parse(make_RDF(<<-EOR), :nothing_raised)
|
||||
#{make_channel}
|
||||
#{make_item}
|
||||
#{make_image}
|
||||
#{make_textinput}
|
||||
EOR
|
||||
end
|
||||
|
||||
assert_parse(make_RDF(<<-EOR), :nothing_raised)
|
||||
#{make_channel}
|
||||
#{make_item}
|
||||
#{make_textinput}
|
||||
#{make_image}
|
||||
EOR
|
||||
|
||||
assert_parse(make_RDF(<<-EOR), :nothing_raised)
|
||||
#{make_channel}
|
||||
|
@ -279,23 +286,6 @@ EOR
|
|||
</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">
|
||||
|
@ -305,6 +295,23 @@ EOR
|
|||
</image>
|
||||
EOR
|
||||
|
||||
rss = make_RDF(<<-EOR)
|
||||
#{make_channel}
|
||||
<image rdf:about="http://example.com/hoge.png">
|
||||
<link>http://example.com/</link>
|
||||
<url>http://example.com/hoge.png</url>
|
||||
<title>hoge</title>
|
||||
</image>
|
||||
EOR
|
||||
|
||||
assert_missing_tag("item", "RDF") do
|
||||
Parser.parse(rss)
|
||||
end
|
||||
|
||||
assert_missing_tag("item", "RDF") do
|
||||
Parser.parse(rss, false).validate
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
def test_item
|
||||
|
@ -439,6 +446,142 @@ EOR
|
|||
|
||||
end
|
||||
|
||||
def test_rss20
|
||||
|
||||
assert_parse(make_rss20(<<-EOR), :missing_tag, "channel", "rss")
|
||||
EOR
|
||||
|
||||
assert_parse(make_rss20(<<-EOR), :nothing_raised)
|
||||
#{make_channel20("")}
|
||||
EOR
|
||||
|
||||
end
|
||||
|
||||
def test_cloud20
|
||||
|
||||
attrs = [
|
||||
["domain", CLOUD_DOMAIN],
|
||||
["port", CLOUD_PORT],
|
||||
["path", CLOUD_PATH],
|
||||
["registerProcedure", CLOUD_REGISTER_PROCEDURE],
|
||||
["protocol", CLOUD_PROTOCOL],
|
||||
]
|
||||
|
||||
(attrs.size + 1).times do |i|
|
||||
missing_attr = attrs[i]
|
||||
if missing_attr
|
||||
meth = :missing_attribute
|
||||
args = ["cloud", missing_attr[0]]
|
||||
else
|
||||
meth = :nothing_raised
|
||||
args = []
|
||||
end
|
||||
|
||||
cloud_attrs = []
|
||||
attrs.each_with_index do |attr, j|
|
||||
unless i == j
|
||||
cloud_attrs << %Q[#{attr[0]}="#{attr[1]}"]
|
||||
end
|
||||
end
|
||||
|
||||
assert_parse(make_rss20(<<-EOR), meth, *args)
|
||||
#{make_channel20(%Q[<cloud #{cloud_attrs.join("\n")}/>])}
|
||||
EOR
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
def test_source20
|
||||
|
||||
assert_parse(make_rss20(<<-EOR), :missing_attribute, "source", "url")
|
||||
#{make_channel20(make_item20(%Q[<source>Example</source>]))}
|
||||
EOR
|
||||
|
||||
assert_parse(make_rss20(<<-EOR), :nothing_raised)
|
||||
#{make_channel20(make_item20(%Q[<source url="http://example.com/" />]))}
|
||||
EOR
|
||||
|
||||
assert_parse(make_rss20(<<-EOR), :nothing_raised)
|
||||
#{make_channel20(make_item20(%Q[<source url="http://example.com/">Example</source>]))}
|
||||
EOR
|
||||
end
|
||||
|
||||
def test_enclosure20
|
||||
|
||||
attrs = [
|
||||
["url", ENCLOSURE_URL],
|
||||
["length", ENCLOSURE_LENGTH],
|
||||
["type", ENCLOSURE_TYPE],
|
||||
]
|
||||
|
||||
(attrs.size + 1).times do |i|
|
||||
missing_attr = attrs[i]
|
||||
if missing_attr
|
||||
meth = :missing_attribute
|
||||
args = ["enclosure", missing_attr[0]]
|
||||
else
|
||||
meth = :nothing_raised
|
||||
args = []
|
||||
end
|
||||
|
||||
enclosure_attrs = []
|
||||
attrs.each_with_index do |attr, j|
|
||||
unless i == j
|
||||
enclosure_attrs << %Q[#{attr[0]}="#{attr[1]}"]
|
||||
end
|
||||
end
|
||||
|
||||
assert_parse(make_rss20(<<-EOR), meth, *args)
|
||||
#{make_channel20(%Q[
|
||||
#{make_item20(%Q[
|
||||
<enclosure
|
||||
#{enclosure_attrs.join("\n")} />
|
||||
])}
|
||||
])}
|
||||
EOR
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
def test_category20
|
||||
|
||||
attrs = [
|
||||
["domain", CATEGORY_DOMAIN],
|
||||
]
|
||||
|
||||
(attrs.size + 1).times do |i|
|
||||
missing_attr = attrs[i]
|
||||
if missing_attr
|
||||
meth = :missing_attribute
|
||||
args = ["category", missing_attr[0]]
|
||||
else
|
||||
meth = :nothing_raised
|
||||
args = []
|
||||
end
|
||||
|
||||
category_attrs = []
|
||||
attrs.each_with_index do |attr, j|
|
||||
unless i == j
|
||||
category_attrs << %Q[#{attr[0]}="#{attr[1]}"]
|
||||
end
|
||||
end
|
||||
|
||||
["", "Example Text"].each do |text|
|
||||
assert_parse(make_rss20(<<-EOR), meth, *args)
|
||||
#{make_channel20(%Q[
|
||||
#{make_item20(%Q[
|
||||
<category
|
||||
#{category_attrs.join("\n")}>#{text}</category>
|
||||
])}
|
||||
])}
|
||||
EOR
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
def test_ignore
|
||||
|
||||
rss = make_RDF(<<-EOR)
|
||||
|
|
|
@ -40,13 +40,13 @@ EOR
|
|||
|
||||
@rss = Parser.parse(@rss_source)
|
||||
|
||||
@rss2_source = make_Rss2(nil, {@prefix => @uri}) do
|
||||
make_channel2(nil) do
|
||||
make_item2(@content_nodes2)
|
||||
@rss20_source = make_rss20(nil, {@prefix => @uri}) do
|
||||
make_channel20(nil) do
|
||||
make_item20(@content_nodes2)
|
||||
end
|
||||
end
|
||||
|
||||
@rss2 = Parser.parse(@rss2_source, false)
|
||||
@rss20 = Parser.parse(@rss20_source, false)
|
||||
end
|
||||
|
||||
def test_parser
|
||||
|
@ -92,18 +92,18 @@ EOR
|
|||
accessor = "#{RSS::TRACKBACK_PREFIX}_#{name}"
|
||||
target_accessor = "resource"
|
||||
target = @rss.send(parent).send(accessor)
|
||||
target2 = @rss2.channel.send(parent, -1)
|
||||
target20 = @rss20.channel.send(parent, -1)
|
||||
assert_equal(value, target.send(target_accessor))
|
||||
assert_equal(value, target2.send(accessor))
|
||||
assert_equal(value, target20.send(accessor))
|
||||
target.send("#{target_accessor}=", new_value[name].to_s)
|
||||
if name == :about
|
||||
# abount is zero or more
|
||||
target2.send("#{accessor}=", 0, new_value[name].to_s)
|
||||
target20.send("#{accessor}=", 0, new_value[name].to_s)
|
||||
else
|
||||
target2.send("#{accessor}=", new_value[name].to_s)
|
||||
target20.send("#{accessor}=", new_value[name].to_s)
|
||||
end
|
||||
assert_equal(new_value[name], target.send(target_accessor))
|
||||
assert_equal(new_value[name], target2.send(accessor))
|
||||
assert_equal(new_value[name], target20.send(accessor))
|
||||
end
|
||||
end
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue