# -*- coding: utf-8 -*- require 'test/unit' require File.join(File.dirname(__FILE__), "..", "lib", "apricoteatsgorilla") class ApricotEatsGorillaTest < Test::Unit::TestCase def setup ApricotEatsGorilla.sort_keys = true end # xml_to_hash def test_xml_to_hash_with_soap_response_example xml = ' secret example ' expected = { 'authValue' => { 'token' => 'secret', 'client' => 'example' } } result = ApricotEatsGorilla.xml_to_hash(xml, '//return') assert_equal expected, result end def test_xml_to_hash_make_sure_boolean_values_get_converted_to_boolean_objects xml = 'truefalsesomething' expected = { 'yes' => true, 'no' => false, 'txt' => 'something' } result = ApricotEatsGorilla.xml_to_hash(xml) assert_equal expected, result end def test_xml_to_hash_make_sure_empty_element_tags_get_converted_to_nil xml = 'Jungle Julia' expected = { 'name' => 'Jungle Julia', 'email' => nil, 'phone' => nil } result = ApricotEatsGorilla.xml_to_hash(xml) assert_equal expected, result end def test_xml_to_hash_make_sure_node_attributes_get_removed xml = 'black' expected = { 'paint' => 'black' } result = ApricotEatsGorilla.xml_to_hash(xml) assert_equal expected, result end def test_xml_to_hash_make_sure_node_groups_with_text_values_get_converted_to_arrays xml = 'firstsecond' expected = { 'items' => [ 'first', 'second' ] } result = ApricotEatsGorilla.xml_to_hash(xml) assert_equal expected, result end def test_xml_to_hash_make_sure_node_groups_with_nesting_get_converted_to_arrays_with_hashes xml = 'firstsecond' expected = { 'items' => [ { 'name' => 'first' }, { 'name' => 'second' } ] } result = ApricotEatsGorilla.xml_to_hash(xml) assert_equal expected, result end # hash_to_xml def test_hash_to_xml_with_simple_hash hash = { "dude" => "likes beer" } expected = "likes beer" result = ApricotEatsGorilla.hash_to_xml(hash) assert_equal expected, result end def test_hash_to_xml_with_nested_hash hash = { "dude" => { "likes" => "beer", "hates" => "appletini" } } expected = "appletinibeer" result = ApricotEatsGorilla.hash_to_xml(hash) assert_equal expected, result end def test_hash_to_xml_with_nested_hash_and_array hash = { "dude" => { "likes" => [ "beer", "helicopters" ] } } expected = "beerhelicopters" result = ApricotEatsGorilla.hash_to_xml(hash) assert_equal expected, result end def test_hash_to_xml_with_nested_hash_and_array_containing_a_hash hash = { "dude" => { "likes" => [ { "beer" => "a lot" }, { "helicopters" => "a little more" } ] } } expected = "a lota little more" result = ApricotEatsGorilla.hash_to_xml(hash) assert_equal expected, result end # soap_envelope def test_soap_envelope_without_namespace_and_block_returns_pure_envelope expected = '' << '' result = ApricotEatsGorilla.soap_envelope assert_equal expected, result end def test_soap_envelope_with_namespace_and_block_returns_envelope_with_namespace_and_content expected = '' << '123' result = ApricotEatsGorilla.soap_envelope("wsdl" => "http://example.com") do "123" end assert_equal expected, result end end