class HashToXmlTest

Public Instance Methods

setup() click to toggle source
# File activesupport/test/core_ext/hash_ext_test.rb, line 537
def setup
  @xml_options = { root: :person, skip_instruct: true, indent: 0 }
end
test_all_caps_key_from_xml() click to toggle source
# File activesupport/test/core_ext/hash_ext_test.rb, line 832
  def test_all_caps_key_from_xml
    test_xml = <<-EOT
      <ABC3XYZ>
        <TEST>Lorem Ipsum</TEST>
      </ABC3XYZ>
    EOT

    expected_hash = {
      "ABC3XYZ" => {
        "TEST" => "Lorem Ipsum"
      }
    }

    assert_equal expected_hash, Hash.from_xml(test_xml)
  end
test_array_with_multiple_entries_from_xml() click to toggle source
# File activesupport/test/core_ext/hash_ext_test.rb, line 881
  def test_array_with_multiple_entries_from_xml
    blog_xml = <<-XML
      <blog>
        <posts type="array">
          <post>a post</post>
          <post>another post</post>
        </posts>
      </blog>
    XML
    expected_blog_hash = { "blog" => { "posts" => ["a post", "another post"] } }
    assert_equal expected_blog_hash, Hash.from_xml(blog_xml)
  end
test_array_with_one_entry_from_xml() click to toggle source
# File activesupport/test/core_ext/hash_ext_test.rb, line 869
  def test_array_with_one_entry_from_xml
    blog_xml = <<-XML
      <blog>
        <posts type="array">
          <post>a post</post>
        </posts>
      </blog>
    XML
    expected_blog_hash = { "blog" => { "posts" => ["a post"] } }
    assert_equal expected_blog_hash, Hash.from_xml(blog_xml)
  end
test_datetime_xml_type_with_far_future_date() click to toggle source
# File activesupport/test/core_ext/hash_ext_test.rb, line 1079
  def test_datetime_xml_type_with_far_future_date
    alert_xml = <<-XML
      <alert>
        <alert_at type="datetime">2050-02-10T15:30:45Z</alert_at>
      </alert>
    XML
    alert_at = Hash.from_xml(alert_xml)["alert"]["alert_at"]
    assert alert_at.utc?
    assert_equal 2050,  alert_at.year
    assert_equal 2,     alert_at.month
    assert_equal 10,    alert_at.day
    assert_equal 15,    alert_at.hour
    assert_equal 30,    alert_at.min
    assert_equal 45,    alert_at.sec
  end
test_datetime_xml_type_with_non_utc_time() click to toggle source
# File activesupport/test/core_ext/hash_ext_test.rb, line 1068
  def test_datetime_xml_type_with_non_utc_time
    alert_xml = <<-XML
      <alert>
        <alert_at type="datetime">2008-02-10T10:30:45-05:00</alert_at>
      </alert>
    XML
    alert_at = Hash.from_xml(alert_xml)["alert"]["alert_at"]
    assert alert_at.utc?
    assert_equal Time.utc(2008, 2, 10, 15, 30, 45), alert_at
  end
test_datetime_xml_type_with_utc_time() click to toggle source
# File activesupport/test/core_ext/hash_ext_test.rb, line 1057
  def test_datetime_xml_type_with_utc_time
    alert_xml = <<-XML
      <alert>
        <alert_at type="datetime">2008-02-10T15:30:45Z</alert_at>
      </alert>
    XML
    alert_at = Hash.from_xml(alert_xml)["alert"]["alert_at"]
    assert alert_at.utc?
    assert_equal Time.utc(2008, 2, 10, 15, 30, 45), alert_at
  end
test_empty_array_from_xml() click to toggle source
# File activesupport/test/core_ext/hash_ext_test.rb, line 848
  def test_empty_array_from_xml
    blog_xml = <<-XML
      <blog>
        <posts type="array"></posts>
      </blog>
    XML
    expected_blog_hash = { "blog" => { "posts" => [] } }
    assert_equal expected_blog_hash, Hash.from_xml(blog_xml)
  end
test_empty_array_with_whitespace_from_xml() click to toggle source
# File activesupport/test/core_ext/hash_ext_test.rb, line 858
  def test_empty_array_with_whitespace_from_xml
    blog_xml = <<-XML
      <blog>
        <posts type="array">
        </posts>
      </blog>
    XML
    expected_blog_hash = { "blog" => { "posts" => [] } }
    assert_equal expected_blog_hash, Hash.from_xml(blog_xml)
  end
test_empty_cdata_from_xml() click to toggle source
# File activesupport/test/core_ext/hash_ext_test.rb, line 931
def test_empty_cdata_from_xml
  xml = "<data><![CDATA[]]></data>"

  assert_equal "", Hash.from_xml(xml)["data"]
end
test_empty_string_works_for_typecast_xml_value() click to toggle source
# File activesupport/test/core_ext/hash_ext_test.rb, line 1023
def test_empty_string_works_for_typecast_xml_value
  assert_nothing_raised do
    ActiveSupport::XMLConverter.new("").to_h
  end
end
test_escaping_to_xml() click to toggle source
# File activesupport/test/core_ext/hash_ext_test.rb, line 1029
def test_escaping_to_xml
  hash = {
    bare_string: "First & Last Name",
    pre_escaped_string: "First &amp; Last Name"
  }.stringify_keys

  expected_xml = "<person><bare-string>First &amp; Last Name</bare-string><pre-escaped-string>First &amp;amp; Last Name</pre-escaped-string></person>"
  assert_equal expected_xml, hash.to_xml(@xml_options)
end
test_expansion_count_is_limited() click to toggle source
# File activesupport/test/core_ext/hash_ext_test.rb, line 1102
  def test_expansion_count_is_limited
    expected =
      case ActiveSupport::XmlMini.backend.name
      when "ActiveSupport::XmlMini_REXML";        RuntimeError
      when "ActiveSupport::XmlMini_Nokogiri";     Nokogiri::XML::SyntaxError
      when "ActiveSupport::XmlMini_NokogiriSAX";  RuntimeError
      when "ActiveSupport::XmlMini_LibXML";       LibXML::XML::Error
      when "ActiveSupport::XmlMini_LibXMLSAX";    LibXML::XML::Error
      end

    assert_raise expected do
      attack_xml = <<-EOT
      <?xml version="1.0" encoding="UTF-8"?>
      <!DOCTYPE member [
        <!ENTITY a "&b;&b;&b;&b;&b;&b;&b;&b;&b;&b;">
        <!ENTITY b "&c;&c;&c;&c;&c;&c;&c;&c;&c;&c;">
        <!ENTITY c "&d;&d;&d;&d;&d;&d;&d;&d;&d;&d;">
        <!ENTITY d "&e;&e;&e;&e;&e;&e;&e;&e;&e;&e;">
        <!ENTITY e "&f;&f;&f;&f;&f;&f;&f;&f;&f;&f;">
        <!ENTITY f "&g;&g;&g;&g;&g;&g;&g;&g;&g;&g;">
        <!ENTITY g "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx">
      ]>
      <member>
      &a;
      </member>
      EOT
      Hash.from_xml(attack_xml)
    end
  end
test_file_from_xml() click to toggle source
# File activesupport/test/core_ext/hash_ext_test.rb, line 894
  def test_file_from_xml
    blog_xml = <<-XML
      <blog>
        <logo type="file" name="logo.png" content_type="image/png">
        </logo>
      </blog>
    XML
    hash = Hash.from_xml(blog_xml)
    assert hash.has_key?("blog")
    assert hash["blog"].has_key?("logo")

    file = hash["blog"]["logo"]
    assert_equal "logo.png", file.original_filename
    assert_equal "image/png", file.content_type
  end
test_file_from_xml_with_defaults() click to toggle source
# File activesupport/test/core_ext/hash_ext_test.rb, line 910
  def test_file_from_xml_with_defaults
    blog_xml = <<-XML
      <blog>
        <logo type="file">
        </logo>
      </blog>
    XML
    file = Hash.from_xml(blog_xml)["blog"]["logo"]
    assert_equal "untitled", file.original_filename
    assert_equal "application/octet-stream", file.content_type
  end
