ps/Modules/Alkami.DevOps.Validations/Public/Test-Should.ps1
2023-05-30 22:51:22 -07:00

70 lines
2.2 KiB
PowerShell

function Test-Should {
<#
.SYNOPSIS
Function that calls validation functions and collects the output for full review.
.DESCRIPTION
Used in conjunction with other Test-* functions in this module.
.PARAMETER result
Result of Invoke-WebRequest containing Html to test against.
.PARAMETER predicate
Script block to execute. Usually a single function in the format of ${function:Test-HaveStatusCode}
.PARAMETER Widget
The widget under test, for better detail logging
.PARAMETER Route
The route under test, for better detail logging
.PARAMETER Login
The Login object used to represent the user under test, for better detail logging
.PARAMETER _args
Remaining arguments that the $predicate function needs.
.EXAMPLE
$passedTest = (Test-Should -Result $webRequest -Predicate ${function:Test-HaveStatusCode} 200)
$passedTest = (Test-Should -Result $webRequest -Predicate ${function:Test-HaveResponseHeader} 'Content-Type' 'text/html;')
.NOTES
Writes out the result of the $predicate test function in Format-Table in Verbose mode and returns the status of the function test.
#>
[cmdletbinding()]
param(
[Parameter(Mandatory)]
$result,
[Parameter(Mandatory)]
[scriptblock]$predicate,
[Parameter(Mandatory)]
[string]$Widget,
[Parameter(Mandatory)]
[string]$Route,
[Parameter(Mandatory)]
$Login,
[Parameter(Mandatory, ValueFromRemainingArguments)]
$_args
)
process {
$StopWatch = [System.Diagnostics.StopWatch]::StartNew()
$isOk, $err = & $predicate $result @_args
$StopWatch.Stop()
$resultObj = New-Object PSObject -Property $([ordered]@{
FunctionName = $predicate.Ast.Name
ReturnValue = $isOk
ReturnMessage = $err
Username = $login.Username
Url = $login.UrlSignature
Route = $Route.TrimStart('/')
Widget = $widget
Parameters = (@($_args) -Join ',')
Runtime = $StopWatch.Elapsed.ToString("ss\.fffffff")
MachineName = (Get-FullyQualifiedServerName)
})
return $resultObj
}
}