-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathPSUnit.Assert.ps1
81 lines (72 loc) · 6.07 KB
/
PSUnit.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
72
73
74
75
76
77
78
79
80
81
. PSUnit.Support.ps1
. PSUnit.Exceptions.ps1
function Assert-That()
{
<#
.Synopsis
Evaluates Assertions in the PSUnit framework based on the Constraint Model
.Description
Assert-That takes two arguments. The first parameter is the value that gets evaluated.
The second parameter is the Scriptblock that defines the evaluation expression.
.Example
Assert-That -ActualValue $(2 + 2) -Constraint {$ActualValue -eq 4}
.Parameter $ActualValue is the value that gets evaluated.
.Parameter $Constraint is a Scriptblock that defines the expression that evaluates $ActualValue
The string consisting of all the parameters to pass to App
.ReturnValue
Returns $True, if evaluation expression results in a boolean true value.
Throws Exception, if evaluation expression results in a boolean false value.
Throws Exception, if evaluation expression results in a non-boolean result.
Re-throws Exception, if evaluation expression results in an Exception.
.Link
about_functions_advanced
about_functions_advanced_methods
about_functions_advanced_parameters
.Notes
NAME: Assert-That
AUTHOR: Klaus Graefensteiner
LASTEDIT: 07/28/2009 12:12:42
#Requires -Version 2.0
#>
[CmdletBinding()]
PARAM(
[Parameter(Position=0, Mandatory=$true, ValueFromPipeline=$true)]
[Alias("A","Actual")]
$ActualValue,
[Parameter(Position=1, Mandatory=$true, ValueFromPipeline=$false)]
[Alias("C","Lamda")]
[ScriptBlock] $Constraint
)
Process{
try
{
$Result = & $Constraint
if( $Result -isnot [bool])
{
Write-Debug "Assert-That: Case: `$Result -isnot [bool]"
$Exception = New-Object -TypeName "PSUnit.Assert.PSUnitAssertEvaluatedToNonBooleanTypeException" -ArgumentList "Assert-That returned type that is not boolean!"
Write-Debug "Assert-That: Throwing exception: $($Exception.GetType().FullName)"
throw $Exception
}
if ($Result)
{
Write-Debug "Assert-That: Case: `$Result -eq `$true"
$True
}
else
{
Write-Debug "Assert-That: Case: `$Result -eq `$false"
$Exception = New-Object -TypeName "PSUnit.Assert.PSUnitAssertEvaluatedToFalseException" -ArgumentList "Assert-That returned false!"
Write-Debug "Assert-That: Throwing exception: $($Exception.GetType().FullName)"
throw $Exception
}
}
catch
{
Write-Debug "Assert-That: Caught exception: $($_.Exception.GetType().Fullname)"
$AssertException = New-Object -TypeName "PSUnit.Assert.PSUnitAssertFailedException" -ArgumentList "$($_.Exception.Message)", "$($_.Exception)"
Write-Debug "Assert-That: Re-throwing exception: $($AssertException.GetType().FullName)"
throw $AssertException
}
}# End Process
}