Private/Tests/Find-Know.Tests.ps1

<#
 
# Find-Know (UTest)
 
Komponententest (Pester UTest) für das Cmdlet Find-Know
 
- **Hashtags** UTest Pester
- **Version** 2020.01.14
 
#>


using Module Pester
using Module PSScriptAnalyzer
using namespace System
using namespace System.Runtime.InteropServices
using namespace System.Management.Automation

$ErrorActionPreference = [ActionPreference]::Continue

Set-StrictMode -Version Latest

#New-Variable -Name AkptModulePath -Value $PSScriptRoot -Option ReadOnly -Visibility Public -Force -Scope Global

$AkptModulePath = Split-Path -Path $MyInvocation.MyCommand.Path -Parent | Split-Path -Parent | Split-Path
$CmdletName = (Split-Path -Leaf $MyInvocation.MyCommand.Path) -replace '\.Tests\.ps1', [String]::Empty
. "$AkptModulePath\Public\$CmdletName.ps1"


Describe "$CmdletName UTest" {

    Context "Parameter Tests" {

        $cmdlet = Get-Command -Name $CmdletName

        It "Ist der Parameter 'Keyword' vom Type [String[]]?" {
            $target = $cmdlet.Parameters['Keyword'].ParameterType.Name
            $target | Should -BeExactly 'String[]'
        }

        It "Ist der Parameter 'IncludeContent' ein Switch-Parameter?" {
            $target = $cmdlet.Parameters['IncludeContent'].SwitchParameter
            $target | Should -BeTrue
        }
    }

    Context "Rückgabe des Cmdlets '$CmdletName' testen" {

        It "Enthalten die Rückgabe-Objekte keine Exceptions?" {
            { Find-Know -ErrorAction Stop -WarningAction Ignore } | Should -Not -Throw
        }

        $cmdletWarnings = @()
        Find-Know -WarningVariable cmdletWarnings -WarningAction SilentlyContinue -ErrorAction Ignore
        It "Enthalten die Rückgabe-Objekte Warning's?" {
            $cmdletWarnings.Count | Should -Be 0
        }
        if ($cmdletWarnings.Count -gt 0) {
            $cmdletWarnings = $cmdletWarnings | Foreach-Object {
                @{ WarningMessage = $_ }
            }
            It "Warning-Message: <WarningMessage>" -TestCases $testCase {
                param($WarningMessage)
                $RuleName | Should BeNullOrEmpty
            }
        }

        It "Werden mehr als 1 Wissensseiten gefunden?" {
            (Find-Know).Count | Should -BeGreaterThan 1
        }

    }

    Context "Fehler und Warnungen von '$CmdletName' testen" {

            It "Warnung wird ausgesprochen, wenn der Datei-Prolog nicht stimmt" {
                $warnings = @()
                "Test Content" | Set-Content -Path ".\Z00_Test_Content.ps1" -Force
                Find-Know -Keyword egal -WarningVariable warnings -WarningAction SilentlyContinue
                Remove-Item -Path ".\Z00_Test_Content.ps1" -Force -ErrorAction Ignore
                $warnings.Count | Should -Be 0
            }
    }

    Context "ScriptAnalyzer für $CmdletName.ps1 ausführen" {
        [Array]$testCase = @()
        $testCase += Invoke-ScriptAnalyzer -Path "$AkptModulePath\Public\$CmdletName.ps1" -Severity Error, Warning -ExcludeRule PSUseSingularNouns | 
            Foreach-Object {
                @{
                    Line       = $_.Line
                    RuleName   = $_.RuleName
                    Message    = $_.Message
                }
            }

        It "Ist die Skript-Datei '$CmdletName.ps1' frei von Regel-Verletzungen?" {
            $testCase.Count | Should -Be 0
        }

        if ($testCase.Count -gt 0) {
            It "REGEL-Verletzung: LINE <Line> | RULE <RuleName> | MESSAGE <Message>" -TestCases $testCase {
                param(
                    $Line,
                    $RuleName,
                    $Message
                )
                $RuleName | Should BeNullOrEmpty
            }
        }
    }
}