require File.expand_path(File.dirname(__FILE__) + "/../spec_helper")
describe Braintree::Xml do
describe "self.hash_from_xml" do
it "typecasts integers" do
hash = Braintree::Xml.hash_from_xml("123")
hash.should == {:root => {:foo => 123}}
end
it "works with dashes or underscores" do
xml = <<-END
END
hash = Braintree::Xml.hash_from_xml(xml)
hash.should == {:root => {:dash_es => "", :under_scores => ""}}
end
it "uses nil if nil=true, otherwise uses empty string" do
xml = <<-END
END
hash = Braintree::Xml.hash_from_xml(xml)
hash.should == {:root => {:a_nil_value => nil, :an_empty_string => ""}}
end
it "typecasts dates and times" do
hash = Braintree::Xml.hash_from_xml <<-END
2009-10-28T10:19:49Z
END
hash.should == {:root => {:created_at => Time.utc(2009, 10, 28, 10, 19, 49)}}
end
it "builds an array if type=array" do
hash = Braintree::Xml.hash_from_xml <<-END
Adam
Ben
END
hash.should == {:root => {:customers => [{:name => "Adam"}, {:name => "Ben"}]}}
end
it "turns 1 and true to boolean if type = boolean" do
hash = Braintree::Xml.hash_from_xml <<-END
true
1
false
anything
true
END
hash.should == {:root => {
:casted_true => true, :casted_one => true, :casted_anything => false, :casted_false => false,
:uncasted_true => "true"
}}
end
it "handles values that are arrays of hashes" do
hash = Braintree::Xml.hash_from_xml("
one
two
three
")
hash.should == {:container => {:elem => [{:value => "one"}, {:value => "two"}, {:value => "three"}]}}
end
end
describe "self.hash_to_xml" do
def verify_to_xml_and_back(hash)
Braintree::Xml.hash_from_xml(Braintree::Xml.hash_to_xml(hash)).should == hash
end
it "works for a simple case" do
hash = {:root => {:foo => "foo_value", :bar => "bar_value"}}
verify_to_xml_and_back hash
end
it "works for arrays" do
hash = {:root => {:items => [{:name => "first"}, {:name => "second"}]}}
verify_to_xml_and_back hash
end
it "works for arrays of strings" do
hash = {:root => {:items => ["first", "second"]}}
verify_to_xml_and_back hash
end
context "Integer" do
it "works for integers" do
hash = { :root => {:foo => 1 } }
Braintree::Xml.hash_to_xml(hash).should include("1")
end
end
context "BigDecimal" do
it "works for BigDecimals" do
hash = {:root => {:foo => BigDecimal("123.45")}}
Braintree::Xml.hash_to_xml(hash).should include("123.45")
end
it "works for BigDecimals with fewer than 2 digits" do
hash = {:root => {:foo => BigDecimal("1000.0")}}
Braintree::Xml.hash_to_xml(hash).should include("1000.00")
end
it "works for BigDecimals with more than 2 digits" do
hash = {:root => {:foo => BigDecimal("12.345")}}
Braintree::Xml.hash_to_xml(hash).should include("12.345")
end
end
it "works for symbols" do
hash = {:root => {:foo => :bar}}
Braintree::Xml.hash_to_xml(hash).should include("bar")
end
it "type casts booleans" do
hash = {:root => {:string_true => "true", :bool_true => true, :bool_false => false, :string_false => "false"}}
verify_to_xml_and_back hash
end
it "type casts time" do
hash = {:root => {:a_time => Time.utc(2009, 10, 28, 1, 2, 3), :a_string_that_looks_like_time => "2009-10-28T10:19:49Z"}}
verify_to_xml_and_back hash
end
it "can distinguish nil from empty string" do
hash = {:root => {:an_empty_string => "", :a_nil_value => nil}}
verify_to_xml_and_back hash
end
it "includes the encoding" do
xml = Braintree::Xml.hash_to_xml(:root => {:root => "bar"})
xml.should include("")
end
it "works for only a root node and a string" do
hash = {:id => "123"}
verify_to_xml_and_back hash
end
it "escapes keys and values" do
hash = { "ke "val>ue" }
Braintree::Xml.hash_to_xml(hash).should include("val>ue")
end
it "escapes nested keys and values" do
hash = { "top<" => { "ke "val>ue" } }
Braintree::Xml.hash_to_xml(hash).gsub(/\s/, '').should include("val>ue")
end
end
end