spec/lib/morph_spec.rb in morph-0.2.7 vs spec/lib/morph_spec.rb in morph-0.2.8
- old
+ new
@@ -483,6 +483,87 @@
xmlns: http://xmlgw.companieshouse.gov.uk/v1-0
xmlns_xsi: http://www.w3.org/2001/XMLSchema-instance
xsi_schema_location: xmlgwdev.companieshouse.gov.uk/v1-0/schema/CompanyDetails.xsd|
end
end
+
+ describe 'creating from xml' do
+
+ def check_councils councils, class_name
+ councils.class.should == Array
+ councils.size.should == 2
+ councils.first.class.name.should == class_name
+ councils.first.name.should == 'Aberdeen City Council'
+ councils.last.name.should == 'Allerdale Borough Council'
+ end
+
+ it 'should create classes and object instances' do
+ councils = Morph.from_xml(xml)
+ check_councils councils, 'Morph::Council'
+ end
+
+ describe 'when module name is supplied' do
+ it 'should create classes and object instances' do
+ Object.const_set 'Ppc', Module.new
+ councils = Morph.from_xml(xml, Ppc)
+ check_councils councils, 'Ppc::Council'
+ end
+ end
+
+ def xml
+%Q[<?xml version="1.0" encoding="UTF-8"?>
+<councils type="array">
+ <council>
+ <name>Aberdeen City Council</name>
+ </council>
+ <council>
+ <name>Allerdale Borough Council</name>
+ </council>
+</councils>]
+ end
+ end
+
+ describe 'creating from tsv (tab separated value)' do
+
+ def check_councillors councillors, class_name
+ councillors.class.should == Array
+ councillors.size.should == 2
+ councillor = councillors.first
+ councillor.class.name.should == class_name
+ councillor.name.should == 'Ted Roe'
+ councillor.party.should == 'labour'
+ councillor.councillors.should == 'Councillor for Stretford Ward'
+ councillor.councils.should == 'Trafford Council'
+ councillor.respond_to?(:council_experience).should be_false
+
+ councillor = councillors.last
+ councillor.name.should == 'Ali Davidson'
+ councillor.party.should == 'labour'
+ councillor.councillors.should == ''
+ councillor.councils.should == 'Basildon District Council'
+ councillor.respond_to?(:council_experience).should be_false
+ end
+
+ describe 'when class name is supplied' do
+ it 'should create classes and object instances' do
+ councillors = Morph.from_tsv(tsv, 'Councillor')
+ check_councillors councillors, 'Morph::Councillor'
+ end
+ end
+
+ describe 'when class name and module name is supplied' do
+ it 'should create classes and object instances' do
+ Object.const_set 'Ppc', Module.new
+ councillors = Morph.from_tsv(tsv, 'Councillor', Ppc)
+ check_councillors councillors, 'Ppc::Councillor'
+ end
+ end
+
+ def tsv
+%Q[name party councillors councils council_experience
+Ted Roe labour Councillor for Stretford Ward Trafford Council
+Ali Davidson labour Basildon District Council
+]
+ end
+ end
+
end