require File.join(File.dirname(__FILE__), "..", "helper")
class TestXmlToHash < 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 = 'example'
expected = { :user => "example" }
result = ApricotEatsGorilla.xml_to_hash(xml)
assert_equal expected, result
end
end
context "with XML containing multiple custom root nodes" do
should "return an Array containing the values of each root node" do
xml = '123456'
expected = [{ :id => "123" }, { :id => "456" }]
result = ApricotEatsGorilla.xml_to_hash(xml, "//return")
assert_equal expected, result
end
end
context "with XML containing no subnodes at custom root node" do
should "return the plain content of the custom root node" do
xml = '123'
expected = "123"
result = ApricotEatsGorilla.xml_to_hash(xml, "//return")
assert_equal expected, result
end
end
context "with XML containing no subnodes in multiple custom root nodes" do
should "return an Array containing the values of each root node" do
xml = '123456'
expected = ["123", "456"]
result = ApricotEatsGorilla.xml_to_hash(xml, "//return")
assert_equal expected, result
end
end
context "with XML containing several subnodes with the same name and text content" do
should "return a Hash containing one key and an array of Strings" do
xml = '123456'
expected = { :items => ["123", "456"] }
result = ApricotEatsGorilla[xml, "//return"]
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