public void Test_WhenFirstParameterIsInSetA_AndSecondParameterInAllSets_ThenSetAIsReturned()
        {
            var result = TestCmdletHost.RunTestHost(
                TestCases.ParameterSetsFirstParameterInSetASecondParameterNotInSet,
                null);

            result.First().BaseObject.Should().Be(Constants.DynamicParameterSetsSetA);
        }
Exemplo n.º 2
0
        Test_WhenValueDoesNotMatchCaseSensitiveRegexObject_ThenNoExceptionIsThrown()
        {
            const string TestValue = "ABC";

            Action action = () => TestCmdletHost.RunTestHost(TestCases.ValidatePatternWithRegexObjectOptionsCaseInsensitive, TestValue);

            action.Should().NotThrow();
        }
Exemplo n.º 3
0
        Test_WhenParameterValueIsNull_AndValidateNotNullOrEmptyAttributeIsPresent_ThenParameterBindingExceptionIsThrown()
        {
            var expectedMessage =
                $"Cannot validate argument on parameter '{Constants.DynamicParameterName}'. The argument is null or empty. Provide an argument that is not null or empty, and then try the command again.";
            Action action = () => TestCmdletHost.RunTestHost(TestCases.ValidateNotNullOrEmpty, null);

            action.Should().Throw <ParameterBindingException>().WithMessage(expectedMessage);
        }
        public void Test_WhenStringLengthIsWithinLengthValidation_ThenNoExceptionIsThrown(int stringLength)
        {
            var testString = string.Empty.PadLeft(stringLength);

            Action action = () => TestCmdletHost.RunTestHost(TestCases.ValidateLength, testString);

            action.Should().NotThrow();
        }
Exemplo n.º 5
0
        public void Test_WhenScalarValueIsGivenAndDoubleIsTheTypeAndNonNumericPassed_ThenParameterBindingExceptionIsThrown()
        {
            var expectedMessage =
                "Cannot bind parameter 'TestParameter'. Cannot convert value \"string\" to type \"System.Double\". Error: \"Input string was not in a correct format.\"";

            Action action = () => TestCmdletHost.RunTestHost(TestCases.NumericArgumentDouble, "string");

            action.Should().Throw <ParameterBindingException>().WithMessage(expectedMessage);
        }
        public void Test_WhenStringLengthIsOutsideLengthValidation_ThenParameterBindingExceptionIsThrown(
            int stringLength)
        {
            var testString = string.Empty.PadLeft(stringLength);

            Action action = () => TestCmdletHost.RunTestHost(TestCases.ValidateLength, testString);

            action.Should().Throw <ParameterBindingException>();
        }
Exemplo n.º 7
0
        Test_WhenInvalidIPAddressesArePassedToParameterWithIPValidationRegex_ThenParameterBindingExceptionIsThrown(
            string ipAddress)
        {
            var expectedMessage =
                $"Cannot validate argument on parameter 'TestParameter'. The argument \"{ipAddress}\" does not match the \"{Constants.IpAddressRegex}\" pattern. Supply an argument that matches \"{Constants.IpAddressRegex}\" and try the command again.";
            Action action = () => TestCmdletHost.RunTestHost(TestCases.ValidatePatternViaArguments, ipAddress);

            action.Should().Throw <ParameterBindingException>().WithMessage(expectedMessage);
        }
        public void Test_WhenValueFromRemainingArgumentsIsSet_ThenArrayPassedInIsReturned()
        {
            var result = TestCmdletHost.RunTestHost(
                TestCases.ValueFromRemainingArguments,
                Constants.RemainingArguments);

            // ReSharper disable once StyleCop.SA1119 - Stylecop gets it wrong here.
            ((string[])(result.First().BaseObject)).SequenceEqual(Constants.RemainingArguments).Should().BeTrue();
        }
        public void Test_WhenValidValuesArePiped_TheyAreReturned(object value)
        {
            var result = TestCmdletHost.RunTestHost(TestCases.ValidateSetFromPipeline, value);

            result.Count.Should().Be(1, "a single value was passed to the dynamic parameter");

            var actual = result.First().BaseObject;

            actual.Should().Be(value);
        }
        public void Test_WhenPocoWithInvalidPropertyValueIsPiped_ThenParameterBindingExceptionIsThrown()
        {
            const string InvalidValue = "Four";

            var poco = new TestPoco(InvalidValue);

            Action action = () => TestCmdletHost.RunTestHost(TestCases.ValidateSetFromPipelineByPropertyName, poco);

            action.Should().Throw <ParameterBindingException>();
        }
        public void Test_WhenValueIsWithinRangeKind_ItIsReturned(int value)
        {
            var result = TestCmdletHost.RunTestHost(TestCases.ValidateRangeWithRangeKindNonNegative, value);

            result.Count.Should().Be(1, "a single value was passed to the dynamic parameter");

            var actual = result.First().BaseObject;

            actual.Should().Be(value);
        }
