Skip to content

Instantly share code, notes, and snippets.

@JeremySkinner
Created August 4, 2010 19:49
Show Gist options
  • Select an option

  • Save JeremySkinner/508684 to your computer and use it in GitHub Desktop.

Select an option

Save JeremySkinner/508684 to your computer and use it in GitHub Desktop.
before_each {
write-host "I will be written before each test is run"
}
when "True is true" {
assert { $true -eq $true }
}
when "True is false, the world may explode" {
assert { $true -eq $false } "True should be false"
}
# Usage: PsSpec.ps1 path/to/specs.ps1
param(
$specfile = $(throw "specfile required")
)
$script:specs = @()
$script:beforeeaches = @()
function assert([scriptblock]$assertion, $message = "") {
$result = & $assertion
if(-not $result) {
throw $message
}
}
function before_each([scriptblock]$block) {
$script:beforeeaches += $block
}
function when($condition, [scriptblock]$spec) {
$specMeta = @{
"name" = $condition;
"spec" = $spec;
}
$script:specs += $specMeta
}
. (resolve-path $specfile)
$specs | % {
$beforeeaches | % { & $_ }
$meta = $_
try {
& $_.spec
write-host $meta.name -foregroundcolor green
} catch {
write-host $meta.name -foregroundcolor red
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment