1+ $directory = Split-Path - Parent $MyInvocation.MyCommand.Path
2+ $testRootDirectory = Split-Path - Parent $directory
3+
14Import-Module PSScriptAnalyzer
5+ Import-Module (Join-Path $testRootDirectory " PSScriptAnalyzerTestHelper.psm1" )
26
37function Get-Extent {
48 param ($line , $startLineNum , $startColumnNum , $endLineNum , $endColumnNum )
@@ -24,3 +28,148 @@ function Test-Extent {
2428}
2529
2630$extNamespace = [Microsoft.Windows.PowerShell.ScriptAnalyzer.Extensions.Extensions ]
31+
32+ Describe " String extension methods" {
33+ Context " When a text is given to GetLines" {
34+ It " Should return only one line if input is a single line." {
35+ $def = " This is a single line"
36+ $extNamespace ::GetLines($def ) | Get-Count | Should Be 1
37+ }
38+
39+ It " Should return 2 lines if input string has 2 lines." {
40+ $def = @'
41+ This is line one.
42+ This is line two.
43+ '@
44+ $extNamespace ::GetLines($def ) | Get-Count | Should Be 2
45+ }
46+ }
47+ }
48+ Describe " IScriptExtent extension methods" {
49+ Context " When a Range type input is given to ToRange" {
50+ It " Should maintain integrity" {
51+ $extent = Get-Extent $null 1 2 3 4
52+ $range = $extNamespace ::ToRange($extent )
53+
54+ $range.Start.Line | Should Be $extent.StartLineNumber
55+ $range.Start.Column | Should Be $extent.StartColumnNumber
56+ $range.End.Line | Should Be $extent.EndLineNumber
57+ $range.End.Column | Should Be $extent.EndColumnNumber
58+ }
59+ }
60+ }
61+
62+ Describe " FunctionDefinitionAst extension methods" {
63+ Context " When a function parameter declaration is given to GetParameterAsts" {
64+ BeforeAll {
65+ $funcDefnAst = {function foo ($param1 , $param2 ) {}}.Ast.EndBlock.Statements[0 ]
66+ $paramBlockAst = $null
67+ $parameterAsts = $extNamespace ::GetParameterAsts($funcDefnAst , [ref ] $paramBlockAst )
68+ }
69+
70+ It " Should return the parameters" {
71+ $parameterAsts | Get-Count | Should Be 2
72+ }
73+
74+ It " Should set paramBlock to `$ null" {
75+ $paramBlock | Should Be $null
76+ }
77+ }
78+
79+ Context " When a function with param block is given to GetParameterAsts" {
80+ BeforeAll {
81+ $funcDefnAst = {
82+ function foo {
83+ param ($param1 , $param2 )
84+ }}.Ast.EndBlock.Statements[0 ]
85+ $paramBlockAst = $null
86+ $parameterAsts = $extNamespace ::GetParameterAsts($funcDefnAst , [ref ] $paramBlockAst )
87+ }
88+
89+ It " Should return the parameters" {
90+ $parameterAsts | Get-Count | Should Be 2
91+ }
92+
93+ It " Should set paramBlock" {
94+ $paramBlockAst | Should Not Be $null
95+ }
96+ }
97+ }
98+
99+ Describe " ParamBlockAst extension methods" {
100+ Context " GetCmdletBindingAttributeAst" {
101+ It " Should return the cmdletbinding attribute if present" {
102+ $funcDefnAst = {
103+ function foo {
104+ [CmdletBinding ()]
105+ param ($param1 , $param2 )
106+ }}.Ast.EndBlock.Statements[0 ]
107+ $extNamespace ::GetCmdletBindingAttributeAst($funcDefnAst.Body.ParamBlock ) | Should Not Be $null
108+ }
109+ }
110+ }
111+
112+ Describe " AttributeAst extension methods" {
113+ Context " IsCmdletBindingAttributeAst" {
114+ It " Should return true if the attribute is a cmdletbinding attribute" {
115+ $funcDefnAst = {
116+ function foo {
117+ [CmdletBinding ()]
118+ param ($param1 , $param2 )
119+ }}.Ast.EndBlock.Statements[0 ]
120+ $extNamespace ::IsCmdletBindingAttributeAst($funcDefnAst.Body.ParamBlock.Attributes [0 ]) |
121+ Should Be $true
122+ }
123+ }
124+
125+ Context " GetSupportsShouldProcessAst" {
126+ It " Should return the SupportsShouldProcess named attribute argument" {
127+ $funcDefnAst = {
128+ function foo {
129+ [CmdletBinding (SupportsShouldProcess )]
130+ param ($param1 , $param2 )
131+ }}.Ast.EndBlock.Statements[0 ]
132+ $attrAst = $extNamespace ::GetSupportsShouldProcessAst($funcDefnAst.Body.ParamBlock.Attributes [0 ])
133+ $attrAst | Should Not Be $null
134+ $attrAst.Extent.Text | Should Be " SupportsShouldProcess"
135+ }
136+ }
137+ }
138+
139+ Describe " NamedAttributeArgumentAst" {
140+ Context " IsTrue" {
141+ It " Should return true if expression is omitted" {
142+ $attrAst = {
143+ function foo {
144+ [CmdletBinding (SupportsShouldProcess )]
145+ param ($param1 , $param2 )
146+ }}.Ast.EndBlock.Statements[0 ].Body.ParamBlock.Attributes[0 ].NamedArguments[0 ]
147+ $expressionAst = $null
148+ $extNamespace ::IsTrue($attrAst , [ref ]$expressionAst ) | Should Be $true
149+ $expressionAst | Should Be $null
150+ }
151+
152+ It " Should return true if argument value is `$ true" {
153+ $attrAst = {
154+ function foo {
155+ [CmdletBinding (SupportsShouldProcess = $true )]
156+ param ($param1 , $param2 )
157+ }}.Ast.EndBlock.Statements[0 ].Body.ParamBlock.Attributes[0 ].NamedArguments[0 ]
158+ $expressionAst = $null
159+ $extNamespace ::IsTrue($attrAst , [ref ]$expressionAst ) | Should Be $true
160+ $expressionAst | Should Not Be $null
161+ }
162+
163+ It " Should return false if argument value is `$ false" {
164+ $attrAst = {
165+ function foo {
166+ [CmdletBinding (SupportsShouldProcess = $false )]
167+ param ($param1 , $param2 )
168+ }}.Ast.EndBlock.Statements[0 ].Body.ParamBlock.Attributes[0 ].NamedArguments[0 ]
169+ $expressionAst = $null
170+ $extNamespace ::IsTrue($attrAst , [ref ]$expressionAst ) | Should Be $false
171+ $expressionAst | Should Not Be $null
172+
173+ }
174+ }
175+ }
0 commit comments