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

Fix Hash#from_xml with frozen strings (#24718)

* Hash#from_xml works with frozen strings

Fixes #24647

* Fix rexml engine test

[Marek Kirejczyk + Rafael Mendonça França]
This commit is contained in:
Marek Kirejczyk 2016-05-21 14:14:22 +02:00 committed by Rafael França
parent 91421984b7
commit dd829df07e
2 changed files with 19 additions and 11 deletions

View file

@ -20,11 +20,9 @@ module ActiveSupport
data = StringIO.new(data || '')
end
char = data.getc
if char.nil?
if data.eof?
{}
else
data.ungetc(char)
silence_warnings { require 'rexml/document' } unless defined?(REXML::Document)
doc = REXML::Document.new(data)

View file

@ -22,14 +22,24 @@ class REXMLEngineTest < ActiveSupport::TestCase
morning
</root>
eoxml
assert_equal_rexml(io)
hash = ActiveSupport::XmlMini.parse(io)
assert hash.has_key?('root')
assert hash['root'].has_key?('products')
assert_match "good", hash['root']['__content__']
products = hash['root']['products']
assert products.has_key?("__content__")
assert_match 'hello everyone', products['__content__']
end
def test_parse_from_empty_string
ActiveSupport::XmlMini.backend = 'REXML'
assert_equal({}, ActiveSupport::XmlMini.parse(""))
end
def test_parse_from_frozen_string
ActiveSupport::XmlMini.backend = 'REXML'
xml_string = "<root></root>".freeze
assert_equal({"root" => {}}, ActiveSupport::XmlMini.parse(xml_string))
end
private
def assert_equal_rexml(xml)
parsed_xml = ActiveSupport::XmlMini.parse(xml)
xml.rewind if xml.respond_to?(:rewind)
hash = ActiveSupport::XmlMini.with_backend('REXML') { ActiveSupport::XmlMini.parse(xml) }
assert_equal(hash, parsed_xml)
end
end