require File.join(File.dirname(__FILE__), "..", "helper")
class XmlToHashTest < Test::Unit::TestCase
context "Calling xml_to_hash" do
setup do
ApricotEatsGorilla.setup do |s|
s.sort_keys = true
s.disable_tag_names_to_lower_camel_case = false
s.disable_hash_keys_to_snake_case = false
s.disable_hash_keys_to_symbol = false
end
end
context "with a SOAP response example and a custom root node" do
should "return a Hash containing the XML content" do
xml = '
secret
example
'
expected = { :auth_value => { :token => "secret", :client => "example" } }
result = ApricotEatsGorilla.xml_to_hash(xml, "//return")
assert_equal expected, result
end
end
context "with XML containing 'true' and 'false' Strings" do
should "convert these Strings into actual Boolean objects" do
xml = "truefalsesomething"
expected = { :yes => true, :no => false, :text => "something" }
result = ApricotEatsGorilla.xml_to_hash(xml)
assert_equal expected, result
end
end
context "with XML containing empty element tags" do
should "convert empty element tags to nil" do
xml = "Jungle Julia"
expected = { :name => "Jungle Julia", :email => nil, :phone => nil }
result = ApricotEatsGorilla.xml_to_hash(xml)
assert_equal expected, result
end
end
context "with XML containing nodes with attributes" do
should "return a Hash without tag attributes" do
xml = 'black'
expected = { :paint => "black" }
result = ApricotEatsGorilla.xml_to_hash(xml)
assert_equal expected, result
end
end
context "with XML containing lowerCamelCase nodes" do
should "convert lowerCamelCase nodes to snake_case" do
xml = "JungleJulia"
expected = { :first_name => "Jungle", :last_name => "Julia" }
result = ApricotEatsGorilla.xml_to_hash(xml)
assert_equal expected, result
end
context "and converting lowerCamelCase Hash keys to snake_case turned off" do
setup { ApricotEatsGorilla.disable_hash_keys_to_snake_case = true }
should "not convert lowerCamelCase Hash keys to snake_case" do
xml = "JungleJulia"
expected = { :firstName => "Jungle", :lastName => "Julia" }
result = ApricotEatsGorilla.xml_to_hash(xml)
assert_equal expected, result
end
end
end
context "with some XML" do
should "convert Hash keys to Symbols" do
xml = "Jungle Julia"
expected = { :name => "Jungle Julia", :address => nil }
result = ApricotEatsGorilla.xml_to_hash(xml)
assert_equal expected, result
end
context "and converting Hash keys to Symbols turned off" do
setup { ApricotEatsGorilla.disable_hash_keys_to_symbol = true }
should "not convert Hash keys to Symbols" do
xml = "Jungle Julia"
expected = { "name" => "Jungle Julia", "address" => nil }
result = ApricotEatsGorilla.xml_to_hash(xml)
assert_equal expected, result
end
end
end
end
end