Esempio n. 1
0
        public void CmdletPipeParamByPropertyNameCantBePartial()
        {
            var cmd = "new-object psobject -property @{f='abc'; b='def'} | "
                      + CmdletName(typeof(TestParametersByPipelinePropertyNamesCommand));

            Assert.Throws <MethodInvocationException>(() => {
                ReferenceHost.Execute(cmd);
            });
        }
Esempio n. 2
0
        public void NewObjectCanCreatePSObject()
        {
            var results = ReferenceHost.RawExecute("New-Object -Type PSObject");

            Assert.AreEqual(1, results.Count, "No results");
            var obj = results[0].BaseObject;

            Assert.True(obj is PSCustomObject);
        }
Esempio n. 3
0
        public void CmdletPipeParamByPropertyNameIgnoresAlreadyBound(string parameters, string expected)
        {
            var cmd = "new-object psobject -property @{foo='abc'; bar='def'} | "
                      + CmdletName(typeof(TestParametersByPipelinePropertyNamesCommand))
                      + parameters;
            var result = ReferenceHost.Execute(cmd);

            Assert.AreEqual(expected + Environment.NewLine, result);
        }
Esempio n. 4
0
        public void CmdletPipeParamByPropertyNameCanBeAlias()
        {
            var cmd = "new-object psobject -property @{baz='abc'; b='def'} | "
                      + CmdletName(typeof(TestParametersByPipelinePropertyNamesCommand));
            var expected = "abc " + Environment.NewLine;
            var result   = ReferenceHost.Execute(cmd);

            Assert.AreEqual(expected, result);
        }
Esempio n. 5
0
        public void AccessingNonExistingPropertiesDoesntFail()
        {
            var result = ReferenceHost.Execute(NewlineJoin(
                                                   "$a = New-Object -Type PSObject",
                                                   "$a.testname"
                                                   ));

            Assert.AreEqual(NewlineJoin(), result);
        }
Esempio n. 6
0
        public void CanGetCustomCSharpObjectAndIdentifyType()
        {
            var result = ReferenceHost.Execute(NewlineJoin(
                                                   "$a = " + CmdletName(typeof(TestCreateCustomObjectCommand)),
                                                   "$a.GetType().FullName"
                                                   ));

            Assert.AreEqual(NewlineJoin(typeof(CustomTestClass).FullName), result);
        }
Esempio n. 7
0
        public void CanAccessCSharpObjectProperty()
        {
            var result = ReferenceHost.Execute(NewlineJoin(
                                                   "$a = " + CmdletName(typeof(TestCreateCustomObjectCommand)) + " 'foo' 'bar'",
                                                   "$a.MessageProperty"
                                                   ));

            Assert.AreEqual(NewlineJoin("foo"), result);
        }
Esempio n. 8
0
        public void ElementsFromPipelineAreStoredAsArray()
        {
            var result = ReferenceHost.Execute(NewlineJoin(
                                                   "$a = @('foo', 'bar', 'baz') | " + CmdletName(typeof(TestCmdletPhasesCommand)),
                                                   "$a.GetType().FullName"
                                                   ));

            Assert.AreEqual(NewlineJoin(typeof(object[]).FullName), result);
        }
Esempio n. 9
0
        public void ReturnEndsScriptAndWritesToPipeline()
        {
            var expected   = NewlineJoin("foo", "bar", "bla");
            var scriptname = CreateScript("'foo'; return 'bar'; 'baz'");
            var command    = NewlineJoin(String.Format("& '{0}'", scriptname), "'bla'");
            var result     = ReferenceHost.Execute(command);

            Assert.AreEqual(expected, result);
        }
Esempio n. 10
0
        public void CmdletPipeParamByPropertyFailsIfAllAreAlreadyBound()
        {
            var cmd = "new-object psobject -property @{foo='abc'; bar='def'} | "
                      + CmdletName(typeof(TestParametersByPipelinePropertyNamesCommand))
                      + " -Foo 'a' -Bar 'a'";

            Assert.Throws <MethodInvocationException>(() => {
                ReferenceHost.Execute(cmd);
            });
        }
Esempio n. 11
0
        public void CmdletPipeParamByPropertyNameCanOnlyProvideOptional()
        {
            var cmd = "new-object psobject -property @{bar='def'} | "
                      + CmdletName(typeof(TestParametersByPipelinePropertyNamesCommand))
                      + " -Foo 'a'";
            var expected = "a def" + Environment.NewLine;
            var result   = ReferenceHost.Execute(cmd);

            Assert.AreEqual(expected, result);
        }
