Пример #1
0
        public void SetParameterizedPropertyValue()
        {
            const string expectedErrorMessage = "Cannot set the Value property for PSMemberInfo object of type \"System.Management.Automation.PSParameterizedProperty\".";
            var obj = new TestParameterizedProperty();
            var psObject = new PSObject(obj);
            var propertyInfo = psObject.Members.FirstOrDefault(m => m.Name == "FileNames") as PSParameterizedProperty;

            var ex = Assert.Throws<ExtendedTypeSystemException>(() => propertyInfo.Value = "a");
            var errorRecordException = ex.ErrorRecord.Exception as ParentContainsErrorRecordException;

            Assert.AreEqual(expectedErrorMessage, ex.Message);
            Assert.IsNull(ex.InnerException);
            Assert.AreEqual("CannotChangePSMethodInfoValue", ex.ErrorRecord.FullyQualifiedErrorId);
            Assert.AreEqual(expectedErrorMessage, errorRecordException.Message);
            Assert.IsNull(errorRecordException.InnerException);
            Assert.AreEqual("", ex.ErrorRecord.CategoryInfo.Activity);
            Assert.AreEqual(ErrorCategory.NotSpecified, ex.ErrorRecord.CategoryInfo.Category);
            Assert.AreEqual("ParentContainsErrorRecordException", ex.ErrorRecord.CategoryInfo.Reason);
            Assert.AreEqual("", ex.ErrorRecord.CategoryInfo.TargetName);
            Assert.AreEqual("", ex.ErrorRecord.CategoryInfo.TargetType);
            Assert.IsNull(ex.ErrorRecord.TargetObject);
        }
Пример #2
0
        public void TooManyArgumentsForParameterizedPropertyInvokeSet()
        {
            var obj = new TestParameterizedProperty(new string[] { "a.txt" });
            var psObject = new PSObject(obj);
            var propertyInfo = psObject.Members.FirstOrDefault(m => m.Name == "FileNames") as PSParameterizedProperty;

            SetValueInvocationException ex = Assert.Throws<SetValueInvocationException>(() => propertyInfo.InvokeSet(0, 1, 2, 3));

            var innerEx = ex.InnerException as MethodException;
            StringAssert.StartsWith("Exception setting \"FileNames\": ", ex.Message);
            Assert.AreEqual("Cannot find an overload for \"FileNames\" and the argument count: \"3\".", innerEx.Message);
        }
Пример #3
0
        public void InvokeParameterizedPropertySetter()
        {
            var obj = new TestParameterizedProperty(new string[] { "a.txt" });
            var psObject = new PSObject(obj);
            var propertyInfo = psObject.Members.FirstOrDefault(m => m.Name == "FileNames") as PSParameterizedProperty;

            propertyInfo.InvokeSet("b.txt", 0);

            Assert.AreEqual("b.txt", obj[0]);
        }
Пример #4
0
        public void CannotInvokeParameterizedPropertyGetterWithInvokeSet()
        {
            var obj = new TestParameterizedProperty(new string[] { "a.txt" });
            var psObject = new PSObject(obj);
            var propertyInfo = psObject.Members.FirstOrDefault(m => m.Name == "FileNames") as PSParameterizedProperty;

            SetValueInvocationException ex = Assert.Throws<SetValueInvocationException>(() => propertyInfo.InvokeSet(0));

            var innerEx = ex.InnerException as MethodException;
            StringAssert.StartsWith("Exception setting \"FileNames\": ", ex.Message);
            Assert.AreEqual("Cannot find an overload for \"FileNames\" and the argument count: \"0\".", innerEx.Message);

            StringAssert.StartsWith("Exception setting \"FileNames\": ", ex.ErrorRecord.Exception.Message);
            Assert.AreEqual("CatchFromBaseAdapterParameterizedPropertySetValue", ex.ErrorRecord.FullyQualifiedErrorId);
            Assert.AreEqual("", ex.ErrorRecord.CategoryInfo.Activity);
            Assert.AreEqual(ErrorCategory.NotSpecified, ex.ErrorRecord.CategoryInfo.Category);
            Assert.AreEqual("ParentContainsErrorRecordException", ex.ErrorRecord.CategoryInfo.Reason);
            Assert.AreEqual("", ex.ErrorRecord.CategoryInfo.TargetName);
            Assert.AreEqual("", ex.ErrorRecord.CategoryInfo.TargetType);
            Assert.IsNull(ex.ErrorRecord.TargetObject);
            Assert.IsInstanceOf(typeof(ParentContainsErrorRecordException), ex.ErrorRecord.Exception);

            StringAssert.StartsWith("Cannot find an overload for \"FileNames\"", innerEx.ErrorRecord.Exception.Message);
            Assert.AreEqual("MethodCountCouldNotFindBest", innerEx.ErrorRecord.FullyQualifiedErrorId);
            Assert.AreEqual("", innerEx.ErrorRecord.CategoryInfo.Activity);
            Assert.AreEqual(ErrorCategory.NotSpecified, innerEx.ErrorRecord.CategoryInfo.Category);
            Assert.AreEqual("ParentContainsErrorRecordException", innerEx.ErrorRecord.CategoryInfo.Reason);
            Assert.AreEqual("", innerEx.ErrorRecord.CategoryInfo.TargetName);
            Assert.AreEqual("", innerEx.ErrorRecord.CategoryInfo.TargetType);
            Assert.IsNull(innerEx.ErrorRecord.TargetObject);
            Assert.IsInstanceOf(typeof(ParentContainsErrorRecordException), innerEx.ErrorRecord.Exception);
        }
Пример #5
0
        public void ObjectWithReadWriteOnlyParameterizedProperty()
        {
            var obj = new TestParameterizedProperty();
            var psObject = new PSObject(obj);

            var propertyInfo = psObject.Members.FirstOrDefault(m => m.Name == "FileNames") as PSParameterizedProperty;

            Assert.IsTrue(propertyInfo.IsGettable);
            Assert.IsTrue(propertyInfo.IsSettable);
            Assert.AreEqual("string FileNames(int index) {get;set;}", propertyInfo.OverloadDefinitions[0]);
        }