2005-11-17 11:56:03 +03:00
|
|
|
require "cgi"
|
|
|
|
|
2013-02-24 17:10:46 +04:00
|
|
|
require_relative "rss-testcase"
|
2005-11-17 11:56:03 +03:00
|
|
|
|
|
|
|
require "rss/1.0"
|
|
|
|
require "rss/2.0"
|
|
|
|
require "rss/taxonomy"
|
|
|
|
|
|
|
|
module RSS
|
|
|
|
class TestTaxonomy < TestCase
|
2009-03-06 06:56:38 +03:00
|
|
|
|
2005-11-17 11:56:03 +03:00
|
|
|
def setup
|
|
|
|
@prefix = "taxo"
|
|
|
|
@uri = "http://purl.org/rss/1.0/modules/taxonomy/"
|
|
|
|
@dc_prefix = "dc"
|
|
|
|
@dc_uri = "http://purl.org/dc/elements/1.1/"
|
|
|
|
|
|
|
|
@ns = {
|
|
|
|
@prefix => @uri,
|
|
|
|
@dc_prefix => @dc_uri,
|
|
|
|
}
|
2009-03-06 06:56:38 +03:00
|
|
|
|
2005-11-23 11:44:05 +03:00
|
|
|
@topics_parents = %w(channel item)
|
2009-03-06 06:56:38 +03:00
|
|
|
|
2005-11-17 11:56:03 +03:00
|
|
|
@topics_lis = [
|
|
|
|
"http://meerkat.oreillynet.com/?c=cat23",
|
|
|
|
"http://meerkat.oreillynet.com/?c=47",
|
|
|
|
"http://dmoz.org/Computers/Data_Formats/Markup_Languages/XML/",
|
|
|
|
]
|
|
|
|
|
|
|
|
@topics_node = "<#{@prefix}:topics>\n"
|
|
|
|
@topics_node << " <rdf:Bag>\n"
|
|
|
|
@topics_lis.each do |value|
|
|
|
|
resource = CGI.escapeHTML(value)
|
|
|
|
@topics_node << " <rdf:li resource=\"#{resource}\"/>\n"
|
|
|
|
end
|
|
|
|
@topics_node << " </rdf:Bag>\n"
|
|
|
|
@topics_node << "</#{@prefix}:topics>"
|
|
|
|
|
|
|
|
@topic_topics_lis = \
|
|
|
|
[
|
|
|
|
"http://meerkat.oreillynet.com/?c=cat23",
|
|
|
|
"http://dmoz.org/Computers/Data_Formats/Markup_Languages/SGML/",
|
|
|
|
"http://dmoz.org/Computers/Programming/Internet/",
|
|
|
|
]
|
|
|
|
|
|
|
|
@topic_contents = \
|
|
|
|
[
|
|
|
|
{
|
|
|
|
:link => "http://meerkat.oreillynet.com/?c=cat23",
|
|
|
|
:title => "Data: XML",
|
|
|
|
:description => "A Meerkat channel",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
:link => "http://dmoz.org/Computers/Data_Formats/Markup_Languages/XML/",
|
|
|
|
:title => "XML",
|
|
|
|
:subject => "XML",
|
|
|
|
:description => "DMOZ category",
|
|
|
|
:topics => @topic_topics_lis,
|
|
|
|
}
|
|
|
|
]
|
|
|
|
|
|
|
|
@topic_nodes = @topic_contents.collect do |info|
|
|
|
|
link = info[:link]
|
|
|
|
rv = "<#{@prefix}:topic rdf:about=\"#{link}\">\n"
|
|
|
|
info.each do |name, value|
|
|
|
|
case name
|
|
|
|
when :topics
|
2005-11-23 11:44:05 +03:00
|
|
|
rv << " <#{@prefix}:topics>\n"
|
|
|
|
rv << " <rdf:Bag>\n"
|
2005-11-17 11:56:03 +03:00
|
|
|
value.each do |li|
|
|
|
|
resource = CGI.escapeHTML(li)
|
2005-11-23 11:44:05 +03:00
|
|
|
rv << " <rdf:li resource=\"#{resource}\"/>\n"
|
2005-11-17 11:56:03 +03:00
|
|
|
end
|
2005-11-23 11:44:05 +03:00
|
|
|
rv << " </rdf:Bag>\n"
|
|
|
|
rv << " </#{@prefix}:topics>\n"
|
2005-11-17 11:56:03 +03:00
|
|
|
else
|
|
|
|
prefix = (name == :link ? @prefix : @dc_prefix)
|
|
|
|
rv << " <#{prefix}:#{name}>#{value}</#{prefix}:#{name}>\n"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
rv << "</#{@prefix}:topic>"
|
2005-11-23 11:44:05 +03:00
|
|
|
end
|
2009-03-06 06:56:38 +03:00
|
|
|
|
2005-11-17 11:56:03 +03:00
|
|
|
@rss_source = make_RDF(<<-EOR, @ns)
|
|
|
|
#{make_channel(@topics_node)}
|
|
|
|
#{make_image()}
|
|
|
|
#{make_item(@topics_node)}
|
|
|
|
#{make_textinput()}
|
2005-11-23 11:44:05 +03:00
|
|
|
#{@topic_nodes.join("\n")}
|
2005-11-17 11:56:03 +03:00
|
|
|
EOR
|
|
|
|
|
|
|
|
@rss = Parser.parse(@rss_source)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_parser
|
|
|
|
assert_nothing_raised do
|
|
|
|
Parser.parse(@rss_source)
|
|
|
|
end
|
2009-03-06 06:56:38 +03:00
|
|
|
|
2005-11-17 11:56:03 +03:00
|
|
|
assert_too_much_tag("topics", "channel") do
|
|
|
|
Parser.parse(make_RDF(<<-EOR, @ns))
|
|
|
|
#{make_channel(@topics_node * 2)}
|
|
|
|
#{make_item()}
|
|
|
|
EOR
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_too_much_tag("topics", "item") do
|
|
|
|
Parser.parse(make_RDF(<<-EOR, @ns))
|
|
|
|
#{make_channel()}
|
|
|
|
#{make_item(@topics_node * 2)}
|
|
|
|
EOR
|
|
|
|
end
|
|
|
|
end
|
2009-03-06 06:56:38 +03:00
|
|
|
|
2005-11-17 11:56:03 +03:00
|
|
|
def test_accessor
|
|
|
|
topics = @rss.channel.taxo_topics
|
|
|
|
assert_equal(@topics_lis.sort,
|
|
|
|
topics.Bag.lis.collect {|li| li.resource}.sort)
|
2005-11-17 12:01:22 +03:00
|
|
|
assert_equal(@topics_lis.sort, topics.resources.sort)
|
2005-11-17 11:56:03 +03:00
|
|
|
|
|
|
|
assert_equal(@rss.taxo_topics.first, @rss.taxo_topic)
|
|
|
|
|
|
|
|
@topic_contents.each_with_index do |info, i|
|
|
|
|
topic = @rss.taxo_topics[i]
|
|
|
|
info.each do |name, value|
|
|
|
|
case name
|
|
|
|
when :link
|
|
|
|
assert_equal(value, topic.about)
|
|
|
|
assert_equal(value, topic.taxo_link)
|
|
|
|
when :topics
|
2005-11-17 12:01:22 +03:00
|
|
|
assert_equal(value.sort, topic.taxo_topics.resources.sort)
|
2005-11-17 11:56:03 +03:00
|
|
|
else
|
|
|
|
assert_equal(value, topic.__send__("dc_#{name}"))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2009-03-06 06:56:38 +03:00
|
|
|
|
2005-11-17 11:56:03 +03:00
|
|
|
def test_to_s
|
2005-11-23 11:44:05 +03:00
|
|
|
@topics_parents.each do |parent|
|
2005-11-17 11:56:03 +03:00
|
|
|
meth = "taxo_topics_element"
|
2007-11-04 23:36:20 +03:00
|
|
|
assert_equal(@topics_node, @rss.__send__(parent).__send__(meth))
|
2005-11-17 11:56:03 +03:00
|
|
|
end
|
2005-11-23 11:44:05 +03:00
|
|
|
|
|
|
|
@topic_nodes.each_with_index do |node, i|
|
2007-10-07 17:42:22 +04:00
|
|
|
expected_xml = taxo_xmlns_container(node)
|
|
|
|
expected = REXML::Document.new(expected_xml).root.elements[1]
|
|
|
|
actual_xml = taxo_xmlns_container(@rss.taxo_topics[i].to_s(true, ""))
|
|
|
|
actual = REXML::Document.new(actual_xml).root.elements[1]
|
2005-11-23 12:33:49 +03:00
|
|
|
expected_elems = expected.reject {|x| x.is_a?(REXML::Text)}
|
|
|
|
actual_elems = actual.reject {|x| x.is_a?(REXML::Text)}
|
2005-11-23 11:44:05 +03:00
|
|
|
expected_elems.sort! {|x, y| x.name <=> y.name}
|
|
|
|
actual_elems.sort! {|x, y| x.name <=> y.name}
|
|
|
|
assert_equal(expected_elems.collect {|x| x.to_s},
|
|
|
|
actual_elems.collect {|x| x.to_s})
|
|
|
|
assert_equal(expected.attributes.sort, actual.attributes.sort)
|
|
|
|
end
|
2005-11-17 11:56:03 +03:00
|
|
|
end
|
2007-10-07 17:42:22 +04:00
|
|
|
|
|
|
|
private
|
|
|
|
def taxo_xmlns_container(content)
|
|
|
|
xmlns_container({
|
|
|
|
@prefix => @uri,
|
|
|
|
"dc" => "http://purl.org/dc/elements/1.1/",
|
|
|
|
"rdf" => "http://www.w3.org/1999/02/22-rdf-syntax-ns#",
|
|
|
|
},
|
|
|
|
content)
|
|
|
|
end
|
2005-11-17 11:56:03 +03:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|