2020-03-24 11:08:37 -07:00
# Copyright (c) Microsoft Corporation.
2018-02-13 09:23:53 -08:00
# Licensed under the MIT License.
2016-07-25 14:01:41 -07:00
Describe "New-Object" -Tags "CI" {
2017-09-29 04:29:33 +04:00
It "Support 'ComObject' parameter on platforms" {
2020-01-13 19:26:54 +00:00
if ( $IsLinux -or $IsMacOS ) {
2018-05-17 14:42:04 -07:00
{ New-Object -ComObject "Shell.Application" } | Should -Throw -ErrorId "NamedParameterNotFound,Microsoft.PowerShell.Commands.NewObjectCommand"
2017-09-29 04:29:33 +04:00
} else {
# It works on NanoServer and IoT too
2018-03-23 10:26:23 -07:00
( Get-Command "New-Object" ). Parameters . ContainsKey ( "ComObject" ) | Should -BeTrue
2017-09-29 04:29:33 +04:00
}
}
2015-07-15 10:15:39 -07:00
It "should create an object with 4 fields" {
2016-03-24 20:20:06 -07:00
$o = New-Object psobject
2017-09-29 04:29:33 +04:00
$val = $o . GetType ()
2015-07-15 10:15:39 -07:00
2018-03-23 10:26:23 -07:00
$val . IsPublic | Should -Not -BeNullOrEmpty
$val . Name | Should -Not -BeNullOrEmpty
$val . IsSerializable | Should -Not -BeNullOrEmpty
$val . BaseType | Should -Not -BeNullOrEmpty
2015-07-15 10:15:39 -07:00
2018-03-23 10:26:23 -07:00
$val . IsPublic | Should -BeTrue
$val . IsSerializable | Should -BeFalse
$val . Name | Should -Be 'PSCustomObject'
$val . BaseType | Should -Be 'System.Object'
2015-07-15 10:15:39 -07:00
}
2016-03-22 19:58:34 -07:00
It "should create an object with using Property switch" {
2016-03-24 20:20:06 -07:00
$hash = @ {
FirstVal = 'test1'
SecondVal = 'test2'
}
2017-09-29 04:29:33 +04:00
$o = New-Object psobject -Property $hash
2016-03-22 19:58:34 -07:00
2018-03-23 10:26:23 -07:00
$o . FirstVal | Should -Be 'test1'
$o . SecondVal | Should -Be 'test2'
2016-03-22 19:58:34 -07:00
}
It "should create a .Net object with using ArgumentList switch" {
2017-09-29 04:29:33 +04:00
$o = New-Object -TypeName System . Version -ArgumentList "1.2.3.4"
2018-03-23 10:26:23 -07:00
$o . GetType () | Should -Be ([ System.Version ])
2016-03-22 19:58:34 -07:00
2018-03-23 10:26:23 -07:00
$o | Should -BeExactly "1.2.3.4"
2016-03-22 19:58:34 -07:00
}
2015-07-24 10:38:51 -07:00
}
2016-05-29 23:11:27 -07:00
2016-07-25 14:01:41 -07:00
Describe "New-Object DRT basic functionality" -Tags "CI" {
2017-09-29 04:29:33 +04:00
It "New-Object with int array should work" {
$result = New-Object -TypeName int [] -Arg 10
2018-03-23 10:26:23 -07:00
$result . Count | Should -Be 10
2017-09-29 04:29:33 +04:00
}
2017-01-16 13:31:14 -08:00
2017-09-29 04:29:33 +04:00
It "New-Object with char should work" {
$result = New-Object -TypeName char
2018-03-23 10:26:23 -07:00
$result . Count | Should -Be 1
2017-09-29 04:29:33 +04:00
$defaultChar = [ char ] 0
2018-03-23 10:26:23 -07:00
([ char ] $result ) | Should -Be $defaultChar
2017-09-29 04:29:33 +04:00
}
2017-01-16 13:31:14 -08:00
2017-09-29 04:29:33 +04:00
It "New-Object with default Coordinates should work" {
$result = New-Object -TypeName System . Management . Automation . Host . Coordinates
2018-03-23 10:26:23 -07:00
$result . Count | Should -Be 1
$result . X | Should -Be 0
$result . Y | Should -Be 0
2017-09-29 04:29:33 +04:00
}
2017-01-16 13:31:14 -08:00
2017-09-29 04:29:33 +04:00
It "New-Object with specified Coordinates should work" {
$result = New-Object -TypeName System . Management . Automation . Host . Coordinates -ArgumentList 1 , 2
2018-03-23 10:26:23 -07:00
$result . Count | Should -Be 1
$result . X | Should -Be 1
$result . Y | Should -Be 2
2017-09-29 04:29:33 +04:00
}
2017-01-16 13:31:14 -08:00
2017-09-29 04:29:33 +04:00
It "New-Object with Employ should work" {
if ( -not ([ System.Management.Automation.PSTypeName ] 'Employee' ). Type )
{
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
2018-03-23 10:26:23 -07:00
$result . Count | Should -Be 1
$result . FirstName | Should -BeExactly "Mary"
$result . LastName | Should -BeExactly "Soe"
$result . YearsInMS | Should -Be 11
2017-09-29 04:29:33 +04:00
}
2017-01-16 13:31:14 -08:00
2017-09-29 04:29:33 +04:00
It "New-Object with invalid type should throw Exception" {
2018-05-17 14:42:04 -07:00
$e = { New-Object -TypeName LiarType -ErrorAction Stop } | Should -Throw -ErrorId "TypeNotFound,Microsoft.PowerShell.Commands.NewObjectCommand" -PassThru
2018-03-23 10:26:23 -07:00
$e . CategoryInfo | Should -Match "PSArgumentException"
2017-09-29 04:29:33 +04:00
}
2017-01-16 13:31:14 -08:00
2017-09-29 04:29:33 +04:00
It "New-Object with invalid argument should throw Exception" {
2018-05-17 14:42:04 -07:00
$e = { New-Object -TypeName System . Management . Automation . PSVariable -ArgumentList "A" , 1 , None , "asd" -ErrorAction Stop } |
2018-03-23 10:26:23 -07:00
Should -Throw -ErrorId "ConstructorInvokedThrowException,Microsoft.PowerShell.Commands.NewObjectCommand" -PassThru
$e . CategoryInfo | Should -Match "MethodException"
2017-09-29 04:29:33 +04:00
}
2017-01-16 13:31:14 -08:00
2017-09-29 04:29:33 +04:00
It "New-Object with abstract class should throw Exception" {
Add-Type -TypeDefinition "public abstract class AbstractEmployee{public AbstractEmployee(){}}"
2018-05-17 14:42:04 -07:00
$e = { New-Object -TypeName AbstractEmployee -ErrorAction Stop } |
2018-03-23 10:26:23 -07:00
Should -Throw -ErrorId "ConstructorInvokedThrowException,Microsoft.PowerShell.Commands.NewObjectCommand" -PassThru
$e . CategoryInfo | Should -Match "MethodInvocationException"
2017-09-29 04:29:33 +04:00
}
2017-01-16 13:31:14 -08:00
2017-09-29 04:29:33 +04:00
It "New-Object with bad argument for class constructor should throw Exception" {
if ( -not ([ System.Management.Automation.PSTypeName ] 'Employee' ). Type )
{
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;}"
}
2018-05-17 14:42:04 -07:00
$e = { New-Object -TypeName Employee -ArgumentList 11 -ErrorAction Stop } | Should -Throw -ErrorId "ConstructorInvokedThrowException,Microsoft.PowerShell.Commands.NewObjectCommand" -PassThru
2018-03-23 10:26:23 -07:00
$e . CategoryInfo | Should -Match "MethodException"
2017-09-29 04:29:33 +04:00
}
2017-01-16 13:31:14 -08:00
2017-09-29 04:29:33 +04:00
#This case will throw "Execution OK" now, just mark as pending now
It "New-Object with not init class constructor should throw Exception" -Pending {
if ( -not ([ System.Management.Automation.PSTypeName ] 'Employee' ). Type )
{
2018-03-23 10:26:23 -07:00
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;}"
2017-09-29 04:29:33 +04:00
}
2018-05-17 14:42:04 -07:00
{ New-Object -TypeName Employee -ErrorAction Stop } | Should -Throw -ErrorId "CannotFindAppropriateCtor,Microsoft.PowerShell.Commands.NewObjectCommand"
2017-09-29 04:29:33 +04:00
}
2017-01-16 13:31:14 -08:00
2017-09-29 04:29:33 +04:00
It "New-Object with Private Nested class should throw Exception" {
Add-Type -TypeDefinition "public class WeirdEmployee{public WeirdEmployee(){}private class PrivateNestedWeirdEmployee{public PrivateNestedWeirdEmployee(){}}}"
2018-05-17 14:42:04 -07:00
$e = { New-Object -TypeName WeirdEmployee + PrivateNestedWeirdEmployee -ErrorAction Stop } | Should -Throw -ErrorId "TypeNotFound,Microsoft.PowerShell.Commands.NewObjectCommand" -PassThru
2018-03-23 10:26:23 -07:00
$e . CategoryInfo | Should -Match "PSArgumentException"
2017-09-29 04:29:33 +04:00
}
2017-01-16 13:31:14 -08:00
2017-09-29 04:29:33 +04:00
It "New-Object with TypeName and Property parameter should work" {
2020-05-07 13:00:30 +01:00
$result = New-Object -TypeName PSObject -Property @ { foo = 123 }
2018-03-23 10:26:23 -07:00
$result . foo | Should -Be 123
2017-09-29 04:29:33 +04:00
}
2016-07-25 14:01:41 -07:00
}
2017-09-11 12:53:34 -07:00
2017-09-29 04:29:33 +04:00
try
2017-09-11 12:53:34 -07:00
{
2020-01-13 19:21:25 +00:00
$defaultParamValues = $PSDefaultParameterValues . Clone ()
2017-09-11 12:53:34 -07:00
$PSDefaultParameterValues [ "it:skip" ] = ![ System.Management.Automation.Platform ]:: IsWindowsDesktop
2017-09-29 04:29:33 +04:00
Describe "New-Object COM functionality" -Tags "CI" {
$testCases = @ (
@ {
Name = 'Microsoft.Update.AutoUpdate'
Property = 'Settings'
Type = 'Object'
}
@ {
Name = 'Microsoft.Update.SystemInfo'
Property = 'RebootRequired'
Type = 'Bool'
}
)
2017-09-11 12:53:34 -07:00
2017-09-29 04:29:33 +04:00
It "Should be able to create <Name> with property <Property> of Type <Type>" -TestCases $testCases {
param ( $Name , $Property , $Type )
$comObject = New-Object -ComObject $name
2018-03-23 10:26:23 -07:00
$comObject . $Property | Should -Not -BeNullOrEmpty
2020-05-07 13:00:30 +01:00
$comObject . $Property | Should -BeOfType $Type
2017-09-29 04:29:33 +04:00
}
2017-09-11 12:53:34 -07:00
2017-09-29 04:29:33 +04:00
It "Should fail with correct error when creating a COM object that dose not exist" {
2018-05-17 14:42:04 -07:00
{ New-Object -ComObject 'doesnotexist' } | Should -Throw -ErrorId 'NoCOMClassIdentified,Microsoft.PowerShell.Commands.NewObjectCommand'
2017-09-29 04:29:33 +04:00
}
}
}
finally
2017-09-11 12:53:34 -07:00
{
$global:PSdefaultParameterValues = $defaultParamValues
}