test_from_trusted_xml_allows_symbol_and_yaml_types() click to toggle source
# File activesupport/test/core_ext/hash_ext_test.rb, line 1006
def test_from_trusted_xml_allows_symbol_and_yaml_types
  expected = { "product" => { "name" => :value } }
  assert_equal expected, Hash.from_trusted_xml('<product><name type="symbol">value</name></product>')
  assert_equal expected, Hash.from_trusted_xml('<product><name type="yaml">:value</name></product>')
end
test_from_xml_array_many() click to toggle source
# File activesupport/test/core_ext/hash_ext_test.rb, line 1001
def test_from_xml_array_many
  expected = { "numbers" => { "type" => "Array", "value" => [ "1", "2" ] } }
  assert_equal expected, Hash.from_xml('<numbers type="Array"><value>1</value><value>2</value></numbers>')
end
test_from_xml_array_one() click to toggle source
# File activesupport/test/core_ext/hash_ext_test.rb, line 996
def test_from_xml_array_one
  expected = { "numbers" => { "type" => "Array", "value" => "1" } }
  assert_equal expected, Hash.from_xml('<numbers type="Array"><value>1</value></numbers>')
end
test_from_xml_disallows_symbol_and_yaml_types_by_default() click to toggle source
# File activesupport/test/core_ext/hash_ext_test.rb, line 986
def test_from_xml_disallows_symbol_and_yaml_types_by_default
  assert_raise ActiveSupport::XMLConverter::DisallowedType do
    Hash.from_xml '<product><name type="symbol">value</name></product>'
  end

  assert_raise ActiveSupport::XMLConverter::DisallowedType do
    Hash.from_xml '<product><name type="yaml">value</name></product>'
  end
end
test_from_xml_raises_on_disallowed_type_attributes() click to toggle source
# File activesupport/test/core_ext/hash_ext_test.rb, line 980
def test_from_xml_raises_on_disallowed_type_attributes
  assert_raise ActiveSupport::XMLConverter::DisallowedType do
    Hash.from_xml '<product><name type="foo">value</name></product>', %w(foo)
  end
end
test_kernel_method_names_to_xml() click to toggle source

The XML builder seems to fail miserably when trying to tag something with the same name as a Kernel method (throw, test, loop, select …)

# File activesupport/test/core_ext/hash_ext_test.rb, line 1014
def test_kernel_method_names_to_xml
  hash     = { throw: { ball: "red" } }
  expected = "<person><throw><ball>red</ball></throw></person>"

  assert_nothing_raised do
    assert_equal expected, hash.to_xml(@xml_options)
  end
end
test_multiple_records_from_xml() click to toggle source
# File activesupport/test/core_ext/hash_ext_test.rb, line 760
  def test_multiple_records_from_xml
    topics_xml = <<-EOT
      <topics type="array">
        <topic>
          <title>The First Topic</title>
          <author-name>David</author-name>
          <id type="integer">1</id>
          <approved type="boolean">false</approved>
          <replies-count type="integer">0</replies-count>
          <replies-close-in type="integer">2592000000</replies-close-in>
          <written-on type="date">2003-07-16</written-on>
          <viewed-at type="datetime">2003-07-16T09:28:00+0000</viewed-at>
          <content>Have a nice day</content>
          <author-email-address>david@loudthinking.com</author-email-address>
          <parent-id nil="true"></parent-id>
        </topic>
        <topic>
          <title>The Second Topic</title>
          <author-name>Jason</author-name>
          <id type="integer">1</id>
          <approved type="boolean">false</approved>
          <replies-count type="integer">0</replies-count>
          <replies-close-in type="integer">2592000000</replies-close-in>
          <written-on type="date">2003-07-16</written-on>
          <viewed-at type="datetime">2003-07-16T09:28:00+0000</viewed-at>
          <content>Have a nice day</content>
          <author-email-address>david@loudthinking.com</author-email-address>
          <parent-id></parent-id>
        </topic>
      </topics>
    EOT

    expected_topic_hash = {
      title: "The First Topic",
      author_name: "David",
      id: 1,
      approved: false,
      replies_count: 0,
      replies_close_in: 2592000000,
      written_on: Date.new(2003, 7, 16),
      viewed_at: Time.utc(2003, 7, 16, 9, 28),
      content: "Have a nice day",
      author_email_address: "david@loudthinking.com",
      parent_id: nil
    }.stringify_keys

    assert_equal expected_topic_hash, Hash.from_xml(topics_xml)["topics"].first
  end
