PageRenderTime 29ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 1ms

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

https://gitlab.com/essere.lab.public/qualitas.class-corpus
Ruby | 581 lines | 476 code | 56 blank | 49 comment | 4 complexity | 73431079cd7ccd0eb689cb6b4da04b14 MD5 | raw file
  1. # coding: binary
  2. require "rexml_test_utils"
  3. require "rexml/document"
  4. require "rexml/parseexception"
  5. require "rexml/formatters/default"
  6. class ContribTester < Test::Unit::TestCase
  7. include REXMLTestUtils
  8. include REXML
  9. XML_STRING_01 = <<DELIMITER
  10. <?xml version="1.0" encoding="UTF-8"?>
  11. <biblio>
  12. <entry type="Book">
  13. <author>Thomas, David; Hunt, Andrew</author>
  14. <language>english</language>
  15. <publisher>Addison-Wesley</publisher>
  16. <title>Programming Ruby. The Pragmatic Programmer's Guide</title>
  17. <year>2000</year>
  18. </entry>
  19. <entry type="Book">
  20. <author>Blammo, Blah</author>
  21. <language>english</language>
  22. <publisher>Hubbabubba</publisher>
  23. <title>Foozboozer's Life</title>
  24. <type>Book</type>
  25. <year>2002</year>
  26. </entry>
  27. </biblio>
  28. DELIMITER
  29. XML_STRING_02 = <<DELIMITER
  30. <biblio>
  31. <entry type="Book">
  32. <language>english</language>
  33. <publisher>Addison-Wesley</publisher>
  34. <title>Programming Ruby. The Pragmatic Programmer's Guide</title>
  35. <type>Book</type>
  36. <year>2000</year>
  37. </entry>
  38. <entry type="Book">
  39. <author>Blammo, Blah</author>
  40. <language>english</language>
  41. <publisher>Hubbabubba</publisher>
  42. <title>Foozboozer's Life</title>
  43. <type>Book</type>
  44. <year>2002</year>
  45. </entry>
  46. </biblio>
  47. DELIMITER
  48. # Tobias Reif <tobiasreif@pinkjuice.com>
  49. def test_bad_doctype_Tobias
  50. source = <<-EOF
  51. <!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.0//EN"
  52. "http://www.w3.org/TR/SVG/DTD/svg10.dtd"
  53. [
  54. <!-- <!ENTITY % fast-slow "0 0 .5 1">-->
  55. <!--<!ENTITY % slow-fast ".5 0 1 1">-->
  56. <!ENTITY hover_ani
  57. '<animateTransform attributeName="transform"
  58. type="scale" restart="whenNotActive" values="1;0.96"
  59. dur="0.5s" calcMode="spline" keySplines="0 0 .5 1"
  60. fill="freeze" begin="mouseover"/>
  61. <animateTransform attributeName="transform"
  62. type="scale" restart="whenNotActive" values="0.96;1"
  63. dur="0.5s" calcMode="spline" keySplines=".5 0 1 1"
  64. fill="freeze" begin="mouseover+0.5s"/>'
  65. >
  66. ]
  67. >
  68. EOF
  69. doc = REXML::Document.new source
  70. doc.write(out="")
  71. assert(out[/>\'>/] != nil, "Couldn't find >'>")
  72. assert(out[/\]>/] != nil, "Couldn't find ]>")
  73. end
  74. # Peter Verhage
  75. def test_namespace_Peter
  76. source = <<-EOF
  77. <?xml version="1.0"?>
  78. <config:myprog-config xmlns:config="http://someurl/program/version">
  79. <!-- main options -->
  80. <config:main>
  81. <config:parameter name="name" value="value"/>
  82. </config:main>
  83. </config:myprog-config>
  84. EOF
  85. doc = REXML::Document.new source
  86. assert_equal "myprog-config", doc.root.name
  87. count = 0
  88. REXML::XPath.each(doc, "x:myprog-config/x:main/x:parameter",
  89. {"x"=>"http://someurl/program/version"}) { |element|
  90. assert_equal "name", element.attributes["name"]
  91. count += 1;
  92. }
  93. assert_equal 1, count
  94. assert_equal "myprog-config", doc.elements["config:myprog-config"].name
  95. end
  96. # Tobias Reif <tobiasreif@pinkjuice.com>
  97. def test_complex_xpath_Tobias
  98. source = <<-EOF
  99. <root>
  100. <foo>
  101. <bar style="baz"/>
  102. <blah style="baz"/>
  103. <blam style="baz"/>
  104. </foo>
  105. <wax>
  106. <fudge>
  107. <noodle/>
  108. </fudge>
  109. </wax>
  110. </root>
  111. EOF
  112. # elements that have child elements
  113. # but not grandchildren
  114. # and not children that don't have a style attribute
  115. # and not children that have a unique style attribute
  116. complex_path = "*[* "+
  117. "and not(*/node()) "+
  118. "and not(*[not(@style)]) "+
  119. "and not(*/@style != */@style)]"
  120. doc = REXML::Document.new source
  121. results = REXML::XPath.match( doc.root, complex_path )
  122. assert(results)
  123. assert_equal 1, results.size
  124. assert_equal "foo", results[0].name
  125. end
  126. # "Chris Morris" <chrismo@charter.net>
  127. def test_extra_newline_on_read_Chris
  128. text = 'test text'
  129. e = REXML::Element.new('Test')
  130. e.add_text(text)
  131. REXML::Formatters::Default.new.write(e,out="")
  132. doc = REXML::Document.new(out)
  133. outtext = doc.root.text
  134. assert_equal(text, outtext)
  135. end
  136. # Tobias Reif <tobiasreif@pinkjuice.com>
  137. def test_other_xpath_Tobias
  138. schema = <<-DELIM
  139. <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
  140. elementFormDefault="qualified">
  141. <xs:element name="rect">
  142. <xs:complexType>
  143. <xs:attribute name="width" type="xs:byte" use="required"/>
  144. <xs:attribute name="height" type="xs:byte" use="required"/>
  145. </xs:complexType>
  146. </xs:element>
  147. <xs:element name="svg">
  148. <xs:complexType>
  149. <xs:sequence>
  150. <xs:element ref="rect"/>
  151. </xs:sequence>
  152. </xs:complexType>
  153. </xs:element>
  154. </xs:schema>
  155. DELIM
  156. doc = REXML::Document.new schema
  157. result = REXML::XPath.first(doc.root, 'xs:element[descendant::xs:element[@ref]]')
  158. assert result
  159. assert_equal "svg", result.attributes['name']
  160. result = REXML::XPath.first(doc, 'element[descendant::element[@ref]]')
  161. assert_nil result
  162. end
  163. #this first test succeeds, to check if stuff is set up correctly
  164. def test_xpath_01_TobiasReif
  165. doc = Document.new XML_STRING_01.dup
  166. desired_result = Document.new '<author>Thomas, David; Hunt, Andrew</author>'
  167. xpath = '//author'
  168. result = XPath.first(doc, xpath)
  169. assert_equal desired_result.to_s, result.to_s
  170. end
  171. def test_xpath_whitespace_TobiasReif
  172. # same as above, with whitespace in XPath
  173. doc = Document.new(XML_STRING_01.dup)
  174. desired_result = Document.new('<author>Thomas, David; Hunt, Andrew</author>')
  175. xpath = "\/\/author\n \n"
  176. result = XPath.first(doc, xpath)
  177. failure_message = "\n[[[TR: AFAIK, whitespace should be allowed]]]\n"
  178. assert_equal(desired_result.to_s, result.to_s, failure_message)
  179. end
  180. def test_xpath_02_TobiasReif
  181. doc = Document.new XML_STRING_01.dup
  182. desired_result = Document.new '<author>Thomas, David; Hunt, Andrew</author>'
  183. # Could that quirky
  184. # Programmer',&quot;'&quot;,'s
  185. # be handled automatically, somehow?
  186. # Or is there a simpler way? (the below XPath should match the author element above,
  187. # AFAIK; I tested it inside an XSLT)
  188. xpath = %q{/biblio/entry[
  189. title/text()=concat('Programming Ruby. The Pragmatic Programmer',"'",'s Guide')
  190. and
  191. year='2000'
  192. ]/author}
  193. result = XPath.first(doc, xpath)
  194. failure_message = "\nHow to handle the apos inside the string inside the XPath?\nXPath = #{xpath}\n"
  195. assert_equal desired_result.to_s, result.to_s, failure_message
  196. end
  197. def test_xpath_03_TobiasReif
  198. doc = Document.new XML_STRING_02.dup
  199. desired_result_string = "<entry type='Book'>
  200. <language>english</language>
  201. <publisher>Addison-Wesley</publisher>
  202. <title>Programming Ruby. The Pragmatic Programmer's Guide</title>
  203. <type>Book</type>
  204. <year>2000</year>
  205. </entry>"
  206. desired_result_tree = Document.new desired_result_string
  207. xpath = "/biblio/entry[not(author)]"
  208. result = XPath.first(doc, xpath)
  209. assert_equal desired_result_string, result.to_s
  210. end
  211. def test_umlaut
  212. koln_iso = "K\xf6ln"
  213. koln_utf = "K\xc3\xb6ln"
  214. source_iso = "<?xml version='1.0' encoding='ISO-8859-1'?><test>#{koln_iso}</test>"
  215. source_utf = "<?xml version='1.0' encoding='UTF-8'?><test>#{koln_utf}</test>"
  216. if String.method_defined? :encode
  217. koln_iso.force_encoding('iso-8859-1')
  218. koln_utf.force_encoding('utf-8')
  219. source_iso.force_encoding('iso-8859-1')
  220. source_utf.force_encoding('utf-8')
  221. end
  222. doc = REXML::Document.new(source_iso)
  223. assert_equal('ISO-8859-1', doc.xml_decl.encoding)
  224. assert_equal(koln_utf, doc.root.text)
  225. doc.write(out="")
  226. assert_equal(source_iso, out )
  227. doc.xml_decl.encoding = 'UTF-8'
  228. doc.write(out="")
  229. assert_equal(source_utf, out)
  230. doc = Document.new <<-EOF
  231. <?xml version="1.0" encoding="ISO-8859-1"?>
  232. <intranet>
  233. <position><aktuell datum="01-10-11">Technik</aktuell></position>
  234. <hauptspalte>
  235. <headline>Technik</headline>
  236. Die Technik ist das R\xFCckgrat der meisten Gesch\xFCftsprozesse bei Home of the Brave. Deshalb sollen hier alle relevanten technischen Abl\xFCufe, Daten und Einrichtungen beschrieben werden, damit jeder im Bedarfsfall die n\xFCtigen Informationen, Anweisungen und Verhaltensempfehlungen nachlesen und/oder abrufen kann.
  237. </hauptspalte>
  238. <nebenspalte>
  239. <link ziel="Flash/">Flash</link><umbruch/>
  240. N\xFCtzliches von Flashern f\xFCr Flasher.<umbruch/>
  241. <link neu="ja" ziel="Cvs/">CVS-FAQ</link><umbruch/>
  242. FAQ zur Benutzung von CVS bei HOB
  243. </nebenspalte>
  244. </intranet>
  245. EOF
  246. tn = XPath.first(doc, "//nebenspalte/text()[2]")
  247. expected_iso = "N\xFCtzliches von Flashern f\xFCr Flasher."
  248. expected_utf = expected_iso.unpack('C*').pack('U*')
  249. expected_iso.force_encoding(::Encoding::ISO_8859_1)
  250. expected_utf.force_encoding(::Encoding::UTF_8)
  251. assert_equal(expected_utf, tn.to_s.strip)
  252. f = REXML::Formatters::Default.new
  253. f.write( tn, Output.new(o = "", "ISO-8859-1") )
  254. assert_equal(expected_iso, o.strip)
  255. doc = Document.new File.new(fixture_path('xmlfile-bug.xml'))
  256. tn = XPath.first(doc, "//nebenspalte/text()[2]")
  257. assert_equal(expected_utf, tn.to_s.strip)
  258. f.write( tn, Output.new(o = "", "ISO-8859-1") )
  259. assert_equal(expected_iso, o.strip)
  260. end
  261. def test_element_cloning_namespace_Chris
  262. aDoc = REXML::Document.new '<h1 tpl:content="title" xmlns:tpl="1">Dummy title</h1>'
  263. anElement = anElement = aDoc.elements[1]
  264. elementAttrPrefix = anElement.attributes.get_attribute('content').prefix
  265. aClone = anElement.clone
  266. cloneAttrPrefix = aClone.attributes.get_attribute('content').prefix
  267. assert_equal( elementAttrPrefix , cloneAttrPrefix )
  268. end
  269. def test_namespaces_in_attlist_tobias
  270. in_string = File.open(fixture_path('foo.xml'), 'r') do |file|
  271. file.read
  272. end
  273. doc = Document.new in_string
  274. assert_nil XPath.first(doc,'//leg')
  275. assert_equal 'http://www.foo.com/human', doc.root.elements[1].namespace
  276. assert_equal 'human leg',
  277. XPath.first(doc, '//x:leg/text()', {'x'=>'http://www.foo.com/human'}).to_s
  278. end
  279. # Alun ap Rhisiart
  280. def test_less_than_in_element_content
  281. source = File.new(fixture_path('ProductionSupport.xml'))
  282. h = Hash.new
  283. doc = REXML::Document.new source
  284. doc.elements.each("//CommonError") { |el|
  285. h[el.elements['Key'].text] = 'okay'
  286. }
  287. assert(h.include?('MotorInsuranceContract(Object)>>#error:'))
  288. end
  289. # XPaths provided by Thomas Sawyer
  290. def test_various_xpath
  291. #@doc = REXML::Document.new('<r a="1"><p><c b="2"/></p></r>')
  292. doc = REXML::Document.new('<r a="1"><p><c b="2">3</c></p></r>')
  293. [['/r', REXML::Element],
  294. ['/r/p/c', REXML::Element],
  295. ['/r/attribute::a', Attribute],
  296. ['/r/@a', Attribute],
  297. ['/r/attribute::*', Attribute],
  298. ['/r/@*', Attribute],
  299. ['/r/p/c/attribute::b', Attribute],
  300. ['/r/p/c/@b', Attribute],
  301. ['/r/p/c/attribute::*', Attribute],
  302. ['/r/p/c/@*', Attribute],
  303. ['//c/attribute::b', Attribute],
  304. ['//c/@b', Attribute],
  305. ['//c/attribute::*', Attribute],
  306. ['//c/@*', Attribute],
  307. ['.//node()', REXML::Node ],
  308. ['.//node()[@a]', REXML::Element ],
  309. ['.//node()[@a="1"]', REXML::Element ],
  310. ['.//node()[@b]', REXML::Element ], # no show, why?
  311. ['.//node()[@b="2"]', REXML::Element ]
  312. ].each do |xpath,kind|
  313. begin
  314. REXML::XPath.each( doc, xpath ) do |what|
  315. assert_kind_of( kind, what, "\n\nWrong type (#{what.class}) returned for #{xpath} (expected #{kind.name})\n\n" )
  316. end
  317. rescue Exception
  318. puts "PATH WAS: #{xpath}"
  319. raise
  320. end
  321. end
  322. [
  323. ['/r', 'attribute::a', Attribute ],
  324. ['/r', '@a', Attribute ],
  325. ['/r', 'attribute::*', Attribute ],
  326. ['/r', '@*', Attribute ],
  327. ['/r/p/c', 'attribute::b', Attribute ],
  328. ['/r/p/c', '@b', Attribute ],
  329. ['/r/p/c', 'attribute::*', Attribute ],
  330. ['/r/p/c', '@*', Attribute ]
  331. ].each do |nodepath, xpath, kind|
  332. begin
  333. context = REXML::XPath.first(doc, nodepath)
  334. REXML::XPath.each( context, xpath ) do |what|
  335. assert_kind_of kind, what, "Wrong type (#{what.class}) returned for #{xpath} (expected #{kind.name})\n"
  336. end
  337. rescue Exception
  338. puts "PATH WAS: #{xpath}"
  339. raise
  340. end
  341. end
  342. end
  343. def test_entities_Holden_Glova
  344. document = <<-EOL
  345. <?xml version="1.0" encoding="UTF-8"?>
  346. <!DOCTYPE rubynet [
  347. <!ENTITY rbconfig.MAJOR "1">
  348. <!ENTITY rbconfig.MINOR "7">
  349. <!ENTITY rbconfig.TEENY "2">
  350. <!ENTITY rbconfig.ruby_version "&rbconfig.MAJOR;.&rbconfig.MINOR;">
  351. <!ENTITY rbconfig.arch "i386-freebsd5">
  352. <!ENTITY rbconfig.prefix "/usr/local">
  353. <!ENTITY rbconfig.libdir "&rbconfig.prefix;/lib">
  354. <!ENTITY rbconfig.includedir "&rbconfig.prefix;/include">
  355. <!ENTITY rbconfig.sitedir "&rbconfig.prefix;/lib/ruby/site_ruby">
  356. <!ENTITY rbconfig.sitelibdir "&rbconfig.sitedir;/&rbconfig.ruby_version;">
  357. <!ENTITY rbconfig.sitearchdir "&rbconfig.sitelibdir;/&rbconfig.arch;">
  358. ]>
  359. <rubynet>
  360. <pkg version="version1.0">
  361. <files>
  362. <file>
  363. <filename>uga.rb</filename>
  364. <mode>0444</mode>
  365. <path>&rbconfig.libdir;/rexml</path>
  366. <content encoding="xml">... the file here</content>
  367. </file>
  368. <file>
  369. <filename>booga.h</filename>
  370. <mode>0444</mode>
  371. <path>&rbconfig.includedir;</path>
  372. <content encoding="xml">... the file here</content>
  373. </file>
  374. <file>
  375. <filename>foo.so</filename>
  376. <mode>0555</mode>
  377. <path>&rbconfig.sitearchdir;/rexml</path>
  378. <content encoding="mime64">Li4uIHRoZSBmaWxlIGhlcmU=\n</content>
  379. </file>
  380. </files>
  381. </pkg>
  382. </rubynet>
  383. EOL
  384. file_xpath = '/rubynet/pkg/files/file'
  385. root = REXML::Document.new(document)
  386. root.elements.each(file_xpath) do |metadata|
  387. text = metadata.elements['path'].get_text.value
  388. assert text !~ /&rbconfig/, "'#{text}' failed"
  389. end
  390. #Error occurred in test_package_file_opens(TC_PackageInstall):
  391. # ArgumentError:
  392. #illegal access mode &rbconfig.prefix;/lib/rexml
  393. #
  394. #[synack@Evergreen] src $ ruby --version
  395. #ruby 1.6.7 (2002-03-01) [i686-linux-gnu]
  396. #
  397. #It looks like it expanded the first entity, but didn't reparse it for more
  398. #entities. possible bug - or have I mucked this up?
  399. end
  400. def test_whitespace_after_xml_decl
  401. d = Document.new <<EOL
  402. <?xml version='1.0'?>
  403. <blo>
  404. <wak>
  405. </wak>
  406. </blo>
  407. EOL
  408. end
  409. def test_external_entity
  410. xp = '//channel/title'
  411. %w{working.rss broken.rss}.each do |path|
  412. File.open(File.join(fixture_path(path))) do |file|
  413. doc = REXML::Document.new file.readlines.join('')
  414. # check to make sure everything is kosher
  415. assert_equal( doc.root.class, REXML::Element )
  416. assert_equal( doc.root.elements.class, REXML::Elements )
  417. # get the title of the feed
  418. assert( doc.root.elements[xp].kind_of?( REXML::Element ) )
  419. end
  420. end
  421. end
  422. def test_maintain_dtd
  423. src = %q{<?xml version="1.0" encoding="UTF-8"?>
  424. <!DOCTYPE ivattacks SYSTEM "../../ivacm.dtd" [
  425. <!ENTITY % extern-packages SYSTEM "../../ivpackages.dtd">
  426. <!ENTITY % extern-packages SYSTEM "../../common-declarations.dtd">
  427. %extern-packages;
  428. %extern-common;
  429. ]>}
  430. doc = Document.new( src )
  431. doc.write( out="" )
  432. src = src.tr('"', "'")
  433. out = out.tr('"', "'")
  434. assert_equal( src, out )
  435. end
  436. def test_text_nodes_nomatch
  437. source = "<root><child>test</child></root>"
  438. d = REXML::Document.new( source )
  439. r = REXML::XPath.match( d, %q{/root/child[text()="no-test"]} )
  440. assert_equal( 0, r.size )
  441. end
  442. def test_raw_Terje_Elde
  443. f = REXML::Formatters::Default.new
  444. txt = 'abc&#248;def'
  445. a = Text.new( txt,false,nil,true )
  446. f.write(a,out="")
  447. assert_equal( txt, out )
  448. txt = '<sean><russell>abc&#248;def</russell></sean>'
  449. a = Document.new( txt, { :raw => ["russell"] } )
  450. f.write(a,out="")
  451. assert_equal( txt, out )
  452. end
  453. def test_indenting_error
  454. a=Element.new("test1")
  455. b=Element.new("test2")
  456. c=Element.new("test3")
  457. b << c
  458. a << b
  459. REXML::Formatters::Pretty.new.write(a,s="")
  460. end
  461. def test_pos
  462. require 'tempfile'
  463. testfile = Tempfile.new("tidal")
  464. testdata = %Q{<calibration>
  465. <section name="parameters">
  466. <param name="barpress">760</param>
  467. <param name="hertz">50</param>
  468. </section>
  469. </calibration>
  470. }
  471. testfile.puts testdata
  472. testfile.rewind
  473. assert_nothing_raised do
  474. d = REXML::Document.new(testfile)
  475. end
  476. testfile.close(true)
  477. end
  478. def test_deep_clone
  479. a = Document.new( '<?xml version="1.0"?><!DOCTYPE html PUBLIC
  480. "-//W3C//DTD
  481. XHTML 1.0 Transitional//EN"
  482. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html
  483. xmlns="http:///www.w3.org/1999/xhtml"></html>' )
  484. b = a.deep_clone
  485. assert_equal a.to_s, b.to_s
  486. end
  487. def test_double_escaping
  488. data = '<title>AT&amp;T</title>'
  489. xml = "<description><![CDATA[#{data}]]></description>"
  490. doc = REXML::Document.new(xml)
  491. description = doc.find {|e| e.name=="description"}
  492. assert_equal data, description.text
  493. end
  494. def test_ticket_12
  495. cfg = "<element><anotherelement><child1>a</child1><child2>b</child2></anotherelement></element>"
  496. config = REXML::Document.new( cfg )
  497. assert_equal( "a", config.elements[ "//child1" ].text )
  498. end
  499. =begin
  500. # This is a silly test, and is low priority
  501. def test_namespace_serialization_tobi_reif
  502. doc = Document.new '<doc xmlns:b="http://www.foo.foo">
  503. <b:p/>
  504. </doc>'
  505. ns = 'http://www.foo.foo'
  506. ns_declaration={'f'=>ns}
  507. returned = XPath.match(doc,'//f:p',ns_declaration)
  508. # passes:
  509. assert( (returned[0].namespace==ns), 'namespace should be '+ns)
  510. serialized = returned.to_s
  511. serialized_and_parsed = Document.new(serialized)
  512. puts 'serialized: '+serialized
  513. # ... currently brings <b:p/>
  514. # prefix b is undeclared (!)
  515. assert( (serialized_and_parsed.namespace==ns),
  516. 'namespace should still be '+ns.inspect+
  517. ' and not '+serialized_and_parsed.namespace.inspect)
  518. # ... currently results in a failure:
  519. # 'namespace should still be "http://www.foo.foo" and not ""'
  520. end
  521. =end
  522. end