From 7551d33fc064cf4f43081355a4b8cb95b78c67b3 Mon Sep 17 00:00:00 2001 From: JumpingYang001 Date: Sun, 29 May 2016 23:11:27 -0700 Subject: [PATCH 1/3] Add New-Object Pester Unit Test --- test/powershell/New-Object.Tests.ps1 | 113 +++++++++++++++++++++++++++ 1 file changed, 113 insertions(+) diff --git a/test/powershell/New-Object.Tests.ps1 b/test/powershell/New-Object.Tests.ps1 index 50d63a9079..5c4bac2d5b 100644 --- a/test/powershell/New-Object.Tests.ps1 +++ b/test/powershell/New-Object.Tests.ps1 @@ -32,3 +32,116 @@ Describe "New-Object" { $o | Should Be "1.2.3.4" } } + +Describe "New-Object DRT basic functionality" -Tags DRT{ + It "New-Object with int array should work"{ + $result = New-Object -TypeName int[] -Arg 10 + $result.Count | Should Be 10 + } + + It "New-Object with char should work"{ + $result = New-Object -TypeName char + $result.Count | Should Be 1 + $defaultChar = [char]0 + ([char]$result) | Should Be $defaultChar + } + + It "New-Object with default Coordinates should work"{ + $result = New-Object -TypeName System.Management.Automation.Host.Coordinates + $result.Count | Should Be 1 + $result.X | Should Be 0 + $result.Y | Should Be 0 + } + + It "New-Object with specified Coordinates should work"{ + $result = New-Object -TypeName System.Management.Automation.Host.Coordinates -ArgumentList 1,2 + $result.Count | Should Be 1 + $result.X | Should Be 1 + $result.Y | Should Be 2 + } + + It "New-Object with Employ should work"{ + Add-Type -TypeDefinition "public class Employee{public Employee(string firstName,string lastName,int yearsInMS){FirstName = firstName;LastName=lastName;YearsInMS = yearsInMS;}public string FirstName;public string LastName;public int YearsInMS;}" + $result = New-Object -TypeName Employee -ArgumentList "Mary", "Soe", 11 + $result.Count | Should Be 1 + $result.FirstName | Should Be "Mary" + $result.LastName | Should Be "Soe" + $result.YearsInMS | Should Be 11 + } + + It "New-Object with invalid type should throw Exception"{ + try + { + New-Object -TypeName LiarType -EA Stop + } + catch + { + $_.CategoryInfo| Should Match "PSArgumentException" + $_.FullyQualifiedErrorId | Should be "TypeNotFound,Microsoft.PowerShell.Commands.NewObjectCommand" + } + } + + It "New-Object with invalid argument should throw Exception"{ + try + { + New-Object -TypeName System.Management.Automation.PSVariable -ArgumentList "A", 1, None, "asd" -EA Stop + } + catch + { + $_.CategoryInfo| Should Match "MethodException" + $_.FullyQualifiedErrorId | Should be "ConstructorInvokedThrowException,Microsoft.PowerShell.Commands.NewObjectCommand" + } + } + + It "New-Object with abstract class should throw Exception"{ + Add-Type -TypeDefinition "public abstract class AbstractEmployee{public AbstractEmployee(){}}" + try + { + New-Object -TypeName AbstractEmployee -EA Stop + } + catch + { + $_.CategoryInfo| Should Match "MethodInvocationException" + $_.FullyQualifiedErrorId | Should be "ConstructorInvokedThrowException,Microsoft.PowerShell.Commands.NewObjectCommand" + } + } + + It "New-Object with bad argument for class construtor should throw Exception"{ + Add-Type -TypeDefinition "public class Employee{public Employee(string firstName,string lastName,int yearsInMS){FirstName = firstName;LastName=lastName;YearsInMS = yearsInMS;}public string FirstName;public string LastName;public int YearsInMS;}" + try + { + New-Object -TypeName Employee -ArgumentList 11 -EA Stop + } + catch + { + $_.CategoryInfo| Should Match "MethodException" + $_.FullyQualifiedErrorId | Should be "ConstructorInvokedThrowException,Microsoft.PowerShell.Commands.NewObjectCommand" + } + } + + It "New-Object with not init class construtor should throw Exception"{ + Add-Type -TypeDefinition "public class Employee{public Employee(string firstName,string lastName,int yearsInMS){FirstName = firstName;LastName=lastName;YearsInMS = yearsInMS;}public string FirstName;public string LastName;public int YearsInMS;}" + try + { + New-Object -TypeName Employee -EA Stop + } + catch + { + $_.CategoryInfo| Should Match "PSArgumentException" + $_.FullyQualifiedErrorId | Should be "CannotFindAppropriateCtor,Microsoft.PowerShell.Commands.NewObjectCommand" + } + } + + It "New-Object with Private Nested class should throw Exception"{ + Add-Type -TypeDefinition "public class WeirdEmployee{public WeirdEmployee(){}private class PrivateNestedWeirdEmployee{public PrivateNestedWeirdEmployee(){}}}" + try + { + New-Object -TypeName WeirdEmployee+PrivateNestedWeirdEmployee -EA Stop + } + catch + { + $_.CategoryInfo| Should Match "PSArgumentException" + $_.FullyQualifiedErrorId | Should be "TypeNotFound,Microsoft.PowerShell.Commands.NewObjectCommand" + } + } +} \ No newline at end of file From 9ed1d89978eee27b153cf118e9357adeb13983bd Mon Sep 17 00:00:00 2001 From: JumpingYang001 Date: Sun, 29 May 2016 23:21:36 -0700 Subject: [PATCH 2/3] Add NewObjectDRTTestsV2 Pester Unit Test --- test/powershell/New-Object.Tests.ps1 | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/test/powershell/New-Object.Tests.ps1 b/test/powershell/New-Object.Tests.ps1 index 5c4bac2d5b..35bf0d01d0 100644 --- a/test/powershell/New-Object.Tests.ps1 +++ b/test/powershell/New-Object.Tests.ps1 @@ -144,4 +144,9 @@ Describe "New-Object DRT basic functionality" -Tags DRT{ $_.FullyQualifiedErrorId | Should be "TypeNotFound,Microsoft.PowerShell.Commands.NewObjectCommand" } } + + It "New-Object with TypeName and Property parameter should work"{ + $result = New-Object -TypeName PSObject -property @{foo=123} + $result.foo | Should Be 123 + } } \ No newline at end of file From ce2b3d06644c518c551cecb4f2f87c5e501e7104 Mon Sep 17 00:00:00 2001 From: JumpingYang001 Date: Tue, 31 May 2016 21:29:14 -0700 Subject: [PATCH 3/3] Update fix based on comments for New-Object Pester Test --- test/powershell/New-Object.Tests.ps1 | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/test/powershell/New-Object.Tests.ps1 b/test/powershell/New-Object.Tests.ps1 index 35bf0d01d0..fd80be27fc 100644 --- a/test/powershell/New-Object.Tests.ps1 +++ b/test/powershell/New-Object.Tests.ps1 @@ -73,6 +73,7 @@ Describe "New-Object DRT basic functionality" -Tags DRT{ try { New-Object -TypeName LiarType -EA Stop + Throw "Execution OK" } catch { @@ -85,6 +86,7 @@ Describe "New-Object DRT basic functionality" -Tags DRT{ try { New-Object -TypeName System.Management.Automation.PSVariable -ArgumentList "A", 1, None, "asd" -EA Stop + Throw "Execution OK" } catch { @@ -98,6 +100,7 @@ Describe "New-Object DRT basic functionality" -Tags DRT{ try { New-Object -TypeName AbstractEmployee -EA Stop + Throw "Execution OK" } catch { @@ -111,6 +114,7 @@ Describe "New-Object DRT basic functionality" -Tags DRT{ try { New-Object -TypeName Employee -ArgumentList 11 -EA Stop + Throw "Execution OK" } catch { @@ -124,6 +128,7 @@ Describe "New-Object DRT basic functionality" -Tags DRT{ try { New-Object -TypeName Employee -EA Stop + Throw "Execution OK" } catch { @@ -137,6 +142,7 @@ Describe "New-Object DRT basic functionality" -Tags DRT{ try { New-Object -TypeName WeirdEmployee+PrivateNestedWeirdEmployee -EA Stop + Throw "Execution OK" } catch {