test_multiple_records_from_xml_with_attributes_other_than_type_ignores_them_without_exploding() click to toggle source
# File activesupport/test/core_ext/hash_ext_test.rb, line 651
  def test_multiple_records_from_xml_with_attributes_other_than_type_ignores_them_without_exploding
    topics_xml = <<-EOT
      <topics type="array" page="1" page-count="1000" per-page="2">
        <topic>
          <title>The First Topic</title>
          <author-name>David</author-name>
          <id type="integer">1</id>
          <approved type="boolean">false</approved>
          <replies-count type="integer">0</replies-count>
          <replies-close-in type="integer">2592000000</replies-close-in>
          <written-on type="date">2003-07-16</written-on>
          <viewed-at type="datetime">2003-07-16T09:28:00+0000</viewed-at>
          <content>Have a nice day</content>
          <author-email-address>david@loudthinking.com</author-email-address>
          <parent-id nil="true"></parent-id>
        </topic>
        <topic>
          <title>The Second Topic</title>
          <author-name>Jason</author-name>
          <id type="integer">1</id>
          <approved type="boolean">false</approved>
          <replies-count type="integer">0</replies-count>
          <replies-close-in type="integer">2592000000</replies-close-in>
          <written-on type="date">2003-07-16</written-on>
          <viewed-at type="datetime">2003-07-16T09:28:00+0000</viewed-at>
          <content>Have a nice day</content>
          <author-email-address>david@loudthinking.com</author-email-address>
          <parent-id></parent-id>
        </topic>
      </topics>
    EOT

    expected_topic_hash = {
      title: "The First Topic",
      author_name: "David",
      id: 1,
      approved: false,
      replies_count: 0,
      replies_close_in: 2592000000,
      written_on: Date.new(2003, 7, 16),
      viewed_at: Time.utc(2003, 7, 16, 9, 28),
      content: "Have a nice day",
      author_email_address: "david@loudthinking.com",
      parent_id: nil
    }.stringify_keys

    assert_equal expected_topic_hash, Hash.from_xml(topics_xml)["topics"].first
  end
test_one_level() click to toggle source
# File activesupport/test/core_ext/hash_ext_test.rb, line 541
def test_one_level
  xml = { name: "David", street: "Paulina" }.to_xml(@xml_options)
  assert_equal "<person>", xml.first(8)
  assert_includes xml, %(<street>Paulina</street>)
  assert_includes xml, %(<name>David</name>)
end
test_one_level_camelize_lower() click to toggle source
# File activesupport/test/core_ext/hash_ext_test.rb, line 569
def test_one_level_camelize_lower
  xml = { name: "David", street_name: "Paulina" }.to_xml(@xml_options.merge(camelize: :lower))
  assert_equal "<person>", xml.first(8)
  assert_includes xml, %(<streetName>Paulina</streetName>)
  assert_includes xml, %(<name>David</name>)
end
test_one_level_camelize_true() click to toggle source
# File activesupport/test/core_ext/hash_ext_test.rb, line 562
def test_one_level_camelize_true
  xml = { name: "David", street_name: "Paulina" }.to_xml(@xml_options.merge(camelize: true))
  assert_equal "<Person>", xml.first(8)
  assert_includes xml, %(<StreetName>Paulina</StreetName>)
  assert_includes xml, %(<Name>David</Name>)
