Пример #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));
        }