diff --git a/Graphical.psm1 b/Graphical.psm1 index c0212ee..8484a8a 100644 --- a/Graphical.psm1 +++ b/Graphical.psm1 @@ -1,6 +1,6 @@ -Get-ChildItem "$PSScriptRoot\src\" -Filter *.ps1 | ForEach-Object { +Get-ChildItem -Path "$PSScriptRoot\src\" -Filter *.ps1 | ForEach-Object -Process { . $_.FullName } # Exporting the members and their aliases -Export-ModuleMember Show-Graph -Alias Graph +Export-ModuleMember -Function Show-Graph -Alias Graph diff --git a/src/Get-BarPlot.ps1 b/src/Get-BarPlot.ps1 index 986caaf..a7bd040 100644 --- a/src/Get-BarPlot.ps1 +++ b/src/Get-BarPlot.ps1 @@ -17,7 +17,7 @@ Function Get-BarPlot { # Create a 2D Array to save datapoints in a 2D format $NumOfRows = $difference/($Step) + 1 - $Array = New-Object 'object[,]' $NumOfRows,$NumOfDatapoints + $Array = New-Object -TypeName 'object[,]' -ArgumentList $NumOfRows,$NumOfDatapoints For($i = 0;$i -lt $Datapoints.count;$i++){ # Fit datapoint in a row, where, a row's data range = Total Datapoints / Step diff --git a/src/Get-LinePlot.ps1 b/src/Get-LinePlot.ps1 index 0dc47d0..3f3e715 100644 --- a/src/Get-LinePlot.ps1 +++ b/src/Get-LinePlot.ps1 @@ -15,7 +15,7 @@ Function Get-LinePlot { # Create a 2D Array to save datapoints in a 2D format $NumOfRows = $difference/($Step) + 1 - $Array = New-Object 'object[,]' $NumOfRows,$NumOfDatapoints + $Array = New-Object -TypeName 'object[,]' -ArgumentList $NumOfRows,$NumOfDatapoints $Marker = [char] 9608 $Line = [char] 9616 diff --git a/src/Get-ScatterPlot.ps1 b/src/Get-ScatterPlot.ps1 index 423dfd8..e49b6f7 100644 --- a/src/Get-ScatterPlot.ps1 +++ b/src/Get-ScatterPlot.ps1 @@ -15,7 +15,7 @@ Function Get-ScatterPlot { $Difference = $EndofRange - $StartOfRange $NumOfRows = $difference/($Step) + 1 $NumOfDatapoints = $Datapoints.Count - $Array = New-Object 'object[,]' ($NumOfRows),$NumOfDatapoints + $Array = New-Object -TypeName 'object[,]' -ArgumentList ($NumOfRows),$NumOfDatapoints For($i = 0;$i -lt $Datapoints.count;$i++){ diff --git a/src/Show-Graph.ps1 b/src/Show-Graph.ps1 index c016daf..5948584 100644 --- a/src/Show-Graph.ps1 +++ b/src/Show-Graph.ps1 @@ -109,8 +109,8 @@ Function Show-Graph { $YAxisTitleStartIdx, $YAxisTitleEndIdx = CenterAlignStringReturnIndices -String $YAxisTitle -Length $NumOfRows If($YAxisTitle.Length -gt $NumOfLabelsOnYAxis){ - Write-Warning "No. Alphabets in YAxisTitle [$($YAxisTitle.Length)] can't be greator than no. of Labels on Y-Axis [$NumOfLabelsOnYAxis]" - Write-Warning "YAxisTitle will be cropped" + Write-Warning -Message "No. Alphabets in YAxisTitle [$($YAxisTitle.Length)] can't be greator than no. of Labels on Y-Axis [$NumOfLabelsOnYAxis]" + Write-Warning -Message "YAxisTitle will be cropped" } # Create a 2D Array to save datapoints in a 2D format @@ -238,9 +238,9 @@ Function Show-Graph { if(![String]::IsNullOrWhiteSpace($XAxisTitle)){ # Position the x-axis label at the center of the axis $XAxisTitle = " "*$LengthOfMaxYAxisLabel + (CenterAlignString $XAxisTitle $XAxis.Length) - Write-Host $VerticalEdge -NoNewline - Write-Host $XAxisTitle -ForegroundColor DarkYellow -NoNewline # Prints XAxisTitle - Write-Host $(" "*$(($LengthOfMaxYAxisLabel + $XAxis.length) - $XAxisTitle.Length - 2)) $VerticalEdge + Write-Host -Object $VerticalEdge -NoNewline + Write-Host -Object $XAxisTitle -ForegroundColor DarkYellow -NoNewline # Prints XAxisTitle + Write-Host -Object $(" "*$(($LengthOfMaxYAxisLabel + $XAxis.length) - $XAxisTitle.Length - 2)) $VerticalEdge } # bottom boundary diff --git a/src/UtilityFunctions.ps1 b/src/UtilityFunctions.ps1 index 7f75b65..31f4b50 100644 --- a/src/UtilityFunctions.ps1 +++ b/src/UtilityFunctions.ps1 @@ -10,10 +10,10 @@ Function CenterAlignStringReturnIndices ($String, $Length) { Function Write-Graph($YAxisLabelAlphabet, $YAxisLabel, $Row, $RowColor, $LabelColor) { - Write-Host $([char]9474) -NoNewline - Write-Host $YAxisLabelAlphabet -ForegroundColor $LabelColor -NoNewline - Write-Host "$($YAxisLabel.tostring().PadLeft($LengthOfMaxYAxisLabel+2) + [Char]9508)" -NoNewline + Write-Host -Object $([char]9474) -NoNewline + Write-Host -Object $YAxisLabelAlphabet -ForegroundColor $LabelColor -NoNewline + Write-Host -Object "$($YAxisLabel.tostring().PadLeft($LengthOfMaxYAxisLabel+2) + [Char]9508)" -NoNewline ##Write-Host "$YAxisLabel|" -NoNewline - Write-Host $Row -ForegroundColor $RowColor -NoNewline - Write-Host " " $([char]9474) + Write-Host -Object $Row -ForegroundColor $RowColor -NoNewline + Write-Host -Object " " $([char]9474) } diff --git a/tests/module.unit.tests.ps1 b/tests/module.unit.tests.ps1 new file mode 100644 index 0000000..c086316 --- /dev/null +++ b/tests/module.unit.tests.ps1 @@ -0,0 +1,116 @@ +<# +.Synopsis + Pester test to verify the content of the manifest and the documentation of each functions. +.Description + Pester test to verify the content of the manifest and the documentation of each functions. +#> +[CmdletBinding()] +PARAM( + [System.String]$modulePath=(split-path -parent -Path $PSScriptRoot), + [System.String]$moduleName='Graphical', + [System.String]$srcPath=(Join-Path -Path (split-path -parent -Path $PSScriptRoot) -ChildPath 'src') +) +begin{ + # Find the Manifest file + $ManifestFile = "$modulePath\$ModuleName.psd1" + + # Unload any module with same name + Get-Module -Name $ModuleName -All | Remove-Module -Force -ErrorAction Ignore + + # Import Module + $ModuleInformation = Import-Module -Name $ManifestFile -Force -ErrorAction Stop -PassThru + + # Get the functions present in the Manifest + $ExportedFunctions = $ModuleInformation.ExportedFunctions.Values.name + + # Public functions + $publicFiles = @(Get-ChildItem -Path $srcPath\public\*.ps1 -ErrorAction SilentlyContinue) +} +end{ + Remove-Module -Name $moduleName -ErrorAction SilentlyContinue} +process{ +Describe "$ModuleName Module - Testing Manifest File (.psd1)"{ + + Context 'Module Version'{'Loaded Version vs Get-Command return for the module'} + Context 'Manifest'{ + It 'Should contains RootModule'{$ModuleInformation.RootModule|Should not BeNullOrEmpty} + It 'Should contains Author'{$ModuleInformation.Author|Should not BeNullOrEmpty} + It 'Should contains Company Name'{$ModuleInformation.CompanyName|Should not BeNullOrEmpty} + It 'Should contains Description'{$ModuleInformation.Description|Should not BeNullOrEmpty} + It 'Should contains Copyright'{$ModuleInformation.Copyright|Should not BeNullOrEmpty} + It 'Should contains License'{$ModuleInformation.LicenseURI|Should not BeNullOrEmpty} + It 'Should contains a Project Link'{$ModuleInformation.ProjectURI|Should not BeNullOrEmpty} + It 'Should contains a Tags (For the PSGallery)'{$ModuleInformation.Tags.count|Should not BeNullOrEmpty} + + It "Should have equal number of Function Exported and the Public PS1 files found ($($ExportedFunctions.count) and $($publicFiles.count))"{ + $ExportedFunctions.count -eq $publicFiles.count |Should -Be $true} + It "Compare the missing function"{ + if (-not($ExportedFunctions.count -eq $publicFiles.count)){ + $Compare = Compare-Object -ReferenceObject $ExportedFunctions -DifferenceObject $publicFiles.basename + $Compare.inputobject -join ',' | + Should BeNullOrEmpty + } + } + } +} + + +# Testing the Module +Describe "$ModuleName Module - HELP" -Tags "Module" { + #$Commands = (get-command -Module ADSIPS).Name + + FOREACH ($c in $ExportedFunctions) + { + $Help = Get-Help -Name $c -Full + $Notes = ($Help.alertSet.alert.text -split '\n') + $FunctionContent = Get-Content function:$c + $AST = [System.Management.Automation.Language.Parser]::ParseInput($FunctionContent, [ref]$null, [ref]$null) + + Context "$c - Help"{ + + It "Synopsis"{$help.Synopsis| Should not BeNullOrEmpty} + It "Description"{$help.Description| Should not BeNullOrEmpty} + It "Notes - Github Project" {$Notes -match "$GithubRepository$ModuleName" | Should Be $true} + + # Get the parameters declared in the Comment Based Help + # minus the RiskMitigationParameters + $RiskMitigationParameters = 'Whatif', 'Confirm' + $HelpParameters = $help.parameters.parameter | Where-Object name -NotIn $RiskMitigationParameters + + # Parameter Count VS AST Parameter + $ASTParameters = $ast.ParamBlock.Parameters.Name.variablepath.userpath + It "Parameter - Compare Count Help/AST" { + $HelpParameters.name.count -eq $ASTParameters.count | Should Be $true} + + # Parameters Description + $HelpParameters| Foreach-Object -Process { + It "Parameter $($_.Name) - Should contains description"{ + $_.description | Should not BeNullOrEmpty + } + } + + # Parameters separated by a space + $ParamText=$ast.ParamBlock.extent.text -split '\r\n' # split on return + $ParamText=$ParamText.trim() + $ParamTextSeparator=$ParamText |select-string ',$' #line that finish by a ',' + + if($ParamTextSeparator) + { + Foreach ($ParamLine in $ParamTextSeparator.linenumber) + { + it "Parameter - Separated by space (Line $ParamLine)"{ + #$ParamText[$ParamLine] -match '\s+' | Should Be $true + $ParamText[$ParamLine] -match '^$|\s+' | Should Be $true + } + } + } + + # Examples + it "Example - Count should be greater than 0"{ + $Help.examples.example.code.count | Should BeGreaterthan 0 + $Help.examples | Should not BeNullOrEmpty + } + } + } +} +} \ No newline at end of file