2004-02-25 14:44:58 +03:00
|
|
|
require "cgi"
|
2004-01-28 06:46:13 +03:00
|
|
|
require "rexml/document"
|
|
|
|
|
2004-03-21 13:03:17 +03:00
|
|
|
require "rss-testcase"
|
|
|
|
|
2004-02-03 17:01:42 +03:00
|
|
|
require "rss/1.0"
|
2004-01-28 06:46:13 +03:00
|
|
|
require "rss/dublincore"
|
|
|
|
|
2004-03-21 13:03:17 +03:00
|
|
|
module RSS
|
2004-10-16 08:51:15 +04:00
|
|
|
class TestDublinCore < TestCase
|
2004-03-21 13:03:17 +03:00
|
|
|
|
2004-10-16 08:51:15 +04:00
|
|
|
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,
|
|
|
|
}
|
2004-03-21 13:03:17 +03:00
|
|
|
|
2004-10-16 08:51:15 +04:00
|
|
|
@dc_nodes = @elems.collect do |name, value|
|
|
|
|
"<#{@prefix}:#{name}>#{value}</#{@prefix}:#{name}>"
|
|
|
|
end.join("\n")
|
2004-03-21 13:03:17 +03:00
|
|
|
|
2004-10-16 08:51:15 +04:00
|
|
|
@rss_source = make_RDF(<<-EOR, {@prefix => @uri})
|
2004-01-28 06:46:13 +03:00
|
|
|
#{make_channel(@dc_nodes)}
|
|
|
|
#{make_image(@dc_nodes)}
|
|
|
|
#{make_item(@dc_nodes)}
|
|
|
|
#{make_textinput(@dc_nodes)}
|
|
|
|
EOR
|
|
|
|
|
2004-10-16 08:51:15 +04:00
|
|
|
@rss = Parser.parse(@rss_source)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_parser
|
|
|
|
assert_nothing_raised do
|
|
|
|
Parser.parse(@rss_source)
|
|
|
|
end
|
|
|
|
|
|
|
|
@elems.each do |tag, value|
|
2005-04-05 10:42:03 +04:00
|
|
|
rss = nil
|
|
|
|
assert_nothing_raised do
|
|
|
|
rss = Parser.parse(make_RDF(<<-EOR, {@prefix => @uri}))
|
2004-01-28 06:46:13 +03:00
|
|
|
#{make_channel(("<" + @prefix + ":" + tag.to_s + ">" +
|
2004-10-16 08:51:15 +04:00
|
|
|
value.to_s +
|
|
|
|
"</" + @prefix + ":" + tag.to_s + ">") * 2)}
|
2004-01-28 06:46:13 +03:00
|
|
|
#{make_item}
|
|
|
|
EOR
|
2004-10-16 08:51:15 +04:00
|
|
|
end
|
2005-04-05 10:42:03 +04:00
|
|
|
plural_reader = "dc_#{tag}" + (tag == :rights ? "es" : "s")
|
|
|
|
values = rss.channel.__send__(plural_reader).collect do |x|
|
|
|
|
val = x.value
|
|
|
|
if val.kind_of?(String)
|
|
|
|
CGI.escapeHTML(val)
|
|
|
|
else
|
|
|
|
val
|
|
|
|
end
|
|
|
|
end
|
|
|
|
assert_equal([value, value], values)
|
2004-10-16 08:51:15 +04:00
|
|
|
end
|
2004-01-28 06:46:13 +03:00
|
|
|
|
2004-10-16 08:51:15 +04:00
|
|
|
end
|
2005-04-05 10:42:03 +04:00
|
|
|
|
|
|
|
def test_singular_accessor
|
2004-10-16 08:51:15 +04:00
|
|
|
new_value = "hoge"
|
2004-01-28 06:46:13 +03:00
|
|
|
|
2004-10-16 08:51:15 +04:00
|
|
|
@elems.each do |name, value|
|
|
|
|
@parents.each do |parent|
|
2005-11-23 12:49:02 +03:00
|
|
|
parsed_value = @rss.__send__(parent).__send__("dc_#{name}")
|
2004-10-16 08:51:15 +04:00
|
|
|
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
|
2005-11-23 12:49:02 +03:00
|
|
|
@rss.__send__(parent).__send__("dc_#{name}=", t.iso8601)
|
|
|
|
assert_equal(t, @rss.__send__(parent).__send__("dc_#{name}"))
|
|
|
|
assert_equal(t, @rss.__send__(parent).date)
|
2005-02-13 17:21:21 +03:00
|
|
|
|
2005-11-23 12:49:02 +03:00
|
|
|
@rss.__send__(parent).date = value
|
|
|
|
assert_equal(value, @rss.__send__(parent).date)
|
|
|
|
assert_equal(value, @rss.__send__(parent).__send__("dc_#{name}"))
|
2004-10-16 08:51:15 +04:00
|
|
|
else
|
2005-11-23 12:49:02 +03:00
|
|
|
@rss.__send__(parent).__send__("dc_#{name}=", new_value)
|
|
|
|
assert_equal(new_value,
|
|
|
|
@rss.__send__(parent).__send__("dc_#{name}"))
|
2004-10-16 08:51:15 +04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2005-04-05 10:42:03 +04:00
|
|
|
end
|
2004-01-28 06:46:13 +03:00
|
|
|
|
2005-04-05 10:42:03 +04:00
|
|
|
def test_plural_accessor
|
|
|
|
new_value = "hoge"
|
|
|
|
|
|
|
|
@elems.each do |name, value|
|
|
|
|
@parents.each do |parent|
|
2005-11-23 12:49:02 +03:00
|
|
|
parsed_value = @rss.__send__(parent).__send__("dc_#{name}")
|
2005-04-05 10:42:03 +04:00
|
|
|
if parsed_value.kind_of?(String)
|
|
|
|
parsed_value = CGI.escapeHTML(parsed_value)
|
|
|
|
end
|
|
|
|
assert_equal(value, parsed_value)
|
|
|
|
|
|
|
|
plural_reader = "dc_#{name}" + (name == :rights ? "es" : "s")
|
|
|
|
klass_name = "DublinCore#{Utils.to_class_name(name.to_s)}"
|
|
|
|
klass = DublinCoreModel.const_get(klass_name)
|
|
|
|
if name == :date
|
|
|
|
t = Time.iso8601("2003-01-01T02:30:23+09:00")
|
|
|
|
class << t
|
|
|
|
alias_method(:to_s, :iso8601)
|
|
|
|
end
|
2005-11-23 12:49:02 +03:00
|
|
|
elems = @rss.__send__(parent).__send__(plural_reader)
|
2005-04-05 10:42:03 +04:00
|
|
|
elems << klass.new(t.iso8601)
|
2005-11-23 12:49:02 +03:00
|
|
|
new_elems = @rss.__send__(parent).__send__(plural_reader)
|
|
|
|
values = new_elems.collect{|x| x.value}
|
|
|
|
assert_equal([@rss.__send__(parent).__send__("dc_#{name}"), t],
|
2005-04-05 10:42:03 +04:00
|
|
|
values)
|
|
|
|
else
|
2005-11-23 12:49:02 +03:00
|
|
|
elems = @rss.__send__(parent).__send__(plural_reader)
|
2005-04-05 10:42:03 +04:00
|
|
|
elems << klass.new(new_value)
|
2005-11-23 12:49:02 +03:00
|
|
|
new_elems = @rss.__send__(parent).__send__(plural_reader)
|
|
|
|
values = new_elems.collect{|x| x.value}
|
|
|
|
assert_equal([
|
|
|
|
@rss.__send__(parent).__send__("dc_#{name}"),
|
|
|
|
new_value
|
|
|
|
],
|
2005-04-05 10:42:03 +04:00
|
|
|
values)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2004-10-16 08:51:15 +04:00
|
|
|
end
|
2004-01-28 06:46:13 +03:00
|
|
|
|
2004-10-16 08:51:15 +04:00
|
|
|
def test_to_s
|
|
|
|
@elems.each do |name, value|
|
|
|
|
excepted = "<#{@prefix}:#{name}>#{value}</#{@prefix}:#{name}>"
|
|
|
|
@parents.each do |parent|
|
2005-11-23 12:49:02 +03:00
|
|
|
assert_equal(excepted,
|
|
|
|
@rss.__send__(parent).funcall("dc_#{name}_elements"))
|
2005-04-05 10:42:03 +04:00
|
|
|
end
|
|
|
|
|
|
|
|
excepted = Array.new(2, excepted).join("\n")
|
|
|
|
@parents.each do |parent|
|
|
|
|
reader = "dc_#{name}" + (name == :rights ? "es" : "s")
|
2005-11-23 12:49:02 +03:00
|
|
|
elems = @rss.__send__(parent).__send__(reader)
|
2005-04-05 10:42:03 +04:00
|
|
|
klass_name = "DublinCore#{Utils.to_class_name(name.to_s)}"
|
|
|
|
klass = DublinCoreModel.const_get(klass_name)
|
2005-11-23 12:49:02 +03:00
|
|
|
elems << klass.new(@rss.__send__(parent).__send__("dc_#{name}"))
|
|
|
|
assert_equal(excepted,
|
|
|
|
@rss.__send__(parent).funcall("dc_#{name}_elements"))
|
2004-10-16 08:51:15 +04:00
|
|
|
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
|
2005-04-05 10:42:03 +04:00
|
|
|
assert_equal(CGI.escapeHTML(elem.text),
|
|
|
|
@elems[elem.name.intern].to_s)
|
2004-10-16 08:51:15 +04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2004-01-28 06:46:13 +03:00
|
|
|
|
2004-10-16 08:51:15 +04:00
|
|
|
end
|
2004-01-28 06:46:13 +03:00
|
|
|
end
|