1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

* test/rexml: Avoid fd leaks.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@46178 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
akr 2014-05-27 13:45:04 +00:00
parent 3c908895ab
commit 3356c312cd
11 changed files with 142 additions and 113 deletions

View file

@ -1,3 +1,7 @@
Tue May 27 22:44:20 2014 Tanaka Akira <akr@fsij.org>
* test/rexml: Avoid fd leaks.
Tue May 27 22:24:25 2014 Kouhei Sutou <kou@cozmixng.org> Tue May 27 22:24:25 2014 Kouhei Sutou <kou@cozmixng.org>
* test/rexml/test_document.rb: Indent. * test/rexml/test_document.rb: Indent.

View file

@ -276,7 +276,7 @@ EOF
f.write( tn, Output.new(o = "", "ISO-8859-1") ) f.write( tn, Output.new(o = "", "ISO-8859-1") )
assert_equal(expected_iso, o.strip) assert_equal(expected_iso, o.strip)
doc = Document.new File.new(fixture_path('xmlfile-bug.xml')) doc = File.open(fixture_path('xmlfile-bug.xml')) {|file| Document.new file }
tn = XPath.first(doc, "//nebenspalte/text()[2]") tn = XPath.first(doc, "//nebenspalte/text()[2]")
assert_equal(expected_utf, tn.to_s.strip) assert_equal(expected_utf, tn.to_s.strip)
f.write( tn, Output.new(o = "", "ISO-8859-1") ) f.write( tn, Output.new(o = "", "ISO-8859-1") )
@ -310,9 +310,10 @@ EOF
# Alun ap Rhisiart # Alun ap Rhisiart
def test_less_than_in_element_content def test_less_than_in_element_content
source = File.new(fixture_path('ProductionSupport.xml')) doc = File.open(fixture_path('ProductionSupport.xml')) do |source|
REXML::Document.new source
end
h = Hash.new h = Hash.new
doc = REXML::Document.new source
doc.elements.each("//CommonError") { |el| doc.elements.each("//CommonError") { |el|
h[el.elements['Key'].text] = 'okay' h[el.elements['Key'].text] = 'okay'
} }

View file

