示例#1
0
        public void TestPropertyAccessors()
        {
            var objProperties = new ObjTestProperties();

            GetterTest("PublicGetterPublicSetterProp", "Public getter / Public setter property");
            GetterTest("PrivateGetterPrivateSetterProp", "Private getter / Private setter property");
            GetterTest("ProtectedGetterProtectedSetterProp", "Protected getter / Protected setter property");
            GetterTest("InternalGetterInternalSetterProp", "Internal getter / Internal setter property");

            SetterTest("PublicGetterPublicSetterProp", "Public getter / Public setter property 2");
            SetterTest("PrivateGetterPrivateSetterProp", "Private getter / Private setter property 2");
            SetterTest("ProtectedGetterProtectedSetterProp", "Protected getter / Protected setter property 2");
            SetterTest("InternalGetterInternalSetterProp", "Internal getter / Internal setter property 2");

            CallerTest("Sum", new object[] { 2, 2 }, 4);
            CallerTest("Mult", new object[] { 3, 3 }, 9);

            void GetterTest(string name, string pValue)
            {
                if (objProperties.TryGetMemberValue(name, out string value))
                {
                    Assert.Equal(pValue, value);
                }
                else
                {
                    throw new KeyNotFoundException();
                }
            }

            void SetterTest(string name, string pValue)
            {
                if (objProperties.TrySetMemberValue(name, pValue))
                {
                    GetterTest(name, pValue);
                }
                else
                {
                    throw new KeyNotFoundException();
                }
            }

            void CallerTest(string name, object[] arguments, object expectedValue)
            {
                if (objProperties.TryInvokeMethod(name, arguments, out object value))
                {
                    Assert.Equal(expectedValue, value);
                }
                else
                {
                    throw new KeyNotFoundException();
                }
            }
        }
示例#2
0
        public void TestPropertyAccessors()
        {
            var objProperties = new ObjTestProperties();

            var duckProperties = objProperties.DuckAs <IObjTestProperties>();

            // Getter
            Assert.Same(duckProperties.PublicGetterPublicSetterProp, "Public getter / Public setter property");
            Assert.Same(duckProperties.PrivateGetterPrivateSetterProp, "Private getter / Private setter property");
            Assert.Same(duckProperties.ProtectedGetterProtectedSetterProp, "Protected getter / Protected setter property");
            Assert.Same(duckProperties.InternalGetterInternalSetterProp, "Internal getter / Internal setter property");

            Assert.Same(duckProperties.PublicGetterPrivateSetterProp, "Public getter / Private setter property");
            Assert.Same(duckProperties.PublicGetterProtectedSetterProp, "Public getter / Protected setter property");
            Assert.Same(duckProperties.PublicGetterInternalSetterProp, "Public getter / Internal setter property");
            Assert.Same(duckProperties.PublicGetterNoSetterProp, "Public getter / no setter property");

            Assert.Same(duckProperties.PrivateGetterPublicSetterProp, "Private getter / Public setter property");
            Assert.Same(duckProperties.ProtectedGetterPublicSetterProp, "Protected getter / Public setter property");
            Assert.Same(duckProperties.InternalGetterPublicSetterProp, "Internal getter / Public setter property");
            ExpectedException <DuckTypePropertyCantBeReadException>(() => _ = duckProperties.NoGetterPublicSetterProp);

            // Setter
            duckProperties.PublicGetterPublicSetterProp       = "Public getter / Public setter property";
            duckProperties.PrivateGetterPrivateSetterProp     = "Private getter / Private setter property";
            duckProperties.ProtectedGetterProtectedSetterProp = "Protected getter / Protected setter property";
            duckProperties.InternalGetterInternalSetterProp   = "Internal getter / Internal setter property";

            duckProperties.PublicGetterPrivateSetterProp   = "Public getter / Private setter property";
            duckProperties.PublicGetterProtectedSetterProp = "Public getter / Protected setter property";
            duckProperties.PublicGetterInternalSetterProp  = "Public getter / Internal setter property";
            ExpectedException <DuckTypePropertyCantBeWrittenException>(() => duckProperties.PublicGetterNoSetterProp = "Public getter / no setter property");

            duckProperties.PrivateGetterPublicSetterProp   = "Private getter / Public setter property";
            duckProperties.ProtectedGetterPublicSetterProp = "Protected getter / Public setter property";
            duckProperties.InternalGetterPublicSetterProp  = "Internal getter / Public setter property";
            duckProperties.NoGetterPublicSetterProp        = "No getter / Public setter property";
        }