Exemplo n.º 12
0
        public void Test_WhenValidIPAddressesArePassedToParameterWithIPValidationRegexObject_TheyAreReturned(string ipAddress)
        {
            var result = TestCmdletHost.RunTestHost(TestCases.ValidatePatternWithRegexObject, ipAddress);

            result.Count.Should().Be(1, "a single value was passed to the dynamic parameter");

            var actual = result.First().BaseObject;

            actual.Should().Be(ipAddress);
        }
        public void Test_WhenValidValuesAreGivenOnCommandLine_TheyAreReturned(object value)
        {
            var result = TestCmdletHost.RunTestHost(TestCases.ValidateSetViaArguments, value);

            result.Count.Should().Be(1, "a single value was passed to the dynamic parameter");

            var actual = result.First().BaseObject;

            actual.Should().Be(value);
        }
Exemplo n.º 14
0
        Test_WhenValueDoesNotMatchCaseSensitiveRegex_ThenParameterBindingExceptionIsThrown()
        {
            const string TestValue = "ABC";

            var expectedMessage =
                $"Cannot validate argument on parameter 'TestParameter'. The argument \"{TestValue}\" does not match the \"{Constants.CaseSensitivityRegex}\" pattern. Supply an argument that matches \"{Constants.CaseSensitivityRegex}\" and try the command again.";

            Action action = () => TestCmdletHost.RunTestHost(TestCases.ValidatePatterWithOptionsCaseSensitive, TestValue);

            action.Should().Throw <ParameterBindingException>().WithMessage(expectedMessage);
        }
Exemplo n.º 15
0
        Test_WhenInvalidIPAddressesArePassedToParameterWithIPValidationRegexAndCustomErrorMessage_ThenParameterBindingExceptionIsThrownWithCustomMessage()
        {
            const string IpAddress       = "256.0.0.0";
            var          customError     = string.Format(Constants.InvalidIpAddressCustomMessage, IpAddress);
            var          expectedMessage =
                $"Cannot validate argument on parameter '{Constants.DynamicParameterName}'. {customError}";

            Action action = () => TestCmdletHost.RunTestHost(TestCases.ValidatePatternWithCustomMessage, IpAddress);

            action.Should().Throw <ParameterBindingException>().WithMessage(expectedMessage);
        }
        public void Test_WhenInvalidValuesAreGivenOnCommandLineAndCustomErrorMessage_ThenParameterBindingExceptionIsThrownWithCustomMessage()
        {
            const string Value           = "Four";
            var          customError     = string.Format(Constants.InvalidParameterValueCustomMessage, Value);
            var          expectedMessage =
                $"Cannot validate argument on parameter '{Constants.DynamicParameterName}'. {customError}";

            Action action = () => TestCmdletHost.RunTestHost(TestCases.ValidateSetWithCustomMessage, Value);

            action.Should().Throw <ParameterBindingException>().WithMessage(expectedMessage);
        }
Exemplo n.º 17
0
        public void Test_WhenScalarValueIsGivenWithNoTypeInfomation_ItIsReturnedAsOriginalType(object value)
        {
            var result = TestCmdletHost.RunTestHost(TestCases.UndecoratedArgument, value);

            result.Count.Should().Be(1, "a single value was passed to the dynamic parameter");

            var actual = result.First().BaseObject;

            actual.GetType().Should().Be(value.GetType(), "we passed the value boxed in an object which should be truthfully returned.");
            result.First().BaseObject.Should().Be(value);
        }
        public void Test_WhenMandatoryValueNotSupplied_ThenParameterBindingExceptionIsThrown()
        {
            var expectedMessage =
                "Cannot process command because of one or more missing mandatory parameters: TestParameter.";

            Action action = () => TestCmdletHost.RunTestHost(
                TestCases.MandatoryArgument,
                null);

            action.Should().Throw <ParameterBindingException>().WithMessage(expectedMessage);
        }
        public void Test_WhenPocoIsPassedWithValueFromPipelineByPropertyName_ThePropertyValueIsReturned()
        {
            var expected = new TestPoco();
            var result   = TestCmdletHost.RunTestHost(TestCases.ValueFromPipelineByPropertyName, expected);

            result.Count.Should().Be(1, "a single value was passed to the dynamic parameter");
            var actual = result.First().BaseObject;

            actual.GetType().Should().Be <string>();
            actual.ToString().Should().Be(Constants.TestPocoPropertyValue);
        }
        public void Test_WhenPocoWithValidPropertyValueIsPiped_TheValueIsReturned(string value)
        {
            var poco = new TestPoco(value);

            var result = TestCmdletHost.RunTestHost(TestCases.ValidateSetFromPipelineByPropertyName, poco);

            result.Count.Should().Be(1, "a single value was passed to the dynamic parameter");

            var actual = result.First().BaseObject;

            actual.Should().Be(value);
        }
