Exemplo n.º 1
0
        public void TestFieldAccessors()
        {
            var field = new ObjTestFields();

            GetterTest("PublicField", "Public Field");
            GetterTest("PrivateField", "Private Field");
            GetterTest("ProtectedField", "Protected Field");
            GetterTest("InternalField", "Internal Field");

            GetterTest("PublicReadonlyField", "Public Readonly Field");
            GetterTest("PrivateReadonlyField", "Private Readonly Field");
            GetterTest("ProtectedReadonlyField", "Protected Readonly Field");
            GetterTest("InternalReadonlyField", "Internal Readonly Field");

            SetterTest("PublicField", "Public Field 2");
            SetterTest("PrivateField", "Private Field 2");
            SetterTest("ProtectedField", "Protected Field 2");
            SetterTest("InternalField", "Internal Field 2");


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

            void SetterTest(string name, string fieldValue)
            {
                if (field.TrySetMemberValue(name, fieldValue))
                {
                    GetterTest(name, fieldValue);
                }
                else
                {
                    throw new KeyNotFoundException();
                }
            }
        }
Exemplo n.º 2
0
        public void TestFieldAccessors()
        {
            var field = new ObjTestFields();

            var duckFieldsNotFound = field.DuckAs <IObjTestFieldsNotFound>();

            Assert.Same(duckFieldsNotFound.PublicField, "Public Field");
            duckFieldsNotFound.PublicField = "Public Field";
            ExpectedException <DuckTypePropertyOrFieldNotFoundException>(() => _ = duckFieldsNotFound.PrivateField);
            ExpectedException <DuckTypePropertyOrFieldNotFoundException>(() => _ = duckFieldsNotFound.ProtectedField);
            ExpectedException <DuckTypePropertyOrFieldNotFoundException>(() => _ = duckFieldsNotFound.InternalField);
            ExpectedException <DuckTypePropertyOrFieldNotFoundException>(() => duckFieldsNotFound.PrivateField   = "_");
            ExpectedException <DuckTypePropertyOrFieldNotFoundException>(() => duckFieldsNotFound.ProtectedField = "_");
            ExpectedException <DuckTypePropertyOrFieldNotFoundException>(() => duckFieldsNotFound.InternalField  = "_");

            var duckField = field.DuckAs <IObjTestFields>();

            Assert.Same(duckField.PublicField, "Public Field");
            Assert.Same(duckField.PrivateField, "Private Field");
            Assert.Same(duckField.ProtectedField, "Protected Field");
            Assert.Same(duckField.InternalField, "Internal Field");
            duckField.PublicField    = "Public Field";
            duckField.PrivateField   = "Private Field";
            duckField.ProtectedField = "Protected Field";
            duckField.InternalField  = "Internal Field";

            var duckReadonlyField = field.DuckAs <IObjTestReadOnlyFields>();

            Assert.Same(duckReadonlyField.PublicReadonlyField, "Public Readonly Field");
            Assert.Same(duckReadonlyField.PrivateReadonlyField, "Private Readonly Field");
            Assert.Same(duckReadonlyField.ProtectedReadonlyField, "Protected Readonly Field");
            Assert.Same(duckReadonlyField.InternalReadonlyField, "Internal Readonly Field");
            ExpectedException <DuckTypeFieldIsReadonlyException>(() => duckReadonlyField.PublicReadonlyField    = "_");
            ExpectedException <DuckTypeFieldIsReadonlyException>(() => duckReadonlyField.PrivateReadonlyField   = "_");
            ExpectedException <DuckTypeFieldIsReadonlyException>(() => duckReadonlyField.ProtectedReadonlyField = "_");
            ExpectedException <DuckTypeFieldIsReadonlyException>(() => duckReadonlyField.InternalReadonlyField  = "_");
        }