end
test_one_level_dasherize_false() click to toggle source
# File activesupport/test/core_ext/hash_ext_test.rb, line 548
def test_one_level_dasherize_false
  xml = { name: "David", street_name: "Paulina" }.to_xml(@xml_options.merge(dasherize: false))
  assert_equal "<person>", xml.first(8)
  assert_includes xml, %(<street_name>Paulina</street_name>)
  assert_includes xml, %(<name>David</name>)
end
test_one_level_dasherize_true() click to toggle source
# File activesupport/test/core_ext/hash_ext_test.rb, line 555
def test_one_level_dasherize_true
  xml = { name: "David", street_name: "Paulina" }.to_xml(@xml_options.merge(dasherize: true))
  assert_equal "<person>", xml.first(8)
  assert_includes xml, %(<street-name>Paulina</street-name>)
  assert_includes xml, %(<name>David</name>)
end
test_one_level_with_nils() click to toggle source
# File activesupport/test/core_ext/hash_ext_test.rb, line 587
def test_one_level_with_nils
  xml = { name: "David", street: "Paulina", age: nil }.to_xml(@xml_options)
  assert_equal "<person>", xml.first(8)
  assert_includes xml, %(<street>Paulina</street>)
  assert_includes xml, %(<name>David</name>)
  assert_includes xml, %(<age nil="true"/>)
end
test_one_level_with_skipping_types() click to toggle source
# File activesupport/test/core_ext/hash_ext_test.rb, line 595
def test_one_level_with_skipping_types
  xml = { name: "David", street: "Paulina", age: nil }.to_xml(@xml_options.merge(skip_types: true))
  assert_equal "<person>", xml.first(8)
  assert_includes xml, %(<street>Paulina</street>)
  assert_includes xml, %(<name>David</name>)
  assert_includes xml, %(<age nil="true"/>)
end
test_one_level_with_types() click to toggle source
# File activesupport/test/core_ext/hash_ext_test.rb, line 576
def test_one_level_with_types
  xml = { name: "David", street: "Paulina", age: 26, age_in_millis: 820497600000, moved_on: Date.new(2005, 11, 15), resident: :yes }.to_xml(@xml_options)
  assert_equal "<person>", xml.first(8)
  assert_includes xml, %(<street>Paulina</street>)
  assert_includes xml, %(<name>David</name>)
  assert_includes xml, %(<age type="integer">26</age>)
  assert_includes xml, %(<age-in-millis type="integer">820497600000</age-in-millis>)
  assert_includes xml, %(<moved-on type="date">2005-11-15</moved-on>)
  assert_includes xml, %(<resident type="symbol">yes</resident>)
end
test_one_level_with_yielding() click to toggle source
# File activesupport/test/core_ext/hash_ext_test.rb, line 603
def test_one_level_with_yielding
  xml = { name: "David", street: "Paulina" }.to_xml(@xml_options) do |x|
    x.creator("Quails")
  end

  assert_equal "<person>", xml.first(8)
  assert_includes xml, %(<street>Paulina</street>)
  assert_includes xml, %(<name>David</name>)
  assert_includes xml, %(<creator>Quails</creator>)
end
test_roundtrip_to_xml_from_xml() click to toggle source
# File activesupport/test/core_ext/hash_ext_test.rb, line 1048
def test_roundtrip_to_xml_from_xml
  hash = {
    bare_string: "First & Last Name",
    pre_escaped_string: "First &amp; Last Name"
  }.stringify_keys

  assert_equal hash, Hash.from_xml(hash.to_xml(@xml_options))["person"]
