PageRenderTime 53ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/source/ruby-1.9.2-p180/test/rss/rss-testcase.rb

https://github.com/akiernan/omnibus
Ruby | 478 lines | 380 code | 69 blank | 29 comment | 5 complexity | e92143a752873ccbce64fe9ac74b477b MD5 | raw file
Possible License(s): Apache-2.0, BSD-3-Clause
  1. require "erb"
  2. require "test/unit"
  3. require 'rss-assertions'
  4. require "rss"
  5. module RSS
  6. class TestCase < Test::Unit::TestCase
  7. include ERB::Util
  8. include RSS
  9. include Assertions
  10. XMLDECL_VERSION = "1.0"
  11. XMLDECL_ENCODING = "UTF-8"
  12. XMLDECL_STANDALONE = "no"
  13. RDF_ABOUT = "http://www.xml.com/xml/news.rss"
  14. RDF_RESOURCE = "http://xml.com/universal/images/xml_tiny.gif"
  15. TITLE_VALUE = "XML.com"
  16. LINK_VALUE = "http://xml.com/pub"
  17. URL_VALUE = "http://xml.com/universal/images/xml_tiny.gif"
  18. NAME_VALUE = "hogehoge"
  19. LANGUAGE_VALUE = "ja"
  20. DESCRIPTION_VALUE = "
  21. XML.com features a rich mix of information and services
  22. for the XML community.
  23. "
  24. RESOURCES = [
  25. "http://xml.com/pub/2000/08/09/xslt/xslt.html",
  26. "http://xml.com/pub/2000/08/09/rdfdb/index.html",
  27. ]
  28. CLOUD_DOMAIN = "data.ourfavoritesongs.com"
  29. CLOUD_PORT = "80"
  30. CLOUD_PATH = "/RPC2"
  31. CLOUD_REGISTER_PROCEDURE = "ourFavoriteSongs.rssPleaseNotify"
  32. CLOUD_PROTOCOL = "xml-rpc"
  33. ENCLOSURE_URL = "http://www.scripting.com/mp3s/weatherReportSuite.mp3"
  34. ENCLOSURE_LENGTH = "12216320"
  35. ENCLOSURE_TYPE = "audio/mpeg"
  36. CATEGORY_DOMAIN = "http://www.superopendirectory.com/"
  37. FEED_TITLE = "dive into mark"
  38. FEED_UPDATED = "2003-12-13T18:30:02Z"
  39. FEED_AUTHOR_NAME = "John Doe"
  40. FEED_ID = "urn:uuid:60a76c80-d399-11d9-b93C-0003939e0af6"
  41. ENTRY_TITLE = "Atom-Powered Robots Run Amok"
  42. ENTRY_LINK = "http://example.org/2003/12/13/atom03"
  43. ENTRY_ID = "urn:uuid:1225c695-cfb8-4ebb-aaaa-80da344efa6a"
  44. ENTRY_UPDATED = "2003-12-13T18:30:02Z"
  45. ENTRY_SUMMARY = "Some text."
  46. t = Time.iso8601("2000-01-01T12:00:05+00:00")
  47. class << t
  48. alias_method(:to_s, :iso8601)
  49. end
  50. DC_ELEMENTS = {
  51. :title => "hoge",
  52. :description =>
  53. " XML is placing increasingly heavy loads on
  54. the existing technical infrastructure of the Internet.",
  55. :creator => "Rael Dornfest (mailto:rael@oreilly.com)",
  56. :subject => "XML",
  57. :publisher => "The O'Reilly Network",
  58. :contributor => "hogehoge",
  59. :type => "fugafuga",
  60. :format => "hohoho",
  61. :identifier => "fufufu",
  62. :source => "barbar",
  63. :language => "ja",
  64. :relation => "cococo",
  65. :rights => "Copyright (c) 2000 O'Reilly &amp; Associates, Inc.",
  66. :date => t,
  67. }
  68. DC_NODES = DC_ELEMENTS.collect do |name, value|
  69. "<#{DC_PREFIX}:#{name}>#{value}</#{DC_PREFIX}:#{name}>"
  70. end.join("\n")
  71. def default_test
  72. # This class isn't tested
  73. end
  74. private
  75. def make_xmldecl(v=XMLDECL_VERSION, e=XMLDECL_ENCODING, s=XMLDECL_STANDALONE)
  76. rv = "<?xml version='#{v}'"
  77. rv << " encoding='#{e}'" if e
  78. rv << " standalone='#{s}'" if s
  79. rv << "?>"
  80. rv
  81. end
  82. def make_RDF(content=nil, xmlns=[])
  83. <<-EORSS
  84. #{make_xmldecl}
  85. <rdf:RDF xmlns="#{URI}" xmlns:rdf="#{RDF::URI}"
  86. #{xmlns.collect {|pre, uri| "xmlns:#{pre}='#{uri}'"}.join(' ')}>
  87. #{block_given? ? yield : content}
  88. </rdf:RDF>
  89. EORSS
  90. end
  91. def make_channel(content=nil)
  92. <<-EOC
  93. <channel rdf:about="#{RDF_ABOUT}">
  94. <title>#{TITLE_VALUE}</title>
  95. <link>#{LINK_VALUE}</link>
  96. <description>#{DESCRIPTION_VALUE}</description>
  97. <image rdf:resource="#{RDF_RESOURCE}" />
  98. <items>
  99. <rdf:Seq>
  100. #{RESOURCES.collect do |res| '<rdf:li resource="' + res + '" />' end.join("\n")}
  101. </rdf:Seq>
  102. </items>
  103. <textinput rdf:resource="#{RDF_RESOURCE}" />
  104. #{block_given? ? yield : content}
  105. </channel>
  106. EOC
  107. end
  108. def make_image(content=nil)
  109. <<-EOI
  110. <image rdf:about="#{RDF_ABOUT}">
  111. <title>#{TITLE_VALUE}</title>
  112. <url>#{URL_VALUE}</url>
  113. <link>#{LINK_VALUE}</link>
  114. #{block_given? ? yield : content}
  115. </image>
  116. EOI
  117. end
  118. def make_item(content=nil)
  119. <<-EOI
  120. <item rdf:about="#{RDF_ABOUT}">
  121. <title>#{TITLE_VALUE}</title>
  122. <link>#{LINK_VALUE}</link>
  123. <description>#{DESCRIPTION_VALUE}</description>
  124. #{block_given? ? yield : content}
  125. </item>
  126. EOI
  127. end
  128. def make_textinput(content=nil)
  129. <<-EOT
  130. <textinput rdf:about="#{RDF_ABOUT}">
  131. <title>#{TITLE_VALUE}</title>
  132. <description>#{DESCRIPTION_VALUE}</description>
  133. <name>#{NAME_VALUE}</name>
  134. <link>#{LINK_VALUE}</link>
  135. #{block_given? ? yield : content}
  136. </textinput>
  137. EOT
  138. end
  139. def make_sample_RDF
  140. make_RDF(<<-EOR)
  141. #{make_channel}
  142. #{make_image}
  143. #{make_item}
  144. #{make_textinput}
  145. EOR
  146. end
  147. def make_rss20(content=nil, xmlns=[])
  148. <<-EORSS
  149. #{make_xmldecl}
  150. <rss version="2.0"
  151. #{xmlns.collect {|pre, uri| "xmlns:#{pre}='#{uri}'"}.join(' ')}>
  152. #{block_given? ? yield : content}
  153. </rss>
  154. EORSS
  155. end
  156. def make_sample_items20
  157. RESOURCES.collect do |res|
  158. elems = ["<link>#{res}</link>"]
  159. elems << "<title>title of #{res}</title>"
  160. elems = elems.join("\n")
  161. item = "<item>\n#{elems}\n</item>"
  162. end.join("\n")
  163. end
  164. def make_channel20(content=nil)
  165. <<-EOC
  166. <channel>
  167. <title>#{TITLE_VALUE}</title>
  168. <link>#{LINK_VALUE}</link>
  169. <description>#{DESCRIPTION_VALUE}</description>
  170. <language>#{LANGUAGE_VALUE}</language>
  171. <image>
  172. <url>#{RDF_RESOURCE}</url>
  173. <title>#{TITLE_VALUE}</title>
  174. <link>#{LINK_VALUE}</link>
  175. </image>
  176. #{make_sample_items20}
  177. <textInput>
  178. <title>#{TITLE_VALUE}</title>
  179. <description>#{DESCRIPTION_VALUE}</description>
  180. <name>#{NAME_VALUE}</name>
  181. <link>#{RDF_RESOURCE}</link>
  182. </textInput>
  183. #{block_given? ? yield : content}
  184. </channel>
  185. EOC
  186. end
  187. def make_item20(content=nil)
  188. <<-EOI
  189. <item>
  190. <title>#{TITLE_VALUE}</title>
  191. <link>#{LINK_VALUE}</link>
  192. <description>#{DESCRIPTION_VALUE}</description>
  193. #{block_given? ? yield : content}
  194. </item>
  195. EOI
  196. end
  197. def make_cloud20
  198. <<-EOC
  199. <cloud
  200. domain="#{CLOUD_DOMAIN}"
  201. port="#{CLOUD_PORT}"
  202. path="#{CLOUD_PATH}"
  203. registerProcedure="#{CLOUD_REGISTER_PROCEDURE}"
  204. protocol="#{CLOUD_PROTOCOL}" />
  205. EOC
  206. end
  207. def make_sample_rss20
  208. make_rss20(<<-EOR)
  209. #{make_channel20}
  210. EOR
  211. end
  212. def make_feed_without_entry(content=nil, xmlns=[])
  213. <<-EOA
  214. <feed xmlns="#{Atom::URI}"
  215. #{xmlns.collect {|pre, uri| "xmlns:#{pre}='#{uri}'"}.join(' ')}>
  216. <id>#{FEED_ID}</id>
  217. <title>#{FEED_TITLE}</title>
  218. <updated>#{FEED_UPDATED}</updated>
  219. <author>
  220. <name>#{FEED_AUTHOR_NAME}</name>
  221. </author>
  222. #{block_given? ? yield : content}
  223. </feed>
  224. EOA
  225. end
  226. def make_entry(content=nil)
  227. <<-EOA
  228. <entry>
  229. <title>#{ENTRY_TITLE}</title>
  230. <id>#{ENTRY_ID}</id>
  231. <updated>#{ENTRY_UPDATED}</updated>
  232. #{block_given? ? yield : content}
  233. </entry>
  234. EOA
  235. end
  236. def make_feed_with_open_entry(content=nil, xmlns=[], &block)
  237. make_feed_without_entry(<<-EOA, xmlns)
  238. #{make_entry(content, &block)}
  239. EOA
  240. end
  241. def make_feed_with_open_entry_source(content=nil, xmlns=[])
  242. make_feed_with_open_entry(<<-EOA, xmlns)
  243. <source>
  244. #{block_given? ? yield : content}
  245. </source>
  246. EOA
  247. end
  248. def make_feed(content=nil, xmlns=[])
  249. make_feed_without_entry(<<-EOA, xmlns)
  250. <entry>
  251. <title>#{ENTRY_TITLE}</title>
  252. <link href="#{ENTRY_LINK}"/>
  253. <id>#{ENTRY_ID}</id>
  254. <updated>#{ENTRY_UPDATED}</updated>
  255. <summary>#{ENTRY_SUMMARY}</summary>
  256. </entry>
  257. #{block_given? ? yield : content}
  258. EOA
  259. end
  260. def make_entry_document(content=nil, xmlns=[])
  261. <<-EOA
  262. <entry xmlns="#{Atom::URI}"
  263. #{xmlns.collect {|pre, uri| "xmlns:#{pre}='#{uri}'"}.join(' ')}>
  264. <id>#{ENTRY_ID}</id>
  265. <title>#{ENTRY_TITLE}</title>
  266. <updated>#{ENTRY_UPDATED}</updated>
  267. <author>
  268. <name>#{FEED_AUTHOR_NAME}</name>
  269. </author>
  270. #{block_given? ? yield : content}
  271. </entry>
  272. EOA
  273. end
  274. def make_entry_document_with_open_source(content=nil, xmlns=[])
  275. make_entry_document(<<-EOA, xmlns)
  276. <source>
  277. #{block_given? ? yield : content}
  278. </source>
  279. EOA
  280. end
  281. def make_element(elem_name, attrs, contents)
  282. attrs_str = attrs.collect do |name, value|
  283. "#{h name}='#{h value}'"
  284. end.join(" ")
  285. attrs_str = " #{attrs_str}" unless attrs_str.empty?
  286. if contents.is_a?(String)
  287. contents_str = h(contents)
  288. else
  289. contents_str = contents.collect do |name, value|
  290. "#{Element::INDENT}<#{h name}>#{h value}</#{h name}>"
  291. end.join("\n")
  292. contents_str = "\n#{contents_str}\n"
  293. end
  294. "<#{h elem_name}#{attrs_str}>#{contents_str}</#{h elem_name}>"
  295. end
  296. def xmlns_container(xmlns_decls, content)
  297. attributes = xmlns_decls.collect do |prefix, uri|
  298. "xmlns:#{h prefix}=\"#{h uri}\""
  299. end.join(" ")
  300. "<dummy #{attributes}>#{content}</dummy>"
  301. end
  302. private
  303. def setup_rss10(rdf)
  304. assert_equal("", rdf.to_s)
  305. channel = RDF::Channel.new
  306. assert_equal("", channel.to_s)
  307. channel.about = "http://example.com/index.rdf"
  308. channel.title = "title"
  309. channel.link = "http://example.com/"
  310. channel.description = "description"
  311. assert_equal("", channel.to_s)
  312. item_title = "item title"
  313. item_link = "http://example.com/item"
  314. channel.items = RDF::Channel::Items.new
  315. channel.items.Seq.lis << RDF::Channel::Items::Seq::Li.new(item_link)
  316. assert_not_equal("", channel.to_s)
  317. rdf.channel = channel
  318. assert_equal("", rdf.to_s)
  319. item = RDF::Item.new
  320. item.title = item_title
  321. item.link = item_link
  322. item.about = item_link
  323. rdf.items << item
  324. assert_not_equal("", rdf.to_s)
  325. end
  326. def setup_rss20(rss)
  327. assert_equal("", rss.to_s)
  328. channel = Rss::Channel.new
  329. assert_equal("", channel.to_s)
  330. channel.title = "title"
  331. channel.link = "http://example.com/"
  332. channel.description = "description"
  333. assert_not_equal("", channel.to_s)
  334. rss.channel = channel
  335. assert_not_equal("", rss.to_s)
  336. end
  337. def setup_dummy_channel(maker)
  338. about = "http://hoge.com"
  339. title = "fugafuga"
  340. link = "http://hoge.com/feed.xml"
  341. description = "fugafugafugafuga"
  342. language = "ja"
  343. maker.channel.about = about
  344. maker.channel.title = title
  345. maker.channel.link = link
  346. maker.channel.description = description
  347. maker.channel.language = language
  348. end
  349. def setup_dummy_channel_atom(maker)
  350. updated = Time.now
  351. author = "Foo"
  352. setup_dummy_channel(maker)
  353. maker.channel.links.first.rel = "self"
  354. maker.channel.links.first.type = "application/atom+xml"
  355. maker.channel.updated = updated
  356. maker.channel.author = author
  357. end
  358. def setup_dummy_image(maker)
  359. title = "fugafuga"
  360. link = "http://hoge.com"
  361. url = "http://hoge.com/hoge.png"
  362. maker.channel.link = link if maker.channel.link.nil?
  363. maker.image.title = title
  364. maker.image.url = url
  365. end
  366. def setup_dummy_textinput(maker)
  367. title = "fugafuga"
  368. description = "text hoge fuga"
  369. name = "hoge"
  370. link = "http://hoge.com/search.cgi"
  371. maker.textinput.title = title
  372. maker.textinput.description = description
  373. maker.textinput.name = name
  374. maker.textinput.link = link
  375. end
  376. def setup_dummy_item(maker)
  377. title = "TITLE"
  378. link = "http://hoge.com/"
  379. item = maker.items.new_item
  380. item.title = title
  381. item.link = link
  382. end
  383. def setup_dummy_item_atom(maker)
  384. setup_dummy_item(maker)
  385. item = maker.items.first
  386. item.id = "http://example.net/xxx"
  387. item.updated = Time.now
  388. end
  389. def setup_taxo_topic(target, topics)
  390. topics.each do |topic|
  391. taxo_topic = target.taxo_topics.new_taxo_topic
  392. topic.each do |name, value|
  393. case name
  394. when :link
  395. taxo_topic.taxo_link = value
  396. when :topics
  397. value.each do |t|
  398. taxo_topic.taxo_topics << t
  399. end
  400. else
  401. dc_elems = taxo_topic.__send__("dc_#{name}s")
  402. dc_elem = dc_elems.__send__("new_#{name}")
  403. dc_elem.value = value
  404. end
  405. end
  406. end
  407. end
  408. end
  409. end