Esempio n. 12
0
        public static void ExecuteAndCompareType(string cmd, params Type[] expectedTypes)
        {
            var results = ReferenceHost.RawExecute(cmd);

            Assert.AreEqual(expectedTypes.Length, results.Count);
            for (int i = 0; i < expectedTypes.Length; i++)
            {
                Assert.AreSame(expectedTypes[i], results[i].BaseObject.GetType());
            }
        }
Esempio n. 13
0
        public void CanSetCSharpObjectField()
        {
            var result = ReferenceHost.Execute(NewlineJoin(
                                                   "$a = " + CmdletName(typeof(TestCreateCustomObjectCommand)) + " 'foo' 'bar'",
                                                   "$a.MessageField = 'baz'",
                                                   "$a.MessageField"
                                                   ));

            Assert.AreEqual(NewlineJoin("baz"), result);
        }
Esempio n. 14
0
        public void CustomPSObjectPropertiesCanBeAccessedCaseInsensitive()
        {
            var result = ReferenceHost.Execute(NewlineJoin(
                                                   "$a = New-Object -Type PSObject",
                                                   "$a | Add-Member -Type NoteProperty -Name TestName -Value TestValue",
                                                   "$a.testname"
                                                   ));

            Assert.AreEqual(NewlineJoin("TestValue"), result);
        }
Esempio n. 15
0
        public void CreatePSObjectWithPropertiesByHashtable()
        {
            var result = ReferenceHost.Execute(NewlineJoin(new string[] {
                "$obj = new-object psobject -property @{foo='abc'; bar='def'}",
                "$obj.FoO",
                "$obj.bAR"
            }));
            var expected = NewlineJoin(new string[] { "abc", "def" });

            Assert.AreEqual(expected, result);
        }
Esempio n. 16
0
        public void TwoParameterAmbiguousSetsWhenNoParameterPassedShouldNotExecuteCmdlet()
        {
            var cmd = CmdletName(typeof(TestTwoAmbiguousParameterSetsCommand));
            var ex  = Assert.Throws <ParameterBindingException>(() =>
            {
                ReferenceHost.Execute(cmd);
            }
                                                                );

            StringAssert.Contains("Ambiguous", ex.ErrorRecord.FullyQualifiedErrorId);
        }
Esempio n. 17
0
        public void ReturnEvaluatesExpression(string returnValue, string returnExpression)
        {
            var expected = NewlineJoin("foo", returnValue, "bla");
            var command  = NewlineJoin(
                "& { $testvar = 'TESTVALUE'; 'foo'; return " + returnExpression + "; 'baz' }",
                "'bla'"
                );
            var result = ReferenceHost.Execute(command);

            Assert.AreEqual(expected, result);
        }
Esempio n. 18
0
        public void CmdletParameterWithSameAliasDontWork()
        {
            var cmd = CmdletName(typeof(TestSameAliasesCommand));

            Assert.Throws(typeof(MetadataException),
                          delegate()
            {
                ReferenceHost.Execute(cmd);
            }
                          );
        }
Esempio n. 19
0
        public void ReturnEndsFunctionAndWritesToPipeline()
        {
            var expected = NewlineJoin("foo", "bar", "bla");
            var command  = NewlineJoin(
                "function testfun { 'foo'; return 'bar'; 'baz' }",
                "testfun",
                "'bla'"
                );
            var result = ReferenceHost.Execute(command);

            Assert.AreEqual(expected, result);
        }
Esempio n. 20
0
        public void UseParameterByAmbiguousNamePrefixThrows()
        {
            var cmd = CmdletName(typeof(TestMandatoryInOneSetCommand)) + " -TestP 'foo'"; // could be TestParam or TestParam2
            var ex  = Assert.Throws(typeof(ParameterBindingException),
                                    delegate()
            {
                ReferenceHost.Execute(cmd);
            }
                                    ) as ParameterBindingException;

            StringAssert.Contains("Ambiguous", ex.ErrorRecord.FullyQualifiedErrorId);
        }
Esempio n. 21
0
        public void NoDefaultSetAndPositionalsResultsInAmbiguousError()
        {
            var cmd = CmdletName(typeof(TestNoDefaultSetAndTwoPositionalsCommand)) + " foo";
            var ex  = Assert.Throws(typeof(ParameterBindingException),
                                    delegate()
            {
                ReferenceHost.Execute(cmd);
            }
                                    ) as ParameterBindingException;

            StringAssert.Contains("Ambiguous", ex.ErrorRecord.FullyQualifiedErrorId);
        }
