test_markupbuilder.rb 14 KB

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