Private/Tests/Find-Know.Tests.ps1

# ? TITEL Find-Know (UTest)
# ? DESCRIPTION Komponententest (Pester UTest) für das Cmdlet Find-Know
# ? TAGS UTest Pester
# ? VERSION 2020.01.14

#using Module Microsoft.WSMan.Management
#using Module Microsoft.PowerShell.Security
#using Module Microsoft.PowerShell.Management
#using Module Microsoft.PowerShell.Utility
#using Module CimCmdlets
#using Module Pester
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 "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
            }
        }
    }
}