require 'spec_helper' require 'active_support/core_ext/string/conversions' # for String#to_time conversion describe "element values" do before(:all) do class ElementValueTerminology include OM::XML::Document set_terminology do |t| t.root(:path => "outer", :xmlns => nil) t.my_date(:type=>:date) t.my_time(:type=>:time) t.my_int(:type=>:integer) t.active(:type=>:boolean) t.wrapper do t.inner_date(:type=>:date) end end end end describe "when the xml template has existing values" do let(:datastream) do ElementValueTerminology.from_xml <<-EOF 2012-10-30 2012-10-30T12:22:33Z 7 true EOF end subject { datastream } describe "reading values" do it "should deserialize date" do subject.my_date.should == [Date.parse('2012-10-30')] end it "should deserialize time" do subject.my_time.should == [DateTime.parse('2012-10-30T12:22:33Z')] end it "should deserialize ints" do subject.my_int.should == [7] end it "should deserialize boolean" do subject.active.should == [true] end end describe "Writing to xml" do context "serializing time" do context "with a valid time" do subject { datastream.to_xml } before { datastream.my_time = [DateTime.parse('2011-01-30T03:45:15Z')] } it { should be_equivalent_to ' 2012-10-30 2011-01-30T03:45:15Z 7 true ' } end context "setting an invalid time" do it "raises a type mismatch error" do expect { datastream.my_time = '' }.to raise_error OM::TypeMismatch end end end context "serializing dates" do subject { datastream.to_xml } context "with a valid date" do before { datastream.my_date = [Date.parse('2012-09-22')] } it { should be_equivalent_to ' 2012-09-22 2012-10-30T12:22:33Z 7 true ' } end end it "should serialize ints" do subject.my_int = [9] subject.to_xml.should be_equivalent_to ' 2012-10-30 2012-10-30T12:22:33Z 9 true ' end it "should serialize boolean" do subject.active = [false] subject.to_xml.should be_equivalent_to ' 2012-10-30 2012-10-30T12:22:33Z 7 false ' end end end describe "when the xml template is empty" do subject do ElementValueTerminology.from_xml <<-EOF EOF end describe "reading values" do it "should deserialize date" do subject.my_date.should == [nil] end it "should deserialize ints" do subject.my_int.should == [nil] end it "should deserialize bools" do subject.active.should == [false] end end describe "Writing to xml" do it "should serialize date" do subject.my_date = [Date.parse('2012-09-22')] subject.to_xml.should be_equivalent_to ' 2012-09-22 ' end it "should serialize ints" do subject.my_int = [9] subject.to_xml.should be_equivalent_to ' 9 ' end it "should serialize booleans" do subject.active = [true] subject.to_xml.should be_equivalent_to ' true ' end it "should serialize empty string values" do subject.my_int = [nil] subject.my_date = [nil] subject.active = [nil] subject.to_xml.should be_equivalent_to ' ' end end end end