Exemple #1
0
        public void Derived()
        {
            //-------------------------------------------------------------
            // Verify that [BaseModel] works.

            var baseModel = new BaseModel();

            Assert.Null(baseModel.ParentProperty);

            baseModel.ParentProperty = "Hello World!";
            Assert.Equal("Hello World!", baseModel.ParentProperty);

            baseModel = BaseModel.CreateFrom(baseModel.ToString());
            Assert.Equal("Hello World!", baseModel.ParentProperty);

            //-------------------------------------------------------------
            // Verify that [DerivedModel] works too.

            var derivedModel = new DerivedModel();

            Assert.Null(derivedModel.ParentProperty);
            Assert.Null(derivedModel.ChildProperty);

            derivedModel.ParentProperty = "parent";
            derivedModel.ChildProperty  = "child";

            derivedModel = DerivedModel.CreateFrom(derivedModel.ToString());

            Assert.Equal("parent", derivedModel.ParentProperty);
            Assert.Equal("child", derivedModel.ChildProperty);

            //-------------------------------------------------------------
            // Verify Equals():

            var value1 = new DerivedModel()
            {
                ParentProperty = "parent", ChildProperty = "child"
            };
            var value2 = new DerivedModel()
            {
                ParentProperty = "parent", ChildProperty = "child"
            };

            Assert.True(value1.Equals(value1));
            Assert.True(value1.Equals(value2));
            Assert.True(value2.Equals(value1));

            Assert.False(value1.Equals(null));
            Assert.False(value1.Equals("Hello World!"));

            // Verify that a change to the parent class property is detected.

            value1.ParentProperty = "DIFFERENT";

            Assert.True(value1.Equals(value1));

            Assert.False(value1.Equals(value2));
            Assert.False(value2.Equals(value1));
            Assert.False(value1.Equals(null));
            Assert.False(value1.Equals("Hello World!"));

            // Verify that a change to the derived class property is detected.

            value1.ParentProperty = "parent";
            value1.ChildProperty  = "DIFFERENT";

            Assert.True(value1.Equals(value1));

            Assert.False(value1.Equals(value2));
            Assert.False(value2.Equals(value1));
            Assert.False(value1.Equals(null));
            Assert.False(value1.Equals("Hello World!"));

            //-------------------------------------------------------------
            // Verify that we can use [ToDerived<TResult>()] to create a derived instance
            // from the base type.  This also exercises [RoundtripDataFactory] a bit.

            derivedModel = new DerivedModel()
            {
                ParentProperty = "parent", ChildProperty = "child"
            };

            baseModel = BaseModel.CreateFrom(derivedModel.ToString());

            Assert.Equal("parent", baseModel.ParentProperty);

            derivedModel = baseModel.ToDerived <DerivedModel>();

            Assert.Equal("parent", derivedModel.ParentProperty);
            Assert.Equal("child", derivedModel.ChildProperty);
        }