require File.expand_path(File.dirname(__FILE__) + "/../../spec_helper") describe Braintree::Xml::Parser do describe "self.hash_from_xml" do it "typecasts integers" do xml = "123" xml.should parse_to(:root => {:foo => 123}) end it "works with dashes or underscores" do xml = <<-END END xml.should parse_to(:root=>{:dash_es=>"", :under_scores=>""}) end it "uses nil if nil=true, otherwise uses empty string" do xml = <<-END END xml.should parse_to(:root => {:a_nil_value => nil, :an_empty_string => ""}) end it "typecasts datetimes" do xml = <<-END 2009-10-28T10:19:49Z END xml.should parse_to(:root => {:created_at => Time.utc(2009, 10, 28, 10, 19, 49)}) end it "doesn't typecast dates" do xml = <<-END 2009-10-28 END xml.should parse_to(:root => {:created_at => "2009-10-28"}) end it "builds an array if type=array" do xml = <<-END Adam Ben END xml.should parse_to(:root => {:customers => [{:name => "Adam"}, {:name => "Ben"}]}) end it "parses using libxml when not using ruby 2.0" do xml = "123" stub_const("RUBY_VERSION", "2.1.1") ::Braintree::Xml::Libxml.should_receive(:parse).and_call_original Braintree::Xml::Parser.hash_from_xml(xml) end it "parses using rexml when using ruby 2.0 to avoid Libxml segfault" do segfault_prone_library_in_ruby_2_0 = ::Braintree::Xml::Libxml xml = "123" stub_const("RUBY_VERSION", "2.0.0") ::Braintree::Xml::Rexml.should_receive(:parse).and_call_original segfault_prone_library_in_ruby_2_0.should_not_receive(:parse) Braintree::Xml::Parser.hash_from_xml(xml) end end end