Skip to content

Instantly share code, notes, and snippets.

@nohwnd
Last active December 20, 2018 21:38
Show Gist options
  • Save nohwnd/525c1de4c30e16423f21bff21ec12528 to your computer and use it in GitHub Desktop.
Save nohwnd/525c1de4c30e16423f21bff21ec12528 to your computer and use it in GitHub Desktop.
p - a minimal implementation of Pester
# runs tests with summary
i {
b "Basic" {
t "Given a scriptblock with 1 test in it, it finds 1 test" {
$true
}
}
}
# runs tests without summary
b "Basic" {
t "Given a scriptblock with 1 test in it, it finds 1 test" {
$true
}
}
# runs a single test
t "Given a scriptblock with 1 test in it, it finds 1 test" {
$true
}
$script:failed = 0
$script:total = 0
function i {
param(
[ScriptBlock] $ScriptBlock
)
$script:failed = 0
$script:total = 0
& $ScriptBlock
Write-Host -NoNewline "`npassed $($total-$failed), " -ForegroundColor Green
Write-Host -NoNewline "failed $($failed), " -ForegroundColor Red
Write-Host "total $($total)" -ForegroundColor Gray
}
function b {
param(
[String] $Name,
[ScriptBlock] $ScriptBlock
)
Write-Host "| - $Name" -ForegroundColor Cyan
$null = & $ScriptBlock
}
function t {
param(
[String] $Name,
[ScriptBlock] $ScriptBlock
)
try {
$script:total++
$null = & $ScriptBlock
Write-Host "[+] - $Name" -ForegroundColor Green
}
catch {
$script:failed++
# verify throws Exception directly, so if the type is someting
# different then chances are I made a mistake and I want more
# information than just the assertion message
if ([Exception] -ne $_.Exception.GetType()) {
Write-Host "ERROR: - $Name -> $($_ | Format-List * -Force | Out-String)" -ForegroundColor Red
}
else {
Write-Host "[-] - $Name -> $_" -ForegroundColor Red
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment