require File.join(File.dirname(__FILE__), "..", "helper")
class TestHashToXml < Test::Unit::TestCase
context "Calling hash_to_xml" 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 Hash consisting of a single key-value-pair" do
should "return an XML String containing one node and a value" do
hash = { "apricot" => "eats gorilla" }
expected = "eats gorilla"
result = ApricotEatsGorilla.hash_to_xml(hash)
assert_equal expected, result
end
end
context "with a Hash containing another Hash" do
should "return an XML String representing the given structure" do
hash = { "apricot" => { "eats" => "gorilla", "drinks" => "beer" } }
expected = "beergorilla"
result = ApricotEatsGorilla.hash_to_xml(hash)
assert_equal expected, result
end
end
context "with a Hash containing a Hash containing an Array" do
should "return an XML String representing the given structure" do
hash = { "apricot" => { "eats" => [ "gorilla", "snake" ] } }
expected = "gorillasnake"
result = ApricotEatsGorilla.hash_to_xml(hash)
assert_equal expected, result
end
end
context "with a Hash containing a Hash containing an Array containing a Hash" do
should "return an XML String representing the given structure" do
hash = { "apricot" =>
{ "eats" => [ { "lotsOf" => "gorillas" }, { "justSome" => "snakes" } ]
} }
expected = "gorillas" <<
"snakes"
result = ApricotEatsGorilla.hash_to_xml(hash)
assert_equal expected, result
end
end
context "with a Hash containing Symbols" do
should "returns an XML String with Symbols converted into Strings" do
hash = { :apricot => { :eats => [ :gorilla, "snake" ] } }
expected = "gorillasnake"
result = ApricotEatsGorilla.hash_to_xml(hash)
assert_equal expected, result
end
end
context "with a Hash containing snake_case keys" do
should "convert snake_case Hash keys to lowerCamelCase" do
hash = { :apricot => { :eats => { :lots_of => "gorillas" } } }
expected = "gorillas"
result = ApricotEatsGorilla.hash_to_xml(hash)
assert_equal expected, result
end
context "and converting snake_case tag names to lowerCamelCase turned off" do
setup { ApricotEatsGorilla.disable_tag_names_to_lower_camel_case = true }
should "not convert snake_case tag names to lowerCamelCase" do
hash = { :apricot => { :eats => { :lots_of => "gorillas" } } }
expected = "gorillas"
result = ApricotEatsGorilla.hash_to_xml(hash)
assert_equal expected, result
end
end
end
context "with a Hash different types of values" do
should "convert values responding to 'to_s' to Strings" do
date_time = DateTime.now
hash = { :apricot => {
:at => date_time,
:with => 100.01,
:when => nil,
:what => :gorillas
} }
expected = "#{date_time}gorillas" <<
"100.01"
result = ApricotEatsGorilla.hash_to_xml(hash)
assert_equal expected, result
end
end
end
end