end
test_single_record_from_xml() click to toggle source
# File activesupport/test/core_ext/hash_ext_test.rb, line 700
  def test_single_record_from_xml
    topic_xml = <<-EOT
      <topic>
        <title>The First Topic</title>
        <author-name>David</author-name>
        <id type="integer">1</id>
        <approved type="boolean"> true </approved>
        <replies-count type="integer">0</replies-count>
        <replies-close-in type="integer">2592000000</replies-close-in>
        <written-on type="date">2003-07-16</written-on>
        <viewed-at type="datetime">2003-07-16T09:28:00+0000</viewed-at>
        <author-email-address>david@loudthinking.com</author-email-address>
        <parent-id></parent-id>
        <ad-revenue type="decimal">1.5</ad-revenue>
        <optimum-viewing-angle type="float">135</optimum-viewing-angle>
      </topic>
    EOT

    expected_topic_hash = {
      title: "The First Topic",
      author_name: "David",
      id: 1,
      approved: true,
      replies_count: 0,
      replies_close_in: 2592000000,
      written_on: Date.new(2003, 7, 16),
      viewed_at: Time.utc(2003, 7, 16, 9, 28),
      author_email_address: "david@loudthinking.com",
      parent_id: nil,
      ad_revenue: BigDecimal("1.50"),
      optimum_viewing_angle: 135.0,
    }.stringify_keys

    assert_equal expected_topic_hash, Hash.from_xml(topic_xml)["topic"]
  end
test_single_record_from_xml_with_attributes_other_than_type() click to toggle source
# File activesupport/test/core_ext/hash_ext_test.rb, line 809
  def test_single_record_from_xml_with_attributes_other_than_type
    topic_xml = <<-EOT
    <rsp stat="ok">
      <photos page="1" pages="1" perpage="100" total="16">
        <photo id="175756086" owner="55569174@N00" secret="0279bf37a1" server="76" title="Colored Pencil PhotoBooth Fun" ispublic="1" isfriend="0" isfamily="0"/>
      </photos>
    </rsp>
    EOT

    expected_topic_hash = {
      id: "175756086",
      owner: "55569174@N00",
      secret: "0279bf37a1",
      server: "76",
      title: "Colored Pencil PhotoBooth Fun",
      ispublic: "1",
      isfriend: "0",
      isfamily: "0",
    }.stringify_keys

    assert_equal expected_topic_hash, Hash.from_xml(topic_xml)["rsp"]["photos"]["photo"]
  end
test_single_record_from_xml_with_nil_values() click to toggle source
# File activesupport/test/core_ext/hash_ext_test.rb, line 736
  def test_single_record_from_xml_with_nil_values
    topic_xml = <<-EOT
      <topic>
        <title></title>
        <id type="integer"></id>
        <approved type="boolean"></approved>
        <written-on type="date"></written-on>
        <viewed-at type="datetime"></viewed-at>
        <parent-id></parent-id>
      </topic>
    EOT

    expected_topic_hash = {
      title: nil,
      id: nil,
      approved: nil,
      written_on: nil,
      viewed_at: nil,
      parent_id: nil
    }.stringify_keys

    assert_equal expected_topic_hash, Hash.from_xml(topic_xml)["topic"]
  end
test_tag_with_attrs_and_whitespace() click to toggle source
# File activesupport/test/core_ext/hash_ext_test.rb, line 922
  def test_tag_with_attrs_and_whitespace
    xml = <<-XML
      <blog name="bacon is the best">
      </blog>
    XML
    hash = Hash.from_xml(xml)
    assert_equal "bacon is the best", hash["blog"]["name"]
  end
test_three_levels_with_array() click to toggle source
# File activesupport/test/core_ext/hash_ext_test.rb, line 637
def test_three_levels_with_array
  xml = { name: "David", addresses: [{ streets: [ { name: "Paulina" }, { name: "Paulina" } ] } ] }.to_xml(@xml_options)
  assert_includes xml, %(<addresses type="array"><address><streets type="array"><street><name>)
