示例#1
0
        public void TrySetProperty_WhenClassDoesNotHaveProperty_ReturnsUnchangedObject()
        {
            var testClass = new PropertyClass();
            var result    = testClass.TrySetProperty("Random", It.IsAny <string>());

            Assert.AreEqual(testClass, result);
        }
示例#2
0
        public void TrySetProperty_WhenValueTypeDoesNotMatchProperty_ThrowsInvalidOperationException()
        {
            var testClass = new PropertyClass();
            var exception = Assert.Catch <InvalidOperationException>(() => testClass.TrySetProperty(nameof(PropertyClass.Writable), 0));

            Assert.AreEqual("Property type is not assignable from the Value type.", exception.Message);
        }
示例#3
0
        public void TrySetProperty_WhenClassObjectIsNull_ReturnsUnchangedObject()
        {
            PropertyClass testClass = null;
            var           result    = testClass.TrySetProperty(nameof(PropertyClass.Writable), It.IsAny <string>());

            Assert.AreEqual(testClass, result);
        }
示例#4
0
        public void TrySetProperty_WhenTryingToSetAReadonlyProperty_ThrowsInvalidOperationException()
        {
            var testClass = new PropertyClass();
            var exception = Assert.Catch <InvalidOperationException>(() => testClass.TrySetProperty(nameof(PropertyClass.Readonly), It.IsAny <string>()));

            Assert.AreEqual("Cannot set a restricted property.", exception.Message);
        }
示例#5
0
        public void TrySetProperty_WhenSettingPropertyWithNonPublicAccessModifiers_ChangesPropertyValue()
        {
            var value     = "test";
            var testClass = new PropertyClass();

            testClass.TrySetProperty(nameof(PropertyClass.PrivateWritable), value);
            Assert.AreEqual(value, testClass.PrivateWritable);
        }
示例#6
0
        public void TrySetProperty_ChangesPropertyValue()
        {
            var value     = "test";
            var testClass = new PropertyClass();

            testClass.TrySetProperty(nameof(PropertyClass.Writable), value);
            Assert.AreEqual(value, testClass.Writable);
        }
示例#7
0
        public void TrySetProperty_WhenPassingNullAsValueForNonNullableProperty_SetPropertyToDefaultValue()
        {
            var testClass = new PropertyClass()
            {
                NonNullableInt = 1
            };

            testClass.TrySetProperty(nameof(PropertyClass.NonNullableInt), null);
            Assert.AreEqual(default(int), testClass.NonNullableInt);
        }
示例#8
0
        public void TrySetProperty_WhenSettingANonNullableProperty_DoesNotThrowException()
        {
            var testClass = new PropertyClass();

            Assert.DoesNotThrow(() => testClass.TrySetProperty(nameof(PropertyClass.DateTime2), DateTime.UtcNow));
        }