Esempio n. 22
0
        public void OnlyOneParameterSetCanBeActive()
        {
            var cmd = CmdletName(typeof(TestNoMandatoriesCommand)) + " -RandomString 'foo' -RandomInt 2";
            var ex  = Assert.Throws(typeof(ParameterBindingException),
                                    delegate()
            {
                ReferenceHost.Execute(cmd);
            }
                                    ) as ParameterBindingException;

            StringAssert.Contains("Ambiguous", ex.ErrorRecord.FullyQualifiedErrorId);
        }
Esempio n. 23
0
        public void CanGetCSharpObjectMethodAndInvokeLater()
        {
            var result = ReferenceHost.Execute(NewlineJoin(
                                                   "$a = " + CmdletName(typeof(TestCreateCustomObjectCommand)) + " 'foo' 'bar'",
                                                   "$b = $a.SetMessages",
                                                   "$c = $a.Combine",
                                                   "$b.Invoke('bla', 'blub')",
                                                   "$c.Invoke()"
                                                   ));

            Assert.AreEqual(NewlineJoin("blablub"), result);
        }
Esempio n. 24
0
        public void CanInvokeCSharpObjectMethodWithArguments()
        {
            var result = ReferenceHost.Execute(NewlineJoin(
                                                   "$a = " + CmdletName(typeof(TestCreateCustomObjectCommand)) + " 'foo' 'bar'",
                                                   "$a.SetMessages('bla', 'blub')",
                                                   "$a.MessageProperty",
                                                   "$a.MessageField"
                                                   ));
            var expected = NewlineJoin("bla", "blub");

            Assert.AreEqual(expected, result);
        }
Esempio n. 25
0
        public void CanInvokeCSharpObjectMethodAndGetResult()
        {
            var result = ReferenceHost.Execute(NewlineJoin(
                                                   "$a = " + CmdletName(typeof(TestCreateCustomObjectCommand)) + " 'foo' 'bar'",
                                                   "$b = $a.Combine()",
                                                   "$b.GetType().FullName",
                                                   "$b"
                                                   ));
            var expected = NewlineJoin(typeof(string).FullName, "foobar");

            Assert.AreEqual(expected, result);
        }
Esempio n. 26
0
        public void ExpressionsAreValidExitCodes(string expectedExitCode, string exitExpression)
        {
            var scriptname = CreateScript(NewlineJoin("exit " + exitExpression));
            var command    = NewlineJoin(
                String.Format(". '{0}'", scriptname),
                "$LastExitCode"
                );
            var expected = NewlineJoin(expectedExitCode);
            var result   = ReferenceHost.Execute(command);

            Assert.AreEqual(expected, result);
        }
Esempio n. 27
0
        public void ReturnEndsScriptBlockAndWritesToPipeline()
        {
            var expected = NewlineJoin("foo", "bar", "bla");
            var command  = NewlineJoin(
                "'foo'",
                "& { return 'bar'; 'baz' }",
                "'bla'"
                );
            var result = ReferenceHost.Execute(command);

            Assert.AreEqual(expected, result);
        }
Esempio n. 28
0
        public void ParameterSetWithoutMandatoriesIsNotChosenOverDefaultWithMandatory()
        {
            var cmd = CmdletName(typeof(TestMandatoryInOneSetCommand)) + " 'works'";
            var ex  = Assert.Throws(typeof(ParameterBindingException),
                                    delegate()
            {
                ReferenceHost.Execute(cmd);
            }
                                    ) as ParameterBindingException;

            StringAssert.Contains("Missing", ex.ErrorRecord.FullyQualifiedErrorId);
        }
Esempio n. 29
0
        public void ExitInScriptBlockExitsCompletely()
        {
            var command = NewlineJoin(
                "'foo'",
                "& { 'bar'; exit; 'baz'; }",
                "'blub'"
                );
            var expected = NewlineJoin("foo", "bar");
            var result   = ReferenceHost.Execute(command);

            Assert.AreEqual(expected, result);
        }
Esempio n. 30
0
        public void TooManyPositionalParametersShouldThrow()
        {
            var cmd = CmdletName(typeof(TestMandatoryInOneSetCommand)) + " 'foo' 'bar' 'baz' 'bla'";
            var ex  = Assert.Throws(typeof(ParameterBindingException),
                                    delegate()
            {
                ReferenceHost.Execute(cmd);
            }
                                    ) as ParameterBindingException;

            StringAssert.Contains("PositionalParameterNotFound", ex.ErrorRecord.FullyQualifiedErrorId);
        }