Esempio n. 1
0
        public void should_return_casted_result_if_it_is_castable()
        {
            var demoClass       = new PolymorphismDemoClass();
            var castToBaseClass = demoClass as PolymorphismDemoClassBase;

            bool isNull = castToBaseClass == null;

            // please change the variable value to fix the test.
            const bool expected = false;

            Assert.Equal(expected, isNull);
        }
Esempio n. 2
0
        public void should_reference_to_same_object_after_casting()
        {
            var demoClass       = new PolymorphismDemoClass();
            var castToBaseClass = (PolymorphismDemoClassBase)demoClass;

            bool referenceEqual = ReferenceEquals(demoClass, castToBaseClass);

            // please change the variable value to fix the test.
            const bool expected = true;

            Assert.Equal(expected, referenceEqual);
        }
Esempio n. 3
0
        [Fact]  /////public修饰的子类强转成父类,调用时是自己的方法;
        public void should_access_member_of_most_derived_class()
        {
            var demoClass       = new PolymorphismDemoClass();
            var castToBaseClass = (PolymorphismDemoClassBase)demoClass;

            string actualValue = castToBaseClass.VirtualMethod();

            // please change the variable value to fix the test.
            const string expected = "DerivedClass";

            Assert.Equal(expected, actualValue);
        }
Esempio n. 4
0
        public void should_throw_exception_if_it_is_not_castable()
        {
            var    demoClass    = new PolymorphismDemoClass();
            object castToObject = demoClass;

            // please change the variable value to fix the test.
            Type expectedExceptionType = typeof(InvalidCastException);

            Assert.NotEqual(typeof(SystemException), expectedExceptionType);
            Assert.NotEqual(typeof(Exception), expectedExceptionType);

            Assert.Throws(expectedExceptionType, () => (StringBuilder)castToObject);
        }
Esempio n. 5
0
        public void should_return_null_if_it_is_not_castable()
        {
            var    demoClass    = new PolymorphismDemoClass();
            object castToObject = demoClass;

            var  castResult = castToObject as StringBuilder;
            bool isNull     = castResult == null;

            // please change the variable value to fix the test.
            const bool expected = true;

            Assert.Equal(expected, isNull);
        }
Esempio n. 6
0
        public void should_return_casted_result_if_it_is_castable()
        {
            var demoClass       = new PolymorphismDemoClass();
            var castToBaseClass = demoClass as PolymorphismDemoClassBase;

            bool isNull = castToBaseClass == null;

            // please change the variable value to fix the test.

            // as works similar with cast, but it will never throw an exception and it only returns null
            const bool expected = false;

            Assert.Equal(expected, isNull);
        }
Esempio n. 7
0
        public void should_return_null_if_it_is_not_castable()
        {
            // normaly (super_class)class is castable, as keyword
            var    demoClass    = new PolymorphismDemoClass();
            object castToObject = demoClass;

            // StringBuilder and object are not in a inheritance chain ?, so caseResult is null
            var  castResult = castToObject as StringBuilder;
            bool isNull     = castResult == null;

            // please change the variable value to fix the test.
            const bool expected = true;

            Assert.Equal(expected, isNull);
        }
Esempio n. 8
0
        public void should_access_member_of_most_derived_class()
        {
            // virtual method use override to override, difference with abstract method?
            // in super class declare method can be override use virtual
            // in sub class declare method with override
            var demoClass       = new PolymorphismDemoClass();
            var castToBaseClass = (PolymorphismDemoClassBase)demoClass;

            string actualValue = castToBaseClass.VirtualMethod();

            // please change the variable value to fix the test.
            const string expected = "DerivedClass";

            Assert.Equal(expected, actualValue);
        }
Esempio n. 9
0
        public void should_return_casted_result_if_it_is_castable()
        {
            // force convert (cast) doesn't change the type of object, only treat object as it's converted type
            // PolymorphismDemoClass and -Base are in the same inheritance chain
            var demoClass = new PolymorphismDemoClass();
            // var baseDemo = new PolymorphismDemoClassBase();
            // Assert.Equal(demoClass, baseDemo); not equal

            // as returns null instead of raising exception when casing is impossible
            var castToBaseClass = demoClass as PolymorphismDemoClassBase;

            bool isNull = castToBaseClass == null;

            // please change the variable value to fix the test.
            const bool expected = false;

            Assert.Equal(expected, isNull);
            // Assert.Equal(demoClass.GetType(), (new PolymorphismDemoClass()).GetType());
            // Assert.Equal(castToBaseClass, baseDemo);
            // Assert.Equal(demoClass, baseDemo); not equal
            // Assert.Equal(demoClass, castToBaseClass); equal
            // Assert.Equal(baseDemo, castToBaseClass); not equal
        }