PageRenderTime 57ms CodeModel.GetById 27ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/gems/facets-2.4.5/test/more/test_xoxo.rb

https://bitbucket.org/mediashelf/fedora-migrator
Ruby | 274 lines | 237 code | 29 blank | 8 comment | 0 complexity | 0939ab09bf2a1e8297c4297e84b2f500 MD5 | raw file
Possible License(s): GPL-3.0, GPL-2.0, IPL-1.0, AGPL-1.0, LGPL-3.0
  1. # Test lib/more/add/facets/xoxo.rb
  2. require 'facets/xoxo.rb'
  3. require 'test/unit'
  4. class TCXOXO < Test::Unit::TestCase
  5. def test_simple_list
  6. l = ['1', '2', '3']
  7. html = XOXO.dump(l)
  8. assert_equal '<ol class="xoxo"><li>1</li><li>2</li><li>3</li></ol>', html
  9. end
  10. def test_nested_list
  11. l = ['1', ['2', '3']]
  12. assert_equal '<ol class="xoxo"><li>1</li><li><ol><li>2</li><li>3</li></ol></li></ol>', XOXO.dump(l)
  13. end
  14. def test_hash
  15. h = {'test' => '1', 'name' => 'Kevin'}
  16. # Changed since Ruby sorts the hash differently.
  17. assert_equal '<ol class="xoxo"><li><dl><dt>name</dt><dd>Kevin</dd><dt>test</dt><dd>1</dd></dl></li></ol>', XOXO.dump(h)
  18. end
  19. def test_single_item
  20. l = 'test'
  21. assert_equal '<ol class="xoxo"><li>test</li></ol>', XOXO.dump(l)
  22. end
  23. def test_wrap_differs
  24. l = 'test'
  25. html = XOXO.dump(l)
  26. html_wrap = XOXO.dump(l, :html_wrap => true)
  27. assert_not_equal html, html_wrap
  28. end
  29. def test_wrap_single_item
  30. l = 'test'
  31. html = XOXO.dump(l, :html_wrap => true)
  32. assert_equal <<EOF.strip, html
  33. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN
  34. http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  35. <html xmlns="http://www.w3.org/1999/xhtml"><head profile=""><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /></head><body><ol class="xoxo"><li>test</li></ol></body></html>
  36. EOF
  37. end
  38. def test_wrap_item_with_css
  39. l = 'test'
  40. html = XOXO.dump(l, :html_wrap => true, :css => 'reaptest.css')
  41. assert_equal <<EOF.strip, html
  42. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN
  43. http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  44. <html xmlns="http://www.w3.org/1999/xhtml"><head profile=""><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><style type="text/css" >@import "reaptest.css";</style></head><body><ol class="xoxo"><li>test</li></ol></body></html>
  45. EOF
  46. end
  47. def test_hash_roundtrip
  48. h = {'test' => '1', 'name' => 'Kevin'}
  49. assert_equal h, XOXO.load(XOXO.dump(h))
  50. end
  51. def test_hash_with_url_roundtrip
  52. h = {'url' => 'http://example.com', 'name' => 'Kevin'}
  53. assert_equal h, XOXO.load(XOXO.dump(h))
  54. end
  55. def test_nested_hash_roundtrip
  56. h = {'test' => '1', 'inner' => {'name' => 'Kevin'}}
  57. assert_equal h, XOXO.load(XOXO.dump(h))
  58. end
  59. def test_nested_hash_with_url_roundtrip
  60. h = {'url' => 'http://example.com', 'inner' => {
  61. 'url' => 'http://slashdot.org', 'name' => 'Kevin'}}
  62. assert_equal h, XOXO.load(XOXO.dump(h))
  63. end
  64. def test_list_round_trip
  65. l = ['3', '2', '1']
  66. assert_equal l, XOXO.load(XOXO.dump(l))
  67. end
  68. def test_list_of_hashes_round_trip
  69. l = ['3', {'a' => '2'}, {'b' => '1', 'c' => '4'}]
  70. assert_equal l, XOXO.load(XOXO.dump(l))
  71. end
  72. def test_list_of_lists_round_trip
  73. l = ['3', ['a', '2'], ['b', ['1', ['c', '4']]]]
  74. assert_equal l, XOXO.load(XOXO.dump(l))
  75. end
  76. def test_hashes_of_lists_roundtrip
  77. h = {
  78. 'test' => ['1', '2'],
  79. 'name' => 'Kevin',
  80. 'nestlist' => ['a', ['b', 'c']],
  81. 'nestdict' => {'e' => '6', 'f' => '7'}
  82. }
  83. assert_equal h, XOXO.load(XOXO.dump(h))
  84. end
  85. def test_xoxo_junk_in_containers
  86. h = XOXO.load '<ol>bad<li><dl>worse<dt>good</dt><dd>buy</dd> now</dl></li></ol>'
  87. assert_equal({'good' => 'buy'}, h)
  88. end
  89. def test_xoxo_junk_in_elements
  90. l = XOXO.load '<ol><li>bad<dl><dt>good</dt><dd>buy</dd></dl>worse</li><li>bag<ol><li>OK</li></ol>fish</li></ol>'
  91. assert_equal([{'good' => 'buy'}, ['OK']], l)
  92. end
  93. def test_xoxo_with_spaces_and_newlines
  94. xoxo_sample = <<EOF.strip
  95. <ol class='xoxo'>
  96. <li>
  97. <dl>
  98. <dt>text</dt>
  99. <dd>item 1</dd>
  100. <dt>description</dt>
  101. <dd> This item represents the main point we're trying to make.</dd>
  102. <dt>url</dt>
  103. <dd>http://example.com/more.xoxo</dd>
  104. <dt>title</dt>
  105. <dd>title of item 1</dd>
  106. <dt>type</dt>
  107. <dd>text/xml</dd>
  108. <dt>rel</dt>
  109. <dd>help</dd>
  110. </dl>
  111. </li>
  112. </ol>
  113. EOF
  114. h = XOXO.load xoxo_sample
  115. h2 = {
  116. 'text' => 'item 1',
  117. 'description' => " This item represents the main point we're trying to make.",
  118. 'url' => 'http://example.com/more.xoxo',
  119. 'title' => 'title of item 1',
  120. 'type' => 'text/xml',
  121. 'rel' => 'help'
  122. }
  123. assert_equal h2, XOXO.load(xoxo_sample)
  124. end
  125. def test_special_attribute_decoding
  126. xoxo_sample = <<EOF.strip
  127. <ol class='xoxo'>
  128. <li>
  129. <dl>
  130. <dt>text</dt>
  131. <dd>item 1</dd>
  132. <dt>url</dt>
  133. <dd>http://example.com/more.xoxo</dd>
  134. <dt>title</dt>
  135. <dd>title of item 1</dd>
  136. <dt>type</dt>
  137. <dd>text/xml</dd>
  138. <dt>rel</dt>
  139. <dd>help</dd>
  140. </dl>
  141. </li>
  142. </ol>
  143. EOF
  144. smart_xoxo_sample = <<EOF.strip
  145. <ol class='xoxo'>
  146. <li><a href="http://example.com/more.xoxo"
  147. title="title of item 1"
  148. type="text/xml"
  149. rel="help">item 1</a>
  150. <!-- note how the "text" property is simply the contents of the <a> element -->
  151. </li>
  152. </ol>
  153. EOF
  154. assert_equal XOXO.load(xoxo_sample), XOXO.load(smart_xoxo_sample)
  155. end
  156. def test_special_attribute_and_dl_decoding
  157. xoxo_sample = <<EOF.strip
  158. <ol class="xoxo">
  159. <li>
  160. <dl>
  161. <dt>text</dt>
  162. <dd>item 1</dd>
  163. <dt>description</dt>
  164. <dd> This item represents the main point we're trying to make.</dd>
  165. <dt>url</dt>
  166. <dd>http://example.com/more.xoxo</dd>
  167. <dt>title</dt>
  168. <dd>title of item 1</dd>
  169. <dt>type</dt>
  170. <dd>text/xml</dd>
  171. <dt>rel</dt>
  172. <dd>help</dd>
  173. </dl>
  174. </li>
  175. </ol>
  176. EOF
  177. smart_xoxo_sample = <<EOF.strip
  178. <ol class="xoxo">
  179. <li><a href="http://example.com/more.xoxo"
  180. title="title of item 1"
  181. type="text/xml"
  182. rel="help">item 1</a>
  183. <!-- note how the "text" property is simply the contents of the <a> element -->
  184. <dl>
  185. <dt>description</dt>
  186. <dd> This item represents the main point we're trying to make.</dd>
  187. </dl>
  188. </li>
  189. </ol>
  190. EOF
  191. assert_equal XOXO.load(xoxo_sample), XOXO.load(smart_xoxo_sample)
  192. end
  193. def test_special_attribute_encode
  194. h = {
  195. 'url' => 'http://example.com/more.xoxo',
  196. 'title' => 'sample url',
  197. 'type' => "text/xml",
  198. 'rel' => 'help',
  199. 'text' => 'an example'
  200. }
  201. assert_equal '<ol class="xoxo"><li><a href="http://example.com/more.xoxo" title="sample url" rel="help" type="text/xml" >an example</a></li></ol>', XOXO.dump(h)
  202. end
  203. def test_special_attribute_roundtrip_full
  204. h = {
  205. 'url' => 'http://example.com/more.xoxo',
  206. 'title' => 'sample url',
  207. 'type' => "text/xml",
  208. 'rel' => 'help',
  209. 'text' => 'an example'
  210. }
  211. assert_equal h, XOXO.load(XOXO.dump(h))
  212. end
  213. def test_special_attribute_roundtrip_no_text
  214. h = {
  215. 'url' => 'http://example.com/more.xoxo',
  216. 'title' => 'sample url',
  217. 'type' => "text/xml",
  218. 'rel' => 'help'
  219. }
  220. assert_equal h, XOXO.load(XOXO.dump(h))
  221. end
  222. def test_special_attribute_roundtrip_no_text_or_title
  223. h = {'url' => 'http://example.com/more.xoxo'}
  224. assert_equal h, XOXO.load(XOXO.dump(h))
  225. end
  226. def test_attention_roundtrip
  227. kmattn = <<EOF.strip
  228. <ol class="xoxo"><li><a href="http://www.boingboing.net/" title="Boing Boing Blog" >Boing Boing Blog</a><dl><dt>alturls</dt><dd><ol><li><a href="http://boingboing.net/rss.xml" >xmlurl</a></li></ol></dd><dt>description</dt><dd>Boing Boing Blog</dd></dl></li><li><a href="http://www.financialcryptography.com/" title="Financial Cryptography" >Financial Cryptography</a><dl><dt>alturls</dt><dd><ol><li><a href="http://www.financialcryptography.com/mt/index.rdf" >xmlurl</a></li></ol></dd><dt>description</dt><dd>Financial Cryptography</dd></dl></li><li><a href="http://hublog.hubmed.org/" title="HubLog" >HubLog</a><dl><dt>alturls</dt><dd><ol><li><a href="http://hublog.hubmed.org/index.xml" >xmlurl</a></li><li><a href="http://hublog.hubmed.org/foaf.rdf" >foafurl</a></li></ol></dd><dt>description</dt><dd>HubLog</dd></dl></li></ol>
  229. EOF
  230. assert_equal kmattn, XOXO.dump(XOXO.load(kmattn))
  231. assert_equal XOXO.load(kmattn), XOXO.load(XOXO.dump(XOXO.load(kmattn)))
  232. assert_equal XOXO.dump(XOXO.load(kmattn)),
  233. XOXO.dump(XOXO.load(XOXO.dump(XOXO.load(kmattn))))
  234. end
  235. def test_unicode_roundtrip
  236. unicode = "Tantek \xc3\x87elik and a snowman \xe2\x98\x83"
  237. assert_equal unicode, XOXO.load(XOXO.dump(unicode))
  238. end
  239. # TBD: Implement proper encodings.
  240. #
  241. # def test_utf8_roundtrip
  242. # end
  243. # def test_windows1252_roundtrip
  244. # end
  245. end