require 'helper' module RubyPsigate class TestSerializer < Test::Unit::TestCase def test_raises_error_if_input_is_not_a_hash my_array = ["hello", "world"] assert_raises ArgumentError do Serializer.new(my_array) end end def test_creates_basic_markup_from_hash expectation = "Hello World" hash = {:Something => "Hello World"} @result = Serializer.new(hash) assert_equal expectation, @result.to_xml end def test_creates_one_level_nested_markup_from_hash expectation = "BobJane" hash = {:Family => {:Dad => "Bob", :Mom => "Jane"}} @result = Serializer.new(hash) assert_equal expectation, @result.to_xml end def test_creates_two_level_nested_markup_from_hash expectation = "BobJaneMikeAnn" hash = { :Family => { :Dad => "Bob", :Mom => "Jane", :Children => { :Son => "Mike", :Daughter => "Ann" } } } @result = Serializer.new(hash) assert_equal expectation, @result.to_xml end def test_includes_header expectation = "Hello World" hash = { :Something => "Hello World" } @result = Serializer.new(hash, :header => true) assert_equal expectation, @result.to_xml end def test_creates_complicated_markup expectation = "teststoretest123410.00CC0411111111111111102153422" hash = { :Order => { :StoreID => "teststore", :Passphrase => "test1234", :Subtotal => "10.00", :PaymentType => "CC", :CardAction => "0", :CardNumber => "4111111111111111", :CardExpMonth => "02", :CardExpYear => "15", :CardIDNumber => "3422" } } @result = Serializer.new(hash) assert_equal expectation, @result.to_xml end def test_creates_markup_from_array_of_hash_under_same_parent_element expectation = "Mercedes Benz30000.00BMW25000.00" hash = { :Item => [ { :Name => "Mercedes Benz", :Price => "30000.00"}, { :Name => "BMW", :Price => "25000.00" } ] } @result = Serializer.new(hash) assert_equal expectation, @result.to_xml end def test_create_markup_from_an_array_of_hash_with_each_array_element_having_its_own_embedded_options expectation = "Mercedes Benz30000.00BMW25000.00" hash = { :Item => [ { :Name => "Mercedes Benz", :Price => "30000.00", :Option => { :model => "sports", :color => "black" } }, { :Name => "BMW", :Price => "25000.00", :Option => { :model => "luxury", :color => "red" } } ] } @result = Serializer.new(hash) assert_equal expectation, @result.to_xml end end end