forked from PowerShell/PowerShell
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScripting.Classes.Attributes.Tests.ps1
More file actions
218 lines (195 loc) · 6.96 KB
/
Copy pathScripting.Classes.Attributes.Tests.ps1
File metadata and controls
218 lines (195 loc) · 6.96 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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
Describe 'Attributes Test' -Tags "CI" {
BeforeAll {
$dummyAttributesSource = @'
using System.Management.Automation;
namespace Dummy
{
public class DoubleStringTransformationAttribute : ArgumentTransformationAttribute
{
public override object Transform(EngineIntrinsics engineIntrinsics, object inputData)
{
string arg = inputData as string;
if (arg != null)
{
return arg + arg;
}
return inputData;
}
}
public class AppendStringTransformationAttribute : ArgumentTransformationAttribute
{
public override object Transform(EngineIntrinsics engineIntrinsics, object inputData)
{
string arg = inputData as string;
if (arg != null)
{
return arg + "___";
}
return inputData;
}
}
public class DoubleInt : ArgumentTransformationAttribute
{
public override object Transform(EngineIntrinsics engineIntrinsics, object inputData)
{
int? arg = inputData as int?;
if (arg != null)
{
return arg + arg;
}
return inputData;
}
}
}
'@
Add-Type -TypeDefinition $dummyAttributesSource
}
Context 'Property.Instance.ValidateSet.String' {
class C1 { [ValidateSet("Present", "Absent")][string]$Ensure }
# This call should not throw exception
[C1]::new().Ensure = "Present"
It 'Error when ValidateSet should be ExceptionWhenSetting' {
try
{
[C1]::new().Ensure = "foo"
throw "Exception expected"
}
catch
{
$_.FullyQualifiedErrorId | Should be 'ExceptionWhenSetting'
}
}
}
Context 'Property.Static.ValidateSet.String' {
class C1 { static [ValidateSet("Present", "Absent")][string]$Ensure }
# This call should not throw exception
[C1]::Ensure = "Present"
It 'Error when ValidateSet should be ExceptionWhenSetting'{
try {
[C1]::Ensure = "foo"
throw "Exception expected"
}
catch {
$_.FullyQualifiedErrorId | Should be 'ExceptionWhenSetting'
}
}
}
Context 'Property.Instance.ValidateRange.Int' {
class C1 { [ValidateRange(1, 10)][int]$f }
# This call should not throw exception
[C1]::new().f = 10
[C1]::new().f = 1
It 'Error when ValidateSet should be ExceptionWhenSetting'{
try {
[C1]::new().f = 20
throw "Exception expected"
}
catch {
$_.FullyQualifiedErrorId | Should be 'ExceptionWhenSetting'
}
}
}
Context 'Property.Static.ValidateRange.Int' {
class C1 { static [ValidateRange(1, 10)][int]$f }
# This call should not throw exception
[C1]::f = 5
It 'Error when ValidateSet should be ExceptionWhenSetting'{
try {
[C1]::f = 20
throw "Exception expected"
}
catch {
$_.FullyQualifiedErrorId | Should be 'ExceptionWhenSetting'
}
}
}
Context 'Property.Static.ValidateSet.ImplicitObject' {
class C1 { static [ValidateSet("abc", 5)]$o }
# This call should not throw exception
[C1]::o = "abc"
[C1]::o = 5
It 'Error when ValidateSet should be ExceptionWhenSetting'{
try {
[C1]::o = 1
throw "Exception expected"
}
catch {
$_.FullyQualifiedErrorId | Should be 'ExceptionWhenSetting'
}
}
}
#
# We use [scriptblock]::Create() here to allow SuiteSetup add Dummy.Transformation type to
# the scope. Otherwise, we will need to have all classes for attributes in parse time.
#
# Invoke() returns an array, we need first element of it.
#
Context 'Property.Instance.Transformation.ImplicitObject' {
$c = [scriptblock]::Create('class C1 { [Dummy.DoubleStringTransformation()]$arg }; [C1]::new()').Invoke()[0]
It 'Implicitly Transform to 100' {
$c.arg = 100
$c.arg | should be 100
}
It 'Implicitly Transform to foo' {
$c.arg = "foo"
$c.arg | should be "foofoo"
}
}
Context 'Property.Instance.Transformation.String' {
$c = [scriptblock]::Create('class C1 { [Dummy.DoubleStringTransformation()][string]$arg }; [C1]::new()').Invoke()[0]
It 'set to foo' {
$c.arg = "foo"
$c.arg | should be "foofoo"
}
}
Context Property.Instance.Transformation.Int {
$c = [scriptblock]::Create('class C1 { [Dummy.DoubleInt()][int]$arg }; [C1]::new()').Invoke()[0]
It 'arg should be 200' {
$c.arg = 100
$c.arg | should be 200
}
It 'Set to string should fail with ExceptionWhenSetting' {
try {
$c.arg = "abc"
throw "Exception expected"
}
catch {
$_.FullyQualifiedErrorId | Should be 'ExceptionWhenSetting'
}
}
}
Context Property.Instance.Transformation.Nullable {
$c = [scriptblock]::Create('class C1 { [Nullable[int]][Dummy.DoubleStringTransformation()]$arg }; [C1]::new()').Invoke()[0]
It 'arg should be 100' {
$c.arg = 100
$c.arg | should be 100
}
}
Context Property.Instance.Transformation.Order {
$c = [scriptblock]::Create('class C1 { [Dummy.DoubleStringTransformation()][Dummy.AppendStringTransformation()]$arg }; [C1]::new()').Invoke()[0]
It 'arg should be 100' {
$c.arg = 100
$c.arg | should be 100
}
It 'arg should be foo___foo___g' {
$c.arg = "foo"
$c.arg | should be "foo___foo___"
}
}
}
Describe 'Type resolution with attributes' -Tag "CI" {
# There is kind of a collision between names
# System.Diagnostics.Tracing.EventSource
# System.Diagnostics.Tracing.EventSourceAttribute
# We need to make sure that we resolve type name to the right class at each usage
Context 'Name collision' {
It 'Resolve System.Diagnostics.Tracing.EventSource to Attribute and to Type in the different contexts' {
[System.Diagnostics.Tracing.EventSource(Name = "MyPSEventSource")]
class MyEventSource : System.Diagnostics.Tracing.EventSource
{
[void] OnEvent([string]$Message) {}
}
[MyEventSource]::new() | Should Not Be $null
}
}
}