Skip to content

Commit

Permalink
Exclude class methods when getting function definitions - Fixes #6
Browse files Browse the repository at this point in the history
  • Loading branch information
MathieuBuisson committed Oct 8, 2017
1 parent 4bcd774 commit 7ca8b07
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 31 deletions.
8 changes: 7 additions & 1 deletion PSCodeHealth/Private/Metrics/Get-FunctionDefinition.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,13 @@ Function Get-FunctionDefinition {

$PowerShellFile = (Resolve-Path -Path $PowerShellFile).Path
$FileAst = [System.Management.Automation.Language.Parser]::ParseFile($PowerShellFile, [ref]$Null, [ref]$Null)
$FileFunctions = $FileAst.FindAll({ $args[0] -is [System.Management.Automation.Language.FunctionDefinitionAst] }, $False)

$AstToInclude = [System.Management.Automation.Language.FunctionDefinitionAst]
# Excluding class methods, since we don't support classes
$AstToExclude = [System.Management.Automation.Language.FunctionMemberAst]

$Predicate = { $args[0] -is $AstToInclude -and $args[0].Parent -isnot $AstToExclude }
$FileFunctions = $FileAst.FindAll($Predicate, $False)
If ( $FileFunctions ) {
Foreach ( $FunctionName in $FileFunctions.Name ) {
Write-VerboseOutput -Message "Found function : $FunctionName"
Expand Down
37 changes: 37 additions & 0 deletions Tests/TestData/3Functions1Class.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
Function Public () {
<#
.SYNOPSIS
This cmdlet configures nothing and does it remarkably well.
.DESCRIPTION
This cmdlet configures nothing and does it remarkably well.
It takes objects as input and it sets nothing to 42.
#>

Function Nested ($InputObject) {
Get-Item $InputObject
}
}

Function Private ($InputObject) {
# A simple line of comment
Get-Item $InputObject
}

Class Car
{
Static [int]$NumberOfWheels = 4

[int]$NumberOfDoors

[datetime]$Year

[String]$Model

[Double] GetNumberOfYearsOld()
{
$Now = [datetime]::now
$TimeSpan = $Now - $this.Year
return $TimeSpan.Days / 365
}
}
86 changes: 56 additions & 30 deletions Tests/Unit/Private/Get-FunctionDefinition.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -3,44 +3,70 @@ Import-Module "$PSScriptRoot\..\..\..\$ModuleName\$($ModuleName).psd1" -Force

Describe 'Get-FunctionDefinition' {
InModuleScope $ModuleName {
Context 'The specified files only contains functions' {

$Files = (Get-ChildItem -Path "$($PSScriptRoot)\..\..\TestData\" -Filter '*.psm1').FullName

$TestDataPublicFunctions = @('Get-Nothing', 'Set-Nothing', 'Public')
$TestDataPrivateFunctions = 'Private'
$TestDataNestedFunctions = 'Nested'
$Files = (Get-ChildItem -Path "$($PSScriptRoot)\..\..\TestData\" -Filter '*.psm1').FullName
$TestDataPublicFunctions = @('Get-Nothing', 'Set-Nothing', 'Public')
$TestDataPrivateFunctions = 'Private'
$TestDataNestedFunctions = 'Nested'

$Results = Get-FunctionDefinition -Path $Files
$PipelineInputResults = $Files | Get-FunctionDefinition
$Results = Get-FunctionDefinition -Path $Files
$PipelineInputResults = $Files | Get-FunctionDefinition

It 'Should return objects of the type [FunctionDefinitionAst]' {
Foreach ( $Result in $Results ) {
$Result | Should BeOfType [System.Management.Automation.Language.FunctionDefinitionAst]
It 'Should return objects of the type [FunctionDefinitionAst]' {
Foreach ( $Result in $Results ) {
$Result | Should BeOfType [System.Management.Automation.Language.FunctionDefinitionAst]
}
}
}
It 'Should return all public functions from all files' {
Foreach ( $PublicFunction in $TestDataPublicFunctions ) {
$Results.Name | Where-Object { $_ -eq $PublicFunction } |
Should Not BeNullOrEmpty
It 'Should return all public functions from all files' {
Foreach ( $PublicFunction in $TestDataPublicFunctions ) {
$Results.Name | Where-Object { $_ -eq $PublicFunction } |
Should Not BeNullOrEmpty
}
}
}
It 'Should return all public functions from all files specified via pipeline input' {
Foreach ( $PublicFunction in $TestDataPublicFunctions ) {
$PipelineInputResults.Name | Where-Object { $_ -eq $PublicFunction } |
It 'Should return all public functions from all files specified via pipeline input' {
Foreach ( $PublicFunction in $TestDataPublicFunctions ) {
$PipelineInputResults.Name | Where-Object { $_ -eq $PublicFunction } |
Should Not BeNullOrEmpty
}
}
It 'Should return private functions' {
$Results.Name | Where-Object { $_ -eq $TestDataPrivateFunctions } |
Should Not BeNullOrEmpty
}
It 'Should not return any nested function' {
$Results.Name | Where-Object { $_ -eq $TestDataNestedFunctions } |
Should BeNullOrEmpty
}
It 'Should return 2 functions from the test data file : 1Public1Nested1Private.psm1' {
($Results.Extent.File | Where-Object { $_ -like '*\TestData\1Public1Nested1Private.psm1' }).Count |
Should Be 2
}
}
It 'Should return private functions' {
$Results.Name | Where-Object { $_ -eq $TestDataPrivateFunctions } |
Should Not BeNullOrEmpty
}
It 'Should not return any nested function' {
$Results.Name | Where-Object { $_ -eq $TestDataNestedFunctions } |
Should BeNullOrEmpty
}
It 'Should return 2 functions from the test data file : 1Public1Nested1Private.psm1' {
($Results.Extent.File | Where-Object { $_ -like '*\TestData\1Public1Nested1Private.psm1' }).Count |
Should Be 2
Context 'The specified file contains a class with a method' {

$File = (Resolve-Path -Path "$($PSScriptRoot)\..\..\TestData\3Functions1Class.ps1").Path
$PublicFunction = 'Public'
$PrivateFunction = 'Private'
$Method = 'GetNumberOfYearsOld'
$Results = Get-FunctionDefinition -Path $File

It 'Should return 2 function definitions' {
$Results.Count | Should -Be 2
}
It 'Should return the public function' {
$Results.Name | Where-Object { $_ -eq $PublicFunction } |
Should -Not -BeNullOrEmpty
}
It 'Should return the private function' {
$Results.Name | Where-Object { $_ -eq $PrivateFunction } |
Should -Not -BeNullOrEmpty
}
It 'Should not return the class method' {
$Results.Name | Where-Object { $_ -eq $Method } |
Should -BeNullOrEmpty
}
}
}
}

0 comments on commit 7ca8b07

Please sign in to comment.