/
SocketToolkitTests.ps1
75 lines (51 loc) · 2.16 KB
/
SocketToolkitTests.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
#Set-ExecutionPolicy -ExecutionPolicy remotesigned -Scope process
# Importing NUnit asserts.
Import-Module -Name NUnit -Force
# Importing PsTest PowerShell testing framework.
Import-Module -Name PsTest
Copy-Item -Path .\SocketToolkit.psm1 -Destination .\SocketToolkit.ps1
# Including cmdlets to test.
.\SocketToolkit.ps1
(New-Test 'New-Socket: blocking socket when $NoneBlocking switched off' {
# Arrange.
$expectedSocket = 'socket'
$expectedAddressFamily = [Net.Sockets.AddressFamily]::InterNetwork
$expectedSocketType = [Net.Sockets.SocketType]::Stream
$expectedProtocolType = [Net.Sockets.ProtocolType]::Tcp
# Mock New-Object cmdlet.
function New-Object ($TypeName, $ArgumentList, $Property) {
# Make sure that a socket is supposed to be created.
$Assert::That($TypeName, $Is::EqualTo('Net.Sockets.Socket'))
# Make sure that socket constructor arguments are correct.
$Assert::That($ArgumentList[0], $Is::EqualTo($expectedAddressFamily))
$Assert::That($ArgumentList[1], $Is::EqualTo($expectedSocketType))
$Assert::That($ArgumentList[2], $Is::EqualTo($expectedProtocolType))
# Make sure that blocking property is set correctly.
$Assert::True($Property.Blocking)
# Return expected socket.
$expectedSocket
}
# Act.
$actualSocket = New-Socket
# Assert.
$Assert::That($actualSocket, $Is::EqualTo($expectedSocket))
}),
(New-Test 'New-Socket: none blocking socket when $NoneBlocking switched on' {
# Arrange.
$expectedSocket = 'socket'
# Mock New-Object cmdlet.
function New-Object ($TypeName, $ArgumentList, $Property) {
# Make sure that blocking property is set correctly.
$Assert::False($Property.Blocking)
# Return expected socket.
$expectedSocket
}
# Act.
$actualSocket = New-Socket -NoneBlocking
# Assert.
$Assert::That($actualSocket, $Is::EqualTo($expectedSocket))
}) |
# Invoke tests.
Invoke-Test |
# Format test results.
Format-TestResult