require 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 "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
end
end