Skip to content

Commit 48500e1

Browse files
Add more tests
1 parent 90b6e69 commit 48500e1

File tree

4 files changed

+327
-14
lines changed

4 files changed

+327
-14
lines changed

src/PSLambda/CompileVisitor.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System;
1+
using System;
22
using System.Collections;
33
using System.Collections.Generic;
44
using System.Globalization;
@@ -549,7 +549,7 @@ public object VisitIndexExpression(IndexExpressionAst indexExpressionAst)
549549
source.Type.GetGenericTypeDefinition() == typeof(IEnumerable<>)))
550550
{
551551
if (genericEnumerable == null)
552-
{
552+
{
553553
genericEnumerable = source.Type;
554554
}
555555

test/Loops.Tests.ps1

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,4 +70,85 @@ Describe 'basic loop functionality' {
7070

7171
$delegate.Invoke() | Should -Be 11
7272
}
73+
74+
It 'break continues after loop' {
75+
$delegate = New-PSDelegate {
76+
$i = 0
77+
$continuedAfterBreak = $false
78+
while ($i -lt 10) {
79+
$i++
80+
if ($i -eq 5) {
81+
break
82+
$continuedAfterBreak = $true
83+
}
84+
}
85+
86+
if ($continuedAfterBreak) {
87+
throw 'code after "break" was executed'
88+
}
89+
90+
return $i
91+
}
92+
93+
$delegate.Invoke() | Should -Be 5
94+
}
95+
96+
It 'continue steps to next interation' {
97+
$delegate = New-PSDelegate {
98+
$i = 0
99+
$continuedAfterContinue = $false
100+
while ($i -lt 10) {
101+
$i++
102+
continue
103+
$continuedAfterContinue = $true
104+
}
105+
106+
if ($continuedAfterContinue) {
107+
throw 'code after "continue" was executed'
108+
}
109+
110+
return $i
111+
}
112+
113+
$delegate.Invoke() | Should -Be 10
114+
}
115+
116+
Context 'switch statement' {
117+
It 'chooses correct value' {
118+
$hitValues = [System.Collections.Generic.List[string]]::new()
119+
$delegate = New-PSDelegate {
120+
foreach ($value in 'value1', 'value2', 'value3', 'invalid') {
121+
switch ($value) {
122+
value1 { $hitValues.Add('option1') }
123+
value2 { $hitValues.Add('option2') }
124+
value3 { $hitValues.Add('option3') }
125+
default { throw }
126+
}
127+
}
128+
}
129+
130+
{ $delegate.Invoke() } | Should -Throw
131+
$hitValues | Should -Be 'option1', 'option2', 'option3'
132+
}
133+
134+
It 'can have a single value' {
135+
$delegate = New-PSDelegate {
136+
switch ('value') {
137+
value { return 'value' }
138+
}
139+
}
140+
141+
$delegate.Invoke() | Should -Be 'value'
142+
}
143+
144+
It 'can have a default without cases' {
145+
$delegate = New-PSDelegate {
146+
switch ('value') {
147+
default { return 'value' }
148+
}
149+
}
150+
151+
$delegate.Invoke() | Should -Be 'value'
152+
}
153+
}
73154
}

test/MiscLanguageFeatures.Tests.ps1

Lines changed: 147 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,33 @@ $manifestPath = "$PSScriptRoot\..\Release\$moduleName\*\$moduleName.psd1"
44
Import-Module $manifestPath -Force
55

66
Describe '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

Comments
 (0)