require File.join(File.dirname(__FILE__), "..", "spec_helper") describe ApricotEatsGorilla do include SpecHelper describe "xml_to_hash" do before { reset_library_options } it "converts Hash keys to Symbols" do xml = "Jungle Julia" ApricotEatsGorilla.xml_to_hash(xml).should == { :name => "Jungle Julia" } end it "does not convert Hash keys to Symbols if this was disabled" do ApricotEatsGorilla.disable_hash_keys_to_symbols = true xml = "Jungle Julia" ApricotEatsGorilla.xml_to_hash(xml).should == { "name" => "Jungle Julia" } end it "converts String values of 'true' and 'false' into TrueClass and FalseClass" do xml = "truefalse" ApricotEatsGorilla.xml_to_hash(xml).should == { :yes => true, :no => false } end it "converts String values matching the SOAP dateTime format to DateTime objects" do date = "2009-08-05T11:22:08" date_with_offset = "2009-08-05T11:22:08+02:00" xml = "#{date}#{date_with_offset}" ApricotEatsGorilla.xml_to_hash(xml).should == { :date => DateTime.parse(date), :date_with_offset => DateTime.parse(date_with_offset) } end it "converts empty-element tags to 'nil'" do xml = "Jungle Julia" ApricotEatsGorilla.xml_to_hash(xml).should == { :name => "Jungle Julia", :email => nil } end it "converts lowerCamelCase nodes to snake_case" do xml = "JungleJulia" ApricotEatsGorilla.xml_to_hash(xml).should == { :first_name => "Jungle", :last_name => "Julia" } end it "does not convert lowerCamelCase nodes to snake_case if this was disabled" do ApricotEatsGorilla.disable_hash_keys_to_snake_case = true xml = "JungleJulia" ApricotEatsGorilla.xml_to_hash(xml).should == { :firstName => "Jungle", :lastName => "Julia" } end it "removes namespaces from nodes" do xml = "Gorilla" ApricotEatsGorilla.xml_to_hash(xml).should == { :apricot => { :eats => "Gorilla" } } end it "removes attributes from nodes" do xml = 'Jungle Julia' ApricotEatsGorilla.xml_to_hash(xml).should == { :contact => "Jungle Julia" } end it "starts translating XML at a given custom root node" do xml = 'Jungle Julia' ApricotEatsGorilla.xml_to_hash(xml, "//contact").should == { :name => "Jungle Julia" } end it "returns an Array of nodes and values in case of multiple root nodes" do xml = '12' ApricotEatsGorilla.xml_to_hash(xml, "//return").should == [{ :id => "1" }, { :id => "2" }] end it "returns the value of a node if it does not contain any subnodes" do xml = '123' ApricotEatsGorilla.xml_to_hash(xml, "//return").should == "123" end it "returns an empty Hash in case the root node is an empty-element tag" do xml = '' ApricotEatsGorilla.xml_to_hash(xml, "//return").should == {} end it "returns an Array of values in case of multiple root nodes without subnodes" do xml = '123456' ApricotEatsGorilla.xml_to_hash(xml, "//return").should == ["123", "456"] end it "returns a Hash containing an Array of content for multiple subnodes" do xml = 'firstsecond' ApricotEatsGorilla.xml_to_hash(xml, "//return").should == { :items => ["first", "second"] } end end end