Tests/IPv4Class.Tests.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
$ModuleName = "IPv4Class"
$RootPath = (Get-Item -Path $PSScriptRoot).Parent.FullName
$ModuleManifest = "$RootPath\$ModuleName.psd1"

Get-Module $ModuleName | Remove-Module
Import-Module $ModuleManifest -Force

describe 'ClassTests' {

    $results = [IPv4Class]::new("10.0.8.0")

    it 'should set IPAddress to 10.0.8.0' {
        $results.IPAddress | should be '10.0.8.0'
    }

    it 'should set Decimal to 167774208' {
        $results.Decimal | should be '167774208'
    }

    it 'should set Binary to 00001010000000000000100000000000' {
        $results.Binary | should be '00001010000000000000100000000000'
    }

    it 'should be have a type of IPv4Class' {
        $results.GetType().Name | should be 'IPv4Class'
    }

    Context 'GetBinaryFromIP' {

        $results = [IPv4Class]::GetBinaryFromIP("10.0.0.4")

        it 'should return 00001010000000000000000000000100' {
            $results | should be '00001010000000000000000000000100'
        }

        it 'should be 32 characters long' {
            $results.ToCharArray() | measure | Select-Object -ExpandProperty Count | should be 32
        }

        it 'should return only one object' {
            $results | measure | Select-Object -ExpandProperty Count | should be 1
        }

        it 'should be a string' {
            $results.GetType().Name | should be 'String'
        }

    }

    Context 'GetDecimalFromIP' {

        $results = [IPv4Class]::GetDecimalFromIP("10.0.0.1")

        it 'should return 167772161' {
            $results | should be '167772161'
        }

        it 'should return only one object' {
            $results | measure | Select-Object -ExpandProperty Count | should be 1
        }

        it 'should be Int64' {
            $results.GetType().Name | should be 'Int64'
        }

    }

    Context 'GetIPFromBinary' {

        $results = [IPv4Class]::GetIPFromBinary("00001010000000000000000000000011")

        it 'should return 10.0.0.3' {
            $results | should be '10.0.0.3'
        }

        it 'should return only one object' {
            $results | measure | Select-Object -ExpandProperty Count | should be 1
        }

        it 'should be a string' {
            $results.GetType().Name | should be 'String'
        }

    }

    Context 'GetIPFromDecimal' {

        $results = [IPv4Class]::GetIPFromDecimal("167772162")

        it 'should return 10.0.0.2' {
            $results | should be '10.0.0.2'
        }

        it 'should return only one object' {
            $results | measure | Select-Object -ExpandProperty Count | should be 1
        }

        it 'should be a string' {
            $results.GetType().Name | should be 'String'
        }

    }

    Context 'GetSubnetID' {

        $test = [IPv4Class]::new("192.168.1.56")
        $results = $test.GetSubnetID("255.255.255.0")

        it 'should return 192.168.1.0' {
            $results | should be '192.168.1.0'
        }

        it 'should return only one object' {
            $results | measure | Select-Object -ExpandProperty Count | should be 1
        }

        it 'should be a string' {
            $results.GetType().Name | should be 'String'
        }

    }

    Context 'GetSubnetHostCount' {

        $results = [IPv4Class]::GetSubnetHostCount("255.255.255.0")

        it 'should return 254' {
            $results | should be 254
        }

        it 'should return only one object' {
            $results | measure | Select-Object -ExpandProperty Count | should be 1
        }

        it 'should be a string' {
            $results.GetType().Name | should be 'String'
        }

    }

    Context 'GetSubnetIPs' {

        $test = [IPv4Class]::new("10.0.8.0")
        $results = $test.GetSubnetIPs("255.255.255.0")

        it 'should return 254' {
            $results | measure | Select-Object -ExpandProperty Count | should be 254
        }

        it 'should return 10.0.8.1 as the first result' {
            $results | Select-Object -First 1 | should be "10.0.8.1"
        }

        it 'should return 10.0.8.254 as the last result' {
            $results | Select-Object -Last 1 | should be "10.0.8.254"
        }

        it 'should be an array' {
            $results.GetType().BaseType.Name | should be 'Array'
        }

    }

}