PageRenderTime 64ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/training-web/vendor/bundle/gems/builder-3.0.4/test/test_markupbuilder.rb

https://bitbucket.org/ohimmelreich/asalia-training
Ruby | 578 lines | 472 code | 95 blank | 11 comment | 6 complexity | 8c23134163bdd35c38023ba83681fe8c MD5 | raw file
  1. #!/usr/bin/env ruby
  2. #--
  3. # Portions copyright 2004 by Jim Weirich (jim@weirichhouse.org).
  4. # Portions copyright 2005 by Sam Ruby (rubys@intertwingly.net).
  5. # All rights reserved.
  6. # Permission is granted for use, copying, modification, distribution,
  7. # and distribution of modified versions of this work as long as the
  8. # above copyright notice is included.
  9. #++
  10. require 'test/unit'
  11. require 'test/preload'
  12. require 'builder'
  13. require 'builder/xmlmarkup'
  14. class TestMarkup < Test::Unit::TestCase
  15. def setup
  16. @xml = Builder::XmlMarkup.new
  17. end
  18. def test_create
  19. assert_not_nil @xml
  20. end
  21. def test_simple
  22. @xml.simple
  23. assert_equal "<simple/>", @xml.target!
  24. end
  25. def test_value
  26. @xml.value("hi")
  27. assert_equal "<value>hi</value>", @xml.target!
  28. end
  29. def test_empty_value
  30. @xml.value("")
  31. assert_equal "<value></value>", @xml.target!
  32. end
  33. def test_nil_value
  34. @xml.value(nil)
  35. assert_equal "<value/>", @xml.target!
  36. end
  37. def test_no_value
  38. @xml.value()
  39. assert_equal "<value/>", @xml.target!
  40. end
  41. def test_nested
  42. @xml.outer { |x| x.inner("x") }
  43. assert_equal "<outer><inner>x</inner></outer>", @xml.target!
  44. end
  45. def test_attributes
  46. @xml.ref(:id => 12)
  47. assert_equal %{<ref id="12"/>}, @xml.target!
  48. end
  49. def test_string_attributes_are_quoted_by_default
  50. @xml.ref(:id => "H&R")
  51. assert_equal %{<ref id="H&amp;R"/>}, @xml.target!
  52. end
  53. def test_symbol_attributes_are_unquoted_by_default
  54. @xml.ref(:id => :"H&amp;R")
  55. assert_equal %{<ref id="H&amp;R"/>}, @xml.target!
  56. end
  57. def test_attributes_quoted_can_be_turned_on
  58. @xml = Builder::XmlMarkup.new
  59. @xml.ref(:id => "<H&R \"block\">")
  60. assert_equal %{<ref id="&lt;H&amp;R &quot;block&quot;&gt;"/>}, @xml.target!
  61. end
  62. def test_mixed_attribute_quoting_with_nested_builders
  63. x = Builder::XmlMarkup.new(:target=>@xml)
  64. @xml.ref(:id=>:"H&amp;R") {
  65. x.element(:tag=>"Long&Short")
  66. }
  67. assert_equal "<ref id=\"H&amp;R\"><element tag=\"Long&amp;Short\"/></ref>",
  68. @xml.target!
  69. end
  70. def test_multiple_attributes
  71. @xml.ref(:id => 12, :name => "bill")
  72. assert_match %r{^<ref( id="12"| name="bill"){2}/>$}, @xml.target!
  73. end
  74. def test_attributes_with_text
  75. @xml.a("link", :href=>"http://onestepback.org")
  76. assert_equal %{<a href="http://onestepback.org">link</a>}, @xml.target!
  77. end
  78. def test_attributes_with_newlines
  79. @xml.abbr("W3C", :title=>"World\nWide\rWeb\r\nConsortium")
  80. assert_equal %{<abbr title="World&#10;Wide&#13;Web&#13;&#10;Consortium">W3C</abbr>},
  81. @xml.target!
  82. end
  83. def test_complex
  84. @xml.body(:bg=>"#ffffff") { |x|
  85. x.title("T", :style=>"red")
  86. }
  87. assert_equal %{<body bg="#ffffff"><title style="red">T</title></body>}, @xml.target!
  88. end
  89. def test_funky_symbol
  90. @xml.tag!("non-ruby-token", :id=>1) { |x| x.ok }
  91. assert_equal %{<non-ruby-token id="1"><ok/></non-ruby-token>}, @xml.target!
  92. end
  93. def test_tag_can_handle_private_method
  94. @xml.tag!("loop", :id=>1) { |x| x.ok }
  95. assert_equal %{<loop id="1"><ok/></loop>}, @xml.target!
  96. end
  97. def test_no_explicit_marker
  98. @xml.p { |x| x.b("HI") }
  99. assert_equal "<p><b>HI</b></p>", @xml.target!
  100. end
  101. def test_reference_local_vars
  102. n = 3
  103. @xml.ol { |x| n.times { x.li(n) } }
  104. assert_equal "<ol><li>3</li><li>3</li><li>3</li></ol>", @xml.target!
  105. end
  106. def test_reference_methods
  107. @xml.title { |x| x.a { x.b(name) } }
  108. assert_equal "<title><a><b>bob</b></a></title>", @xml.target!
  109. end
  110. def test_append_text
  111. @xml.p { |x| x.br; x.text! "HI" }
  112. assert_equal "<p><br/>HI</p>", @xml.target!
  113. end
  114. def test_ambiguous_markup
  115. ex = assert_raise(ArgumentError) {
  116. @xml.h1("data1") { b }
  117. }
  118. assert_match(/\btext\b/, ex.message)
  119. assert_match(/\bblock\b/, ex.message)
  120. end
  121. def test_capitalized_method
  122. @xml.P { |x| x.B("hi"); x.BR(); x.EM { x.text! "world" } }
  123. assert_equal "<P><B>hi</B><BR/><EM>world</EM></P>", @xml.target!
  124. end
  125. def test_escaping
  126. @xml.div { |x| x.text! "<hi>"; x.em("H&R Block") }
  127. assert_equal %{<div>&lt;hi&gt;<em>H&amp;R Block</em></div>}, @xml.target!
  128. end
  129. def test_non_escaping
  130. @xml.div("ns:xml"=>:"&xml;") { |x| x << "<h&i>"; x.em("H&R Block") }
  131. assert_equal %{<div ns:xml="&xml;"><h&i><em>H&amp;R Block</em></div>}, @xml.target!
  132. end
  133. def test_return_value
  134. str = @xml.x("men")
  135. assert_equal @xml.target!, str
  136. end
  137. def test_stacked_builders
  138. b = Builder::XmlMarkup.new( :target => @xml )
  139. b.div { @xml.span { @xml.a("text", :href=>"ref") } }
  140. assert_equal "<div><span><a href=\"ref\">text</a></span></div>", @xml.target!
  141. end
  142. def name
  143. "bob"
  144. end
  145. end
  146. class TestAttributeEscaping < Test::Unit::TestCase
  147. def setup
  148. @xml = Builder::XmlMarkup.new
  149. end
  150. def test_element_gt
  151. @xml.title('1<2')
  152. assert_equal '<title>1&lt;2</title>', @xml.target!
  153. end
  154. def test_element_amp
  155. @xml.title('AT&T')
  156. assert_equal '<title>AT&amp;T</title>', @xml.target!
  157. end
  158. def test_element_amp2
  159. @xml.title('&amp;')
  160. assert_equal '<title>&amp;amp;</title>', @xml.target!
  161. end
  162. def test_attr_less
  163. @xml.a(:title => '2>1')
  164. assert_equal '<a title="2&gt;1"/>', @xml.target!
  165. end
  166. def test_attr_amp
  167. @xml.a(:title => 'AT&T')
  168. assert_equal '<a title="AT&amp;T"/>', @xml.target!
  169. end
  170. def test_attr_quot
  171. @xml.a(:title => '"x"')
  172. assert_equal '<a title="&quot;x&quot;"/>', @xml.target!
  173. end
  174. end
  175. class TestNameSpaces < Test::Unit::TestCase
  176. def setup
  177. @xml = Builder::XmlMarkup.new(:indent=>2)
  178. end
  179. def test_simple_name_spaces
  180. @xml.rdf :RDF
  181. assert_equal "<rdf:RDF/>\n", @xml.target!
  182. end
  183. def test_long
  184. xml = Builder::XmlMarkup.new(:indent=>2)
  185. xml.instruct!
  186. xml.rdf :RDF,
  187. "xmlns:rdf" => :"&rdf;",
  188. "xmlns:rdfs" => :"&rdfs;",
  189. "xmlns:xsd" => :"&xsd;",
  190. "xmlns:owl" => :"&owl;" do
  191. xml.owl :Class, :'rdf:ID'=>'Bird' do
  192. xml.rdfs :label, 'bird'
  193. xml.rdfs :subClassOf do
  194. xml.owl :Restriction do
  195. xml.owl :onProperty, 'rdf:resource'=>'#wingspan'
  196. xml.owl :maxCardinality,1,'rdf:datatype'=>'&xsd;nonNegativeInteger'
  197. end
  198. end
  199. end
  200. end
  201. assert_match(/^<\?xml/, xml.target!)
  202. assert_match(/\n<rdf:RDF/m, xml.target!)
  203. assert_match(/xmlns:rdf="&rdf;"/m, xml.target!)
  204. assert_match(/<owl:Restriction>/m, xml.target!)
  205. end
  206. def test_ensure
  207. xml = Builder::XmlMarkup.new
  208. xml.html do
  209. xml.body do
  210. begin
  211. xml.p do
  212. raise Exception.new('boom')
  213. end
  214. rescue Exception => e
  215. xml.pre e
  216. end
  217. end
  218. end
  219. assert_match %r{<p>}, xml.target!
  220. assert_match %r{</p>}, xml.target!
  221. end
  222. end
  223. class TestDeclarations < Test::Unit::TestCase
  224. def setup
  225. @xml = Builder::XmlMarkup.new(:indent=>2)
  226. end
  227. def test_declare
  228. @xml.declare! :element
  229. assert_equal "<!element>\n", @xml.target!
  230. end
  231. def test_bare_arg
  232. @xml.declare! :element, :arg
  233. assert_equal"<!element arg>\n", @xml.target!
  234. end
  235. def test_string_arg
  236. @xml.declare! :element, "string"
  237. assert_equal"<!element \"string\">\n", @xml.target!
  238. end
  239. def test_mixed_args
  240. @xml.declare! :element, :x, "y", :z, "-//OASIS//DTD DocBook XML//EN"
  241. assert_equal "<!element x \"y\" z \"-//OASIS//DTD DocBook XML//EN\">\n", @xml.target!
  242. end
  243. def test_nested_declarations
  244. @xml = Builder::XmlMarkup.new
  245. @xml.declare! :DOCTYPE, :chapter do |x|
  246. x.declare! :ELEMENT, :chapter, "(title,para+)".intern
  247. end
  248. assert_equal "<!DOCTYPE chapter [<!ELEMENT chapter (title,para+)>]>", @xml.target!
  249. end
  250. def test_nested_indented_declarations
  251. @xml.declare! :DOCTYPE, :chapter do |x|
  252. x.declare! :ELEMENT, :chapter, "(title,para+)".intern
  253. end
  254. assert_equal "<!DOCTYPE chapter [\n <!ELEMENT chapter (title,para+)>\n]>\n", @xml.target!
  255. end
  256. def test_complex_declaration
  257. @xml.declare! :DOCTYPE, :chapter do |x|
  258. x.declare! :ELEMENT, :chapter, "(title,para+)".intern
  259. x.declare! :ELEMENT, :title, "(#PCDATA)".intern
  260. x.declare! :ELEMENT, :para, "(#PCDATA)".intern
  261. end
  262. expected = %{<!DOCTYPE chapter [
  263. <!ELEMENT chapter (title,para+)>
  264. <!ELEMENT title (#PCDATA)>
  265. <!ELEMENT para (#PCDATA)>
  266. ]>
  267. }
  268. assert_equal expected, @xml.target!
  269. end
  270. end
  271. class TestSpecialMarkup < Test::Unit::TestCase
  272. def setup
  273. @xml = Builder::XmlMarkup.new(:indent=>2)
  274. end
  275. def test_comment
  276. @xml.comment!("COMMENT")
  277. assert_equal "<!-- COMMENT -->\n", @xml.target!
  278. end
  279. def test_indented_comment
  280. @xml.p { @xml.comment! "OK" }
  281. assert_equal "<p>\n <!-- OK -->\n</p>\n", @xml.target!
  282. end
  283. def test_instruct
  284. @xml.instruct! :abc, :version=>"0.9"
  285. assert_equal "<?abc version=\"0.9\"?>\n", @xml.target!
  286. end
  287. def test_indented_instruct
  288. @xml.p { @xml.instruct! :xml }
  289. assert_match %r{<p>\n <\?xml version="1.0" encoding="UTF-8"\?>\n</p>\n},
  290. @xml.target!
  291. end
  292. def test_instruct_without_attributes
  293. @xml.instruct! :zz
  294. assert_equal "<?zz?>\n", @xml.target!
  295. end
  296. def test_xml_instruct
  297. @xml.instruct!
  298. assert_match(/^<\?xml version="1.0" encoding="UTF-8"\?>$/, @xml.target!)
  299. end
  300. def test_xml_instruct_with_overrides
  301. @xml.instruct! :xml, :encoding=>"UCS-2"
  302. assert_match(/^<\?xml version="1.0" encoding="UCS-2"\?>$/, @xml.target!)
  303. end
  304. def test_xml_instruct_with_standalong
  305. @xml.instruct! :xml, :encoding=>"UCS-2", :standalone=>"yes"
  306. assert_match(/^<\?xml version="1.0" encoding="UCS-2" standalone="yes"\?>$/, @xml.target!)
  307. end
  308. def test_no_blocks
  309. assert_raise(Builder::IllegalBlockError) do
  310. @xml.instruct! { |x| x.hi }
  311. end
  312. assert_raise(Builder::IllegalBlockError) do
  313. @xml.comment!(:element) { |x| x.hi }
  314. end
  315. end
  316. def test_cdata
  317. @xml.cdata!("TEST")
  318. assert_equal "<![CDATA[TEST]]>\n", @xml.target!
  319. end
  320. def test_cdata_with_ampersand
  321. @xml.cdata!("TEST&CHECK")
  322. assert_equal "<![CDATA[TEST&CHECK]]>\n", @xml.target!
  323. end
  324. def test_cdata_with_included_close
  325. @xml.cdata!("TEST]]>CHECK")
  326. assert_equal "<![CDATA[TEST]]]]><![CDATA[>CHECK]]>\n", @xml.target!
  327. end
  328. end
  329. class TestIndentedXmlMarkup < Test::Unit::TestCase
  330. def setup
  331. @xml = Builder::XmlMarkup.new(:indent=>2)
  332. end
  333. def test_one_level
  334. @xml.ol { |x| x.li "text" }
  335. assert_equal "<ol>\n <li>text</li>\n</ol>\n", @xml.target!
  336. end
  337. def test_two_levels
  338. @xml.p { |x|
  339. x.ol { x.li "text" }
  340. x.br
  341. }
  342. assert_equal "<p>\n <ol>\n <li>text</li>\n </ol>\n <br/>\n</p>\n", @xml.target!
  343. end
  344. def test_initial_level
  345. @xml = Builder::XmlMarkup.new(:indent=>2, :margin=>4)
  346. @xml.name { |x| x.first("Jim") }
  347. assert_equal " <name>\n <first>Jim</first>\n </name>\n", @xml.target!
  348. end
  349. class TestUtfMarkup < Test::Unit::TestCase
  350. if ! String.method_defined?(:encode)
  351. def setup
  352. @old_kcode = $KCODE
  353. end
  354. def teardown
  355. $KCODE = @old_kcode
  356. end
  357. def test_use_entities_if_no_encoding_is_given_and_kcode_is_none
  358. $KCODE = 'NONE'
  359. xml = Builder::XmlMarkup.new
  360. xml.p("\xE2\x80\x99")
  361. assert_match(%r(<p>&#8217;</p>), xml.target!) #
  362. end
  363. def test_use_entities_if_encoding_is_utf_but_kcode_is_not
  364. $KCODE = 'NONE'
  365. xml = Builder::XmlMarkup.new
  366. xml.instruct!(:xml, :encoding => 'UTF-8')
  367. xml.p("\xE2\x80\x99")
  368. assert_match(%r(<p>&#8217;</p>), xml.target!) #
  369. end
  370. else
  371. # change in behavior. As there is no $KCODE anymore, the default
  372. # moves from "does not understand utf-8" to "supports utf-8".
  373. def test_use_entities_if_no_encoding_is_given_and_kcode_is_none
  374. xml = Builder::XmlMarkup.new
  375. xml.p("\xE2\x80\x99")
  376. assert_match("<p>\u2019</p>", xml.target!) #
  377. end
  378. def test_use_entities_if_encoding_is_utf_but_kcode_is_not
  379. xml = Builder::XmlMarkup.new
  380. xml.instruct!(:xml, :encoding => 'UTF-8')
  381. xml.p("\xE2\x80\x99")
  382. assert_match("<p>\u2019</p>", xml.target!) #
  383. end
  384. end
  385. def encode string, encoding
  386. if !String.method_defined?(:encode)
  387. $KCODE = encoding
  388. string
  389. elsif encoding == 'UTF8'
  390. string.force_encoding('UTF-8')
  391. else
  392. string
  393. end
  394. end
  395. def test_use_entities_if_kcode_is_utf_but_encoding_is_dummy_encoding
  396. xml = Builder::XmlMarkup.new
  397. xml.instruct!(:xml, :encoding => 'UTF-16')
  398. xml.p(encode("\xE2\x80\x99", 'UTF8'))
  399. assert_match(%r(<p>&#8217;</p>), xml.target!) #
  400. end
  401. def test_use_entities_if_kcode_is_utf_but_encoding_is_unsupported_encoding
  402. xml = Builder::XmlMarkup.new
  403. xml.instruct!(:xml, :encoding => 'UCS-2')
  404. xml.p(encode("\xE2\x80\x99", 'UTF8'))
  405. assert_match(%r(<p>&#8217;</p>), xml.target!) #
  406. end
  407. def test_use_utf8_if_encoding_defaults_and_kcode_is_utf8
  408. xml = Builder::XmlMarkup.new
  409. xml.p(encode("\xE2\x80\x99",'UTF8'))
  410. assert_equal encode("<p>\xE2\x80\x99</p>",'UTF8'), xml.target!
  411. end
  412. def test_use_utf8_if_both_encoding_and_kcode_are_utf8
  413. xml = Builder::XmlMarkup.new
  414. xml.instruct!(:xml, :encoding => 'UTF-8')
  415. xml.p(encode("\xE2\x80\x99",'UTF8'))
  416. assert_match encode("<p>\xE2\x80\x99</p>",'UTF8'), xml.target!
  417. end
  418. def test_use_utf8_if_both_encoding_and_kcode_are_utf8_with_lowercase
  419. xml = Builder::XmlMarkup.new
  420. xml.instruct!(:xml, :encoding => 'utf-8')
  421. xml.p(encode("\xE2\x80\x99",'UTF8'))
  422. assert_match encode("<p>\xE2\x80\x99</p>",'UTF8'), xml.target!
  423. end
  424. end
  425. class TestXmlEvents < Test::Unit::TestCase
  426. def setup
  427. @handler = EventHandler.new
  428. @xe = Builder::XmlEvents.new(:target=>@handler)
  429. end
  430. def test_simple
  431. @xe.p
  432. assert_equal [:start, :p, nil], @handler.events.shift
  433. assert_equal [:end, :p], @handler.events.shift
  434. end
  435. def test_text
  436. @xe.p("HI")
  437. assert_equal [:start, :p, nil], @handler.events.shift
  438. assert_equal [:text, "HI"], @handler.events.shift
  439. assert_equal [:end, :p], @handler.events.shift
  440. end
  441. def test_attributes
  442. @xe.p("id"=>"2")
  443. ev = @handler.events.shift
  444. assert_equal [:start, :p], ev[0,2]
  445. assert_equal "2", ev[2]['id']
  446. assert_equal [:end, :p], @handler.events.shift
  447. end
  448. def test_indented
  449. @xml = Builder::XmlEvents.new(:indent=>2, :target=>@handler)
  450. @xml.p { |x| x.b("HI") }
  451. assert_equal [:start, :p, nil], @handler.events.shift
  452. assert_equal "\n ", pop_text
  453. assert_equal [:start, :b, nil], @handler.events.shift
  454. assert_equal "HI", pop_text
  455. assert_equal [:end, :b], @handler.events.shift
  456. assert_equal "\n", pop_text
  457. assert_equal [:end, :p], @handler.events.shift
  458. end
  459. def pop_text
  460. result = ''
  461. while ! @handler.events.empty? && @handler.events[0][0] == :text
  462. result << @handler.events[0][1]
  463. @handler.events.shift
  464. end
  465. result
  466. end
  467. class EventHandler
  468. attr_reader :events
  469. def initialize
  470. @events = []
  471. end
  472. def start_tag(sym, attrs)
  473. @events << [:start, sym, attrs]
  474. end
  475. def end_tag(sym)
  476. @events << [:end, sym]
  477. end
  478. def text(txt)
  479. @events << [:text, txt]
  480. end
  481. end
  482. end
  483. end