-
Notifications
You must be signed in to change notification settings - Fork 4
/
Assert.ps1
71 lines (57 loc) · 2 KB
/
Assert.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
function Assert {
<#
.SYNOPSIS
Helper function for "Design by Contract" assertion checking.
.DESCRIPTION
This is a helper function that makes the code less noisy by eliminating many of the "if" statements that are normally required to verify assumptions in the code.
.PARAMETER conditionToCheck
The boolean condition to evaluate
.PARAMETER failureMessage
The error message used for the exception if the conditionToCheck parameter is false
.EXAMPLE
C:\PS>Assert $false "This always throws an exception"
Example of an assertion that will always fail.
.EXAMPLE
C:\PS>Assert ( ($i % 2) -eq 0 ) "$i is not an even number"
This exmaple may throw an exception if $i is not an even number
Note:
It might be necessary to wrap the condition with paranthesis to force PS to evaluate the condition
so that a boolean value is calculated and passed into the 'conditionToCheck' parameter.
Example:
Assert 1 -eq 2 "1 doesn't equal 2"
PS will pass 1 into the condtionToCheck variable and PS will look for a parameter called "eq" and
throw an exception with the following message "A parameter cannot be found that matches parameter name 'eq'"
The solution is to wrap the condition in () so that PS will evaluate it first.
Assert (1 -eq 2) "1 doesn't equal 2"
.LINK
Exec
.LINK
FormatTaskName
.LINK
Framework
.LINK
Get-PSakeScriptTasks
.LINK
Include
.LINK
Invoke-psake
.LINK
Properties
.LINK
Task
.LINK
TaskSetup
.LINK
TaskTearDown
#>
[CmdletBinding()]
param(
[Parameter(Mandatory = $true)]
$conditionToCheck,
[Parameter(Mandatory = $true)]
[string]$failureMessage
)
if (-not $conditionToCheck) {
throw ('Assert: {0}' -f $failureMessage)
}
}