forked from PowerShell/PSScriptAnalyzer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
EditableText.tests.ps1
141 lines (129 loc) · 4.25 KB
/
EditableText.tests.ps1
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
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.
BeforeAll {
$testRootDirectory = Split-Path -Parent $PSScriptRoot
Import-Module (Join-Path $testRootDirectory "PSScriptAnalyzerTestHelper.psm1")
$editableTextType = "Microsoft.Windows.PowerShell.ScriptAnalyzer.EditableText"
$textEditType = "Microsoft.Windows.PowerShell.ScriptAnalyzer.TextEdit"
}
Describe "EditableText class" {
Context "When a single edit is given for application" {
It "Should replace in a single line string in the middle" {
$def = 'This is just a single line.'
$edit = New-Object -TypeName $textEditType -ArgumentList 1,14,1,22,"one"
$editableText = New-Object -TypeName $editableTextType -ArgumentList $def
$result = $editableText.ApplyEdit($edit)
$result.ToString() | Should -Be "This is just one line."
}
It "Should replace in a single line string in the start" {
$def = 'This is just a single line.'
$edit = New-Object -TypeName $textEditType -ArgumentList 1,1,1,5,"That"
$editableText = New-Object -TypeName $editableTextType -ArgumentList $def
$result = $editableText.ApplyEdit($edit)
$result.ToString() | Should -Be 'That is just a single line.'
}
It "Should replace in a single line string in the end" {
$def = 'This is just a single line.'
$edit = New-Object -TypeName $textEditType -ArgumentList 1,23,1,27,"sentence"
$editableText = New-Object -TypeName $editableTextType -ArgumentList $def
$result = $editableText.ApplyEdit($edit)
$result.ToString() | Should -Be 'This is just a single sentence.'
}
It "Should replace in multi-line string" {
$def = @"
This is an outer string
that spans more than
two lines
"@
$expected = @"
This is an outer string
that spans three
lines
"@
$newText = @"
three
"@
$edit = New-Object -TypeName $textEditType -ArgumentList 2,12,3,5,$newText
$editableText = New-Object -TypeName $editableTextType -ArgumentList $def
$result = $editableText.ApplyEdit($edit)
$result.ToString() | Should -Be $expected
}
It "Should delete in a multi-line string" {
$def = @'
function foo {
param(
[bool] $param1,
[bool] $whatif
)
}
'@
$expected = @'
function foo {
param(
[bool] $param1
)
}
'@
$newText = ''
$edit = New-Object -TypeName $textEditType -ArgumentList 3,23,4,23,$newText
$editableText = New-Object -TypeName $editableTextType -ArgumentList $def
$result = $editableText.ApplyEdit($edit)
$result.ToString() | Should -Be $expected
}
It "Should insert in a multi-line string" {
$def = @'
function foo {
param(
[bool] $param1
)
}
'@
$expected = @'
function foo {
[CmdletBinding()]
param(
[bool] $param1
)
}
'@
# Editor does not allow trailing white-spaces, hence this weird construct.
$newText = @"
[CmdletBinding()]
"@
$edit = New-Object -TypeName $textEditType -ArgumentList 2,1,2,1,$newText
$editableText = New-Object -TypeName $editableTextType -ArgumentList $def
$result = $editableText.ApplyEdit($edit)
$result.ToString() | Should -Be $expected
}
It "Should return a read-only collection of lines in the text" {
$def = @'
function foo {
param(
[bool] $param1
)
}
'@
$text = New-Object `
-TypeName "Microsoft.Windows.PowerShell.ScriptAnalyzer.EditableText" `
-ArgumentList @($def)
{$text.Lines.Add("abc")} | Should -Throw
}
It "Should return the correct number of lines in the text" {
$def = @'
function foo
{
get-childitem
$x=1+2
$hashtable = @{
property1 = "value"
anotherProperty = "another value"
}
}
'@
$text = New-Object `
-TypeName "Microsoft.Windows.PowerShell.ScriptAnalyzer.EditableText" `
-ArgumentList @($def)
$text.LineCount | Should -Be 9
}
}
}