Skip to content

Commit 03cdb79

Browse files
author
Kapil Borle
committed
Add tests to verify Range parameters
1 parent 470311b commit 03cdb79

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed

Engine/Commands/InvokeFormatterCommand.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,10 @@ private void SetRange()
133133
return;
134134
}
135135

136-
var range = Range as Range;
136+
// When the range object is constructed with `[range]::new syntax`, `Range as Range` cast works.
137+
// However, if the range object is constructed with `new-object "range"` syntax, then
138+
// we need to use the ImmediatedBaseObject property to cast.
139+
var range = (Range as Range ?? (Range as PSObject)?.ImmediateBaseObject as Range);
137140
if (range != null)
138141
{
139142
this.range = range;

Tests/Engine/InvokeFormatter.tests.ps1

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,46 @@ function foo {
3131
Invoke-Formatter $def $settings | Should Be $expected
3232
}
3333
}
34+
35+
Context "When a range is given" {
36+
It "Should format only within the range when a range list is given" {
37+
$def = @"
38+
function foo {
39+
"xyz"
40+
"abc"
41+
}
42+
"@
43+
44+
$expected = @"
45+
function foo {
46+
"xyz"
47+
"abc"
48+
}
49+
"@
50+
51+
Invoke-Formatter -ScriptDefinition $def -Range @(3, 1, 4, 1) | Should Be $expected
52+
}
53+
54+
It "Should format only within the range when a range object is given" {
55+
$def = @"
56+
function foo {
57+
"xyz"
58+
"abc"
59+
}
60+
"@
61+
62+
$expected = @"
63+
function foo {
64+
"xyz"
65+
"abc"
66+
}
67+
"@
68+
69+
$range = New-Object -TypeName "Microsoft.Windows.PowerShell.ScriptAnalyzer.Range" -ArgumentList 3, 1, 4, 1
70+
Invoke-Formatter -ScriptDefinition $def -Range $range | Should Be $expected
71+
}
72+
}
73+
3474
Context "When no settings are given" {
3575
It "Should format using default settings" {
3676
$def = @'

0 commit comments

Comments
 (0)