Exemplo n.º 21
0
        public void Test_WhenScalarValueIsGivenAndDoubleIsTheType_ItIsReturnedAsDouble(object value)
        {
            var result = TestCmdletHost.RunTestHost(TestCases.NumericArgumentDouble, value);

            var expected = value is int i ? i : (double)value;

            result.Count.Should().Be(1, "a single value was passed to the dynamic parameter");
            var actual = result.First().BaseObject;

            actual.GetType().Should().Be <double>();
            actual.Should().Be(expected);
        }
Exemplo n.º 22
0
        public void Test_WhenPocoIsGivenWithTypeInfomation_ItIsReturned()
        {
            var expected = new TestPoco();
            var result   = TestCmdletHost.RunTestHost(TestCases.PocoArgument, expected);

            result.Count.Should().Be(1, "a single value was passed to the dynamic parameter");
            var actual = result.First().BaseObject;

            actual.GetType().Should().Be <TestPoco>();
            ReferenceEquals(expected, actual).Should().BeTrue(
                "the actual instance should pass through the PowerShell pipeline unfettered");
        }
        public void Test_WhenArrayLengthIsWithinCountValidation_ThenNoExceptionIsThrown(int arrayLength)
        {
            var testArray = new string[arrayLength];

            for (var i = 0; i < arrayLength; ++i)
            {
                testArray[i] = $"{i}";
            }

            Action action = () => TestCmdletHost.RunTestHost(TestCases.ValidateCount, testArray);

            action.Should().NotThrow();
        }
        public void Test_WhenFirstParameterIsInSetA_AndSecondParameterInSetB_AndBothParametersPresent_ThenParameterBindingExceptionIsThrown()
        {
#if NETCOREAPP
            const string ExpectedMessage = "Parameter set cannot be resolved using the specified named parameters. One or more parameters issued cannot be used together or an insufficient number of parameters were provided.";
#else
            const string ExpectedMessage = "Parameter set cannot be resolved using the specified named parameters.";
#endif
            Action action = () => TestCmdletHost.RunTestHost(
                TestCases.ParameterSetInvalidCombination,
                null);

            action.Should().Throw <ParameterBindingException>().WithMessage(ExpectedMessage);
        }
        public void Test_WhenArrayLengthIsOutsideCountValidation_ThenParameterBindingExceptionIsThrown(int arrayLength)
        {
            var testArray = new string[arrayLength];

            for (var i = 0; i < arrayLength; ++i)
            {
                testArray[i] = $"{i}";
            }

            Action action = () => TestCmdletHost.RunTestHost(TestCases.ValidateCount, testArray);

            action.Should().Throw <ParameterBindingException>();
        }
        public void Test_WhenMandatoryValueIsExplicitlyNullAndAllowNullAttributeIsSet_ThenNoExceptionIsThrown()
        {
            Action action = () => TestCmdletHost.RunTestHost(TestCases.MandatoryWithAllowNull, null);

            action.Should().NotThrow();
        }
        public void Test_WhenValueIsOutOfRangeKind_ThenParameterBindingExceptionIsThrown(int value)
        {
            Action action = () => TestCmdletHost.RunTestHost(TestCases.ValidateRangeWithRangeKindNonNegative, value);

            action.Should().Throw <ParameterBindingException>();
        }
        public void Test_WhenValueIsOutOfRangeCheckedByScript_ThenParameterBindingExceptionIsThrown(int value)
        {
            Action action = () => TestCmdletHost.RunTestHost(TestCases.ValidateScript, value);

            action.Should().Throw <ParameterBindingException>();
        }
        public void Test_WhenInvalidValuesAreGivenOnCommandLine_ThenParameterBindingExceptionIsThrown(object value)
        {
            Action action = () => TestCmdletHost.RunTestHost(TestCases.ValidateSetViaArguments, value);

            action.Should().Throw <ParameterBindingException>();
        }
        public void Test_WhenInvalidValuesArePiped_ThenParameterBindingExceptionIsThrown(object value)
        {
            Action action = () => TestCmdletHost.RunTestHost(TestCases.ValidateSetFromPipeline, value);

            action.Should().Throw <ParameterBindingException>();
        }