forked from PowerShell/PowerShell
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNew-Object.Tests.ps1
More file actions
167 lines (149 loc) · 5.6 KB
/
Copy pathNew-Object.Tests.ps1
File metadata and controls
167 lines (149 loc) · 5.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
Describe "New-Object" -Tags "CI" {
It "should create an object with 4 fields" {
$o = New-Object psobject
$val = $o.GetType()
$val.IsPublic | Should Not BeNullOrEmpty
$val.Name | Should Not BeNullOrEmpty
$val.IsSerializable | Should Not BeNullOrEmpty
$val.BaseType | Should Not BeNullOrEmpty
$val.IsPublic | Should Be $true
$val.IsSerializable | Should Be $false
$val.Name | Should Be 'PSCustomObject'
$val.BaseType | Should Be 'System.Object'
}
It "should create an object with using Property switch" {
$hash = @{
FirstVal = 'test1'
SecondVal = 'test2'
}
$o = New-Object psobject -Property $hash
$o.FirstVal | Should Be 'test1'
$o.SecondVal | Should Be 'test2'
}
It "should create a .Net object with using ArgumentList switch" {
$o = New-Object -TypeName System.Version -ArgumentList "1.2.3.4"
$o.GetType() | Should Be ([System.Version])
$o | Should Be "1.2.3.4"
}
}
Describe "New-Object DRT basic functionality" -Tags "CI" {
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"{
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
$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
Throw "Execution OK"
}
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
Throw "Execution OK"
}
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
Throw "Execution OK"
}
catch
{
$_.CategoryInfo| Should Match "MethodInvocationException"
$_.FullyQualifiedErrorId | Should be "ConstructorInvokedThrowException,Microsoft.PowerShell.Commands.NewObjectCommand"
}
}
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;}"
}
try
{
New-Object -TypeName Employee -ArgumentList 11 -EA Stop
Throw "Execution OK"
}
catch
{
$_.CategoryInfo| Should Match "MethodException"
$_.FullyQualifiedErrorId | Should be "ConstructorInvokedThrowException,Microsoft.PowerShell.Commands.NewObjectCommand"
}
}
#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)
{
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
Throw "Execution OK"
}
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
Throw "Execution OK"
}
catch
{
$_.CategoryInfo| Should Match "PSArgumentException"
$_.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
}
}