require "./spec/spec_helper" describe "Entity descriptor" do before :all do @subject = CDM::EntityDescriptor @dummy = @subject.new(:lorem_ipsum) end it "exists" do @subject.should be end it "has the name 'lorem_ipsum'" do @dummy.name.should eq "lorem_ipsum" end it "is syncable" do @dummy.syncable.should eq "YES" end describe "when initialized with options" do before :each do @optioned = @subject.new(:optioned, syncable: "NO") end it "has the name 'optioned'" do @optioned.name.should eq "optioned" end it "is not syncable" do @optioned.syncable.should eq "NO" end end describe ".binary" do before :each do @dummy.fields.clear end it "has an empty set of fields" do @dummy.fields.length.should eq 0 end context "adding a binary type field" do before do @dummy.binary :binary end it "adds an element to the fields collection" do @dummy.fields.length.should eq 1 end it "adds a string field to the descriptor" do @dummy.fields.first.type.should eq "Binary" end it "adds a field with the name 'binary'" do @dummy.fields.first.name.should eq "binary" end end end describe ".int16" do before :each do @dummy.fields.clear end context "adding an 'Integer 16' type field" do before :each do @dummy.int16 :age end it "adds an 'Integer 16' field to the descriptor" do @dummy.fields.first.type.should eq "Integer 16" end it "adds a field with the name 'age'" do @dummy.fields.first.name.should eq "age" end it "adds a field with a defaultValueString equal to '0'" do @dummy.fields.first.defaultValueString.should eq "0" end end end describe ".string" do before :each do @dummy.fields.clear end context "adding a string type field" do before :each do @dummy.string :lorem_ipsum end it "adds a string field to the descriptor" do @dummy.fields.first.type.should eq "String" end it "adds a field with the name 'lorem_ipsum'" do @dummy.fields.first.name.should eq "lorem_ipsum" end end end describe ".xml_attributes" do context "when initialized with no options" do before :each do @serializable = @subject.new(:serializable) end it "returns a hash with two elements" do @serializable.xml_attributes.keys.length.should eq 2 end it "returns a hash with the name key" do @serializable.xml_attributes[:name].should eq "serializable" end it "returns a hash with the syncable key pointing to the 'YES' string" do @serializable.xml_attributes[:syncable].should eq "YES" end end describe "when initialized with a few options" do before :each do @serializable = @subject.new(:serializable, syncable: "NO") end it "returns a hash with two elements" do @serializable.xml_attributes.keys.length.should eq 2 end it "returns a hash with the name key" do @serializable.xml_attributes[:name].should eq "serializable" end it "returns a hash with the syncable key pointing to the 'YES' string" do @serializable.xml_attributes[:syncable].should eq "NO" end end end describe ".xml_attributes_as_string" do before :each do @serializable = @subject.new(:serializable) end it "returns a string with two xml node attributes" do expected = 'name="serializable" syncable="YES"' @serializable.xml_attributes_as_string.should eq(expected) end end describe ".to_xml" do context "when the entity descriptor has no fields" do before :each do @serializable = @subject.new(:serializable) end it "returns an xml snippet of two lines" do @serializable.to_xml.split("\n").length.should eq 2 end it "returns an expected first line" do expected = ' ' @serializable.to_xml.split("\n").first.should eq(expected) end it "returns an expected last line" do expected = ' ' @serializable.to_xml.split("\n").last.should eq(expected) end end context "when the entity descriptor has two fields" do before :each do @fielded = @subject.new(:fielded) @fielded.string :name @fielded.int32 :age end it "returns an xml snippet of four lines" do @fielded.to_xml.split("\n").length.should eq 4 end it "returns an expected line at index 1" do expected = ' ' @fielded.to_xml.split("\n")[1].should eq(expected) end it "returns an expected line at index 2" do expected = ' ' @fielded.to_xml.split("\n")[2].should eq(expected) end end end end