require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper.rb') describe "xml parser" do it "BaseHelper.xml_post_process_1_5 for root hash formatted xml" do expected_hash = { "farms"=>nil, "animals"=>[ {"species"=>"chicken", "type"=>'bird'}, {"species"=>"chicken", "type"=>'bird'}, {"species"=>"horse", "type"=>'mammal'}, {"species"=>"cow", "type"=>'mammal'}, {"species"=>"bull", "type"=>'mammal'}, {"species"=>"rooster", "type"=>'bird'}, {"species"=>"camel","type"=>'mammal'}, {"species"=>"sheep", "type"=>'mammal'}, {"species"=>"wolf", "type"=>'mammal'}, ] } session_index_1_5_xml = %@ @ RightDevelop::Parsers::SaxParser.parse(session_index_1_5_xml, :post_parser => lambda { |xml| RightDevelop::Parsers::XmlPostParser.remove_nesting(xml) } ).should == expected_hash end it "BaseHelper.xml_post_process_1_5 for root array formatted xml" do expected_array = [ { 'farms'=>[{'name'=>'chicken farm'}], 'farmers'=>[ {'name'=>'John', 'specialty'=>'harvesting'}, {'name'=>'Bill', 'specialty'=>'eggs'}, {'name'=>'Bob', 'specialty'=>'milk'}, {'name'=>'Joe', 'specialty'=>'butchering'} ], 'description'=>'animal farms', }, { 'farms'=>[{'name'=>'wheat farm'},{'name'=>'corn farm'}], 'farmers'=>[ {'name'=>'May', 'specialty'=>'milling'}, {'name'=>'Mike', 'specialty'=>'corn'}, {'name'=>'Will', 'specialty'=>'harvesting'}, {'name'=>'Moe', 'specialty'=>'sowing'} ], 'description'=>'vegetable farms', } ] servers_index_1_5_xml = %@ animal farms vegetable farms @ RightDevelop::Parsers::SaxParser.parse(servers_index_1_5_xml, :post_parser => lambda { |xml| RightDevelop::Parsers::XmlPostParser.remove_nesting(xml) } ).should == expected_array end it "BaseHelper.xml_post_process_1_5 for node with empty text string" do expected_array = [ { 'farms'=>[{'name'=>'server farm'}], 'description'=>'big time computing' }, { 'farms'=>[{'name'=>'unlisted farm'}], 'description'=>nil } ] servers_index_1_5_xml = %@ big time computing @ RightDevelop::Parsers::SaxParser.parse(servers_index_1_5_xml, :post_parser => lambda { |xml| RightDevelop::Parsers::XmlPostParser.remove_nesting(xml) } ).should == expected_array end it "BaseHelper.xml_post_process_1_5 custom multiple nested xml" do expected_array = [{ "parents"=>[{ "children"=> ["Bob", "Mike"] }, { "children"=> ["Bob", "Mike"] }, { "children"=> ["Bob", "Mike"] } ] }, { "parents"=>[{ "children"=> ["Bob", "Mike"] }, { "children"=> ["Bob", "Mike"] }, { "children"=> ["Bob", "Mike"] } ] } ] custom_nested_xml = %@ Bob Mike Bob Mike Bob Mike Bob Mike Bob Mike Bob Mike @ RightDevelop::Parsers::SaxParser.parse(custom_nested_xml, :post_parser => lambda { |xml| RightDevelop::Parsers::XmlPostParser.remove_nesting(xml) } ).should == expected_array end end