require "./spec/spec_helper" describe "EntityDefinition" do before do @subject = CDM::EntityDefinition end it "exists" do @subject.should be end describe ".create_entity" do before do @dummy = @subject.new end context "when adding the :foo entity with no attributes" do before do @dummy.create_entity :foo do |e| end end it "has an entities collection of length 1" do @dummy.entities.length.should eq 1 end it "has an entity named 'foo'" do @dummy.entities.first.name.should eq "foo" end end context "when adding the :person entity with a few attributes" do before do @dummy.create_entity :person do |e| e.string :name, optional: "NO" e.int16 :age end end it "has an entities collection of length 1" do @dummy.entities.length.should eq 1 end it "has an entity named 'person'" do @dummy.entities.first.name.should eq "person" end it "has the 'name' attribute in the first entity" do @dummy.entities.first.fields.first.name.should eq "name" end end context "when adding three entities" do before do @dummy.create_entity :foo do |e| e.string :foo_attribute e.date :foo_date_attribute e.int16 :foo_int_attribute end @dummy.create_entity :bar do |e| e.binary :bar_binary_attribute e.boolean :bar_boolean_attribute e.float :bar_float_attribute end @dummy.create_entity :baz do |e| e.transformable :baz_transformable_attribute e.decimal :baz_decimal_attribute end end it "has 3 entities in the colection" do @dummy.entities.length.should eq 3 end it "has the foo entity in the first spot of the collection" do @dummy.entities.first.name.should eq "foo" end end end describe ".xml_attributes" do before do @dummy = @subject.new end it "returns 9 attributes" do @dummy.xml_attributes.keys.length.should eq 9 end context "default values" do before do @result = @dummy.xml_attributes end it "returns empty string for name" do @result[:name].should eq "" end it "returns empty string for userDefinedModelVersionIdentifier" do @result[:userDefinedModelVersionIdentifier].should eq "" end it "returns 'com.apple.IDECoreDataModeler.DataModel' for type" do @result[:type].should eq "com.apple.IDECoreDataModeler.DataModel" end it "returns '1.0' for documentVersion" do @result[:documentVersion].should eq "1.0" end it "returns '2061' for documentVersion" do @result[:lastSavedToolsVersion].should eq "2061" end it "returns '12D78 for systemVersion'" do @result[:systemVersion].should eq "12D78" end it "returns 'Automatic for minimumToolsVersion'" do @result[:minimumToolsVersion].should eq "Automatic" end it "returns 'Automatic for macOSVersion'" do @result[:macOSVersion].should eq "Automatic" end it "returns 'Automatic for iOSVersion'" do @result[:iOSVersion].should eq "Automatic" end end context "custom values" do before do @dummy.name = "Lorem Ipsum" @dummy.document_version = "2.4" @result = @dummy.xml_attributes end it "returns 'Lorem Ipsum' for name" do @result[:name].should eq "Lorem Ipsum" end it "returns '2.4' for documentVersion" do @result[:documentVersion].should eq "2.4" end end end describe ".to_xml" do before do class ModelDefinition < CDM::EntityDefinition def define_model create_entity :foo do |e| e.string :foo_string e.date :foo_date e.int16 :foo_int end create_entity :bar do |e| e.binary :bar_binary e.boolean :bar_boolean e.float :bar_float end create_entity :baz do |e| e.transformable :baz_transformable e.decimal :baz_decimal end end end @model_definition = ModelDefinition.new @model_definition.define_model @xml_array = @model_definition.to_xml.split("\n") end it "contains entity name 'foo' in the line at index 2" do @xml_array[2].should eq ' ' end end end