-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathPSUnit.Example.Exception.ps1
166 lines (131 loc) · 11.8 KB
/
PSUnit.Example.Exception.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
. PSUnit.ps1 #Load PSUnit frame work (Accessible via PATH environment variable set in the profile)
. PSUnit.Example.AdvancedFunction.ps1
function Throw-Error()
{
$Exception = New-Object -TypeName "System.InvalidOperationException"
Throw $Exception
}
function DontThrow-Error()
{
"Sorry, no error!"
}
function Test.Throw-Error_ThrowsError([System.InvalidOperationException] $ExpectedException = $(new-object -TypeName "System.InvalidOperationException") )
{
"Throwing Error"
Throw-Error
}
function Test.DontThrow-Error_FailsTest([System.InvalidOperationException] $ExpectedException = $(new-object -TypeName "System.InvalidOperationException") )
{
"This test is not supposed to trigger the expected exception"
DontThrow-Error
}
function Test.ThisFunctionHasAllTestMetaData([System.InvalidOperationException] $ExpectedException = $(new-object -TypeName "System.InvalidOperationException"), [switch] $Skip, [switch] $Category_FastTests)
{
"Function with all test meta data attributes"
}
function Test.ThisFunctionHasAllMetaExceptSkip([System.InvalidOperationException] $ExpectedException = $(new-object -TypeName "System.InvalidOperationException"), [switch] $Category_FastTests)
{
"Function with all test meta data attributes, except the skip parameter"
}
function Test.ThisFunctionHasCategoryFastTests([switch] $Category_FastTests)
{
"Category Fast Tests"
Assert-That $(2 + 2) {$ActualValue -eq 4}
}
function Test.ThisFunctionHasCategoryFastTestsAndUsesTheDefaultException([switch] $Category_FastTests, [System.InvalidOperationException] $ExpectedException = $(DefaultException))
{
"Category Fast Tests"
Throw $(DefaultException)
Assert-That $(2 + 2) {[System.InvalidOperationException] $ExpectedException = $(DefaultException)}
}
function Test.ThisFunctionCausesAnAssertToANonBoolean()
{
Assert-That $(2 + 2) {"NonBooleanContraintEvaluation"}
}
function Test.ThisFunctionThrowsAnUnexpectedInvalidOperationException()
{
Throw New-Object -TypeName "System.InvalidOperationException" -ArgumentList "Unexpected Exception!"
}
function Test.ThisFunctionThrowsSomeUnforeseenException()
{
& (blabla.exe)
}
function Test.ThisFunctionCausesAnAssertToANull()
{
Assert-That $(2 + 2) {$Null}
}
function Test.ThisFunctionCausesAnAssertToThrowAnException()
{
Assert-That $(2 + 2) {& (blabla.exe)}
}
function Test.ThisFunctionUsesAnUnknownExceptionTypeInTheExpectedExceptionParameter([System.InvalidOperationException] $ExpectedException = $(New-Object -TypeName "System.CPUOverclockedExcpetion"))
{
Assert-That $(2 + 2) {$ActualValue -eq 4}
}
function Test.ExpectPSUnitExpectedExceptionNotThrownExceptionCausesTestToPassIfNoOtherExceptionIsThrown([PSUnit.Assert.PSUnitExpectedExceptionNotThrownException] $ExpectedException = $(New-Object -TypeName "PSUnit.Assert.PSUnitExpectedExceptionNotThrownException"))
{
"PSUnitExpectedExceptionNotThrownException is expected, but not thrown"
}
function Test.ExpectPSUnitExpectedExceptionNotThrownExceptionCausesTestToFailIfRuntimeExceptionIsThrown([PSUnit.Assert.PSUnitExpectedExceptionNotThrownException] $ExpectedException = $(New-Object -TypeName "PSUnit.Assert.PSUnitExpectedExceptionNotThrownException"))
{
Throw-Error
}
function Test.ExpectSystemManagementAutomationValidationExceptionValidateUsingTryCatchAndComparingOfExceptionRuntimeObjects()
{
#Arrange
#Act
try
{
$Result = Validate-Validation -CantBeNullOrEmpty "Test" -ScriptBlockExpressionNeedsToReturnTrue "C:\PathThatWillNeverExist\"
}
catch
{
$Actual = $_.Exception
}
#Assert
$Expected = Get-ParameterBindingValidationException
Assert-That -ActualValue $Actual -Constraint {$ActualValue.GetType() -eq $Expected.GetType()}
}
function Test.ExpectSystemManagementAutomationValidationExceptionValidateUsingEncodedExceptionNameInParameterNameOfTypePSUnitExpectedException([PSUnit.Assert.PSUnitExpectedException] $System_DOT_Management_DOT_Automation_DOT_ParameterBindingValidationException)
{
#Arrange
#Act
$Actual = Validate-Validation -CantBeNullOrEmpty "Test" -ScriptBlockExpressionNeedsToReturnTrue "C:\PathThatWillNeverExist\"
#Assert
Assert-That -ActualValue $Actual -Constraint {$ActualValue -eq "400"}
}
function Test.ExpectSystemManagementAutomationValidationExceptionWhenPassingEmptyString([PSUnit.Assert.PSUnitExpectedException] $System_DOT_Management_DOT_Automation_DOT_ParameterBindingValidationException)
{
#Arrange
#Act
$Actual = Validate-Validation -CantBeNullOrEmpty ""
#Assert
Assert-That -ActualValue $Actual -Constraint {$ActualValue -eq "400"}
}
function Test.ExpectedExceptionISAssertThatExceptionAndAssertFails([PSUnit.Assert.PSUnitExpectedException] $PSUnit_DOT_Assert_DOT_PSUnitAssertFailedException)
{
#Arrange
#Act
$Actual = Validate-Validation -CantBeNullOrEmpty "Test"
$Actual = "200"
#Assert
Assert-That -ActualValue $Actual -Constraint {$ActualValue -eq "400"}
}
function Test.DontExpectSystemManagementAutomationValidationExceptionWhenPassingValidStringTestFailsInAssert([PSUnit.Assert.PSUnitExpectedException] $System_DOT_Management_DOT_Automation_DOT_ParameterBindingValidationException)
{
#Arrange
#Act
$Actual = Validate-Validation -CantBeNullOrEmpty "Test"
$Actual = "200"
#Assert
Assert-That -ActualValue $Actual -Constraint {$ActualValue -eq "400"}
}
function Test.ExpectedExceptionISAssertThatExceptionAndAssertSucceeds([PSUnit.Assert.PSUnitExpectedException] $PSUnit_DOT_Assert_DOT_PSUnitAssertFailedException)
{
#Arrange
#Act
$Actual = Validate-Validation -CantBeNullOrEmpty "Test"
$Actual = "400"
#Assert
Assert-That -ActualValue $Actual -Constraint {$ActualValue -eq "400"}
}