2016-07-25 23:01:41 +02:00
Describe " New-Object " -Tags " CI " {
2015-07-15 19:15:39 +02:00
It " should create an object with 4 fields " {
2016-03-25 04:20:06 +01:00
$o = New-Object psobject
$val = $o . GetType ( )
2015-07-15 19:15:39 +02:00
2016-03-25 04:20:06 +01:00
$val . IsPublic | Should Not BeNullOrEmpty
$val . Name | Should Not BeNullOrEmpty
$val . IsSerializable | Should Not BeNullOrEmpty
$val . BaseType | Should Not BeNullOrEmpty
2015-07-15 19:15:39 +02:00
2016-03-25 04:20:06 +01:00
$val . IsPublic | Should Be $true
$val . IsSerializable | Should Be $false
$val . Name | Should Be 'PSCustomObject'
$val . BaseType | Should Be 'System.Object'
2015-07-15 19:15:39 +02:00
}
2016-03-23 03:58:34 +01:00
It " should create an object with using Property switch " {
2016-03-25 04:20:06 +01:00
$hash = @ {
FirstVal = 'test1'
SecondVal = 'test2'
}
$o = New-Object psobject -Property $hash
2016-03-23 03:58:34 +01:00
2016-03-25 04:20:06 +01:00
$o . FirstVal | Should Be 'test1'
$o . SecondVal | Should Be 'test2'
2016-03-23 03:58:34 +01:00
}
It " should create a .Net object with using ArgumentList switch " {
2016-03-25 04:20:06 +01:00
$o = New-Object -TypeName System . Version -ArgumentList " 1.2.3.4 "
$o . GetType ( ) | Should Be ( [ System.Version ] )
2016-03-23 03:58:34 +01:00
2016-03-25 04:20:06 +01:00
$o | Should Be " 1.2.3.4 "
2016-03-23 03:58:34 +01:00
}
2015-07-24 19:38:51 +02:00
}
2016-05-30 08:11:27 +02:00
2016-07-25 23:01:41 +02:00
Describe " New-Object DRT basic functionality " -Tags " CI " {
2016-05-30 08:11:27 +02:00
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 " {
2016-06-12 07:53:01 +02:00
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;} "
}
2016-05-30 08:11:27 +02:00
$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
2016-06-01 06:29:14 +02:00
Throw " Execution OK "
2016-05-30 08:11:27 +02:00
}
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
2016-06-01 06:29:14 +02:00
Throw " Execution OK "
2016-05-30 08:11:27 +02:00
}
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
2016-06-01 06:29:14 +02:00
Throw " Execution OK "
2016-05-30 08:11:27 +02:00
}
catch
{
$_ . CategoryInfo | Should Match " MethodInvocationException "
$_ . FullyQualifiedErrorId | Should be " ConstructorInvokedThrowException,Microsoft.PowerShell.Commands.NewObjectCommand "
}
}
2016-08-25 19:49:32 +02:00
It " New-Object with bad argument for class constructor should throw Exception " {
2016-06-12 07:53:01 +02:00
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;} "
}
2016-05-30 08:11:27 +02:00
try
{
New-Object -TypeName Employee -ArgumentList 11 -EA Stop
2016-06-01 06:29:14 +02:00
Throw " Execution OK "
2016-05-30 08:11:27 +02:00
}
catch
{
$_ . CategoryInfo | Should Match " MethodException "
$_ . FullyQualifiedErrorId | Should be " ConstructorInvokedThrowException,Microsoft.PowerShell.Commands.NewObjectCommand "
}
}
2016-06-12 07:53:01 +02:00
#This case will throw "Execution OK" now, just mark as pending now
2016-08-25 19:49:32 +02:00
It " New-Object with not init class constructor should throw Exception " -Pending {
2016-06-12 07:53:01 +02:00
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;} "
}
2016-05-30 08:11:27 +02:00
try
{
New-Object -TypeName Employee -EA Stop
2016-06-01 06:29:14 +02:00
Throw " Execution OK "
2016-05-30 08:11:27 +02:00
}
catch
{
$_ . 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
2016-06-01 06:29:14 +02:00
Throw " Execution OK "
2016-05-30 08:11:27 +02:00
}
catch
{
$_ . CategoryInfo | Should Match " PSArgumentException "
$_ . FullyQualifiedErrorId | Should be " TypeNotFound,Microsoft.PowerShell.Commands.NewObjectCommand "
}
}
2016-05-30 08:21:36 +02:00
It " New-Object with TypeName and Property parameter should work " {
$result = New-Object -TypeName PSObject -property @ { foo = 123 }
$result . foo | Should Be 123
}
2016-07-25 23:01:41 +02:00
}