@@ -4,24 +4,33 @@ $manifestPath = "$PSScriptRoot\..\Release\$moduleName\*\$moduleName.psd1"
44Import-Module $manifestPath - Force
55
66Describe ' Misc Language Features' {
7- It ' Hashtable expression' {
7+ It ' expandable string expression' {
88 $delegate = New-PSDelegate {
9+ $one = " 1"
10+ $two = 2
11+ $three = ' something'
12+ return " This is a string with $one random $two numbers and $three "
13+ }
14+
15+ $delegate.Invoke () | Should - Be ' This is a string with 1 random 2 numbers and something'
16+ }
17+
918 Context ' hashtable tests' {
1019 It ' handles varied types of values' {
1120 $delegate = New-PSDelegate {
12- return @ {
13- ' string' = ' value'
14- string2 = 10
15- Object = [object ]::new()
21+ return @ {
22+ ' string' = ' value'
23+ string2 = 10
24+ Object = [object ]::new()
25+ }
1626 }
17- }
1827
19- $hashtable = $delegate.Invoke ()
20- $hashtable [' string' ] | Should - Be ' value'
21- $hashtable [' string2' ] | Should - Be 10
22- $hashtable [' Object' ] | Should - BeOfType object
23- $hashtable [' object' ] | Should - BeOfType object
24- }
28+ $hashtable = $delegate.Invoke ()
29+ $hashtable [' string' ] | Should - Be ' value'
30+ $hashtable [' string2' ] | Should - Be 10
31+ $hashtable [' Object' ] | Should - BeOfType object
32+ $hashtable [' object' ] | Should - BeOfType object
33+ }
2534
2635 It ' can initialize an empty hashtable' {
2736 (New-PSDelegate { @ {} }).Invoke().GetType() | Should - Be ([hashtable ])
@@ -68,6 +77,26 @@ Describe 'Misc Language Features' {
6877 $result [0 ].GetType() | Should - Be ([int ])
6978 $result | Should - Be 1
7079 }
80+
81+ It ' creates an empty array' {
82+ $result = (New-PSDelegate { @ () }).Invoke()
83+ $result.GetType () | Should - Be ([object []])
84+ $result.Length | Should - Be 0
85+ }
86+
87+ It ' can take multiple statements in an array' {
88+ $delegate = New-PSDelegate {
89+ return @ (
90+ 0 .. 10
91+ 10 .. 20 )
92+ }
93+
94+ $result = $delegate.Invoke ()
95+ $result.GetType () | Should - Be ([int [][]])
96+ $result.Count | Should - Be 2
97+ $result [0 ] | Should - Be (0 .. 10 )
98+ $result [1 ] | Should - Be (10 .. 20 )
99+ }
71100 }
72101
73102 Context ' assignments' {
@@ -94,5 +123,111 @@ Describe 'Misc Language Features' {
94123 { (New-PSDelegate { if ($true ) { $a = 10 }; return $a }).Invoke() } |
95124 Should - Throw ' The variable "a" was referenced before it was defined or was defined in a sibling scope'
96125 }
126+
127+ It ' can assign to an index operation' {
128+ $delegate = New-PSDelegate {
129+ $hash = @ { Key = ' Value' }
130+ $hash [' Key' ] = ' NewValue'
131+ return $hash
132+ }
133+
134+ $delegate.Invoke ().Key | Should - Be ' NewValue'
135+ }
136+
137+ It ' can assign to a property' {
138+ $delegate = New-PSDelegate {
139+ $verboseRecord = [System.Management.Automation.VerboseRecord ]::new(' original message' )
140+ $verboseRecord.Message = ' new message'
141+ return $verboseRecord
142+ }
143+
144+ $delegate.Invoke ().Message | Should - Be ' new message'
145+ }
146+
147+ It ' minus equals' {
148+ $delegate = New-PSDelegate {
149+ $a = 10
150+ $a -= 5
151+ return $a
152+ }
153+
154+ $delegate.Invoke () | Should - Be 5
155+ }
156+
157+ It ' multiply equals' {
158+ $delegate = New-PSDelegate {
159+ $a = 10
160+ $a *= 5
161+ return $a
162+ }
163+
164+ $delegate.Invoke () | Should - Be 50
165+ }
166+
167+ It ' divide equals' {
168+ $delegate = New-PSDelegate {
169+ $a = 10
170+ $a /= 5
171+ return $a
172+ }
173+
174+ $delegate.Invoke () | Should - Be 2
175+ }
176+
177+ It ' remainder equals' {
178+ $delegate = New-PSDelegate {
179+ $a = 10
180+ $a % = 6
181+ return $a
182+ }
183+
184+ $delegate.Invoke () | Should - Be 4
185+ }
186+ }
187+
188+ Context ' indexer inference' {
189+ It ' indexed IList<> are typed property' {
190+ $delegate = New-PSDelegate {
191+ $list = [System.Collections.Generic.List [string ]]::new()
192+ $list.Add (' test' )
193+ return $list [0 ].EndsWith(' t' )
194+ }
195+
196+ $delegate.Invoke () | Should - Be $true
197+ }
198+
199+ It ' indexed IDictionary<,> are typed property' {
200+ $delegate = New-PSDelegate {
201+ $list = [System.Collections.Generic.Dictionary [string , type ]]::new()
202+ $list.Add (' test' , [type ])
203+ return $list [' test' ].Namespace
204+ }
205+
206+ $delegate.Invoke () | Should - Be ' System'
207+ }
208+
209+ It ' indexed IEnumerable<> are typed properly' {
210+ $delegate = New-PSDelegate {
211+ $strings = generic(
212+ [System.Linq.Enumerable ]::Select(
213+ (' test' , ' test2' , ' test3' ),
214+ [func [string , string ]]{ ($string ) = > { $string }}),
215+ [string ], [string ])
216+
217+ return $strings [1 ].EndsWith(' 2' )
218+ }
219+
220+ $delegate.Invoke () | Should - Be $true
221+ }
222+
223+ It ' can index IList' {
224+ $delegate = New-PSDelegate {
225+ $list = [System.Collections.ArrayList ]::new()
226+ $list.AddRange ([object []](' one' , ' two' , ' three' ))
227+ return [string ]$list [1 ] -eq ' two'
228+ }
229+
230+ $delegate.Invoke () | Should - Be $true
231+ }
97232 }
98233}
0 commit comments