require File.expand_path(File.dirname(__FILE__) + "/../../spec_helper")
describe Cardflex::Xml::Serializer do
describe 'hash to xml' do
it 'should raise an argument error if no root' do
expect do
Cardflex::Xml::Serializer.hash_to_xml({})
end.to raise_error ArgumentError
end
it 'should turn a simple string hash into xml' do
hash = { :root => 'text' }
expect(Cardflex::Xml::Serializer.hash_to_xml(hash)).to match 'text'
end
it 'should escape strings to xml' do
expect(Cardflex::Xml::Serializer._xml_escape('snake_case')).to eq 'snake-case'
expect(Cardflex::Xml::Serializer._xml_escape(:snake_case)).to eq 'snake-case'
expect(Cardflex::Xml::Serializer._xml_escape(' ')).to eq ' '
end
it 'should convert nested hashes to xml' do
simple_hash = { :root => { :nested => 'nested' }}
simple_xml = <<-END
nested
END
complex_hash = { :root => { :L1 => { :L2A => { :key => 'value' }, :L2B => { :key => 'value' }}}}
complex_xml = <<-END
value
value
END
expect(Cardflex::Xml::Serializer.hash_to_xml(simple_hash)).to match simple_xml
expect(Cardflex::Xml::Serializer.hash_to_xml(complex_hash)).to match complex_xml
end
it 'should convert symbols to strings' do
hash = { :root => { :nested => :value }}
xml = <<-END
value
END
expect(Cardflex::Xml::Serializer.hash_to_xml(hash)).to match xml
end
it 'should format numbers with decimals to 2 decimal places' do
num1_xml = <<-END
1.00
END
num2_xml = <<-END
1
END
expect(Cardflex::Xml::Serializer.hash_to_xml(:root => { :num => 1.0 })).to match num1_xml
expect(Cardflex::Xml::Serializer.hash_to_xml(:root => { :num => 1 })).to match num2_xml
end
end
describe 'xml to hash' do
it 'should return an empty has for a nil string' do
expect(Cardflex::Xml::Parser.xml_to_hash(nil)).to eq({})
end
it 'should parse simple xml' do
xml = 'nested'
expect(Cardflex::Xml::Parser.xml_to_hash(xml)).to eq({ :root => 'nested' })
end
it 'should parse complex xml' do
xml = 'valuevaluevalue'
hash = { :root => { :L1 => { :L2 => { :L3A => 'value', :L3B => 'value' }}}}
expect(Cardflex::Xml::Parser.xml_to_hash(xml)).to eq(hash)
end
end
end