|
| 1 | +# Keep in sync with the rule's reserved words list in |
| 2 | +# Rules/AvoidReservedWordsAsFunctionNames.cs |
| 3 | +$reservedWords = @( |
| 4 | + 'assembly','base','begin','break', |
| 5 | + 'catch','class','command','configuration', |
| 6 | + 'continue','data','define','do', |
| 7 | + 'dynamicparam','else','elseif','end', |
| 8 | + 'enum','exit','filter','finally', |
| 9 | + 'for','foreach','from','function', |
| 10 | + 'hidden','if','in','inlinescript', |
| 11 | + 'interface','module','namespace','parallel', |
| 12 | + 'param','private','process','public', |
| 13 | + 'return','sequence','static','switch', |
| 14 | + 'throw','trap','try','type', |
| 15 | + 'until','using','var','while','workflow' |
| 16 | +) |
| 17 | + |
| 18 | +$randomCasedReservedWords = @( |
| 19 | + 'aSSeMbLy','bASe','bEgIN','bReAk', |
| 20 | + 'cAtCh','CLasS','cOMmAnD','cONfiGuRaTioN', |
| 21 | + 'cONtiNuE','dAtA','dEFInE','Do', |
| 22 | + 'DyNaMiCpArAm','eLsE','eLsEiF','EnD', |
| 23 | + 'EnUm','eXiT','fIlTeR','fINaLLy', |
| 24 | + 'FoR','fOrEaCh','fROm','fUnCtIoN', |
| 25 | + 'hIdDeN','iF','IN','iNlInEsCrIpT', |
| 26 | + 'InTeRfAcE','mOdUlE','nAmEsPaCe','pArAlLeL', |
| 27 | + 'PaRaM','pRiVaTe','pRoCeSs','pUbLiC', |
| 28 | + 'ReTuRn','sEqUeNcE','StAtIc','SwItCh', |
| 29 | + 'tHrOw','TrAp','tRy','TyPe', |
| 30 | + 'uNtIl','UsInG','VaR','wHiLe','wOrKfLoW' |
| 31 | +) |
| 32 | + |
| 33 | +$substringReservedWords = $reservedWords | ForEach-Object { |
| 34 | + "$($_)Func" |
| 35 | +} |
| 36 | + |
| 37 | +$safeFunctionNames = @( |
| 38 | + 'Get-Something','Do-Work','Classify-Data','Begin-Process' |
| 39 | +) |
| 40 | + |
| 41 | +BeforeAll { |
| 42 | + $ruleName = 'PSAvoidReservedWordsAsFunctionNames' |
| 43 | +} |
| 44 | + |
| 45 | +Describe 'AvoidReservedWordsAsFunctionNames' { |
| 46 | + Context 'When function names are reserved words' { |
| 47 | + It 'flags reserved word "<_>" as a violation' -TestCases $reservedWords { |
| 48 | + |
| 49 | + $scriptDefinition = "function $_ { 'test' }" |
| 50 | + $violations = Invoke-ScriptAnalyzer -ScriptDefinition $scriptDefinition -IncludeRule @($ruleName) |
| 51 | + |
| 52 | + $violations.Count | Should -Be 1 |
| 53 | + $violations[0].Severity | Should -Be 'Warning' |
| 54 | + $violations[0].RuleName | Should -Be $ruleName |
| 55 | + # Message text should include the function name as used |
| 56 | + $violations[0].Message | Should -Be "The reserved word '$_' was used as a function name. This should be avoided." |
| 57 | + # Extent should ideally capture only the function name |
| 58 | + $violations[0].Extent.Text | Should -Be $_ |
| 59 | + } |
| 60 | + |
| 61 | + It 'detects case-insensitively for "<_>"' -TestCases $randomCasedReservedWords { |
| 62 | + $scriptDefinition = "function $_ { }" |
| 63 | + $violations = Invoke-ScriptAnalyzer -ScriptDefinition $scriptDefinition -IncludeRule @($ruleName) |
| 64 | + $violations.Count | Should -Be 1 |
| 65 | + $violations[0].Message | Should -Be "The reserved word '$_' was used as a function name. This should be avoided." |
| 66 | + } |
| 67 | + |
| 68 | + It 'reports one finding per offending function' { |
| 69 | + $scriptDefinition = 'function class { };function For { };function Safe-Name { };function TRy { }' |
| 70 | + $violations = Invoke-ScriptAnalyzer -ScriptDefinition $scriptDefinition -IncludeRule @($ruleName) |
| 71 | + |
| 72 | + $violations.Count | Should -Be 3 |
| 73 | + $violations | ForEach-Object { $_.Severity | Should -Be 'Warning' } |
| 74 | + ($violations | Select-Object -ExpandProperty Extent | Select-Object -ExpandProperty Text) | |
| 75 | + Sort-Object | |
| 76 | + Should -Be @('class','For','TRy') |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + Context 'When there are no violations' { |
| 81 | + It 'does not flag safe function name "<_>"' -TestCases $safeFunctionNames { |
| 82 | + $scriptDefinition = "function $_ { }" |
| 83 | + $violations = Invoke-ScriptAnalyzer -ScriptDefinition $scriptDefinition -IncludeRule @($ruleName) |
| 84 | + $violations.Count | Should -Be 0 |
| 85 | + } |
| 86 | + |
| 87 | + It 'does not flag when script has no functions' { |
| 88 | + $scriptDefinition = '"hello";$x = 1..3 | ForEach-Object { $_ }' |
| 89 | + $violations = Invoke-ScriptAnalyzer -ScriptDefinition $scriptDefinition -IncludeRule @($ruleName) |
| 90 | + $violations.Count | Should -Be 0 |
| 91 | + } |
| 92 | + |
| 93 | + It 'does not flag substring-like name "<_>"' -TestCases $substringReservedWords { |
| 94 | + $scriptDefinition = "function $_ { }" |
| 95 | + $violations = Invoke-ScriptAnalyzer -ScriptDefinition $scriptDefinition -IncludeRule @($ruleName) |
| 96 | + $violations.Count | Should -Be 0 |
| 97 | + } |
| 98 | + } |
| 99 | +} |
0 commit comments