0
require File.dirname(__FILE__) + '/../../spec_helper'
0
describe Hash, "environmentize_keys!" do
0
it "should transform keys to uppercase text" do
0
@@ -145,7 +146,7 @@ describe Hash, "from_xml" do
0
it "should typecast a false boolean" do
0
- ["false"
, "1", "0", "some word" ].each do |w|
0
Hash.from_xml("<tag type='boolean'>#{w}</tag>")['tag'].should be_false
0
@@ -234,6 +235,277 @@ describe Hash, "from_xml" do
0
Hash.from_xml(xml).should == hash
0
+ it "should properly handle nil values (ActiveSupport Compatible)" do
0
+ <id type="integer"></id>
0
+ <approved type="boolean"></approved>
0
+ <written-on type="date"></written-on>
0
+ <viewed-at type="datetime"></viewed-at>
0
+ <content type="yaml"></content>
0
+ <parent-id></parent-id>
0
+ expected_topic_hash = {
0
+ Hash.from_xml(topic_xml)["topic"].should == expected_topic_hash
0
+ it "should handle a single record from xml (ActiveSupport Compatible)" do
0
+ <title>The First Topic</title>
0
+ <author-name>David</author-name>
0
+ <id type="integer">1</id>
0
+ <approved type="boolean"> true </approved>
0
+ <replies-count type="integer">0</replies-count>
0
+ <replies-close-in type="integer">2592000000</replies-close-in>
0
+ <written-on type="date">2003-07-16</written-on>
0
+ <viewed-at type="datetime">2003-07-16T09:28:00+0000</viewed-at>
0
+ <content type="yaml">--- \n1: should be an integer\n:message: Have a nice day\narray: \n- should-have-dashes: true\n should_have_underscores: true\n</content>
0
+ <author-email-address>david@loudthinking.com</author-email-address>
0
+ <parent-id></parent-id>
0
+ <ad-revenue type="decimal">1.5</ad-revenue>
0
+ <optimum-viewing-angle type="float">135</optimum-viewing-angle>
0
+ <resident type="symbol">yes</resident>
0
+ expected_topic_hash = {
0
+ 'title' => "The First Topic",
0
+ 'author_name' => "David",
0
+ 'replies_close_in' => 2592000000,
0
+ 'written_on' => Date.new(2003, 7, 16),
0
+ 'viewed_at' => Time.utc(2003, 7, 16, 9, 28),
0
+ # Changed this line where the key is :message. The yaml specifies this as a symbol, and who am I to change what you specify
0
+ # The line in ActiveSupport is
0
+ # 'content' => { 'message' => "Have a nice day", 1 => "should be an integer", "array" => [{ "should-have-dashes" => true, "should_have_underscores" => true }] },
0
+ 'content' => { :message => "Have a nice day", 1 => "should be an integer", "array" => [{ "should-have-dashes" => true, "should_have_underscores" => true }] },
0
+ 'author_email_address' => "david@loudthinking.com",
0
+ 'ad_revenue' => BigDecimal("1.50"),
0
+ 'optimum_viewing_angle' => 135.0,
0
+ Hash.from_xml(topic_xml)["topic"].each do |k,v|
0
+ v.should == expected_topic_hash[k]
0
+ it "should handle multiple records (ActiveSupport Compatible)" do
0
+ <title>The First Topic</title>
0
+ <author-name>David</author-name>
0
+ <id type="integer">1</id>
0
+ <approved type="boolean">false</approved>
0
+ <replies-count type="integer">0</replies-count>
0
+ <replies-close-in type="integer">2592000000</replies-close-in>
0
+ <written-on type="date">2003-07-16</written-on>
0
+ <viewed-at type="datetime">2003-07-16T09:28:00+0000</viewed-at>
0
+ <content>Have a nice day</content>
0
+ <author-email-address>david@loudthinking.com</author-email-address>
0
+ <parent-id nil="true"></parent-id>
0
+ <title>The Second Topic</title>
0
+ <author-name>Jason</author-name>
0
+ <id type="integer">1</id>
0
+ <approved type="boolean">false</approved>
0
+ <replies-count type="integer">0</replies-count>
0
+ <replies-close-in type="integer">2592000000</replies-close-in>
0
+ <written-on type="date">2003-07-16</written-on>
0
+ <viewed-at type="datetime">2003-07-16T09:28:00+0000</viewed-at>
0
+ <content>Have a nice day</content>
0
+ <author-email-address>david@loudthinking.com</author-email-address>
0
+ <parent-id></parent-id>
0
+ expected_topic_hash = {
0
+ 'title' => "The First Topic",
0
+ 'author_name' => "David",
0
+ 'replies_close_in' => 2592000000,
0
+ 'written_on' => Date.new(2003, 7, 16),
0
+ 'viewed_at' => Time.utc(2003, 7, 16, 9, 28),
0
+ 'content' => "Have a nice day",
0
+ 'author_email_address' => "david@loudthinking.com",
0
+ # puts Hash.from_xml(topics_xml)['topics'].first.inspect
0
+ Hash.from_xml(topics_xml)["topics"].first.each do |k,v|
0
+ v.should == expected_topic_hash[k]
0
+ it "should handle a single record from_xml with attributes other than type (ActiveSupport Compatible)" do
0
+ <photos page="1" pages="1" perpage="100" total="16">
0
+ <photo id="175756086" owner="55569174@N00" secret="0279bf37a1" server="76" title="Colored Pencil PhotoBooth Fun" ispublic="1" isfriend="0" isfamily="0"/>
0
+ expected_topic_hash = {
0
+ 'owner' => "55569174@N00",
0
+ 'secret' => "0279bf37a1",
0
+ 'title' => "Colored Pencil PhotoBooth Fun",
0
+ Hash.from_xml(topic_xml)["rsp"]["photos"]["photo"].each do |k,v|
0
+ v.should == expected_topic_hash[k]
0
+ it "should handle an emtpy array (ActiveSupport Compatible)" do
0
+ <posts type="array"></posts>
0
+ expected_blog_hash = {"blog" => {"posts" => []}}
0
+ Hash.from_xml(blog_xml).should == expected_blog_hash
0
+ it "should handle empty array with whitespace from xml (ActiveSupport Compatible)" do
0
+ expected_blog_hash = {"blog" => {"posts" => []}}
0
+ Hash.from_xml(blog_xml).should == expected_blog_hash
0
+ it "should handle array with one entry from_xml (ActiveSupport Compatible)" do
0
+ expected_blog_hash = {"blog" => {"posts" => ["a post"]}}
0
+ Hash.from_xml(blog_xml).should == expected_blog_hash
0
+ it "should handle array with multiple entries from xml (ActiveSupport Compatible)" do
0
+ <post>another post</post>
0
+ expected_blog_hash = {"blog" => {"posts" => ["a post", "another post"]}}
0
+ Hash.from_xml(blog_xml).should == expected_blog_hash
0
+ it "should handle file types (ActiveSupport Compatible)" do
0
+ <logo type="file" name="logo.png" content_type="image/png">
0
+ hash = Hash.from_xml(blog_xml)
0
+ hash.should have_key('blog')
0
+ hash['blog'].should have_key('logo')
0
+ file = hash['blog']['logo']
0
+ file.original_filename.should == 'logo.png'
0
+ file.content_type.should == 'image/png'
0
+ it "should handle file from xml with defaults (ActiveSupport Compatible)" do
0
+ file = Hash.from_xml(blog_xml)['blog']['logo']
0
+ file.original_filename.should == 'untitled'
0
+ file.content_type.should == 'application/octet-stream'
0
+ it "should handle xsd like types from xml (ActiveSupport Compatible)" do
0
+ <weight type="double">0.5</weight>
0
+ <price type="decimal">12.50</price>
0
+ <chunky type="boolean"> 1 </chunky>
0
+ <expires-at type="dateTime">2007-12-25T12:34:56+0000</expires-at>
0
+ <notes type="string"></notes>
0
+ <illustration type="base64Binary">YmFiZS5wbmc=</illustration>
0
+ expected_bacon_hash = {
0
+ 'price' => BigDecimal("12.50"),
0
+ 'expires_at' => Time.utc(2007,12,25,12,34,56),
0
+ 'illustration' => "babe.png"
0
+ Hash.from_xml(bacon_xml)["bacon"].should == expected_bacon_hash
0
+ it "should let type trickle through when unknown (ActiveSupport Compatible)" do
0
+ <weight type="double">0.5</weight>
0
+ <image type="ProductImage"><filename>image.gif</filename></image>
0
+ expected_product_hash = {
0
+ 'image' => {'type' => 'ProductImage', 'filename' => 'image.gif' },
0
+ Hash.from_xml(product_xml)["product"].should == expected_product_hash
0
+ it "should handle unescaping from xml (ActiveResource Compatible)" do
0
+ xml_string = '<person><bare-string>First & Last Name</bare-string><pre-escaped-string>First &amp; Last Name</pre-escaped-string></person>'
0
+ 'bare_string' => 'First & Last Name',
0
+ 'pre_escaped_string' => 'First & Last Name'
0
+ Hash.from_xml(xml_string)['person'].should == expected_hash
0
describe Hash, 'to_params' do
Comments
No one has commented yet.