require 'test_helper' class XmlTest < Test::Unit::TestCase should "should transform a simple tag with content" do xml = "This is the contents" Crack::XML.parse(xml).should == { 'tag' => 'This is the contents' } end should "should work with cdata tags" do xml = <<-END END Crack::XML.parse(xml)["tag"].strip.should == "text inside cdata" end should "should transform a simple tag with attributes" do xml = "" hash = { 'tag' => { 'attr1' => '1', 'attr2' => '2' } } Crack::XML.parse(xml).should == hash end should "should transform repeating siblings into an array" do xml =<<-XML XML Crack::XML.parse(xml)['opt']['user'].class.should == Array hash = { 'opt' => { 'user' => [{ 'login' => 'grep', 'fullname' => 'Gary R Epstein' },{ 'login' => 'stty', 'fullname' => 'Simon T Tyson' }] } } Crack::XML.parse(xml).should == hash end should "should not transform non-repeating siblings into an array" do xml =<<-XML XML Crack::XML.parse(xml)['opt']['user'].class.should == Hash hash = { 'opt' => { 'user' => { 'login' => 'grep', 'fullname' => 'Gary R Epstein' } } } Crack::XML.parse(xml).should == hash end should "should typecast an integer" do xml = "10" Crack::XML.parse(xml)['tag'].should == 10 end should "should typecast a true boolean" do xml = "true" Crack::XML.parse(xml)['tag'].should be(true) end should "should typecast a false boolean" do ["false"].each do |w| Crack::XML.parse("#{w}")['tag'].should be(false) end end should "should typecast a datetime" do xml = "2007-12-31 10:32" Crack::XML.parse(xml)['tag'].should == Time.parse( '2007-12-31 10:32' ).utc end should "should typecast a date" do xml = "2007-12-31" Crack::XML.parse(xml)['tag'].should == Date.parse('2007-12-31') end should "should unescape html entities" do values = { "<" => "<", ">" => ">", '"' => """, "'" => "'", "&" => "&" } values.each do |k,v| xml = "Some content #{v}" Crack::XML.parse(xml)['tag'].should =~ Regexp.new(k) end end should "should undasherize keys as tags" do xml = "Stuff" Crack::XML.parse(xml).keys.should include( 'tag_1' ) end should "should undasherize keys as attributes" do xml = "" Crack::XML.parse(xml)['tag1'].keys.should include( 'attr_1') end should "should undasherize keys as tags and attributes" do xml = "" Crack::XML.parse(xml).keys.should include( 'tag_1' ) Crack::XML.parse(xml)['tag_1'].keys.should include( 'attr_1') end should "should render nested content correctly" do xml = "Tag1 Content This is strong" Crack::XML.parse(xml)['root']['tag1'].should == "Tag1 Content This is strong" end should "should render nested content with splshould text nodes correctly" do xml = "Tag1 ContentStuff Hi There" Crack::XML.parse(xml)['root'].should == "Tag1 ContentStuff Hi There" end should "should ignore attributes when a child is a text node" do xml = "Stuff" Crack::XML.parse(xml).should == { "root" => "Stuff" } end should "should ignore attributes when any child is a text node" do xml = "Stuff in italics" Crack::XML.parse(xml).should == { "root" => "Stuff in italics" } end should "should correctly transform multiple children" do xml = <<-XML 35 Home Simpson 1988-01-01 2000-04-28 23:01 true XML hash = { "user" => { "gender" => "m", "age" => 35, "name" => "Home Simpson", "dob" => Date.parse('1988-01-01'), "joined_at" => Time.parse("2000-04-28 23:01"), "is_cool" => true } } Crack::XML.parse(xml).should == hash end should "should properly handle nil values (ActiveSupport Compatible)" do topic_xml = <<-EOT EOT expected_topic_hash = { 'title' => nil, 'id' => nil, 'approved' => nil, 'written_on' => nil, 'viewed_at' => nil, 'content' => nil, 'parent_id' => nil } Crack::XML.parse(topic_xml)["topic"].should == expected_topic_hash end should "should handle a single record from xml (ActiveSupport Compatible)" do topic_xml = <<-EOT The First Topic David 1 true 0 2592000000 2003-07-16 2003-07-16T09:28:00+0000 --- \n1: should be an integer\n:message: Have a nice day\narray: \n- should-have-dashes: true\n should_have_underscores: true\n david@loudthinking.com 1.5 135 yes 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), # Changed this line where the key is :message. The yaml specifies this as a symbol, and who am I to change what you specify # The line in ActiveSupport is # 'content' => { 'message' => "Have a nice day", 1 => "should be an integer", "array" => [{ "should-have-dashes" => true, "should_have_underscores" => true }] }, 'content' => { :message => "Have a nice day", 1 => "should be an integer", "array" => [{ "should-have-dashes" => true, "should_have_underscores" => true }] }, 'author_email_address' => "david@loudthinking.com", 'parent_id' => nil, 'ad_revenue' => BigDecimal("1.50"), 'optimum_viewing_angle' => 135.0, 'resident' => :yes } Crack::XML.parse(topic_xml)["topic"].each do |k,v| v.should == expected_topic_hash[k] end end should "should handle multiple records (ActiveSupport Compatible)" do topics_xml = <<-EOT The First Topic David 1 false 0 2592000000 2003-07-16 2003-07-16T09:28:00+0000 Have a nice day david@loudthinking.com The Second Topic Jason 1 false 0 2592000000 2003-07-16 2003-07-16T09:28:00+0000 Have a nice day david@loudthinking.com 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 } # puts Crack::XML.parse(topics_xml)['topics'].first.inspect Crack::XML.parse(topics_xml)["topics"].first.each do |k,v| v.should == expected_topic_hash[k] end end should "should handle a single record from_xml with attributes other than type (ActiveSupport Compatible)" do topic_xml = <<-EOT EOT expected_topic_hash = { 'id' => "175756086", 'owner' => "55569174@N00", 'secret' => "0279bf37a1", 'server' => "76", 'title' => "Colored Pencil PhotoBooth Fun", 'ispublic' => "1", 'isfriend' => "0", 'isfamily' => "0", } Crack::XML.parse(topic_xml)["rsp"]["photos"]["photo"].each do |k,v| v.should == expected_topic_hash[k] end end should "should handle an emtpy array (ActiveSupport Compatible)" do blog_xml = <<-XML XML expected_blog_hash = {"blog" => {"posts" => []}} Crack::XML.parse(blog_xml).should == expected_blog_hash end should "should handle empty array with whitespace from xml (ActiveSupport Compatible)" do blog_xml = <<-XML XML expected_blog_hash = {"blog" => {"posts" => []}} Crack::XML.parse(blog_xml).should == expected_blog_hash end should "should handle array with one entry from_xml (ActiveSupport Compatible)" do blog_xml = <<-XML a post XML expected_blog_hash = {"blog" => {"posts" => ["a post"]}} Crack::XML.parse(blog_xml).should == expected_blog_hash end should "should handle array with multiple entries from xml (ActiveSupport Compatible)" do blog_xml = <<-XML a post another post XML expected_blog_hash = {"blog" => {"posts" => ["a post", "another post"]}} Crack::XML.parse(blog_xml).should == expected_blog_hash end should "should handle file types (ActiveSupport Compatible)" do blog_xml = <<-XML XML hash = Crack::XML.parse(blog_xml) hash.keys.should include('blog') hash['blog'].keys.should include('logo') file = hash['blog']['logo'] file.original_filename.should == 'logo.png' file.content_type.should == 'image/png' end should "should handle file from xml with defaults (ActiveSupport Compatible)" do blog_xml = <<-XML XML file = Crack::XML.parse(blog_xml)['blog']['logo'] file.original_filename.should == 'untitled' file.content_type.should == 'application/octet-stream' end should "should handle xsd like types from xml (ActiveSupport Compatible)" do bacon_xml = <<-EOT 0.5 12.50 1 2007-12-25T12:34:56+0000 YmFiZS5wbmc= 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" } Crack::XML.parse(bacon_xml)["bacon"].should == expected_bacon_hash end should "should let type trickle through when unknown (ActiveSupport Compatible)" do product_xml = <<-EOT 0.5 image.gif EOT expected_product_hash = { 'weight' => 0.5, 'image' => {'type' => 'ProductImage', 'filename' => 'image.gif' }, } Crack::XML.parse(product_xml)["product"].should == expected_product_hash end should "should handle unescaping from xml (ActiveResource Compatible)" do xml_string = 'First & Last NameFirst &amp; Last Name' expected_hash = { 'bare_string' => 'First & Last Name', 'pre_escaped_string' => 'First & Last Name' } Crack::XML.parse(xml_string)['person'].should == expected_hash end should "handle an empty xml string" do Crack::XML.parse('').should == {} end # As returned in the response body by the unfuddle XML API when creating objects should "handle an xml string containing a single space" do Crack::XML.parse(' ').should == {} end end