end
test_timezoned_attributes() click to toggle source
# File activesupport/test/core_ext/hash_ext_test.rb, line 642
def test_timezoned_attributes
  xml = {
    created_at: Time.utc(1999, 2, 2),
    local_created_at: Time.utc(1999, 2, 2).in_time_zone("Eastern Time (US & Canada)")
  }.to_xml(@xml_options)
  assert_match %r{<created-at type=\"dateTime\">1999-02-02T00:00:00Z</created-at>}, xml
  assert_match %r{<local-created-at type=\"dateTime\">1999-02-01T19:00:00-05:00</local-created-at>}, xml
end
test_to_xml_dups_options() click to toggle source
# File activesupport/test/core_ext/hash_ext_test.rb, line 1095
def test_to_xml_dups_options
  options = { skip_instruct: true }
  {}.to_xml(options)
  # :builder, etc, shouldn't be added to options
  assert_equal({ skip_instruct: true }, options)
end
test_two_levels() click to toggle source
# File activesupport/test/core_ext/hash_ext_test.rb, line 614
def test_two_levels
  xml = { name: "David", address: { street: "Paulina" } }.to_xml(@xml_options)
  assert_equal "<person>", xml.first(8)
  assert_includes xml, %(<address><street>Paulina</street></address>)
  assert_includes xml, %(<name>David</name>)
end
test_two_levels_with_array() click to toggle source
# File activesupport/test/core_ext/hash_ext_test.rb, line 628
def test_two_levels_with_array
  xml = { name: "David", addresses: [{ street: "Paulina" }, { street: "Evergreen" }] }.to_xml(@xml_options)
  assert_equal "<person>", xml.first(8)
  assert_includes xml, %(<addresses type="array"><address>)
  assert_includes xml, %(<address><street>Paulina</street></address>)
  assert_includes xml, %(<address><street>Evergreen</street></address>)
  assert_includes xml, %(<name>David</name>)
end
test_two_levels_with_second_level_overriding_to_xml() click to toggle source
# File activesupport/test/core_ext/hash_ext_test.rb, line 621
def test_two_levels_with_second_level_overriding_to_xml
  xml = { name: "David", address: { street: "Paulina" }, child: IWriteMyOwnXML.new }.to_xml(@xml_options)
  assert_equal "<person>", xml.first(8)
  assert_includes xml, %(<address><street>Paulina</street></address>)
  assert_includes xml, %(<level_one><second_level>content</second_level></level_one>)
end
test_type_trickles_through_when_unknown() click to toggle source
# File activesupport/test/core_ext/hash_ext_test.rb, line 963
  def test_type_trickles_through_when_unknown
    product_xml = <<-EOT
    <product>
      <weight type="double">0.5</weight>
      <image type="ProductImage"><filename>image.gif</filename></image>

    </product>
    EOT

    expected_product_hash = {
      weight: 0.5,
      image: { "type" => "ProductImage", "filename" => "image.gif" },
    }.stringify_keys

    assert_equal expected_product_hash, Hash.from_xml(product_xml)["product"]
  end
test_unescaping_from_xml() click to toggle source
# File activesupport/test/core_ext/hash_ext_test.rb, line 1039
def test_unescaping_from_xml
  xml_string = "<person><bare-string>First &amp; Last Name</bare-string><pre-escaped-string>First &amp;amp; Last Name</pre-escaped-string></person>"
  expected_hash = {
    bare_string: "First & Last Name",
    pre_escaped_string: "First &amp; Last Name"
  }.stringify_keys
  assert_equal expected_hash, Hash.from_xml(xml_string)["person"]
end
test_xsd_like_types_from_xml() click to toggle source
# File activesupport/test/core_ext/hash_ext_test.rb, line 937
  def test_xsd_like_types_from_xml
    bacon_xml = <<-EOT
    <bacon>
      <weight type="double">0.5</weight>
      <price type="decimal">12.50</price>
      <chunky type="boolean"> 1 </chunky>
      <expires-at type="dateTime">2007-12-25T12:34:56+0000</expires-at>
      <notes type="string"></notes>
      <illustration type="base64Binary">YmFiZS5wbmc=</illustration>
      <caption type="binary" encoding="base64">VGhhdCdsbCBkbywgcGlnLg==</caption>
    </bacon>
    EOT

    expected_bacon_hash = {
      weight: 0.5,
      chunky: true,
      price: BigDecimal("12.50"),
      expires_at: Time.utc(2007, 12, 25, 12, 34, 56),
      notes: "",
      illustration: "babe.png",
      caption: "That'll do, pig."
    }.stringify_keys

    assert_equal expected_bacon_hash, Hash.from_xml(bacon_xml)["bacon"]
  end