public void CmdletPipeParamByPropertyNameCantBePartial() { var cmd = "new-object psobject -property @{f='abc'; b='def'} | " + CmdletName(typeof(TestParametersByPipelinePropertyNamesCommand)); Assert.Throws <MethodInvocationException>(() => { ReferenceHost.Execute(cmd); }); }
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); }
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); }
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); }
public void AccessingNonExistingPropertiesDoesntFail() { var result = ReferenceHost.Execute(NewlineJoin( "$a = New-Object -Type PSObject", "$a.testname" )); Assert.AreEqual(NewlineJoin(), result); }
public void CanGetCustomCSharpObjectAndIdentifyType() { var result = ReferenceHost.Execute(NewlineJoin( "$a = " + CmdletName(typeof(TestCreateCustomObjectCommand)), "$a.GetType().FullName" )); Assert.AreEqual(NewlineJoin(typeof(CustomTestClass).FullName), result); }
public void CanAccessCSharpObjectProperty() { var result = ReferenceHost.Execute(NewlineJoin( "$a = " + CmdletName(typeof(TestCreateCustomObjectCommand)) + " 'foo' 'bar'", "$a.MessageProperty" )); Assert.AreEqual(NewlineJoin("foo"), result); }
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); }
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); }
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); }); }
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); }
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()); } }
public void CanSetCSharpObjectField() { var result = ReferenceHost.Execute(NewlineJoin( "$a = " + CmdletName(typeof(TestCreateCustomObjectCommand)) + " 'foo' 'bar'", "$a.MessageField = 'baz'", "$a.MessageField" )); Assert.AreEqual(NewlineJoin("baz"), result); }
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); }
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); }
public void TwoParameterAmbiguousSetsWhenNoParameterPassedShouldNotExecuteCmdlet() { var cmd = CmdletName(typeof(TestTwoAmbiguousParameterSetsCommand)); var ex = Assert.Throws <ParameterBindingException>(() => { ReferenceHost.Execute(cmd); } ); StringAssert.Contains("Ambiguous", ex.ErrorRecord.FullyQualifiedErrorId); }
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); }
public void CmdletParameterWithSameAliasDontWork() { var cmd = CmdletName(typeof(TestSameAliasesCommand)); Assert.Throws(typeof(MetadataException), delegate() { ReferenceHost.Execute(cmd); } ); }
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); }
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); }
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); }
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); }
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); }
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); }
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); }
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); }
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); }
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); }
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); }
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); }