1+ BeforeDiscovery { Import-Module " $PSScriptRoot /../../../src/common/Analyser.psm1" }
2+
3+ Describe ' Analyser Module Tests' {
4+ Context ' Module Import and Type Loading' {
5+ It ' Should successfully import the module' {
6+ Get-Module Analyser | Should -Not - BeNullOrEmpty
7+ }
8+
9+ It ' Should load SuppressAnalyserAttribute type' {
10+ $TypeExists = $null -ne ([System.Type ]' Compiler.Analyser.SuppressAnalyserAttribute' -as [type ])
11+ $TypeExists | Should - Be $true
12+ }
13+
14+ It ' Should export SuppressAnalyserAttribute type via Export-Types' {
15+ # Verify the type is accessible as a type accelerator
16+ $TypeAcceleratorsClass = [PSObject ].Assembly.GetType(' System.Management.Automation.TypeAccelerators' )
17+ $ExistingTypeAccelerators = $TypeAcceleratorsClass ::Get
18+
19+ # The type should be available
20+ $ExistingTypeAccelerators.Keys -contains ' Compiler.Analyser.SuppressAnalyserAttribute' | Should - Be $true
21+ }
22+ }
23+
24+ Context ' SuppressAnalyserAttribute Functionality' {
25+ It ' Should create SuppressAnalyserAttribute with CheckType and Data' {
26+ $Attribute = [Compiler.Analyser.SuppressAnalyserAttribute ]::new(' TestCheck' , ' TestData' )
27+
28+ $Attribute | Should -Not - BeNullOrEmpty
29+ $Attribute.CheckType | Should - Be ' TestCheck'
30+ $Attribute.Data | Should - Be ' TestData'
31+ $Attribute.Justification | Should - BeNullOrEmpty
32+ }
33+
34+ It ' Should allow setting Justification property' {
35+ $Attribute = [Compiler.Analyser.SuppressAnalyserAttribute ]::new(' TestCheck' , ' TestData' )
36+ $Attribute.Justification = ' This is a test justification'
37+
38+ $Attribute.Justification | Should - Be ' This is a test justification'
39+ }
40+
41+ It ' Should support various data types for Data parameter' {
42+ # Test with string
43+ $StringAttr = [Compiler.Analyser.SuppressAnalyserAttribute ]::new(' StringCheck' , ' StringData' )
44+ $StringAttr.Data | Should - Be ' StringData'
45+
46+ # Test with number
47+ $NumberAttr = [Compiler.Analyser.SuppressAnalyserAttribute ]::new(' NumberCheck' , 42 )
48+ $NumberAttr.Data | Should - Be 42
49+
50+ # Test with null
51+ $NullAttr = [Compiler.Analyser.SuppressAnalyserAttribute ]::new(' NullCheck' , $null )
52+ $NullAttr.Data | Should - Be $null
53+ }
54+
55+ It ' Should be usable as an attribute on script elements' {
56+ # Create a simple test to verify the attribute exists and can be instantiated
57+ $Attribute = [Compiler.Analyser.SuppressAnalyserAttribute ]::new(' UseOfUndefinedFunction' , ' TestFunction' )
58+ $Attribute | Should -Not - BeNullOrEmpty
59+ $Attribute.CheckType | Should - Be ' UseOfUndefinedFunction'
60+ $Attribute.Data | Should - Be ' TestFunction'
61+ }
62+ }
63+
64+ Context ' PowerShell Version Compatibility' {
65+ It ' Should work in PowerShell 5.1+ environments' {
66+ # Test that the module behaves correctly across versions
67+ $PSVersionMajor = $PSVersionTable.PSVersion.Major
68+ $PSVersionMajor | Should - BeGreaterOrEqual 5
69+
70+ # The type should be available regardless of version
71+ [Compiler.Analyser.SuppressAnalyserAttribute ] | Should -Not - BeNullOrEmpty
72+ }
73+
74+ It ' Should handle compiled script scenarios' {
75+ # Test the compiled script detection logic
76+ $IsCompiledScript = Get-Variable - Name ' CompiledScript' - Scope Global - ValueOnly - ErrorAction SilentlyContinue
77+
78+ # In our test environment, this should be null/false
79+ $IsCompiledScript | Should - BeNullOrEmpty
80+ }
81+ }
82+
83+ Context ' CS File Integration' {
84+ It ' Should check for Suppression.cs file path' {
85+ $ExpectedPath = " $PSScriptRoot /../../../src/Compiler/Analyser/Suppression.cs"
86+
87+ # The test verifies the path calculation logic, not necessarily file existence
88+ $ExpectedPath | Should -Not - BeNullOrEmpty
89+ $ExpectedPath | Should - BeLike ' *Suppression.cs'
90+ }
91+
92+ It ' Should prefer CS file when available in PowerShell 6+' {
93+ $PSVersion = $PSVersionTable.PSVersion.Major
94+ $CSFilePath = " $PSScriptRoot /../../../src/Compiler/Analyser/Suppression.cs"
95+
96+ if ($PSVersion -ge 6 -and (Test-Path $CSFilePath )) {
97+ # If PS 6+ and file exists, it should use Add-Type -LiteralPath
98+ Test-Path $CSFilePath | Should - Be $true
99+ } else {
100+ # Otherwise, should use inline C# definition
101+ $true | Should - Be $true # Always passes for fallback scenario
102+ }
103+ }
104+ }
105+
106+ Context ' Attribute Usage Patterns' {
107+ It ' Should support multiple attributes on the same element' {
108+ # Test that multiple instances of the attribute can be created
109+ $Attr1 = [Compiler.Analyser.SuppressAnalyserAttribute ]::new(' Check1' , ' Data1' )
110+ $Attr2 = [Compiler.Analyser.SuppressAnalyserAttribute ]::new(' Check2' , ' Data2' )
111+
112+ $Attr1.CheckType | Should - Be ' Check1'
113+ $Attr1.Data | Should - Be ' Data1'
114+ $Attr2.CheckType | Should - Be ' Check2'
115+ $Attr2.Data | Should - Be ' Data2'
116+ }
117+
118+ It ' Should support common analyzer check types' {
119+ $CommonChecks = @ (
120+ ' UseOfUndefinedFunction' ,
121+ ' MissingCmdlet' ,
122+ ' UnreachableCode' ,
123+ ' UnusedVariable'
124+ )
125+
126+ foreach ($Check in $CommonChecks ) {
127+ $Attr = [Compiler.Analyser.SuppressAnalyserAttribute ]::new($Check , ' TestData' )
128+ $Attr.CheckType | Should - Be $Check
129+ }
130+ }
131+ }
132+
133+ Context ' Error Handling' {
134+ It ' Should handle null CheckType gracefully' {
135+ $Attr = [Compiler.Analyser.SuppressAnalyserAttribute ]::new($null , ' TestData' )
136+ # Null gets converted to empty string in C#
137+ $Attr.CheckType | Should - Be ' '
138+ $Attr.Data | Should - Be ' TestData'
139+ }
140+
141+ It ' Should handle empty CheckType' {
142+ $Attr = [Compiler.Analyser.SuppressAnalyserAttribute ]::new(' ' , ' TestData' )
143+ $Attr.CheckType | Should - Be ' '
144+ $Attr.Data | Should - Be ' TestData'
145+ }
146+ }
147+ }
0 commit comments