PageRenderTime 28ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 1ms

/projects/jruby-1.7.3/test/externals/ruby1.9/rexml/test_core.rb

https://gitlab.com/essere.lab.public/qualitas.class-corpus
Ruby | 1462 lines | 1312 code | 95 blank | 55 comment | 2 complexity | 6dce2100908b4520a5ca3ab52844efe7 MD5 | raw file
  1. # coding: binary
  2. require "rexml_test_utils"
  3. require "rexml/document"
  4. require "rexml/parseexception"
  5. require "rexml/output"
  6. require "rexml/source"
  7. require "rexml/formatters/pretty"
  8. require "rexml/undefinednamespaceexception"
  9. require "listener"
  10. class Tester < Test::Unit::TestCase
  11. include REXMLTestUtils
  12. include REXML
  13. def setup
  14. @xsa_source = <<-EOL
  15. <?xml version="1.0"?>
  16. <?xsl stylesheet="blah.xsl"?>
  17. <!-- The first line tests the XMLDecl, the second tests PI.
  18. The next line tests DocType. This line tests comments. -->
  19. <!DOCTYPE xsa PUBLIC
  20. "-//LM Garshol//DTD XML Software Autoupdate 1.0//EN//XML"
  21. "http://www.garshol.priv.no/download/xsa/xsa.dtd">
  22. <xsa>
  23. <vendor id="blah">
  24. <name>Lars Marius Garshol</name>
  25. <email>larsga@garshol.priv.no</email>
  26. <url>http://www.stud.ifi.uio.no/~lmariusg/</url>
  27. </vendor>
  28. </xsa>
  29. EOL
  30. end
  31. def test_bad_markup
  32. [
  33. "<pkg='version'> foo </pkg>",
  34. '<0/>',
  35. '<a>&</a>',
  36. '<a>&a</a>',
  37. # '<a>&a;</a>', # FIXME
  38. '<a a="<"/>',
  39. '<a 3="<"/>',
  40. '<a a="1" a="2"/>',
  41. '<a><!-- -- --></a>',
  42. '<a><!-- ---></a>',
  43. '<a>&#x00;</a>',
  44. '<a>&#0;</a>',
  45. "<a a='&#0;' />",
  46. "<a>\f</a>",
  47. "<a a='\f' />",
  48. "<a>\000</a>",
  49. # FIXME '<a' + [65535].pack('U') + ' />',
  50. '<a>&#xfffe;</a>',
  51. '<a>&#65535;</a>',
  52. # FIXME '<a' + [0x0371].pack('U') + ' />',
  53. # FIXME '<a a' + [0x0371].pack('U') + '="" />',
  54. ].each do |src|
  55. assert_raise( ParseException, %Q{Parse #{src.inspect} should have failed!} ) do
  56. Document.new(src)
  57. end
  58. end
  59. end
  60. def test_attribute
  61. # Testing constructors
  62. #a = Attribute.new "hello", "dolly"
  63. #b = Attribute.new a
  64. #d = Document.new( "<a hello='dolly' href='blah'/>" )
  65. #c = d[0].attributes.get_attribute( "hello" )
  66. #assert_equal a, b
  67. #for attr in [ a, b, c]
  68. # assert_equal "hello", attr.name
  69. # assert_equal "dolly", attr.value
  70. #end
  71. # This because of a reported bug in attribute handling in 1.0a8
  72. source = '<a att="A">blah</a>'
  73. doc = Document.new source
  74. doc.elements.each do |a|
  75. a.attributes['att'] << 'B'
  76. assert_equal "AB", a.attributes['att']
  77. a.attributes['att'] = 'C'
  78. assert_equal "C", a.attributes['att']
  79. end
  80. # Bryan Murphy <murphybryanp@yahoo.com>
  81. text = "this is a {target[@name='test']/@value} test"
  82. source = <<-EOL
  83. <?xml version="1.0"?>
  84. <doc search="#{text}"/>
  85. EOL
  86. xml = Document.new source
  87. value = xml.root.attributes["search"]
  88. assert_equal text, value.to_s
  89. e = Element.new "test"
  90. e.add_attributes({ "name1" => "test1", "name4" => "test4" })
  91. e.add_attributes([["name3","test3"], ["name2","test2"]])
  92. assert_equal "test1", e.attributes["name1"]
  93. assert_equal "test2", e.attributes["name2"]
  94. assert_equal "test3", e.attributes["name3"]
  95. assert_equal "test4", e.attributes["name4"]
  96. # ensure that the attributes come out in sorted order
  97. assert_equal %w(<test
  98. name1='test1'
  99. name2='test2'
  100. name3='test3'
  101. name4='test4'/>).join(' '), e.to_s
  102. end
  103. def test_cdata
  104. test = "The quick brown fox jumped
  105. & < & < \" '
  106. over the lazy dog."
  107. source = "<a><![CDATA[#{test}]]></a>"
  108. d = REXML::Document.new( source )
  109. # Test constructors
  110. cdata = d[0][0]
  111. assert_equal test, cdata.value
  112. end
  113. def test_comment
  114. string = "This is a new comment!"
  115. source = "<!--#{string}-->"
  116. comment = Comment.new string
  117. REXML::Formatters::Default.new.write( comment, out = "" )
  118. assert_equal(source, out)
  119. comment2 = Comment.new comment
  120. assert_equal(comment, comment2)
  121. assert_raise(ParseException) {
  122. REXML::Document.new("<d><!- foo --></d>")
  123. }
  124. assert_raise(ParseException) {
  125. REXML::Document.new("<d><!-- foo -></d>")
  126. }
  127. end
  128. def test_whitespace
  129. doc = Document.new "<root-element><first-element/></root-element>"
  130. assert_equal 1, doc.root.size
  131. assert_equal 1, doc.root.elements.size
  132. doc = Document.new "<root-element>
  133. <first-element/>
  134. </root-element>"
  135. assert_equal 3, doc.root.size
  136. assert_equal 1, doc.root.elements.size
  137. text = " This is text
  138. with a lot of whitespace "
  139. source = "<a>#{text}<b>#{text}</b><c>#{text}</c>#{text}</a>"
  140. doc = Document.new( source, {
  141. :respect_whitespace => %w{ a c }
  142. } )
  143. assert_equal text, doc.elements["//c"].text
  144. string = ""
  145. doc.root.each { |n| string << n.to_s if n.kind_of? Text }
  146. assert_equal text+text, string
  147. string =" lots of blank
  148. space"
  149. doc.root.add_element("d").add_element("c").text = string
  150. doc.root.add_element("e").text = string
  151. assert_equal string, doc.elements["/a/d/c"].text
  152. assert string != doc.elements["/a/e"].text, "Text wasn't properly compressed"
  153. doc = Document.new source, { :respect_whitespace => :all }
  154. doc.root.add_element("d").text = string
  155. assert_equal text, doc.root.text
  156. nxt = ""
  157. doc.root.each { |n| nxt << n.to_s if n.kind_of? Text }
  158. assert_equal text+text, nxt
  159. assert_equal text, doc.root.elements["b"].text
  160. assert_equal text, doc.root.elements["c"].text
  161. assert_equal string, doc.root.elements["d"].text
  162. end
  163. # This isn't complete. We need to check declarations and comments
  164. def test_doctype
  165. string = "something"
  166. correct = "<!DOCTYPE something>"
  167. doc = DocType.new(string)
  168. assert_equal(string, doc.name)
  169. doc.write(out="")
  170. assert_equal(correct, out)
  171. doc2 = DocType.new(doc)
  172. assert_equal(doc.name, doc2.name)
  173. assert_equal(doc.external_id, doc2.external_id)
  174. correct = '<!DOCTYPE xsa PUBLIC "-//LM Garshol//DTD XML Software Autoupdate 1.0//EN//XML" "http://www.garshol.priv.no/download/xsa/xsa.dtd">'
  175. one_line_source = '<!DOCTYPE xsa PUBLIC "-//LM Garshol//DTD XML Software Autoupdate 1.0//EN//XML" "http://www.garshol.priv.no/download/xsa/xsa.dtd"><a/>'
  176. doc = Document.new( one_line_source )
  177. doc = doc[0]
  178. assert(doc)
  179. doc.write(test="")
  180. assert_equal(correct, test)
  181. multi_line_source = '<!DOCTYPE xsa PUBLIC
  182. "-//LM Garshol//DTD XML Software Autoupdate 1.0//EN//XML"
  183. "http://www.garshol.priv.no/download/xsa/xsa.dtd">
  184. <a/>'
  185. d = Document.new( multi_line_source )
  186. doc = d[0]
  187. assert(doc)
  188. doc.write(test="")
  189. assert_equal(correct, test)
  190. odd_space_source = ' <!DOCTYPE
  191. xsa PUBLIC "-//LM Garshol//DTD XML Software Autoupdate 1.0//EN//XML"
  192. "http://www.garshol.priv.no/download/xsa/xsa.dtd"> <a/>'
  193. d = Document.new( odd_space_source )
  194. dt = d.doctype
  195. dt.write(test="")
  196. assert_equal(correct, test)
  197. # OK, the BIG doctype test, numba wun
  198. docin = File.new(fixture_path("doctype_test.xml"))
  199. doc = Document.new(docin)
  200. doc.write(test="")
  201. assert_equal(31, doc.doctype.size)
  202. end
  203. def test_document
  204. # Testing cloning
  205. source = "<element/>"
  206. doc = Document.new source
  207. # Testing Root
  208. assert_equal doc.root.name.to_s, "element"
  209. # Testing String source
  210. source = @xsa_source
  211. doc = Document.new source
  212. assert_instance_of XMLDecl, doc.xml_decl
  213. assert_instance_of DocType, doc.doctype
  214. assert_equal doc.version, "1.0"
  215. source = File.new(fixture_path("dash.xml"))
  216. doc = Document.new source
  217. assert_equal "content-2", doc.elements["//content-2"].name
  218. end
  219. def test_instruction
  220. target = "use"
  221. content = "ruby"
  222. source = "<?#{target} #{content}?>"
  223. instruction = Instruction.new target, content
  224. instruction2 = Instruction.new instruction
  225. assert_equal(instruction, instruction2)
  226. REXML::Formatters::Default.new.write( instruction, out = "" )
  227. assert_equal(source, out)
  228. d = Document.new( source )
  229. instruction2 = d[0]
  230. assert_equal(instruction.to_s, instruction2.to_s)
  231. assert_raise(ParseException) {
  232. REXML::Document.new("<d><?foo bar></d>")
  233. }
  234. end
  235. def test_parent
  236. parent = Parent.new
  237. begin
  238. parent << "Something"
  239. rescue Exception
  240. parent << Comment.new("Some comment")
  241. assert parent.size == 1, "size of parent should be 1"
  242. else
  243. assert_fail "should have gotten an exception trying to add a "+ "String to a Parent"
  244. end
  245. source = "<a><one/><three/><five/></a>"
  246. doc = Document.new source
  247. three = doc.root.elements["three"]
  248. doc.root.insert_before( three, Element.new("two") )
  249. nxt = doc.root.elements["one"]
  250. string = ""
  251. while nxt
  252. string << nxt.name
  253. nxt = nxt.next_sibling
  254. end
  255. assert_equal "onetwothreefive", string
  256. doc.root.insert_after( three, Element.new("four") )
  257. string = ""
  258. doc.root.each { |element| string << element.name }
  259. assert_equal "onetwothreefourfive", string
  260. string = ""
  261. nxt = doc.root.elements["five"]
  262. while nxt
  263. string << nxt.name
  264. nxt = nxt.previous_sibling
  265. end
  266. assert_equal "fivefourthreetwoone", string
  267. doc.insert_after "//two", Element.new("two-and-half")
  268. string = doc.root.elements.collect {|x| x.name}.join
  269. assert_equal "onetwotwo-and-halfthreefourfive", string
  270. doc.elements["/a/five"].insert_before "../four", Element.new("three-and-half")
  271. string = doc.root.elements.collect {|x| x.name}.join
  272. assert_equal "onetwotwo-and-halfthreethree-and-halffourfive", string
  273. doc.elements["/a/five"].previous_sibling = Element.new("four-and-half")
  274. string = doc.root.elements.collect {|x| x.name}.join
  275. assert_equal "onetwotwo-and-halfthreethree-and-halffourfour-and-halffive", string
  276. doc.elements["/a/one"].next_sibling = Element.new("one-and-half")
  277. string = doc.root.elements.collect {|x| x.name}.join
  278. assert_equal "oneone-and-halftwotwo-and-halfthreethree-and-halffourfour-and-halffive", string
  279. doc = Document.new "<a><one/><three/></a>"
  280. doc.root[1,0] = Element.new "two"
  281. string = ""
  282. doc.root.each { |el| string << el.name }
  283. assert_equal "onetwothree", string
  284. end
  285. # The Source classes are tested extensively throughout the test suite
  286. def test_source
  287. # Testing string source
  288. source = @xsa_source
  289. doc = Document.new source
  290. assert_equal doc.root.name.to_s, "xsa"
  291. # Testing IO source
  292. doc = Document.new File.new(fixture_path("project.xml"))
  293. assert_equal doc.root.name.to_s, "Project"
  294. end
  295. def test_text
  296. f = REXML::Formatters::Default.new
  297. string = "Some text"
  298. text = Text.new(string)
  299. assert_equal(string, text.to_s)
  300. text2 = Text.new(text)
  301. assert_equal(text, text2)
  302. #testing substitution
  303. string = "0 < ( 1 & 1 )"
  304. correct = "0 &lt; ( 1 &amp; 1 )"
  305. text = Text.new(string, true)
  306. f.write(text,out="")
  307. assert_equal(correct, out)
  308. string = "Cats &amp; dogs"
  309. text = Text.new(string, false, nil, true)
  310. assert_equal(string, text.to_s)
  311. string2 = "<a>#{string}</a>"
  312. doc = Document.new( string2, {
  313. :raw => %w{ a b }
  314. } )
  315. f.write(doc,out="")
  316. assert_equal(string2, out)
  317. b = doc.root.add_element( "b" )
  318. b.text = string
  319. assert_equal(string, b.get_text.to_s)
  320. c = doc.root.add_element("c")
  321. c.text = string
  322. assert_equal("Cats &amp;amp; dogs", c.get_text.to_s)
  323. # test all
  324. string = "<a>&amp;<b>&lt;</b><c>&gt;<d>&quot;</d></c></a>"
  325. doc = Document.new(string, { :raw => :all })
  326. assert_equal( "&amp;", doc.elements["/a"][0].to_s )
  327. assert_equal( "&", doc.elements["/a"].text )
  328. assert_equal( "&lt;", doc.elements["/a/b"][0].to_s )
  329. assert_equal( "<", doc.elements["/a/b"].text )
  330. assert_equal( "&gt;", doc.elements["/a/c"][0].to_s )
  331. assert_equal( ">", doc.elements["/a/c"].text )
  332. assert_equal( '&quot;', doc.elements["//d"][0].to_s )
  333. assert_equal( '"', doc.elements["//d"].text )
  334. # test some other stuff
  335. doc = Document.new('<a><b/></a>')
  336. doc.root.text = 'Sean'
  337. assert_equal( '<a><b/>Sean</a>', doc.to_s )
  338. doc.root.text = 'Elliott'
  339. assert_equal( '<a><b/>Elliott</a>', doc.to_s )
  340. doc.root.add_element( 'c' )
  341. assert_equal( '<a><b/>Elliott<c/></a>', doc.to_s )
  342. doc.root.text = 'Russell'
  343. assert_equal( '<a><b/>Russell<c/></a>', doc.to_s )
  344. doc.root.text = nil
  345. assert_equal( '<a><b/><c/></a>', doc.to_s )
  346. end
  347. def test_text_frozen
  348. string = "Frozen".freeze
  349. text = Text.new(string)
  350. assert_equal(string, text.to_s)
  351. end
  352. def test_xmldecl
  353. source = "<?xml version='1.0'?>"
  354. # test args
  355. # test no args
  356. decl2 = XMLDecl.new
  357. assert_equal source, decl2.to_s
  358. # test XMLDecl
  359. decl2 = XMLDecl.new "1.0"
  360. assert_equal source, decl2.to_s
  361. end
  362. def test_xmldecl_utf_16be_encoding_name
  363. assert_equal("<?xml version='1.0' encoding='UTF-16'?>",
  364. XMLDecl.new("1.0", "UTF-16").to_s)
  365. end
  366. def each_test( element, xpath, num_children )
  367. count = 0
  368. element.each_element( xpath ) { |child|
  369. count += 1
  370. yield child if block_given?
  371. }
  372. assert_equal num_children, count
  373. end
  374. # This is the biggest test, as the number of permutations of xpath are
  375. # enormous.
  376. def test_element_access
  377. # Testing each_element
  378. doc = Document.new File.new(fixture_path("project.xml"))
  379. each_test( doc, "/", 1 ) { |child|
  380. assert_equal doc.name, child.name
  381. }
  382. each_test(doc, ".", 1) { |child| assert_equal doc, child }
  383. each_test(doc.root, "..", 1) { |child| assert_equal doc, child }
  384. each_test(doc.root, "*", 5)
  385. each_test(doc, "Project/Datasets", 1) { |child|
  386. assert_equal "Datasets", child.name
  387. }
  388. each_test(doc, "Project/Datasets/link", 2 )
  389. each_test(doc.root, "/Project/Description", 1) {|child|
  390. assert_equal "Description", child.name
  391. }
  392. each_test(doc.root, "./Description",1 ) { |child|
  393. assert_equal "Description",child.name
  394. }
  395. each_test(doc.root, "../Project",1 ) { |child|
  396. assert_equal doc.root, child
  397. }
  398. #each_test(doc,".../link",2) {|child| assert_equal "link",child.name.to_s}
  399. # test get_element
  400. first = doc.elements[ "Project" ]
  401. assert_equal doc.root, first
  402. second = doc.elements[ "Project" ].elements[1]
  403. third = doc.elements[ "Project/Creator" ]
  404. assert_equal second, third
  405. fourth = doc.elements[ "Project/Datasets/link[@idref='18']" ]
  406. assert_equal "Test data 1", fourth.attributes["name"]
  407. # Testing each_predicate
  408. each_test( doc, "Project/Datasets/link[@idref='18']", 1 ) { |child|
  409. assert_equal "Test data 1", child.attributes["name"]
  410. }
  411. # testing next/previous_element
  412. creator = doc.elements["//Creator"]
  413. lm = creator.next_element
  414. assert_equal "LastModifier", lm.name
  415. assert_equal "Creator", lm.previous_element.name
  416. end
  417. def test_child
  418. sean = Element.new "Sean"
  419. rubbell = Element.new "Rubbell"
  420. elliott = sean.add_element "Elliott"
  421. sean << rubbell
  422. assert_equal elliott, rubbell.previous_sibling
  423. assert_equal rubbell, elliott.next_sibling
  424. russell = Element.new "Russell"
  425. rubbell.replace_with russell
  426. assert_equal elliott, russell.previous_sibling
  427. assert_equal russell, elliott.next_sibling
  428. assert_nil russell.document
  429. assert_equal sean, russell.root
  430. end
  431. # Most of this class is tested elsewhere. Here are the methods which
  432. # aren't used in any other class
  433. def test_element
  434. sean = Element.new "Sean"
  435. string = "1) He's a great guy!"
  436. sean.text = string
  437. russell = Element.new "Russell"
  438. sean << russell
  439. russell.attributes["email"] = "ser@germane-software.com"
  440. assert_equal russell.attributes["email"], "ser@germane-software.com"
  441. russell.attributes["webpage"] = "http://www.germane-software.com/~ser"
  442. assert sean.has_text?, "element should have text"
  443. assert_equal sean.text, string
  444. assert sean.has_elements?, "element should have one element"
  445. string = "2) What a stud!"
  446. sean.add_text string
  447. sean.text = "3) Super programmer!"
  448. sean.text = nil
  449. assert sean.has_text?, "element should still have text"
  450. assert_equal sean.text, string
  451. russell.delete_attribute "email"
  452. assert_nil russell.attributes["email"]
  453. russell.attributes.delete "webpage"
  454. assert !russell.has_attributes?, "element should have no attributes"
  455. end
  456. def test_no_format
  457. source = "<a><b><c>blah</c><d/></b></a>"
  458. out = ""
  459. doc = Document.new( source )
  460. doc.write(out)
  461. assert_equal(source, out)
  462. end
  463. def test_namespace
  464. source = <<-EOF
  465. <x xmlns:foo="http://www.bar.com/schema">
  466. </x>
  467. EOF
  468. doc = Document.new(source)
  469. assert_equal("http://www.bar.com/schema", doc.root.namespace( "foo" ))
  470. source = <<-EOF
  471. <!-- bar namespace is "someuri" -->
  472. <foo:bar xmlns="default" xmlns:foo="someuri">
  473. <!-- a namespace is "default" -->
  474. <a/>
  475. <!-- foo:b namespace is "someuri" -->
  476. <foo:b>
  477. <!-- c namespace is "default" -->
  478. <c/>
  479. </foo:b>
  480. <!-- d namespace is "notdefault" -->
  481. <d xmlns="notdefault">
  482. <!-- e namespace is "notdefault" -->
  483. <e/>
  484. <f xmlns="">
  485. <g/>
  486. </f>
  487. </d>
  488. </foo:bar>
  489. EOF
  490. doc = Document.new source
  491. assert_equal "someuri", doc.root.namespace
  492. assert_equal "default", doc.root.elements[1].namespace
  493. assert_equal "someuri", doc.root.elements[2].namespace
  494. assert_equal "notdefault", doc.root.elements[ 3 ].namespace
  495. # Testing namespaces in attributes
  496. source = <<-EOF
  497. <a xmlns:b="uri">
  498. <b b:a="x" a="y"/>
  499. <c xmlns="foo">
  500. </c>
  501. </a>
  502. EOF
  503. doc = Document.new source
  504. b = doc.root.elements["b"]
  505. assert_equal "x", b.attributes["b:a"]
  506. assert_equal "y", b.attributes["a"]
  507. doc = Document.new
  508. doc.add_element "sean:blah"
  509. doc.root.text = "Some text"
  510. out = ""
  511. doc.write(out)
  512. assert_equal "<sean:blah>Some text</sean:blah>", out
  513. end
  514. def test_add_namespace
  515. e = Element.new 'a'
  516. e.add_namespace 'someuri'
  517. e.add_namespace 'foo', 'otheruri'
  518. e.add_namespace 'xmlns:bar', 'thirduri'
  519. assert_equal 'someuri', e.attributes['xmlns']
  520. assert_equal 'otheruri', e.attributes['xmlns:foo']
  521. assert_equal 'thirduri', e.attributes['xmlns:bar']
  522. end
  523. def test_big_documentation
  524. f = File.new(fixture_path("documentation.xml"))
  525. d = Document.new f
  526. assert_equal "Sean Russell", d.elements["documentation/head/author"].text.tr("\n\t", " ").squeeze(" ")
  527. out = ""
  528. d.write out
  529. end
  530. def test_tutorial
  531. doc = Document.new File.new(fixture_path("tutorial.xml"))
  532. out = ""
  533. doc.write out
  534. end
  535. def test_stream
  536. c = Listener.new
  537. Document.parse_stream( File.new(fixture_path("documentation.xml")), c )
  538. assert(c.ts, "Stream parsing apparantly didn't parse the whole file")
  539. assert(c.te, "Stream parsing dropped end tag for documentation")
  540. Document.parse_stream("<a.b> <c/> </a.b>", c)
  541. Document.parse_stream("<a>&lt;&gt;&amp;</a>", c)
  542. assert_equal('<>&', c.normalize)
  543. end
  544. def test_line
  545. Document.new File.new(fixture_path("bad.xml"))
  546. assert_fail "There should have been an error"
  547. rescue Exception
  548. # We should get here
  549. assert($!.line == 5, "Should have been an error on line 5, "+
  550. "but was reported as being on line #{$!.line}" )
  551. end
  552. def test_substitution
  553. val = "a'b\"c"
  554. el = Element.new("a")
  555. el.attributes["x"] = val
  556. REXML::Formatters::Default.new.write(el, out="")
  557. nel = Document.new( out)
  558. assert_equal( val, nel.root.attributes["x"] )
  559. end
  560. def test_exception
  561. source = SourceFactory.create_from "<a/>"
  562. p = ParseException.new( "dummy message", source )
  563. begin
  564. raise "dummy"
  565. rescue Exception
  566. p.continued_exception = $!
  567. end
  568. end
  569. def test_bad_content
  570. in_gt = '<root-el>content>content</root-el>'
  571. in_lt = '<root-el>content<content</root-el>'
  572. # This is OK
  573. tree_gt = Document.new in_gt
  574. assert_equal "content>content", tree_gt.elements[1].text
  575. # This isn't
  576. begin
  577. Document.new in_lt
  578. assert_fail "Should have gotten a parse error"
  579. rescue ParseException
  580. end
  581. end
  582. def test_iso_8859_1_output_function
  583. out = ""
  584. output = Output.new( out )
  585. koln_iso_8859_1 = "K\xF6ln"
  586. koln_utf8 = "K\xc3\xb6ln"
  587. source = Source.new( koln_iso_8859_1, 'iso-8859-1' )
  588. results = source.scan(/.*/)[0]
  589. koln_utf8.force_encoding('UTF-8') if koln_utf8.respond_to?(:force_encoding)
  590. assert_equal koln_utf8, results
  591. output << results
  592. if koln_iso_8859_1.respond_to?(:force_encoding)
  593. koln_iso_8859_1.force_encoding('ISO-8859-1')
  594. end
  595. assert_equal koln_iso_8859_1, out
  596. end
  597. def test_attributes_each
  598. doc = Document.new("<a xmlns:a='foo'><b x='1' y='2' z='3' a:x='4'/></a>")
  599. count = 0
  600. doc.root.elements[1].attributes.each {|k,v| count += 1 }
  601. assert_equal 4, count
  602. end
  603. def test_delete_namespace
  604. doc = Document.new "<a xmlns='1' xmlns:x='2'/>"
  605. doc.root.delete_namespace
  606. doc.root.delete_namespace 'x'
  607. assert_equal "<a/>", doc.to_s
  608. end
  609. def test_each_element_with_attribute
  610. doc = Document.new "<a><b id='1'/><c id='2'/><d id='1'/><e/></a>"
  611. arry = []
  612. block = proc { |e|
  613. assert arry.include?(e.name)
  614. arry.delete e.name
  615. }
  616. # Yields b, c, d
  617. arry = %w{b c d}
  618. doc.root.each_element_with_attribute( 'id', &block )
  619. assert_equal 0, arry.size
  620. # Yields b, d
  621. arry = %w{b d}
  622. doc.root.each_element_with_attribute( 'id', '1', &block )
  623. assert_equal 0, arry.size
  624. # Yields b
  625. arry = ['b']
  626. doc.root.each_element_with_attribute( 'id', '1', 1, &block )
  627. assert_equal 0, arry.size
  628. # Yields d
  629. arry = ['d']
  630. doc.root.each_element_with_attribute( 'id', '1', 0, 'd', &block )
  631. assert_equal 0, arry.size
  632. end
  633. def test_each_element_with_text
  634. doc = Document.new '<a><b>b</b><c>b</c><d>d</d><e/></a>'
  635. arry = []
  636. block = proc { |e|
  637. assert arry.include?(e.name)
  638. arry.delete e.name
  639. }
  640. # Yields b, c, d
  641. arry = %w{b c d}
  642. doc.root.each_element_with_text(&block)
  643. assert_equal 0, arry.size
  644. # Yields b, d
  645. arry = %w{b c}
  646. doc.root.each_element_with_text( 'b', &block )
  647. assert_equal 0, arry.size
  648. # Yields b
  649. arry = ['b']
  650. doc.root.each_element_with_text( 'b', 1, &block )
  651. assert_equal 0, arry.size
  652. # Yields d
  653. arry = ['d']
  654. doc.root.each_element_with_text( nil, 0, 'd', &block )
  655. assert_equal 0, arry.size
  656. end
  657. def test_element_parse_stream
  658. s = Source.new( "<a>some text</a>" )
  659. l = Listener.new
  660. class << l
  661. def tag_start name, attributes
  662. raise "Didn't find proper tag name" unless 'a'==name
  663. end
  664. end
  665. Document::parse_stream(s, l)
  666. end
  667. def test_deep_clone
  668. a = Document.new( '<?xml version="1"?><a x="y"><b>text</b>text<c><d><e>text</e></d></c></a>' )
  669. b = a.deep_clone
  670. assert_equal a.to_s, b.to_s
  671. a = Document.new( '<a>some &lt; text <b> more &gt; text </b> &gt; </a>' )
  672. b = a.deep_clone
  673. assert_equal a.to_s, b.to_s
  674. c = Document.new( b.to_s )
  675. assert_equal a.to_s, c.to_s
  676. end
  677. def test_whitespace_before_root
  678. a = <<EOL
  679. <?xml version='1.0'?>
  680. <blo>
  681. <wak>
  682. </wak>
  683. </blo>
  684. EOL
  685. d = Document.new(a)
  686. b = ""
  687. d.write( b )
  688. assert_equal a,b
  689. end
  690. def test_entities
  691. a = Document.new( '<a>&#101;&#x65;&#252;</a>' )
  692. assert_equal('eeü'.force_encoding("UTF-8"), a.root.text)
  693. end
  694. def test_element_decl
  695. element_decl = Source.new("<!DOCTYPE foo [
  696. <!ELEMENT bar (#PCDATA)>
  697. ]>")
  698. doc = Document.new( element_decl )
  699. d = doc[0]
  700. assert_equal("<!ELEMENT bar (#PCDATA)>", d.to_s.split(/\n/)[1].strip)
  701. end
  702. def test_attlist_decl
  703. doc = Document.new <<-EOL
  704. <!DOCTYPE blah [
  705. <!ATTLIST blah
  706. xmlns CDATA "foo">
  707. <!ATTLIST a
  708. bar CDATA "gobble"
  709. xmlns:one CDATA "two"
  710. >
  711. ]>
  712. <a xmlns:three='xxx' three='yyy'><one:b/><three:c/></a>
  713. EOL
  714. assert_equal 'gobble', doc.root.attributes['bar']
  715. assert_equal 'xxx', doc.root.elements[2].namespace
  716. assert_equal 'two', doc.root.elements[1].namespace
  717. assert_equal 'foo', doc.root.namespace
  718. doc = Document.new <<-EOL
  719. <?xml version="1.0"?>
  720. <!DOCTYPE schema SYSTEM "XMLSchema.dtd" [
  721. <!ENTITY % p ''>
  722. <!ENTITY % s ''>
  723. <!ATTLIST schema
  724. xmlns:svg CDATA #FIXED "http://www.w3.org/2000/svg"
  725. xmlns:xlink CDATA #FIXED "http://www.w3.org/1999/xlink"
  726. xmlns:xml CDATA #FIXED "http://www.w3.org/XML/1998/namespace"
  727. >]>
  728. <schema/>
  729. EOL
  730. prefixes = doc.root.prefixes.sort
  731. correct = ['svg', 'xlink', 'xml']
  732. assert_equal correct, prefixes
  733. end
  734. def test_attlist_write
  735. file=File.new(fixture_path("foo.xml"))
  736. doc=Document.new file
  737. out = ''
  738. doc.write(out)
  739. end
  740. def test_more_namespaces
  741. assert_raise( REXML::UndefinedNamespaceException,
  742. %Q{Should have gotten an Undefined Namespace error} ) {
  743. Document.new("<r><p><n:c/></p></r>")
  744. }
  745. doc2 = Document.new("<r xmlns:n='1'><p><n:c/></p></r>")
  746. es = XPath.match(doc2, '//c')
  747. assert_equal 0, es.size
  748. es = XPath.match(doc2, '//n:c')
  749. assert_equal 1, es.size
  750. doc2.root.add_namespace('m', '2')
  751. doc2.root.add_element("m:o")
  752. es = XPath.match(doc2, './/o')
  753. assert_equal 0, es.size
  754. es = XPath.match(doc2, '//n:c')
  755. assert_equal 1, es.size
  756. end
  757. def test_ticket_51
  758. doc = REXML::Document.new <<-EOL
  759. <test xmlns='1' xmlns:x='1'>
  760. <a>X</a>
  761. <x:a>Y</x:a>
  762. <b xmlns='2'>
  763. <a>Z</a>
  764. </b>
  765. </test>
  766. EOL
  767. # The most common case. People not caring about the namespaces much.
  768. assert_equal( "XY", XPath.match( doc, "/test/a/text()" ).join )
  769. assert_equal( "XY", XPath.match( doc, "/test/x:a/text()" ).join )
  770. # Surprising? I don't think so, if you believe my definition of the "common case"
  771. assert_equal( "XYZ", XPath.match( doc, "//a/text()" ).join )
  772. # These are the uncommon cases. Namespaces are actually important, so we define our own
  773. # mappings, and pass them in.
  774. assert_equal( "XY", XPath.match( doc, "/f:test/f:a/text()", { "f" => "1" } ).join )
  775. # The namespaces are defined, and override the original mappings
  776. assert_equal( "", XPath.match( doc, "/test/a/text()", { "f" => "1" } ).join )
  777. assert_equal( "", XPath.match( doc, "/x:test/x:a/text()", { "f" => "1" } ).join )
  778. assert_equal( "", XPath.match( doc, "//a/text()", { "f" => "1" } ).join )
  779. end
  780. def test_processing_instruction
  781. d = Document.new("<a><?foo bar?><?foo2 bar2?><b><?foo3 bar3?></b><?foo4 bar4?></a>")
  782. assert_equal 4, XPath.match(d, '//processing-instruction()' ).size
  783. match = XPath.match(d, "//processing-instruction('foo3')" )
  784. assert_equal 1, match.size
  785. assert_equal 'bar3', match[0].content
  786. end
  787. def test_oses_with_bad_EOLs
  788. Document.new("\n\n\n<?xml version='1.0'?>\n\n\n<a/>\n\n")
  789. end
  790. # Contributed (with patch to fix bug) by Kouhei
  791. def test_ignore_whitespace
  792. source = "<a> <b/> abc <![CDATA[def]]> </a>"
  793. context_all = {:ignore_whitespace_nodes => :all}
  794. context_a = {:ignore_whitespace_nodes => %(a)}
  795. context_b = {:ignore_whitespace_nodes => %(b)}
  796. tests = [[[" abc ", "def"], context_all],
  797. [[" abc ", "def"], context_a],
  798. [[" ", " abc ", "def", " "], context_b]]
  799. tests.each do |test|
  800. assert_equal(test[0], Document.new(source, test[1]).root.texts.collect{|x|
  801. x.to_s})
  802. end
  803. end
  804. def test_0xD_in_preface
  805. doc = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\x0D<opml version=\"1.0\">\x0D</opml>"
  806. doc = Document.new doc
  807. end
  808. def test_hyphens_in_doctype
  809. doc = REXML::Document.new <<-EOQ
  810. <?xml version="1.0"?>
  811. <!DOCTYPE a-b-c>
  812. <a-b-c>
  813. <a/>
  814. </a-b-c>
  815. EOQ
  816. assert_equal('a-b-c', doc.doctype.name)
  817. end
  818. def test_accents
  819. docs = [
  820. %Q{<?xml version="1.0" encoding="ISO-8859-1"?>
  821. <gnuPod>
  822. <files>
  823. <file id="57" artist="Coralie Cl\357\277\275ent" />
  824. </files>
  825. </gnuPod>},
  826. '<?xml version="1.0" encoding="ISO-8859-1"?>
  827. <gnuPod>
  828. <files>
  829. <file id="71" album="Astrakan Caf" />
  830. </files>
  831. </gnuPod>',
  832. %Q{<?xml version="1.0" encoding="ISO-8859-1"?>
  833. <gnuPod>
  834. <files>
  835. <file id="71" album="Astrakan Caf\357\277\275eria" />
  836. </files>
  837. </gnuPod>},
  838. %Q{<?xml version="1.0" encoding="ISO-8859-1"?>
  839. <gnuPod>
  840. <files>
  841. <file id="71" album="Astrakan Caf\357\277\275" />
  842. </files>
  843. </gnuPod>} ]
  844. docs.each_with_index { |d,i|
  845. begin
  846. REXML::Document.new(d)
  847. rescue
  848. puts "#{i} => #{docs[i]}"
  849. raise
  850. end
  851. }
  852. end
  853. def test_replace_text
  854. e = REXML::Element.new( "a" )
  855. e.add_text( "foo" )
  856. assert_equal( "<a>foo</a>", e.to_s )
  857. e[0].value = "bar"
  858. assert_equal( "<a>bar</a>", e.to_s )
  859. e[0].value = "<"
  860. assert_equal( "<a>&lt;</a>", e.to_s )
  861. assert_equal( "<", e[0].value )
  862. end
  863. def test_write_doctype
  864. ## XML Document and Declaration
  865. document = REXML::Document.new
  866. xmldecl = REXML::XMLDecl.new("1.0", "UTF-8")
  867. document.add(xmldecl)
  868. s = ""
  869. document.write(s)
  870. ## XML Doctype
  871. str = '<!DOCTYPE foo "bar">'
  872. source = REXML::Source.new(str)
  873. doctype = REXML::DocType.new(source)
  874. document.add(doctype)
  875. document.write(s)
  876. ## Element
  877. element = REXML::Element.new("hoge")
  878. document.add(element)
  879. document.write(s)
  880. end
  881. def test_write_cdata
  882. src = "<a>A</a>"
  883. doc = REXML::Document.new( src )
  884. out = ""
  885. doc.write( out )
  886. assert_equal( src, out )
  887. src = "<a><![CDATA[A]]></a>"
  888. doc = REXML::Document.new( src )
  889. out = ""
  890. doc.write( out )
  891. assert_equal( src, out )
  892. end
  893. def test_namespace_attributes
  894. source = <<-EOL
  895. <a xmlns:x="1">
  896. <x:b x:n="foo"/>
  897. </a>
  898. EOL
  899. d = Document.new( source )
  900. assert_equal( 'foo', REXML::XPath.first(d.root, "//x:b/@x:n").value )
  901. assert_equal( nil, REXML::XPath.first(d.root, "//x:b/@x:n", {}))
  902. end
  903. def test_null_element_name
  904. a = REXML::Document.new
  905. assert_raise( RuntimeError ) {
  906. a.add_element( nil )
  907. }
  908. end
  909. def test_text_raw
  910. # From the REXML tutorial
  911. # (http://www.germane-software.com/software/rexml/test/data/tutorial.html)
  912. doc = Document.new <<-EOL
  913. <?xml version="1.0"?>
  914. <!DOCTYPE schema SYSTEM "XMLSchema.dtd" [
  915. <!ENTITY % s 'Sean'>
  916. ]>
  917. <a/>
  918. EOL
  919. a = doc.root
  920. # This makes sure that RAW text nodes don't have their entity strings
  921. # replaced
  922. t = Text.new "Sean", false, nil, true
  923. a.text = t
  924. assert_equal( "Sean", t.to_s )
  925. assert_equal( "Sean", t.value )
  926. # This makes sure that they do
  927. t = Text.new "Sean", false, nil, false
  928. a.text = t
  929. assert_equal( "&s;", t.to_s )
  930. assert_equal( "Sean", t.value )
  931. t = Text.new "&s;", false, nil, true
  932. a.text = t
  933. assert_equal( "&s;", t.to_s )
  934. assert_equal( "Sean", t.value )
  935. t = Text.new "&s;", false, nil, true
  936. a.text = t
  937. assert_equal( "&s;", t.to_s )
  938. assert_equal( "Sean", t.value )
  939. # Ticket #44
  940. t = REXML::Text.new( "&amp;", false, nil, true )
  941. assert_equal( "&amp;", t.to_s )
  942. t = REXML::Text.new("&amp;", false, false)
  943. assert_equal( "&amp;amp;", t.to_s )
  944. end
  945. def test_to_xpath
  946. doc = REXML::Document.new( %q{<tag1>
  947. <tag2 name="tag2"/>
  948. <tag2 name="tag2"/>
  949. </tag1>})
  950. names = %w{ /tag1/tag2[1] /tag1/tag2[2] }
  951. doc.root.elements.each_with_index {|el, i|
  952. assert_equal( names[i], el.xpath )
  953. }
  954. end
  955. def test_transitive
  956. doc = REXML::Document.new( "<a/>")
  957. s = ""
  958. doc.write( s, 0, true )
  959. end
  960. # This is issue #40
  961. def test_replace_with
  962. old = '<doc>old<foo/>old</doc>'
  963. d = REXML::Document.new(old).root
  964. new = REXML::Text.new('new',true,nil,true)
  965. child = d.children[2]
  966. child.replace_with(new)
  967. assert_equal( new, d.children[2] )
  968. end
  969. def test_repeated_writes
  970. a = IO.read(fixture_path("iso8859-1.xml"))
  971. f = REXML::Formatters::Pretty.new
  972. xmldoc = REXML::Document.new( a )
  973. a_andre = xmldoc.elements['//image'].attributes['caption']
  974. f.write(xmldoc,b="")
  975. xmldoc = REXML::Document.new(b)
  976. b_andre = xmldoc.elements['//image'].attributes['caption']
  977. assert_equal( a_andre, b_andre )
  978. f.write(xmldoc,c="")
  979. xmldoc = REXML::Document.new(c)
  980. c_andre = xmldoc.elements['//image'].attributes['caption']
  981. assert_equal( b_andre, c_andre )
  982. o = Output.new(d="","UTF-8")
  983. f.write(xmldoc,o)
  984. assert_not_equal( c, d )
  985. end
  986. def test_pretty_format_long_text_finite
  987. n = 1_000_000
  988. long_text = 'aaaa ' * n
  989. xml = "<doc>#{long_text}</doc>"
  990. formatter = REXML::Formatters::Pretty.new
  991. document = nil
  992. begin
  993. document = REXML::Document.new(xml)
  994. rescue REXML::ParseException
  995. skip_message = "skip this test because we can't check Pretty#wrap " +
  996. "works without #<SystemStackError: stack level too deep> on " +
  997. "small memory system. #<RegexpError: failed to allocate memory> " +
  998. "will be raised on the system. See also [ruby-dev:42599]."
  999. return skip_message
  1000. end
  1001. output = ""
  1002. formatter.write(document, output)
  1003. assert_equal("<doc>\n" +
  1004. ((" " + (" aaaa" * 15) + "\n") * (n / 15)) +
  1005. " " + ("aaaa " * (n % 15)) + "\n" +
  1006. "</doc>",
  1007. output)
  1008. end
  1009. def test_pretty_format_deep_indent
  1010. n = 6
  1011. elements = ""
  1012. n.times do |i|
  1013. elements << "<element#{i}>"
  1014. elements << "element#{i} " * 5
  1015. end
  1016. (n - 1).downto(0) do |i|
  1017. elements << "</element#{i}>"
  1018. end
  1019. xml = "<doc>#{elements}</doc>"
  1020. document = REXML::Document.new(xml)
  1021. formatter = REXML::Formatters::Pretty.new
  1022. formatter.width = 20
  1023. output = ""
  1024. formatter.write(document, output)
  1025. assert_equal(<<-XML.strip, output)
  1026. <doc>
  1027. <element0>
  1028. element0
  1029. element0
  1030. element0
  1031. element0
  1032. element0
  1033. <element1>
  1034. element1
  1035. element1
  1036. element1
  1037. element1
  1038. element1
  1039. <element2>
  1040. element2
  1041. element2
  1042. element2
  1043. element2
  1044. element2
  1045. <element3>
  1046. element3
  1047. element3
  1048. element3
  1049. element3
  1050. element3
  1051. <element4>
  1052. element4
  1053. element4
  1054. element4
  1055. element4
  1056. element4
  1057. <element5>
  1058. element5 element5 element5 element5 element5
  1059. </element5>
  1060. </element4>
  1061. </element3>
  1062. </element2>
  1063. </element1>
  1064. </element0>
  1065. </doc>
  1066. XML
  1067. end
  1068. def test_ticket_58
  1069. doc = REXML::Document.new
  1070. doc << REXML::XMLDecl.default
  1071. doc << REXML::Element.new("a")
  1072. str = ""
  1073. doc.write(str)
  1074. assert_equal("<a/>", str)
  1075. doc = REXML::Document.new
  1076. doc << REXML::XMLDecl.new("1.0", "UTF-8")
  1077. doc << REXML::Element.new("a")
  1078. str = ""
  1079. doc.write(str)
  1080. assert_equal("<?xml version='1.0' encoding='UTF-8'?><a/>", str)
  1081. end
  1082. # Incomplete tags should generate an error
  1083. def test_ticket_53
  1084. assert_raise( REXML::ParseException ) {
  1085. REXML::Document.new( "<a><b></a>" )
  1086. }
  1087. assert_raise( REXML::ParseException ) {
  1088. REXML::Document.new( "<a><b>" )
  1089. }
  1090. assert_raise( REXML::ParseException ) {
  1091. REXML::Document.new( "<a><b/>" )
  1092. }
  1093. end
  1094. def test_ticket_52
  1095. source = "<!-- this is a single line comment -->"
  1096. d = REXML::Document.new(source)
  1097. d.write(k="")
  1098. assert_equal( source, k )
  1099. source = "<a><!-- Comment --></a>"
  1100. target = "<a>\n <!-- Comment -->\n</a>"
  1101. d = REXML::Document.new(source)
  1102. REXML::Formatters::Pretty.new(4).write(d,k="")
  1103. assert_equal( target, k )
  1104. end
  1105. def test_ticket_76
  1106. src = "<div>at&t"
  1107. assert_raise( ParseException, %Q{"#{src}" is invalid XML} ) {
  1108. REXML::Document.new(src)
  1109. }
  1110. end
  1111. def test_ticket_21
  1112. src = "<foo bar=value/>"
  1113. assert_raise( ParseException, "invalid XML should be caught" ) {
  1114. Document.new(src)
  1115. }
  1116. begin
  1117. Document.new(src)
  1118. rescue
  1119. assert_match( /missing attribute quote/, $!.message )
  1120. end
  1121. end
  1122. def test_ticket_63
  1123. Document.new(File.new(fixture_path("t63-1.xml")))
  1124. end
  1125. def test_ticket_75
  1126. d = REXML::Document.new(File.new(fixture_path("t75.xml")))
  1127. assert_equal("tree", d.root.name)
  1128. end
  1129. def test_ticket_48_part_II
  1130. f = REXML::Formatters::Pretty.new
  1131. #- rexml sanity check (bugs in ruby 1.8.4, ruby 1.8.6)
  1132. xmldoc = Document.new("<test/>")
  1133. xmldoc << XMLDecl.new(XMLDecl::DEFAULT_VERSION, "UTF-8")
  1134. content = ['61c3a927223c3e26'].pack("H*")
  1135. content.force_encoding('UTF-8') if content.respond_to?(:force_encoding)
  1136. #- is some UTF-8 text but just to make sure my editor won't magically convert..
  1137. xmldoc.root.add_attribute('attr', content)
  1138. f.write(xmldoc,out=[])
  1139. xmldoc = REXML::Document.new(out.join)
  1140. sanity1 = xmldoc.root.attributes['attr']
  1141. f.write(xmldoc,out=[])
  1142. xmldoc = REXML::Document.new(out.join)
  1143. sanity2 = xmldoc.root.attributes['attr']
  1144. f.write(xmldoc,out=[])
  1145. assert_equal( sanity1, sanity2 )
  1146. end
  1147. def test_ticket_88
  1148. doc = REXML::Document.new("<?xml version=\"1.0\" encoding=\"shift_jis\"?>")
  1149. assert_equal("<?xml version='1.0' encoding='SHIFT_JIS'?>", doc.to_s)
  1150. doc = REXML::Document.new("<?xml version = \"1.0\" encoding = \"shift_jis\"?>")
  1151. assert_equal("<?xml version='1.0' encoding='SHIFT_JIS'?>", doc.to_s)
  1152. end
  1153. def test_ticket_85
  1154. xml = <<ENDXML
  1155. <foo>
  1156. <bar>
  1157. <bob name='jimmy'/>
  1158. </bar>
  1159. </foo>
  1160. ENDXML
  1161. yml = "<foo>
  1162. <bar>
  1163. <bob name='jimmy'/>
  1164. </bar>
  1165. </foo>"
  1166. # The pretty printer ignores all whitespace, anyway so output1 == output2
  1167. f = REXML::Formatters::Pretty.new( 2 )
  1168. d = Document.new( xml, :ignore_whitespace_nodes=>:all )
  1169. f.write( d, output1="" )
  1170. d = Document.new( xml )
  1171. f.write( d, output2="" )
  1172. # Output directives should override whitespace directives.
  1173. assert_equal( output1, output2 )
  1174. # The base case.
  1175. d = Document.new(yml)
  1176. f.write( d, output3="" )
  1177. assert_equal( output3.strip, output2.strip )
  1178. d = Document.new(yml)
  1179. f.write( d, output4="" )
  1180. assert_equal( output3.strip, output4.strip )
  1181. end
  1182. def test_ticket_91
  1183. source="<root>
  1184. <bah something='1' somethingelse='bah'>
  1185. <something>great</something>
  1186. </bah>
  1187. </root>"
  1188. expected="<root>
  1189. <bah something='1' somethingelse='bah'>
  1190. <something>great</something>
  1191. </bah>
  1192. <bah/>
  1193. </root>"
  1194. d = Document.new( source )
  1195. d.root.add_element( "bah" )
  1196. p=REXML::Formatters::Pretty.new(2)
  1197. p.compact = true # Don't add whitespace to text nodes unless necessary
  1198. p.write(d,out="")
  1199. assert_equal( expected, out )
  1200. end
  1201. def test_ticket_95
  1202. testd = REXML::Document.new "<a><b><c/><c/><c/></b></a>"
  1203. testd.write(out1="")
  1204. testd.elements["//c[2]"].xpath
  1205. testd.write(out2="")
  1206. assert_equal(out1,out2)
  1207. end
  1208. def test_ticket_102
  1209. doc = REXML::Document.new '<doc xmlns="ns"><item name="foo"/></doc>'
  1210. assert_equal( "foo", doc.root.elements["item"].attribute("name","ns").to_s )
  1211. assert_equal( "item", doc.root.elements["item[@name='foo']"].name )
  1212. end
  1213. def test_ticket_14
  1214. # Per .2.5 Node Tests of XPath spec
  1215. assert_raise( REXML::UndefinedNamespaceException,
  1216. %Q{Should have gotten an Undefined Namespace error} ) {
  1217. Document.new("<a><n:b/></a>")
  1218. }
  1219. end
  1220. # 5.7 Text Nodes
  1221. # Character data is grouped into text nodes. As much character data as
  1222. # possible is grouped into each text node: a text node never has an
  1223. # immediately following or preceding sibling that is a text node. The
  1224. # string-value of a text node is the character data. A text node always has
  1225. # at least one character of data.
  1226. def test_ticket_105
  1227. d = Document.new("<a/>")
  1228. d.root.add_text( "a" )
  1229. d.root.add_text( "b" )
  1230. assert_equal( 1, d.root.children.size )
  1231. end
  1232. # phantom namespace same as default namespace
  1233. def test_ticket_121
  1234. doc = REXML::Document.new(
  1235. '<doc xmlns="ns" xmlns:phantom="ns"><item name="foo">text</item></doc>'
  1236. )
  1237. assert_equal 'text', doc.text( "/doc/item[@name='foo']" )
  1238. assert_equal "name='foo'",
  1239. doc.root.elements["item"].attribute("name", "ns").inspect
  1240. assert_equal "<item name='foo'>text</item>",
  1241. doc.root.elements["item[@name='foo']"].to_s
  1242. end
  1243. def test_ticket_135
  1244. bean_element = REXML::Element.new("bean")
  1245. textToAdd = "(&#38;(|(memberof=CN=somegroupabcdefgh,OU=OUsucks,DC=hookemhorns,DC=com)(mail=*someco.com))(acct=%u)(!(extraparameter:2.2.222.222222.2.2.222:=2)))"
  1246. bean_element.add_element("prop", {"key"=> "filter"}).add_text(textToAdd)
  1247. doc = REXML::Document.new
  1248. doc.add_element(bean_element)
  1249. REXML::Formatters::Pretty.new(3).write( doc, out = "" )
  1250. assert_equal "<bean>\n <prop key='filter'>\n (&amp;#38;(|(memberof=CN=somegroupabcdefgh,OU=OUsucks,DC=hookemhorns,DC=com)(mail=*someco.com))(acct=%u)(!(extraparameter:2.2.222.222222.2.2.222:=2)))\n </prop>\n</bean>", out
  1251. end
  1252. def test_ticket_138
  1253. doc = REXML::Document.new(
  1254. '<svg xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape" ' +
  1255. 'inkscape:version="0.44" version="1.0"/>'
  1256. )
  1257. expected = {
  1258. "inkscape" => attribute("xmlns:inkscape",
  1259. "http://www.inkscape.org/namespaces/inkscape"),
  1260. "version" => {
  1261. "inkscape" => attribute("inkscape:version", "0.44"),
  1262. "" => attribute("version", "1.0"),
  1263. },
  1264. }
  1265. assert_equal(expected, doc.root.attributes)
  1266. assert_equal(expected, REXML::Document.new(doc.root.to_s).root.attributes)
  1267. end
  1268. def test_empty_doc
  1269. assert(REXML::Document.new('').children.empty?)
  1270. end
  1271. private
  1272. def attribute(name, value)
  1273. REXML::Attribute.new(name, value)
  1274. end
  1275. end