@ -227,8 +227,9 @@ module REXMLTests
assert_equal(correct, test) assert_equal(correct, test)
# OK, the BIG doctype test, numba wun # OK, the BIG doctype test, numba wun
docin = File.new(fixture_path("doctype_test.xml")) doc = File.open(fixture_path("doctype_test.xml")) do |docin|
doc = Document.new(docin) Document.new(docin)
end
doc.write(test="") doc.write(test="")
assert_equal(31, doc.doctype.size) assert_equal(31, doc.doctype.size)
end end
@ -248,8 +249,7 @@ module REXMLTests
assert_instance_of DocType, doc.doctype assert_instance_of DocType, doc.doctype
assert_equal doc.version, "1.0" assert_equal doc.version, "1.0"
source = File.new(fixture_path("dash.xml")) doc = File.open(fixture_path("dash.xml")) {|source| Document.new source }
doc = Document.new source
assert_equal "content-2", doc.elements["//content-2"].name assert_equal "content-2", doc.elements["//content-2"].name
end end
@ -339,7 +339,7 @@ module REXMLTests
assert_equal doc.root.name.to_s, "xsa" assert_equal doc.root.name.to_s, "xsa"
# Testing IO source # Testing IO source
doc = Document.new File.new(fixture_path("project.xml")) doc = File.open(fixture_path("project.xml")) {|f| Document.new f }
assert_equal doc.root.name.to_s, "Project" assert_equal doc.root.name.to_s, "Project"
end end
@ -436,7 +436,7 @@ module REXMLTests
# enormous. # enormous.
def test_element_access def test_element_access
# Testing each_element # Testing each_element
doc = Document.new File.new(fixture_path("project.xml")) doc = File.open(fixture_path("project.xml")) {|f| Document.new f }
each_test( doc, "/", 1 ) { |child| each_test( doc, "/", 1 ) { |child|
assert_equal doc.name, child.name assert_equal doc.name, child.name
@ -601,22 +601,23 @@ module REXMLTests
def test_big_documentation def test_big_documentation
f = File.new(fixture_path("documentation.xml")) d = File.open(fixture_path("documentation.xml")) {|f| Document.new f }
d = Document.new f
assert_equal "Sean Russell", d.elements["documentation/head/author"].text.tr("\n\t", " ").squeeze(" ") assert_equal "Sean Russell", d.elements["documentation/head/author"].text.tr("\n\t", " ").squeeze(" ")
out = "" out = ""
d.write out d.write out
end end
def test_tutorial def test_tutorial
doc = Document.new File.new(fixture_path("tutorial.xml")) doc = File.open(fixture_path("tutorial.xml")) {|f| Document.new f }
out = "" out = ""
doc.write out doc.write out
end end
def test_stream def test_stream
c = Listener.new c = Listener.new
Document.parse_stream( File.new(fixture_path("documentation.xml")), c ) File.open(fixture_path("documentation.xml")) do |f|
Document.parse_stream( f, c )
end
assert(c.ts, "Stream parsing apparantly didn't parse the whole file") assert(c.ts, "Stream parsing apparantly didn't parse the whole file")
assert(c.te, "Stream parsing dropped end tag for documentation") assert(c.te, "Stream parsing dropped end tag for documentation")
@ -627,12 +628,15 @@ module REXMLTests
end end
def test_line def test_line
Document.new File.new(fixture_path("bad.xml")) f = File.new(fixture_path("bad.xml"))
Document.new f
assert_fail "There should have been an error" assert_fail "There should have been an error"
rescue Exception rescue Exception
# We should get here # We should get here
assert($!.line == 5, "Should have been an error on line 5, "+ assert($!.line == 5, "Should have been an error on line 5, "+
"but was reported as being on line #{$!.line}" ) "but was reported as being on line #{$!.line}" )
ensure
f.close if f
end end
def test_substitution def test_substitution
@ -836,8 +840,7 @@ EOL
end end
def test_attlist_write def test_attlist_write
file=File.new(fixture_path("foo.xml")) doc = File.open(fixture_path("foo.xml")) {|file| Document.new file }
doc=Document.new file
out = '' out = ''
doc.write(out) doc.write(out)
end end
@ -1281,11 +1284,11 @@ EOL
end end
def test_ticket_63 def test_ticket_63
Document.new(File.new(fixture_path("t63-1.xml"))) File.open(fixture_path("t63-1.xml")) {|f| Document.new(f) }
end end
def test_ticket_75 def test_ticket_75
d = REXML::Document.new(File.new(fixture_path("t75.xml"))) d = File.open(fixture_path("t75.xml")) {|f| REXML::Document.new(f) }
assert_equal("tree", d.root.name) assert_equal("tree", d.root.name)
end end

View file

@ -88,7 +88,9 @@ module REXMLTests
end end
def test_ticket_110 def test_ticket_110
utf16 = REXML::Document.new(File.new(fixture_path("ticket_110_utf16.xml"))) utf16 = File.open(fixture_path("ticket_110_utf16.xml")) do |f|
REXML::Document.new(f)
end
assert_equal(utf16.encoding, "UTF-16") assert_equal(utf16.encoding, "UTF-16")
assert( utf16[0].kind_of?(REXML::XMLDecl)) assert( utf16[0].kind_of?(REXML::XMLDecl))
end end

View file

@ -39,8 +39,9 @@ module REXMLTests
def test( fname ) def test( fname )
# Dir.entries( xml_dir ).each { |fname| # Dir.entries( xml_dir ).each { |fname|
# if fname =~ /\.xml$/ # if fname =~ /\.xml$/
file = File.new(fixture_path(fname+".xml")) doc = File.open(fixture_path(fname+".xml")) do |file|
doc = Document.new( file ) Document.new(file)
end
XPath.each( doc, "/tests/document" ) {|e| handleDocument(e)} XPath.each( doc, "/tests/document" ) {|e| handleDocument(e)}
# end # end
# } # }

View file

@ -6,9 +6,10 @@ module REXMLTests
include REXMLTestUtils include REXMLTestUtils
include REXML include REXML
def test_parsing def test_parsing
f = File.new(fixture_path("documentation.xml")) File.open(fixture_path("documentation.xml")) do |f|
parser = REXML::Parsers::LightParser.new( f ) parser = REXML::Parsers::LightParser.new( f )
parser.parse parser.parse
end
end end
end end
end end

View file

@ -93,10 +93,9 @@ module REXMLTests
a.value.force_encoding('binary') a.value.force_encoding('binary')
end end
assert_equal( "\xC3\xA9", a.value) assert_equal( "\xC3\xA9", a.value)
doc = REXML::Document.parse_stream( doc = File::open(fixture_path("stream_accents.xml")) do |f|
File::new(fixture_path("stream_accents.xml")), REXML::Document.parse_stream(f, AccentListener::new)
AccentListener::new end
)
end end
end end

View file

@ -47,7 +47,9 @@ END
end end
# Provided by Tom Talbott # Provided by Tom Talbott
def test_more_ordering def test_more_ordering
doc = REXML::Document.new(Zlib::GzipReader.open(fixture_path('LostineRiver.kml.gz'), encoding: 'utf-8')) doc = Zlib::GzipReader.open(fixture_path('LostineRiver.kml.gz'), encoding: 'utf-8') do |f|
REXML::Document.new(f)
end
actual = [ actual = [
"Head south from Phinney Ave N", "Head south from Phinney Ave N",
"Turn left at N 36th St", "Turn left at N 36th St",

View file

@ -5,7 +5,9 @@ module REXMLTests
class TestIssuezillaParsing < Test::Unit::TestCase class TestIssuezillaParsing < Test::Unit::TestCase
include REXMLTestUtils include REXMLTestUtils
def test_rexml def test_rexml
doc = REXML::Document.new(File.new(fixture_path("ofbiz-issues-full-177.xml"))) doc = File.open(fixture_path("ofbiz-issues-full-177.xml")) do |f|
REXML::Document.new(f)
end
ctr = 1 ctr = 1
doc.root.each_element('//issue') do |issue| doc.root.each_element('//issue') do |issue|
assert_equal( ctr, issue.elements['issue_id'].text.to_i ) assert_equal( ctr, issue.elements['issue_id'].text.to_i )

View file

@ -32,63 +32,62 @@ module REXMLTests
end end
def test_sax2 def test_sax2
f = File.new(fixture_path("documentation.xml")) File.open(fixture_path("documentation.xml")) do |f|
parser = Parsers::SAX2Parser.new( f ) parser = Parsers::SAX2Parser.new( f )
# Listen to all events on the following elements # Listen to all events on the following elements
count = 0 count = 0
blok = proc { |uri,localname,qname,attributes| blok = proc { |uri,localname,qname,attributes|
assert %w{ bugs todo }.include?(localname), assert %w{ bugs todo }.include?(localname),
"Mismatched name; we got '#{qname}'\nArgs were:\n\tURI: #{uri}\n\tLOCALNAME: #{localname}\n\tQNAME: #{qname}\n\tATTRIBUTES: #{attributes.inspect}\n\tSELF=#{blok}" "Mismatched name; we got '#{qname}'\nArgs were:\n\tURI: #{uri}\n\tLOCALNAME: #{localname}\n\tQNAME: #{qname}\n\tATTRIBUTES: #{attributes.inspect}\n\tSELF=#{blok}"
count += 1 count += 1
} }
start_document = 0 start_document = 0
end_document = 0 end_document = 0
parser.listen( :start_document ) { start_document += 1 } parser.listen( :start_document ) { start_document += 1 }
parser.listen( :end_document ) { end_document += 1 } parser.listen( :end_document ) { end_document += 1 }
parser.listen( :start_element, %w{ changelog bugs todo }, &blok ) parser.listen( :start_element, %w{ changelog bugs todo }, &blok )
# Listen to all events on the following elements. Synonymous with # Listen to all events on the following elements. Synonymous with
# listen( :start_element, %w{ ... } ) # listen( :start_element, %w{ ... } )
parser.listen( %w{ changelog bugs todo }, &blok ) parser.listen( %w{ changelog bugs todo }, &blok )
# Listen for all start element events # Listen for all start element events
parser.listen( :start_element ) { |uri,localname,qname,attributes| parser.listen( :start_element ) { |uri,localname,qname,attributes|
} }
listener = MySAX2Listener.new listener = MySAX2Listener.new
# Listen for all events # Listen for all events
parser.listen( listener ) parser.listen( listener )
# Listen for all events on the given elements. Does not include children # Listen for all events on the given elements. Does not include children
# events. Regular expressions work as well! # events. Regular expressions work as well!
parser.listen( %w{ /change/ bugs todo }, listener ) parser.listen( %w{ /change/ bugs todo }, listener )
# Test the deafening method # Test the deafening method
blok = proc { |uri,localname,qname,attributes| blok = proc { |uri,localname,qname,attributes|
assert_fail "This listener should have been deafened!" assert_fail "This listener should have been deafened!"
} }
parser.listen( %w{ changelog }, &blok ) parser.listen( %w{ changelog }, &blok )
parser.deafen( &blok ) parser.deafen( &blok )
tc = 0 tc = 0
parser.listen( :characters, %w{version} ) {|text| parser.listen( :characters, %w{version} ) {|text|
assert(text=~/@ANT_VERSION@/, "version was '#{text}'") assert(text=~/@ANT_VERSION@/, "version was '#{text}'")
tc += 1 tc += 1
} }
begin begin
parser.parse parser.parse
rescue => exception rescue => exception
if exception.kind_of? Test::Unit::AssertionFailedError if exception.kind_of? Test::Unit::AssertionFailedError
raise exception raise exception
end
puts $!
puts exception.backtrace
end end
puts $! assert_equal 2, count
puts exception.backtrace assert_equal 1, tc
assert_equal 1, start_document
assert_equal 1, end_document
end end
assert_equal 2, count
assert_equal 1, tc
assert_equal 1, start_document
assert_equal 1, end_document
end end
# used by test_simple_doctype_listener # used by test_simple_doctype_listener
# submitted by Jeff Barczewski # submitted by Jeff Barczewski
class SimpleDoctypeListener class SimpleDoctypeListener
@ -223,21 +222,26 @@ module REXMLTests
def test_socket def test_socket
require 'socket' require 'socket'
server = TCPServer.new('127.0.0.1', 0) TCPServer.open('127.0.0.1', 0) do |server|
socket = TCPSocket.new('127.0.0.1', server.addr[1]) TCPSocket.open('127.0.0.1', server.addr[1]) do |socket|
ok = false
ok = false session = server.accept
session = server.accept begin
session << '<foo>' session << '<foo>'
parser = REXML::Parsers::SAX2Parser.new(socket) parser = REXML::Parsers::SAX2Parser.new(socket)
Fiber.new do Fiber.new do
parser.listen(:start_element) do parser.listen(:start_element) do
ok = true ok = true
Fiber.yield Fiber.yield
end
parser.parse
end.resume
assert(ok)
ensure
session.close
end
end end
parser.parse end
end.resume
assert(ok)
end end
def test_char_ref_sax2() def test_char_ref_sax2()
@ -261,15 +265,17 @@ module REXMLTests
include REXML::SAX2Listener include REXML::SAX2Listener
end end
def test_ticket_68 def test_ticket_68
parser = REXML::Parsers::SAX2Parser.new(File.new(fixture_path('ticket_68.xml'))) File.open(fixture_path('ticket_68.xml')) do |f|
parser.listen( Ticket68.new ) parser = REXML::Parsers::SAX2Parser.new(f)
begin parser.listen( Ticket68.new )
parser.parse begin
rescue parser.parse
p parser.source.position rescue
p parser.source.current_line p parser.source.position
puts $!.backtrace.join("\n") p parser.source.current_line
flunk $!.message puts $!.backtrace.join("\n")
flunk $!.message
end
end end
end end
end end

View file

@ -130,7 +130,9 @@ module REXMLTests
assert_equal("b", XPath::first(c, "..").name) assert_equal("b", XPath::first(c, "..").name)
assert_equal("a", XPath::first(@@doc, "a/b/..").name) assert_equal("a", XPath::first(@@doc, "a/b/..").name)
doc = REXML::Document.new(File.new(fixture_path("project.xml"))) doc = File.open(fixture_path("project.xml")) do |f|
REXML::Document.new(f)
end
c = each_test(doc.root, "./Description" ) { |child| c = each_test(doc.root, "./Description" ) { |child|
assert_equal("Description",child.name) assert_equal("Description",child.name)
} }
@ -213,11 +215,11 @@ module REXMLTests
xmlsource = fixture_path("testsrc.xml") xmlsource = fixture_path("testsrc.xml")
xpathtests = fixture_path("xp.tst") xpathtests = fixture_path("xp.tst")
doc = REXML::Document.new(File.new(xmlsource)) doc = File.open(xmlsource) {|f| REXML::Document.new(f) }
#results = "" #results = ""
results = REXML::Document.new results = REXML::Document.new
results.add_element "test-results" results.add_element "test-results"
for line in File.new(xpathtests) File.foreach(xpathtests) do |line|
line.strip! line.strip!
begin begin
doc.root doc.root
@ -315,7 +317,7 @@ module REXMLTests
end end
def test_lang def test_lang
doc = Document.new(File.new(fixture_path("lang0.xml"))) doc = File.open(fixture_path("lang0.xml")) {|f| Document.new(f) }
#puts IO.read( "test/lang.xml" ) #puts IO.read( "test/lang.xml" )
#puts XPath.match( doc, "//language/*" ).size #puts XPath.match( doc, "//language/*" ).size
@ -936,10 +938,14 @@ EOF
def test_ticket_43 def test_ticket_43
#url = http://news.search.yahoo.com/news/rss?p=market&ei=UTF-8&fl=0&x=wrt #url = http://news.search.yahoo.com/news/rss?p=market&ei=UTF-8&fl=0&x=wrt
sum = Document.new(File.new(fixture_path("yahoo.xml"))).elements.to_a("//item").size sum = File.open(fixture_path("yahoo.xml")) do |f|
Document.new(f).elements.to_a("//item").size
end
assert_equal( 10, sum ) assert_equal( 10, sum )
text = Document.new(File.new(fixture_path("yahoo.xml"))).elements.to_a(%Q{//title[contains(text(), "'")]}).collect{|e| e.text}.join text = File.open(fixture_path("yahoo.xml")) do |f|
Document.new(f).elements.to_a(%Q{//title[contains(text(), "'")]}).collect{|e| e.text}.join
end
assert_equal( "Broward labor market's a solid performer (Miami Herald)", text ) assert_equal( "Broward labor market's a solid performer (Miami Herald)", text )
end end
@ -997,14 +1003,16 @@ EOF
end end
def test_ticket_61_text def test_ticket_61_text
file = File.open(fixture_path("ticket_61.xml")) doc = File.open(fixture_path("ticket_61.xml")) do |file|
doc = REXML::Document.new file REXML::Document.new file
end
ticket_61_fixture( doc, "//div[text()='Add' and @class='ButtonText']" ) ticket_61_fixture( doc, "//div[text()='Add' and @class='ButtonText']" )
end end
def test_ticket_61_contains def test_ticket_61_contains
file = File.open(fixture_path("ticket_61.xml")) doc = File.open(fixture_path("ticket_61.xml")) do |file|
doc = REXML::Document.new file REXML::Document.new file
end
ticket_61_fixture( doc, "//div[contains(.,'Add') and @class='ButtonText']" ) ticket_61_fixture( doc, "//div[contains(.,'Add') and @class='ButtonText']" )
end end